Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load a smaller model for faster build on Spaces (change to "large-v3" if you have resources)
|
| 6 |
+
model = whisper.load_model("base")
|
| 7 |
+
|
| 8 |
+
def format_time(seconds):
|
| 9 |
+
h = int(seconds // 3600)
|
| 10 |
+
m = int((seconds % 3600) // 60)
|
| 11 |
+
s = int(seconds % 60)
|
| 12 |
+
ms = int((seconds - int(seconds)) * 1000)
|
| 13 |
+
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
| 14 |
+
|
| 15 |
+
def generate_srt(video_path):
|
| 16 |
+
# video_path is path to uploaded file on server
|
| 17 |
+
result = model.transcribe(video_path, language="en", task="transcribe")
|
| 18 |
+
segments = result.get("segments", [])
|
| 19 |
+
|
| 20 |
+
srt_lines = []
|
| 21 |
+
for i, seg in enumerate(segments, start=1):
|
| 22 |
+
start = seg["start"]
|
| 23 |
+
end = seg["end"]
|
| 24 |
+
text = seg["text"].strip()
|
| 25 |
+
srt_lines.append(str(i))
|
| 26 |
+
srt_lines.append(f"{format_time(start)} --> {format_time(end)}")
|
| 27 |
+
srt_lines.append(text)
|
| 28 |
+
srt_lines.append("") # blank line
|
| 29 |
+
|
| 30 |
+
srt_text = "\n".join(srt_lines)
|
| 31 |
+
srt_path = os.path.join(os.getcwd(), "subtitles.srt")
|
| 32 |
+
with open(srt_path, "w", encoding="utf-8") as f:
|
| 33 |
+
f.write(srt_text)
|
| 34 |
+
|
| 35 |
+
return srt_path
|
| 36 |
+
|
| 37 |
+
def process(video):
|
| 38 |
+
# Gradio passes a file path for uploaded file
|
| 39 |
+
if isinstance(video, dict):
|
| 40 |
+
# older gradio versions sometimes pass dict with "name"
|
| 41 |
+
video_path = video.get("name") or video.get("filename") or list(video.values())[0]
|
| 42 |
+
else:
|
| 43 |
+
video_path = video
|
| 44 |
+
return generate_srt(video_path)
|
| 45 |
+
|
| 46 |
+
demo = gr.Interface(
|
| 47 |
+
fn=process,
|
| 48 |
+
inputs=gr.Video(source="upload", label="Upload your video"),
|
| 49 |
+
outputs=gr.File(label="Download SRT (.srt)"),
|
| 50 |
+
title="BollySubs — Indian English Subtitle Generator",
|
| 51 |
+
description="Upload a video and get SRT subtitles optimized for Indian English accents."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|