Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ import torchaudio
|
|
| 12 |
import gradio as gr
|
| 13 |
from speechbrain.inference.classifiers import EncoderClassifier
|
| 14 |
from yt_dlp import YoutubeDL
|
| 15 |
-
from yt_dlp.utils import DownloadError
|
| 16 |
-
from pytubefix import YouTube as PyTubeFixYT
|
| 17 |
|
| 18 |
# βββββββββββββββ Model setup βββββββββββββββ
|
| 19 |
ACCENT_MODEL_ID = "Jzuluaga/accent-id-commonaccent_ecapa"
|
|
@@ -40,34 +38,19 @@ def sec_to_hms(sec: int) -> str:
|
|
| 40 |
|
| 41 |
def download_audio(url: str, out_path: Path) -> Path:
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Returns the actual downloaded file path (.m4a, .webm, etc.).
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
"format": "bestaudio/best",
|
| 49 |
"outtmpl": str(out_path.with_suffix(".%(ext)s")),
|
| 50 |
-
"postprocessors": [],
|
| 51 |
"quiet": True,
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
except DownloadError:
|
| 58 |
-
if "youtube.com" in url or "youtu.be" in url:
|
| 59 |
-
# fallback to pytubefix
|
| 60 |
-
try:
|
| 61 |
-
yt = PyTubeFixYT(url,use_po_token=True)
|
| 62 |
-
audio_stream = yt.streams.get_audio_only()
|
| 63 |
-
out_file = audio_stream.download(
|
| 64 |
-
output_path=str(out_path.parent),
|
| 65 |
-
filename=out_path.stem + ".mp4"
|
| 66 |
-
)
|
| 67 |
-
return Path(out_file)
|
| 68 |
-
except HTTPError as e:
|
| 69 |
-
raise RuntimeError(f"pytubefix download failed: {e}") from e
|
| 70 |
-
raise # re-raise for non-YouTube errors
|
| 71 |
|
| 72 |
def extract_wav(src: Path, dst: Path, start: int, dur: int = 8) -> None:
|
| 73 |
target_sr = 16000
|
|
@@ -189,4 +172,4 @@ if __name__ == "__main__":
|
|
| 189 |
parser.add_argument("--share", action="store_true",
|
| 190 |
help="Enable public share link")
|
| 191 |
args = parser.parse_args()
|
| 192 |
-
app().launch(share=args.share)
|
|
|
|
| 12 |
import gradio as gr
|
| 13 |
from speechbrain.inference.classifiers import EncoderClassifier
|
| 14 |
from yt_dlp import YoutubeDL
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# βββββββββββββββ Model setup βββββββββββββββ
|
| 17 |
ACCENT_MODEL_ID = "Jzuluaga/accent-id-commonaccent_ecapa"
|
|
|
|
| 38 |
|
| 39 |
def download_audio(url: str, out_path: Path) -> Path:
|
| 40 |
"""
|
| 41 |
+
Download best audio only via yt_dlp Python API.
|
| 42 |
+
Returns the actual file saved (could be .m4a, .webm, etc.).
|
|
|
|
| 43 |
"""
|
| 44 |
+
opts = {
|
| 45 |
"format": "bestaudio/best",
|
| 46 |
"outtmpl": str(out_path.with_suffix(".%(ext)s")),
|
| 47 |
+
"postprocessors": [],
|
| 48 |
"quiet": True,
|
| 49 |
}
|
| 50 |
+
with YoutubeDL(opts) as ydl:
|
| 51 |
+
info = ydl.extract_info(url, download=True)
|
| 52 |
+
filename = ydl.prepare_filename(info)
|
| 53 |
+
return Path(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def extract_wav(src: Path, dst: Path, start: int, dur: int = 8) -> None:
|
| 56 |
target_sr = 16000
|
|
|
|
| 172 |
parser.add_argument("--share", action="store_true",
|
| 173 |
help="Enable public share link")
|
| 174 |
args = parser.parse_args()
|
| 175 |
+
app().launch(share=args.share)
|