Update app.py
Browse files
app.py
CHANGED
|
@@ -100,9 +100,10 @@ def is_youtube_url(url):
|
|
| 100 |
return True
|
| 101 |
return False
|
| 102 |
|
| 103 |
-
def extract_youtube_audio(url, max_duration_sec=300):
|
| 104 |
"""Extract audio from YouTube video (first max_duration_sec seconds).
|
| 105 |
Returns path to the downloaded audio file.
|
|
|
|
| 106 |
"""
|
| 107 |
import yt_dlp
|
| 108 |
from yt_dlp.utils import download_range_func
|
|
@@ -124,6 +125,9 @@ def extract_youtube_audio(url, max_duration_sec=300):
|
|
| 124 |
'no_warnings': True,
|
| 125 |
}
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 128 |
info = ydl.extract_info(url, download=True)
|
| 129 |
title = info.get('title', 'YouTube Video')
|
|
@@ -533,7 +537,7 @@ def hash_file(filepath):
|
|
| 533 |
return hashlib.md5(f.read(1024*1024)).hexdigest()
|
| 534 |
|
| 535 |
@spaces.GPU(duration=120)
|
| 536 |
-
def process_pdf(pdf_file, url_input, yt_url_input, translit_lang, translit_format, target_lang, max_text_char, repetition_penalty_val, last_source_hash, last_korean_words, progress=gr.Progress()):
|
| 537 |
global tts, voice_style
|
| 538 |
|
| 539 |
# Clean language choices from "Family - Language" to just "Language"
|
|
@@ -573,7 +577,8 @@ def process_pdf(pdf_file, url_input, yt_url_input, translit_lang, translit_forma
|
|
| 573 |
try:
|
| 574 |
if is_youtube:
|
| 575 |
progress(0, desc="Downloading YouTube audio (first 5 min)...")
|
| 576 |
-
|
|
|
|
| 577 |
|
| 578 |
progress(0.1, desc=f"Transcribing audio with Cohere ASR...")
|
| 579 |
content_text = transcribe_audio_with_asr(audio_path)
|
|
@@ -1179,6 +1184,9 @@ def create_demo():
|
|
| 1179 |
placeholder=r"e.g. https://www.youtube.com/watch?v=...",
|
| 1180 |
value="https://www.youtube.com/watch?v=9Nj7l73PBWE",
|
| 1181 |
info="Audio from the first 5 minutes will be transcribed using Cohere ASR")
|
|
|
|
|
|
|
|
|
|
| 1182 |
|
| 1183 |
pdf_input = gr.File(label="Or Upload Book PDF 📚", file_types=[".pdf"], value=example_pdf)
|
| 1184 |
|
|
@@ -1221,7 +1229,7 @@ def create_demo():
|
|
| 1221 |
|
| 1222 |
generate_event = submit_btn.click(
|
| 1223 |
fn=process_pdf,
|
| 1224 |
-
inputs=[pdf_input, url_input, yt_url_input, translit_lang, translit_format, target_lang, max_text_char_input, repetition_penalty_input, last_source_state, last_korean_words_state],
|
| 1225 |
outputs=[output_html, last_source_state, last_korean_words_state, stream_box, extracted_text_box, extracted_images_gallery, extracted_audio_player]
|
| 1226 |
)
|
| 1227 |
|
|
|
|
| 100 |
return True
|
| 101 |
return False
|
| 102 |
|
| 103 |
+
def extract_youtube_audio(url, max_duration_sec=300, cookiefile=None):
|
| 104 |
"""Extract audio from YouTube video (first max_duration_sec seconds).
|
| 105 |
Returns path to the downloaded audio file.
|
| 106 |
+
cookiefile: optional path to a Netscape-format cookies.txt to bypass bot filtering.
|
| 107 |
"""
|
| 108 |
import yt_dlp
|
| 109 |
from yt_dlp.utils import download_range_func
|
|
|
|
| 125 |
'no_warnings': True,
|
| 126 |
}
|
| 127 |
|
| 128 |
+
if cookiefile:
|
| 129 |
+
ydl_opts['cookiefile'] = cookiefile
|
| 130 |
+
|
| 131 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 132 |
info = ydl.extract_info(url, download=True)
|
| 133 |
title = info.get('title', 'YouTube Video')
|
|
|
|
| 537 |
return hashlib.md5(f.read(1024*1024)).hexdigest()
|
| 538 |
|
| 539 |
@spaces.GPU(duration=120)
|
| 540 |
+
def process_pdf(pdf_file, url_input, yt_url_input, yt_cookies_file, translit_lang, translit_format, target_lang, max_text_char, repetition_penalty_val, last_source_hash, last_korean_words, progress=gr.Progress()):
|
| 541 |
global tts, voice_style
|
| 542 |
|
| 543 |
# Clean language choices from "Family - Language" to just "Language"
|
|
|
|
| 577 |
try:
|
| 578 |
if is_youtube:
|
| 579 |
progress(0, desc="Downloading YouTube audio (first 5 min)...")
|
| 580 |
+
cookies_path = yt_cookies_file.name if yt_cookies_file else None
|
| 581 |
+
audio_path, yt_title = extract_youtube_audio(yt_url_input.strip(), max_duration_sec=300, cookiefile=cookies_path)
|
| 582 |
|
| 583 |
progress(0.1, desc=f"Transcribing audio with Cohere ASR...")
|
| 584 |
content_text = transcribe_audio_with_asr(audio_path)
|
|
|
|
| 1184 |
placeholder=r"e.g. https://www.youtube.com/watch?v=...",
|
| 1185 |
value="https://www.youtube.com/watch?v=9Nj7l73PBWE",
|
| 1186 |
info="Audio from the first 5 minutes will be transcribed using Cohere ASR")
|
| 1187 |
+
yt_cookies_input = gr.File(label="YouTube Cookies (cookies.txt)", file_types=[".txt"], value=None,
|
| 1188 |
+
type="filepath",
|
| 1189 |
+
info="Optional. Helps bypass YouTube bot detection. Get it: install 'cookies.txt' extension for Firefox (https://addons.mozilla.org/firefox/addon/cookies-txt/) or Chrome, go to youtube.com while logged in, click the extension icon → 'Current Site' to export.")
|
| 1190 |
|
| 1191 |
pdf_input = gr.File(label="Or Upload Book PDF 📚", file_types=[".pdf"], value=example_pdf)
|
| 1192 |
|
|
|
|
| 1229 |
|
| 1230 |
generate_event = submit_btn.click(
|
| 1231 |
fn=process_pdf,
|
| 1232 |
+
inputs=[pdf_input, url_input, yt_url_input, yt_cookies_input, translit_lang, translit_format, target_lang, max_text_char_input, repetition_penalty_input, last_source_state, last_korean_words_state],
|
| 1233 |
outputs=[output_html, last_source_state, last_korean_words_state, stream_box, extracted_text_box, extracted_images_gallery, extracted_audio_player]
|
| 1234 |
)
|
| 1235 |
|