Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,31 +4,35 @@ import moviepy.editor as mp
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Load Whisper (
|
| 8 |
-
model = whisper.load_model("
|
| 9 |
|
| 10 |
-
def generate_subtitles(
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
# Extract audio
|
| 18 |
-
clip = mp.VideoFileClip(
|
| 19 |
-
audio_path =
|
| 20 |
clip.audio.write_audiofile(audio_path, logger=None)
|
| 21 |
|
| 22 |
-
# Transcribe using Whisper
|
| 23 |
-
result = model.transcribe(audio_path
|
| 24 |
text = result["text"]
|
| 25 |
lang = result["language"]
|
| 26 |
|
| 27 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
| 32 |
final.write_videofile(output_path, codec="libx264", audio_codec="aac", logger=None)
|
| 33 |
|
| 34 |
return output_path, lang, text
|
|
@@ -36,22 +40,29 @@ def generate_subtitles(video):
|
|
| 36 |
except Exception as e:
|
| 37 |
return None, "Error", str(e)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
-
detected_lang = gr.Textbox(label="Detected Language")
|
| 50 |
-
|
| 51 |
|
| 52 |
-
run_btn = gr.Button("π Generate")
|
| 53 |
|
| 54 |
-
run_btn.click(fn=generate_subtitles, inputs=[
|
| 55 |
-
outputs=[
|
| 56 |
|
| 57 |
-
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Load Whisper model (tiny = fast, small = accurate)
|
| 8 |
+
model = whisper.load_model("tiny")
|
| 9 |
|
| 10 |
+
def generate_subtitles(video_path):
|
| 11 |
try:
|
| 12 |
+
# Create temporary directory
|
| 13 |
+
temp_dir = tempfile.mkdtemp()
|
| 14 |
+
input_path = os.path.join(temp_dir, "input.mp4")
|
| 15 |
+
os.rename(video_path, input_path)
|
| 16 |
|
| 17 |
# Extract audio
|
| 18 |
+
clip = mp.VideoFileClip(input_path)
|
| 19 |
+
audio_path = os.path.join(temp_dir, "audio.wav")
|
| 20 |
clip.audio.write_audiofile(audio_path, logger=None)
|
| 21 |
|
| 22 |
+
# Transcribe audio using Whisper
|
| 23 |
+
result = model.transcribe(audio_path)
|
| 24 |
text = result["text"]
|
| 25 |
lang = result["language"]
|
| 26 |
|
| 27 |
+
# Make green background video
|
| 28 |
w, h = clip.size
|
| 29 |
bg = mp.ColorClip(size=(w, h), color=(0, 255, 0), duration=clip.duration)
|
| 30 |
+
|
| 31 |
+
# Combine background + video
|
| 32 |
final = mp.CompositeVideoClip([bg, clip.set_position("center")])
|
| 33 |
+
|
| 34 |
+
# Save final video
|
| 35 |
+
output_path = os.path.join(temp_dir, "output.mp4")
|
| 36 |
final.write_videofile(output_path, codec="libx264", audio_codec="aac", logger=None)
|
| 37 |
|
| 38 |
return output_path, lang, text
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return None, "Error", str(e)
|
| 42 |
|
| 43 |
+
|
| 44 |
+
# ----------- UI (VOZO Blink Style) -----------
|
| 45 |
+
with gr.Blocks(title="VOZO-style Auto Subtitle Generator") as demo:
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
"""
|
| 48 |
+
# π¬ **Auto Subtitle Generator (VOZO Blink Style)**
|
| 49 |
+
Upload any short Hindi, English, Hinglish, or Indian English video.<br>
|
| 50 |
+
This AI auto-detects the language and generates subtitles with a green background overlay.<br>
|
| 51 |
+
_(Inspired by Blink Captions / VOZO)_
|
| 52 |
+
"""
|
| 53 |
+
)
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
+
input_video = gr.Video(label="π₯ Upload your video", interactive=True)
|
| 57 |
+
output_video = gr.Video(label="πΊ Subtitled Output")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
detected_lang = gr.Textbox(label="π Detected Language")
|
| 61 |
+
subtitle_text = gr.Textbox(label="π Generated Subtitles")
|
| 62 |
|
| 63 |
+
run_btn = gr.Button("π Generate Subtitles", variant="primary")
|
| 64 |
|
| 65 |
+
run_btn.click(fn=generate_subtitles, inputs=[input_video],
|
| 66 |
+
outputs=[output_video, detected_lang, subtitle_text])
|
| 67 |
|
| 68 |
+
demo.launch()
|