Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import gradio as gr
|
|
| 10 |
import torchaudio
|
| 11 |
import whisper.tokenizer
|
| 12 |
import ffmpeg
|
|
|
|
| 13 |
|
| 14 |
# -----------------------------
|
| 15 |
# Helper Functions
|
|
@@ -58,6 +59,21 @@ def generate_srt(segments):
|
|
| 58 |
subs.append(srt.Subtitle(index=i+1, start=start, end=end, content=text))
|
| 59 |
return srt.compose(subs)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# -----------------------------
|
| 62 |
# Transcription Functions
|
| 63 |
# -----------------------------
|
|
@@ -106,6 +122,16 @@ def process_video(youtube_url, video_file, selected_model, translate, target_lan
|
|
| 106 |
yield status, "", None
|
| 107 |
audio_path = download_youtube_audio(youtube_url)
|
| 108 |
elif video_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
status = "πΌ Extracting audio from video..."
|
| 110 |
yield status, "", None
|
| 111 |
audio_path = extract_audio_from_video(video_file.name)
|
|
@@ -173,6 +199,8 @@ model_desc_khaiii = """
|
|
| 173 |
"""
|
| 174 |
|
| 175 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
| 176 |
gr.Markdown("## π Multilingual Subtitle Generator")
|
| 177 |
gr.Markdown("Upload a video or paste a YouTube link. Automatically detect language and optionally translate subtitles.")
|
| 178 |
|
|
@@ -187,21 +215,27 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 187 |
khaiii_btn = gr.Button("β
Select Khaiii Wav2Vec2")
|
| 188 |
gr.HTML(model_desc_khaiii)
|
| 189 |
|
| 190 |
-
selected_model = gr.State(value="kotani")
|
| 191 |
-
|
| 192 |
def select_kotani():
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
def select_khaiii():
|
| 196 |
-
|
|
|
|
| 197 |
|
| 198 |
-
kotani_btn.click(fn=select_kotani, outputs=selected_model)
|
| 199 |
-
khaiii_btn.click(fn=select_khaiii, outputs=selected_model)
|
| 200 |
|
| 201 |
gr.Markdown("### π₯ Input Source")
|
| 202 |
with gr.Row():
|
| 203 |
youtube_url = gr.Textbox(label="YouTube URL", scale=2)
|
| 204 |
-
video_upload = gr.File(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
gr.Markdown("### π Translation Options")
|
| 207 |
with gr.Row():
|
|
@@ -213,7 +247,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 213 |
|
| 214 |
translate_checkbox.change(fn=toggle_translate, inputs=translate_checkbox, outputs=target_lang)
|
| 215 |
|
| 216 |
-
status_box = gr.Textbox(label="Status", interactive=False)
|
| 217 |
subtitle_preview = gr.Textbox(label="Generated Subtitles", lines=10)
|
| 218 |
download_file = gr.File(label="Download .srt File")
|
| 219 |
|
|
|
|
| 10 |
import torchaudio
|
| 11 |
import whisper.tokenizer
|
| 12 |
import ffmpeg
|
| 13 |
+
import time
|
| 14 |
|
| 15 |
# -----------------------------
|
| 16 |
# Helper Functions
|
|
|
|
| 59 |
subs.append(srt.Subtitle(index=i+1, start=start, end=end, content=text))
|
| 60 |
return srt.compose(subs)
|
| 61 |
|
| 62 |
+
# -----------------------------
|
| 63 |
+
# Model Loading Functions
|
| 64 |
+
# -----------------------------
|
| 65 |
+
|
| 66 |
+
def load_kotani_model():
|
| 67 |
+
status = "π₯ Loading Kotani Whisper Small model..."
|
| 68 |
+
whisper.load_model("small", download_root=".")
|
| 69 |
+
return status
|
| 70 |
+
|
| 71 |
+
def load_khaiii_model():
|
| 72 |
+
status = "π₯ Loading Khaiii Wav2Vec2 model..."
|
| 73 |
+
Wav2Vec2Processor.from_pretrained("khaiii/wav2vec2-xls1r-aishell-korean")
|
| 74 |
+
Wav2Vec2ForCTC.from_pretrained("khaiii/wav2vec2-xls1r-aishell-korean")
|
| 75 |
+
return status
|
| 76 |
+
|
| 77 |
# -----------------------------
|
| 78 |
# Transcription Functions
|
| 79 |
# -----------------------------
|
|
|
|
| 122 |
yield status, "", None
|
| 123 |
audio_path = download_youtube_audio(youtube_url)
|
| 124 |
elif video_file:
|
| 125 |
+
status = "πΌ Waiting for upload to complete..."
|
| 126 |
+
yield status, "", None
|
| 127 |
+
|
| 128 |
+
# Wait until file exists
|
| 129 |
+
start_time = time.time()
|
| 130 |
+
while not os.path.exists(video_file.name):
|
| 131 |
+
if time.time() - start_time > 30:
|
| 132 |
+
raise RuntimeError("Timeout: File upload took too long.")
|
| 133 |
+
time.sleep(1)
|
| 134 |
+
|
| 135 |
status = "πΌ Extracting audio from video..."
|
| 136 |
yield status, "", None
|
| 137 |
audio_path = extract_audio_from_video(video_file.name)
|
|
|
|
| 199 |
"""
|
| 200 |
|
| 201 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 202 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 203 |
+
|
| 204 |
gr.Markdown("## π Multilingual Subtitle Generator")
|
| 205 |
gr.Markdown("Upload a video or paste a YouTube link. Automatically detect language and optionally translate subtitles.")
|
| 206 |
|
|
|
|
| 215 |
khaiii_btn = gr.Button("β
Select Khaiii Wav2Vec2")
|
| 216 |
gr.HTML(model_desc_khaiii)
|
| 217 |
|
|
|
|
|
|
|
| 218 |
def select_kotani():
|
| 219 |
+
msg = load_kotani_model()
|
| 220 |
+
return "kotani", msg
|
| 221 |
|
| 222 |
def select_khaiii():
|
| 223 |
+
msg = load_khaiii_model()
|
| 224 |
+
return "khaiii", msg
|
| 225 |
|
| 226 |
+
kotani_btn.click(fn=select_kotani, outputs=[selected_model, status_box])
|
| 227 |
+
khaiii_btn.click(fn=select_khaiii, outputs=[selected_model, status_box])
|
| 228 |
|
| 229 |
gr.Markdown("### π₯ Input Source")
|
| 230 |
with gr.Row():
|
| 231 |
youtube_url = gr.Textbox(label="YouTube URL", scale=2)
|
| 232 |
+
video_upload = gr.File(
|
| 233 |
+
label="Upload Video",
|
| 234 |
+
type="filepath",
|
| 235 |
+
file_types=["video"],
|
| 236 |
+
elem_props={"accept": "video/*"},
|
| 237 |
+
scale=1
|
| 238 |
+
)
|
| 239 |
|
| 240 |
gr.Markdown("### π Translation Options")
|
| 241 |
with gr.Row():
|
|
|
|
| 247 |
|
| 248 |
translate_checkbox.change(fn=toggle_translate, inputs=translate_checkbox, outputs=target_lang)
|
| 249 |
|
|
|
|
| 250 |
subtitle_preview = gr.Textbox(label="Generated Subtitles", lines=10)
|
| 251 |
download_file = gr.File(label="Download .srt File")
|
| 252 |
|