Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
-
import uuid
|
| 5 |
from pathlib import Path
|
| 6 |
import gradio as gr
|
| 7 |
import ffmpeg
|
|
@@ -33,19 +31,19 @@ def segments_to_srt(segments: list) -> str:
|
|
| 33 |
|
| 34 |
|
| 35 |
# -------- Config --------
|
| 36 |
-
MODEL_NAME = "Systran/faster-whisper-small" #
|
| 37 |
DEVICE = "cpu"
|
| 38 |
OUTPUT_DIR = Path("outputs/subtitles")
|
| 39 |
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 40 |
|
| 41 |
print(f"Loading model {MODEL_NAME} on {DEVICE} ...")
|
| 42 |
model = WhisperModel(MODEL_NAME, device=DEVICE)
|
| 43 |
-
print("Model loaded.")
|
| 44 |
|
| 45 |
|
| 46 |
# -------- Core functions --------
|
| 47 |
def extract_audio(input_path: str, out_path: str):
|
| 48 |
-
"""
|
| 49 |
try:
|
| 50 |
(
|
| 51 |
ffmpeg
|
|
@@ -57,28 +55,23 @@ def extract_audio(input_path: str, out_path: str):
|
|
| 57 |
except ffmpeg.Error as e:
|
| 58 |
stderr = getattr(e, "stderr", None)
|
| 59 |
msg = stderr.decode() if stderr else str(e)
|
| 60 |
-
raise RuntimeError(f"
|
| 61 |
|
| 62 |
|
| 63 |
def transcribe_file_to_srt(file_obj, language: str = "en"):
|
| 64 |
-
"""Transcribe uploaded file to SRT
|
| 65 |
tmp_dir = Path(tempfile.mkdtemp(prefix="subgen_"))
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
input_path = Path(file_obj.name)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if hasattr(file_obj, "read_bytes"):
|
| 72 |
-
with open(input_path, "wb") as f:
|
| 73 |
-
f.write(file_obj.read_bytes())
|
| 74 |
-
else:
|
| 75 |
-
with open(file_obj.name, "rb") as src, open(input_path, "wb") as dst:
|
| 76 |
-
dst.write(src.read())
|
| 77 |
|
| 78 |
# Extract audio and transcribe
|
| 79 |
audio_path = tmp_dir / "audio.wav"
|
| 80 |
extract_audio(str(input_path), str(audio_path))
|
| 81 |
segments, _ = model.transcribe(str(audio_path), language=language)
|
|
|
|
| 82 |
segs = [{"start": s.start, "end": s.end, "text": s.text} for s in segments]
|
| 83 |
srt_text = segments_to_srt(segs)
|
| 84 |
|
|
@@ -86,6 +79,7 @@ def transcribe_file_to_srt(file_obj, language: str = "en"):
|
|
| 86 |
output_path = OUTPUT_DIR / f"{Path(file_obj.name).stem}.srt"
|
| 87 |
with open(output_path, "w", encoding="utf-8") as f:
|
| 88 |
f.write(srt_text)
|
|
|
|
| 89 |
return str(output_path), "β
Subtitles generated successfully!"
|
| 90 |
|
| 91 |
|
|
@@ -116,25 +110,44 @@ with gr.Blocks(title="AI Subtitle Generator") as demo:
|
|
| 116 |
theme_btn = gr.Button("π Toggle Light/Dark Mode")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
-
input_file = gr.File(
|
|
|
|
|
|
|
|
|
|
| 120 |
output_file = gr.File(label="Download .srt file")
|
| 121 |
|
| 122 |
status_box = gr.Textbox(label="Status", interactive=False)
|
| 123 |
|
| 124 |
def on_click(file):
|
|
|
|
|
|
|
| 125 |
srt_path, msg = transcribe_file_to_srt(file)
|
| 126 |
return srt_path, msg
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
theme_btn.click(
|
| 129 |
toggle_theme, inputs=[theme_state], outputs=[theme_state]
|
| 130 |
).then(apply_theme, inputs=[theme_state], outputs=[style_box])
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
gr.HTML(
|
| 136 |
"<p style='text-align:center;font-size:14px;opacity:0.7;'>Powered by Faster-Whisper + Gradio UI</p>"
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
import ffmpeg
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# -------- Config --------
|
| 34 |
+
MODEL_NAME = "Systran/faster-whisper-small" # best for HF CPU
|
| 35 |
DEVICE = "cpu"
|
| 36 |
OUTPUT_DIR = Path("outputs/subtitles")
|
| 37 |
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 38 |
|
| 39 |
print(f"Loading model {MODEL_NAME} on {DEVICE} ...")
|
| 40 |
model = WhisperModel(MODEL_NAME, device=DEVICE)
|
| 41 |
+
print("Model loaded successfully.")
|
| 42 |
|
| 43 |
|
| 44 |
# -------- Core functions --------
|
| 45 |
def extract_audio(input_path: str, out_path: str):
|
| 46 |
+
"""Extract mono 16kHz WAV using ffmpeg"""
|
| 47 |
try:
|
| 48 |
(
|
| 49 |
ffmpeg
|
|
|
|
| 55 |
except ffmpeg.Error as e:
|
| 56 |
stderr = getattr(e, "stderr", None)
|
| 57 |
msg = stderr.decode() if stderr else str(e)
|
| 58 |
+
raise RuntimeError(f"FFmpeg error: {msg}")
|
| 59 |
|
| 60 |
|
| 61 |
def transcribe_file_to_srt(file_obj, language: str = "en"):
|
| 62 |
+
"""Transcribe uploaded file to SRT (compatible with HF Spaces)."""
|
| 63 |
tmp_dir = Path(tempfile.mkdtemp(prefix="subgen_"))
|
| 64 |
|
| 65 |
+
# Ensure we can read uploaded file correctly
|
| 66 |
+
input_path = tmp_dir / Path(file_obj.name).name
|
| 67 |
+
with open(input_path, "wb") as f:
|
| 68 |
+
f.write(file_obj.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Extract audio and transcribe
|
| 71 |
audio_path = tmp_dir / "audio.wav"
|
| 72 |
extract_audio(str(input_path), str(audio_path))
|
| 73 |
segments, _ = model.transcribe(str(audio_path), language=language)
|
| 74 |
+
|
| 75 |
segs = [{"start": s.start, "end": s.end, "text": s.text} for s in segments]
|
| 76 |
srt_text = segments_to_srt(segs)
|
| 77 |
|
|
|
|
| 79 |
output_path = OUTPUT_DIR / f"{Path(file_obj.name).stem}.srt"
|
| 80 |
with open(output_path, "w", encoding="utf-8") as f:
|
| 81 |
f.write(srt_text)
|
| 82 |
+
|
| 83 |
return str(output_path), "β
Subtitles generated successfully!"
|
| 84 |
|
| 85 |
|
|
|
|
| 110 |
theme_btn = gr.Button("π Toggle Light/Dark Mode")
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
+
input_file = gr.File(
|
| 114 |
+
label="Upload video/audio file",
|
| 115 |
+
file_types=["audio/*", "video/*"]
|
| 116 |
+
)
|
| 117 |
output_file = gr.File(label="Download .srt file")
|
| 118 |
|
| 119 |
status_box = gr.Textbox(label="Status", interactive=False)
|
| 120 |
|
| 121 |
def on_click(file):
|
| 122 |
+
if file is None:
|
| 123 |
+
return None, "β οΈ Please upload a file first!"
|
| 124 |
srt_path, msg = transcribe_file_to_srt(file)
|
| 125 |
return srt_path, msg
|
| 126 |
|
| 127 |
+
def clear_all():
|
| 128 |
+
return None, None, ""
|
| 129 |
+
|
| 130 |
theme_btn.click(
|
| 131 |
toggle_theme, inputs=[theme_state], outputs=[theme_state]
|
| 132 |
).then(apply_theme, inputs=[theme_state], outputs=[style_box])
|
| 133 |
|
| 134 |
+
with gr.Row():
|
| 135 |
+
generate_btn = gr.Button("π§ Generate Subtitles")
|
| 136 |
+
clear_btn = gr.Button("π§Ή Clear")
|
| 137 |
+
|
| 138 |
+
generate_btn.click(
|
| 139 |
+
on_click, inputs=[input_file], outputs=[output_file, status_box]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
clear_btn.click(
|
| 143 |
+
clear_all,
|
| 144 |
+
inputs=[],
|
| 145 |
+
outputs=[input_file, output_file, status_box],
|
| 146 |
+
)
|
| 147 |
|
| 148 |
gr.HTML(
|
| 149 |
"<p style='text-align:center;font-size:14px;opacity:0.7;'>Powered by Faster-Whisper + Gradio UI</p>"
|
| 150 |
)
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
demo.queue(concurrency_count=1).launch()
|