Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,125 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import tempfile
|
4 |
-
|
5 |
-
|
6 |
-
from
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
#
|
|
|
10 |
LANGUAGES = {
|
11 |
"English": "en",
|
12 |
-
"Spanish": "es",
|
13 |
"French": "fr",
|
14 |
"German": "de",
|
15 |
"Japanese": "ja",
|
16 |
"Hindi": "hi"
|
17 |
}
|
18 |
|
19 |
-
def
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
with tempfile.TemporaryDirectory() as tmpdir:
|
27 |
try:
|
28 |
-
progress(0.1,
|
29 |
audio_path = extract_audio(video_file.name, tmpdir)
|
30 |
|
31 |
-
progress(0.3,
|
32 |
srt_path = generate_subtitles(audio_path, tmpdir)
|
33 |
|
34 |
-
progress(0.5,
|
35 |
translated_subs = translate_subtitles(
|
36 |
srt_path,
|
37 |
[LANGUAGES[lang] for lang in target_langs],
|
38 |
tmpdir
|
39 |
)
|
40 |
|
41 |
-
progress(0.7,
|
42 |
output_files = []
|
43 |
for lang, sub_path in translated_subs.items():
|
44 |
output_path = os.path.join(tmpdir, f"output_{lang}.mp4")
|
45 |
-
merge_video_subtitles(
|
46 |
-
video_file.name,
|
47 |
-
sub_path,
|
48 |
-
output_path
|
49 |
-
)
|
50 |
output_files.append(output_path)
|
51 |
|
52 |
-
progress(1.0,
|
53 |
return output_files
|
54 |
|
55 |
except Exception as e:
|
56 |
raise gr.Error(f"Processing failed: {str(e)}")
|
57 |
|
58 |
-
# Gradio
|
59 |
with gr.Blocks(title="Video Translator") as demo:
|
60 |
-
gr.Markdown("
|
61 |
-
gr.Markdown("Upload a video to generate multilingual versions")
|
62 |
|
63 |
with gr.Row():
|
64 |
with gr.Column():
|
@@ -73,13 +134,13 @@ with gr.Blocks(title="Video Translator") as demo:
|
|
73 |
choices=list(LANGUAGES.keys()),
|
74 |
value=["Spanish", "French"]
|
75 |
)
|
76 |
-
submit_btn = gr.Button("Translate
|
77 |
|
78 |
with gr.Column():
|
79 |
output_videos = gr.Files(label="Translated Videos")
|
80 |
|
81 |
submit_btn.click(
|
82 |
-
|
83 |
inputs=[video_input, source_lang, target_langs],
|
84 |
outputs=output_videos
|
85 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import tempfile
|
4 |
+
import moviepy.editor as mp
|
5 |
+
import assemblyai as aai
|
6 |
+
from deep_translator import GoogleTranslator
|
7 |
+
import pysrt
|
8 |
+
from gtts import gTTS
|
9 |
+
from pydub import AudioSegment
|
10 |
|
11 |
+
# Configuration
|
12 |
+
aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY") # Set in HF Secrets
|
13 |
LANGUAGES = {
|
14 |
"English": "en",
|
15 |
+
"Spanish": "es",
|
16 |
"French": "fr",
|
17 |
"German": "de",
|
18 |
"Japanese": "ja",
|
19 |
"Hindi": "hi"
|
20 |
}
|
21 |
|
22 |
+
def extract_audio(video_path, output_dir):
|
23 |
+
"""Extract audio from video file"""
|
24 |
+
try:
|
25 |
+
video = mp.VideoFileClip(video_path)
|
26 |
+
audio_path = os.path.join(output_dir, "extracted_audio.wav")
|
27 |
+
video.audio.write_audiofile(audio_path)
|
28 |
+
return audio_path
|
29 |
+
except Exception as e:
|
30 |
+
raise Exception(f"Audio extraction failed: {str(e)}")
|
31 |
+
|
32 |
+
def generate_subtitles(audio_path, output_dir):
|
33 |
+
"""Generate subtitles from audio file"""
|
34 |
+
try:
|
35 |
+
transcriber = aai.Transcriber()
|
36 |
+
transcript = transcriber.transcribe(audio_path)
|
37 |
+
srt_path = os.path.join(output_dir, "subtitles.srt")
|
38 |
+
with open(srt_path, "w") as f:
|
39 |
+
f.write(transcript.export_subtitles_srt())
|
40 |
+
return srt_path
|
41 |
+
except Exception as e:
|
42 |
+
raise Exception(f"Subtitle generation failed: {str(e)}")
|
43 |
+
|
44 |
+
def translate_subtitles(srt_path, target_langs, output_dir):
|
45 |
+
"""Translate subtitles to multiple languages"""
|
46 |
+
try:
|
47 |
+
subs = pysrt.open(srt_path)
|
48 |
+
results = {}
|
49 |
+
|
50 |
+
for lang in target_langs:
|
51 |
+
translated_subs = subs[:]
|
52 |
+
translator = GoogleTranslator(source="auto", target=lang)
|
53 |
+
|
54 |
+
for sub in translated_subs:
|
55 |
+
sub.text = translator.translate(sub.text)
|
56 |
+
|
57 |
+
output_path = os.path.join(output_dir, f"subtitles_{lang}.srt")
|
58 |
+
translated_subs.save(output_path, encoding='utf-8')
|
59 |
+
results[lang] = output_path
|
60 |
+
|
61 |
+
return results
|
62 |
+
except Exception as e:
|
63 |
+
raise Exception(f"Translation failed: {str(e)}")
|
64 |
+
|
65 |
+
def merge_video_subtitles(video_path, srt_path, output_path):
|
66 |
+
"""Merge video with subtitles"""
|
67 |
+
try:
|
68 |
+
video = mp.VideoFileClip(video_path)
|
69 |
+
subs = pysrt.open(srt_path)
|
70 |
+
|
71 |
+
subtitle_clips = []
|
72 |
+
for sub in subs:
|
73 |
+
txt_clip = mp.TextClip(
|
74 |
+
sub.text,
|
75 |
+
fontsize=24,
|
76 |
+
color='white',
|
77 |
+
font='Arial',
|
78 |
+
size=(video.w * 0.8, None)
|
79 |
+
).set_start(sub.start.ordinal / 1000.0
|
80 |
+
).set_duration((sub.end.ordinal - sub.start.ordinal) / 1000.0
|
81 |
+
).set_pos(('center', 'bottom'))
|
82 |
+
subtitle_clips.append(txt_clip)
|
83 |
+
|
84 |
+
final_video = mp.CompositeVideoClip([video] + subtitle_clips)
|
85 |
+
final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
86 |
+
return output_path
|
87 |
+
except Exception as e:
|
88 |
+
raise Exception(f"Video merging failed: {str(e)}")
|
89 |
+
|
90 |
+
def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
|
91 |
+
"""Main processing function"""
|
92 |
with tempfile.TemporaryDirectory() as tmpdir:
|
93 |
try:
|
94 |
+
progress(0.1, "Extracting audio...")
|
95 |
audio_path = extract_audio(video_file.name, tmpdir)
|
96 |
|
97 |
+
progress(0.3, "Generating subtitles...")
|
98 |
srt_path = generate_subtitles(audio_path, tmpdir)
|
99 |
|
100 |
+
progress(0.5, "Translating subtitles...")
|
101 |
translated_subs = translate_subtitles(
|
102 |
srt_path,
|
103 |
[LANGUAGES[lang] for lang in target_langs],
|
104 |
tmpdir
|
105 |
)
|
106 |
|
107 |
+
progress(0.7, "Generating outputs...")
|
108 |
output_files = []
|
109 |
for lang, sub_path in translated_subs.items():
|
110 |
output_path = os.path.join(tmpdir, f"output_{lang}.mp4")
|
111 |
+
merge_video_subtitles(video_file.name, sub_path, output_path)
|
|
|
|
|
|
|
|
|
112 |
output_files.append(output_path)
|
113 |
|
114 |
+
progress(1.0, "Done!")
|
115 |
return output_files
|
116 |
|
117 |
except Exception as e:
|
118 |
raise gr.Error(f"Processing failed: {str(e)}")
|
119 |
|
120 |
+
# Gradio Interface
|
121 |
with gr.Blocks(title="Video Translator") as demo:
|
122 |
+
gr.Markdown("## 🎥 Video Translation System")
|
|
|
123 |
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
|
|
134 |
choices=list(LANGUAGES.keys()),
|
135 |
value=["Spanish", "French"]
|
136 |
)
|
137 |
+
submit_btn = gr.Button("Translate")
|
138 |
|
139 |
with gr.Column():
|
140 |
output_videos = gr.Files(label="Translated Videos")
|
141 |
|
142 |
submit_btn.click(
|
143 |
+
process_video,
|
144 |
inputs=[video_input, source_lang, target_langs],
|
145 |
outputs=output_videos
|
146 |
)
|