Realmeas commited on
Commit
f9b294f
Β·
verified Β·
1 Parent(s): 5443f5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -49
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
- def generate_auto_subtitles(video):
8
- if video is None:
9
- return "Please upload a video first!"
10
 
11
- # Temporary save video
12
- temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
13
- temp_video.write(video.read())
14
- temp_video.close()
 
 
15
 
16
- # Load Whisper model
17
- model = whisper.load_model("small")
 
 
18
 
19
- # Auto language detection + transcription
20
- result = model.transcribe(temp_video.name)
21
- detected_lang = result.get("language", "unknown").upper()
 
22
 
23
- # Prepare video
24
- video_clip = VideoFileClip(temp_video.name)
25
- subtitle_clips = []
 
 
 
26
 
27
- # Generate subtitle overlays
28
- for seg in result["segments"]:
29
- txt = seg["text"].strip()
30
- if not txt:
31
- continue
32
 
33
- subtitle = TextClip(
34
- txt,
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
- # Combine subtitles with video
46
- final = CompositeVideoClip([video_clip, *subtitle_clips])
47
- output_path = os.path.join(tempfile.gettempdir(), "vozo_auto_subs.mp4")
48
- final.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
49
 
50
- return output_path, f"Detected Language: {detected_lang}"
 
 
51
 
52
- # Gradio UI
53
- interface = gr.Interface(
54
- fn=generate_auto_subtitles,
55
- inputs=gr.Video(label="πŸŽ₯ Upload your video (Hindi / English / Hinglish)"),
56
- outputs=[
57
- gr.Video(label="🎬 Video with Auto Subtitles"),
58
- gr.Textbox(label="🌐 Detected Language")
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
- interface.launch()
 
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()