Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
|
|
|
| 3 |
import tempfile
|
| 4 |
-
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return "Please upload a video first!"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
for seg in result["segments"]:
|
| 29 |
-
txt = seg["text"].strip()
|
| 30 |
-
if not txt:
|
| 31 |
-
continue
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
fontsize=60,
|
| 36 |
-
font="DejaVu-Sans-Bold",
|
| 37 |
-
color="white",
|
| 38 |
-
bg_color="green",
|
| 39 |
-
method="caption",
|
| 40 |
-
size=(video_clip.w, None)
|
| 41 |
-
)
|
| 42 |
-
subtitle = subtitle.set_start(seg["start"]).set_duration(seg["end"] - seg["start"]).set_pos(("center", "bottom"))
|
| 43 |
-
subtitle_clips.append(subtitle)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
title="ποΈ Auto Subtitle Generator (VOZO-style)",
|
| 61 |
-
description="Upload any short Hindi, English, Hinglish, or Indian English video β this AI auto-detects language and adds VOZO-style subtitles with a green background."
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
+
import moviepy.editor as mp
|
| 4 |
import tempfile
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Load Whisper (base or small model for performance)
|
| 8 |
+
model = whisper.load_model("small")
|
|
|
|
| 9 |
|
| 10 |
+
def generate_subtitles(video):
|
| 11 |
+
try:
|
| 12 |
+
# Save temp video file
|
| 13 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
| 14 |
+
tmp.write(video.read())
|
| 15 |
+
tmp_path = tmp.name
|
| 16 |
|
| 17 |
+
# Extract audio
|
| 18 |
+
clip = mp.VideoFileClip(tmp_path)
|
| 19 |
+
audio_path = tmp_path.replace(".mp4", ".wav")
|
| 20 |
+
clip.audio.write_audiofile(audio_path, logger=None)
|
| 21 |
|
| 22 |
+
# Transcribe using Whisper
|
| 23 |
+
result = model.transcribe(audio_path, task="transcribe")
|
| 24 |
+
text = result["text"]
|
| 25 |
+
lang = result["language"]
|
| 26 |
|
| 27 |
+
# Add green background
|
| 28 |
+
w, h = clip.size
|
| 29 |
+
bg = mp.ColorClip(size=(w, h), color=(0, 255, 0), duration=clip.duration)
|
| 30 |
+
final = mp.CompositeVideoClip([bg, clip.set_position("center")])
|
| 31 |
+
output_path = tmp_path.replace(".mp4", "_subtitled.mp4")
|
| 32 |
+
final.write_videofile(output_path, codec="libx264", audio_codec="aac", logger=None)
|
| 33 |
|
| 34 |
+
return output_path, lang, text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return None, "Error", str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Gradio UI
|
| 40 |
+
with gr.Blocks(title="Auto Subtitle Generator (VOZO-style)") as app:
|
| 41 |
+
gr.Markdown("## π¬ Auto Subtitle Generator (VOZO-style)")
|
| 42 |
+
gr.Markdown("Upload a short video in Hindi, English, Hinglish, or Indian English β the AI auto-detects the language and adds VOZO-style subtitles with a green screen background.")
|
| 43 |
|
| 44 |
+
with gr.Row():
|
| 45 |
+
video_input = gr.Video(label="π₯ Upload your video")
|
| 46 |
+
video_output = gr.Video(label="πΊ Subtitled Output")
|
| 47 |
|
| 48 |
+
with gr.Row():
|
| 49 |
+
detected_lang = gr.Textbox(label="Detected Language")
|
| 50 |
+
subtitles_text = gr.Textbox(label="Generated Subtitles")
|
| 51 |
+
|
| 52 |
+
run_btn = gr.Button("π Generate")
|
| 53 |
+
|
| 54 |
+
run_btn.click(fn=generate_subtitles, inputs=[video_input],
|
| 55 |
+
outputs=[video_output, detected_lang, subtitles_text])
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
app.launch()
|