Realmeas commited on
Commit
3e6ca13
Β·
verified Β·
1 Parent(s): 8040db2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -4,31 +4,35 @@ 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
@@ -36,22 +40,29 @@ def generate_subtitles(video):
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()
 
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()