Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
MODEL_SIZE = "small" # Change to "base", "small", "medium", or "large"
|
| 8 |
+
|
| 9 |
+
class GradioSRTGenerator:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
print(f"Loading Whisper model: {MODEL_SIZE}")
|
| 12 |
+
self.model = whisper.load_model(MODEL_SIZE)
|
| 13 |
+
print("Model loaded successfully")
|
| 14 |
+
|
| 15 |
+
@staticmethod
|
| 16 |
+
def format_time(seconds: float) -> str:
|
| 17 |
+
"""Convert seconds to SRT time format (HH:MM:SS,mmm)"""
|
| 18 |
+
hours = int(seconds // 3600)
|
| 19 |
+
minutes = int((seconds % 3600) // 60)
|
| 20 |
+
secs = int(seconds % 60)
|
| 21 |
+
millis = int((seconds - int(seconds)) * 1000)
|
| 22 |
+
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
|
| 23 |
+
|
| 24 |
+
def build_srt_file(self, segments, srt_path: str):
|
| 25 |
+
"""Write Whisper segments into an SRT file at srt_path."""
|
| 26 |
+
with open(srt_path, "w", encoding="utf-8") as srt_file:
|
| 27 |
+
for i, seg in enumerate(segments, start=1):
|
| 28 |
+
start_time = self.format_time(seg["start"])
|
| 29 |
+
end_time = self.format_time(seg["end"])
|
| 30 |
+
text = seg["text"].strip()
|
| 31 |
+
srt_file.write(f"{i}\n")
|
| 32 |
+
srt_file.write(f"{start_time} --> {end_time}\n")
|
| 33 |
+
srt_file.write(f"{text}\n\n")
|
| 34 |
+
|
| 35 |
+
def generate_srt(self, video_path, progress=gr.Progress()):
|
| 36 |
+
"""
|
| 37 |
+
Transcribe the uploaded video and produce an SRT file.
|
| 38 |
+
Returns: (path_to_srt, status_message)
|
| 39 |
+
"""
|
| 40 |
+
if not video_path:
|
| 41 |
+
return None, "β οΈ Please upload a video first."
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
progress(0.1, desc="Transcribing audio with Whisper...")
|
| 45 |
+
result = self.model.transcribe(video_path, task="translate")
|
| 46 |
+
progress(0.6, desc="Building SRT file...")
|
| 47 |
+
|
| 48 |
+
# Create a temporary file to hold the SRT
|
| 49 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt", mode="w", encoding="utf-8") as tmp:
|
| 50 |
+
srt_path = tmp.name
|
| 51 |
+
|
| 52 |
+
# Write segments into that SRT
|
| 53 |
+
self.build_srt_file(result["segments"], srt_path)
|
| 54 |
+
progress(1.0, desc="β
SRT ready!")
|
| 55 |
+
|
| 56 |
+
return srt_path, "β
Transcription complete. Download your SRT below."
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return None, f"β Error during transcription: {str(e)}"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def create_ui():
|
| 63 |
+
generator = GradioSRTGenerator()
|
| 64 |
+
|
| 65 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="blue", secondary_hue="indigo")) as app:
|
| 66 |
+
gr.Markdown(
|
| 67 |
+
"""
|
| 68 |
+
# π Video β SRT Generator (Queued)
|
| 69 |
+
|
| 70 |
+
Only one transcription job will run at a time; additional users will be placed in a queue.
|
| 71 |
+
Upload any supported video file (MP4, MOV, AVI, MKV, etc.) and click **Generate SRT**.
|
| 72 |
+
Whisper will transcribe and produce an SRT subtitle file you can download immediately.
|
| 73 |
+
"""
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column():
|
| 78 |
+
input_video = gr.Video(label="Upload Video", source="upload", interactive=True)
|
| 79 |
+
generate_btn = gr.Button("π― Generate SRT", variant="primary")
|
| 80 |
+
status_text = gr.Textbox(label="Status", interactive=False, show_copy_button=True)
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
srt_download = gr.File(label="Download SRT", interactive=False, visible=False)
|
| 84 |
+
|
| 85 |
+
# Bind the button and wrap the handler in .queue()
|
| 86 |
+
generate_btn.click(
|
| 87 |
+
fn=generator.generate_srt,
|
| 88 |
+
inputs=[input_video],
|
| 89 |
+
outputs=[srt_download, status_text],
|
| 90 |
+
api_name="generate_srt"
|
| 91 |
+
).queue() # <-- ensures this function is queued if another request is running
|
| 92 |
+
|
| 93 |
+
# Add a queue to the entire Blocks app with exactly 1 worker
|
| 94 |
+
app.queue(concurrency_count=1, max_size=10)
|
| 95 |
+
return app
|
| 96 |
+
|
| 97 |
+
app = create_ui()
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
app.launch()
|