Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
from core.audio_processor import extract_audio
|
5 |
+
from core.subtitle_gen import generate_subtitles
|
6 |
+
from core.translator import translate_subtitles
|
7 |
+
from core.video_merger import merge_video_subtitles
|
8 |
+
|
9 |
+
# Available languages
|
10 |
+
LANGUAGES = {
|
11 |
+
"English": "en",
|
12 |
+
"Spanish": "es",
|
13 |
+
"French": "fr",
|
14 |
+
"German": "de",
|
15 |
+
"Japanese": "ja",
|
16 |
+
"Hindi": "hi"
|
17 |
+
}
|
18 |
+
|
19 |
+
def process_video(
|
20 |
+
video_file,
|
21 |
+
source_lang,
|
22 |
+
target_langs,
|
23 |
+
progress=gr.Progress()
|
24 |
+
):
|
25 |
+
# Create temp directory
|
26 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
27 |
+
try:
|
28 |
+
progress(0.1, desc="Extracting audio...")
|
29 |
+
audio_path = extract_audio(video_file.name, tmpdir)
|
30 |
+
|
31 |
+
progress(0.3, desc="Generating subtitles...")
|
32 |
+
srt_path = generate_subtitles(audio_path, tmpdir)
|
33 |
+
|
34 |
+
progress(0.5, desc="Translating subtitles...")
|
35 |
+
translated_subs = translate_subtitles(
|
36 |
+
srt_path,
|
37 |
+
[LANGUAGES[lang] for lang in target_langs],
|
38 |
+
tmpdir
|
39 |
+
)
|
40 |
+
|
41 |
+
progress(0.7, desc="Generating output videos...")
|
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, desc="Done!")
|
53 |
+
return output_files
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
raise gr.Error(f"Processing failed: {str(e)}")
|
57 |
+
|
58 |
+
# Gradio interface
|
59 |
+
with gr.Blocks(title="Video Translator") as demo:
|
60 |
+
gr.Markdown("# 🎥 Video Translation System")
|
61 |
+
gr.Markdown("Upload a video to generate multilingual versions")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
video_input = gr.File(label="Upload Video", type="file")
|
66 |
+
source_lang = gr.Dropdown(
|
67 |
+
label="Source Language",
|
68 |
+
choices=list(LANGUAGES.keys()),
|
69 |
+
value="English"
|
70 |
+
)
|
71 |
+
target_langs = gr.CheckboxGroup(
|
72 |
+
label="Target Languages",
|
73 |
+
choices=list(LANGUAGES.keys()),
|
74 |
+
value=["Spanish", "French"]
|
75 |
+
)
|
76 |
+
submit_btn = gr.Button("Translate Video")
|
77 |
+
|
78 |
+
with gr.Column():
|
79 |
+
output_videos = gr.Files(label="Translated Videos")
|
80 |
+
|
81 |
+
submit_btn.click(
|
82 |
+
fn=process_video,
|
83 |
+
inputs=[video_input, source_lang, target_langs],
|
84 |
+
outputs=output_videos
|
85 |
+
)
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
demo.launch()
|