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,14 +31,14 @@ 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 --------
|
|
@@ -61,28 +59,21 @@ def extract_audio(input_path: str, out_path: str):
|
|
| 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 |
-
# Handle Hugging Face NamedString / Path
|
| 68 |
input_path = Path(file_obj.name)
|
|
|
|
| 69 |
if not input_path.exists():
|
| 70 |
input_path = tmp_dir / Path(file_obj.name).name
|
| 71 |
-
|
| 72 |
-
|
| 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 |
|
| 85 |
-
# Save .srt file
|
| 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)
|
|
@@ -97,44 +88,72 @@ with gr.Blocks(title="AI Subtitle Generator") as demo:
|
|
| 97 |
return "dark" if current == "light" else "light"
|
| 98 |
|
| 99 |
def apply_theme(theme_mode):
|
|
|
|
| 100 |
if theme_mode == "dark":
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
else:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
gr.HTML("<h1 style='text-align:center;'>π¬ AI Subtitle Generator</h1>")
|
| 111 |
-
gr.HTML(
|
| 112 |
-
"<p style='text-align:center;'>Upload a video or audio file to generate English <b>.srt</b> subtitles.</p>"
|
| 113 |
-
)
|
| 114 |
|
| 115 |
style_box = gr.HTML("")
|
| 116 |
theme_btn = gr.Button("π Toggle Light/Dark Mode")
|
| 117 |
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
input_file = gr.File(label="Upload video/audio 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 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
generate_btn.click(on_click, inputs=[input_file], outputs=[output_file, status_box])
|
|
|
|
| 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"
|
| 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.")
|
| 42 |
|
| 43 |
|
| 44 |
# -------- Core functions --------
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def transcribe_file_to_srt(file_obj, language: str = "en"):
|
| 62 |
+
"""Transcribe uploaded file to SRT"""
|
| 63 |
tmp_dir = Path(tempfile.mkdtemp(prefix="subgen_"))
|
|
|
|
|
|
|
| 64 |
input_path = Path(file_obj.name)
|
| 65 |
+
|
| 66 |
if not input_path.exists():
|
| 67 |
input_path = tmp_dir / Path(file_obj.name).name
|
| 68 |
+
with open(input_path, "wb") as f:
|
| 69 |
+
f.write(file_obj.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 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 |
segs = [{"start": s.start, "end": s.end, "text": s.text} for s in segments]
|
| 75 |
srt_text = segments_to_srt(segs)
|
| 76 |
|
|
|
|
| 77 |
output_path = OUTPUT_DIR / f"{Path(file_obj.name).stem}.srt"
|
| 78 |
with open(output_path, "w", encoding="utf-8") as f:
|
| 79 |
f.write(srt_text)
|
|
|
|
| 88 |
return "dark" if current == "light" else "light"
|
| 89 |
|
| 90 |
def apply_theme(theme_mode):
|
| 91 |
+
"""Apply CSS for dark/light theme dynamically"""
|
| 92 |
if theme_mode == "dark":
|
| 93 |
+
gradient = "linear-gradient(135deg, #0f2027, #203a43, #2c5364)"
|
| 94 |
+
text_color = "#ffffff"
|
| 95 |
+
btn_color = "#00adb5"
|
| 96 |
else:
|
| 97 |
+
gradient = "linear-gradient(135deg, #fdfbfb, #ebedee)"
|
| 98 |
+
text_color = "#000000"
|
| 99 |
+
btn_color = "#1e90ff"
|
| 100 |
+
|
| 101 |
+
style = f"""
|
| 102 |
+
<style>
|
| 103 |
+
body {{
|
| 104 |
+
background: {gradient};
|
| 105 |
+
color: {text_color};
|
| 106 |
+
transition: background 0.5s ease, color 0.5s ease;
|
| 107 |
+
}}
|
| 108 |
+
.gr-button {{
|
| 109 |
+
background-color: {btn_color} !important;
|
| 110 |
+
color: white !important;
|
| 111 |
+
border-radius: 10px !important;
|
| 112 |
+
transition: background 0.3s ease;
|
| 113 |
+
}}
|
| 114 |
+
</style>
|
| 115 |
+
"""
|
| 116 |
+
return gr.update(value=style)
|
| 117 |
+
|
| 118 |
+
# Header
|
| 119 |
gr.HTML("<h1 style='text-align:center;'>π¬ AI Subtitle Generator</h1>")
|
| 120 |
+
gr.HTML("<p style='text-align:center;'>Upload a video or audio file to generate English <b>.srt</b> subtitles.</p>")
|
|
|
|
|
|
|
| 121 |
|
| 122 |
style_box = gr.HTML("")
|
| 123 |
theme_btn = gr.Button("π Toggle Light/Dark Mode")
|
| 124 |
|
| 125 |
+
# Input / Output
|
| 126 |
with gr.Row():
|
| 127 |
input_file = gr.File(label="Upload video/audio file")
|
| 128 |
output_file = gr.File(label="Download .srt file")
|
| 129 |
|
| 130 |
status_box = gr.Textbox(label="Status", interactive=False)
|
| 131 |
|
| 132 |
+
# Buttons
|
| 133 |
+
with gr.Row():
|
| 134 |
+
generate_btn = gr.Button("π Generate Subtitles")
|
| 135 |
+
clear_btn = gr.Button("π§Ή Clear Chat")
|
| 136 |
+
|
| 137 |
+
# --- Logic ---
|
| 138 |
def on_click(file):
|
| 139 |
+
if not file:
|
| 140 |
+
return None, "β οΈ Please upload a file first!"
|
| 141 |
srt_path, msg = transcribe_file_to_srt(file)
|
| 142 |
return srt_path, msg
|
| 143 |
|
| 144 |
+
def clear_fields():
|
| 145 |
+
"""Reset all UI components"""
|
| 146 |
+
return None, None, ""
|
| 147 |
+
|
| 148 |
+
# Theme button actions
|
| 149 |
+
theme_btn.click(toggle_theme, inputs=[theme_state], outputs=[theme_state]) \
|
| 150 |
+
.then(apply_theme, inputs=[theme_state], outputs=[style_box])
|
| 151 |
|
| 152 |
+
# Main buttons
|
| 153 |
generate_btn.click(on_click, inputs=[input_file], outputs=[output_file, status_box])
|
| 154 |
+
clear_btn.click(clear_fields, outputs=[input_file, output_file, status_box])
|
| 155 |
|
| 156 |
+
gr.HTML("<p style='text-align:center;font-size:14px;opacity:0.7;'>Powered by Faster-Whisper + Gradio</p>")
|
|
|
|
|
|
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
demo.launch()
|