Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,14 +20,14 @@ subtitle_storage = {}
|
|
20 |
|
21 |
# Supported languages with their codes
|
22 |
languages = {
|
23 |
-
"Persian
|
24 |
-
"French
|
25 |
-
"Spanish
|
26 |
-
"German
|
27 |
-
"Chinese
|
28 |
-
"Arabic
|
29 |
-
"Hindi
|
30 |
-
"Russian
|
31 |
}
|
32 |
|
33 |
def transcribe_audio(chunk):
|
@@ -66,22 +66,23 @@ def add_subtitle(video):
|
|
66 |
except Exception as e:
|
67 |
return f"Error in adding subtitle: {e}"
|
68 |
|
69 |
-
def translate_subtitle(video):
|
70 |
try:
|
71 |
# Translate the stored subtitle
|
72 |
original_subtitle = subtitle_storage.get("original")
|
73 |
if not original_subtitle:
|
74 |
return "No subtitle to translate!"
|
75 |
|
|
|
76 |
translated_subtitle = translator(
|
77 |
original_subtitle,
|
78 |
src_lang="en", # Source language (assuming the subtitle is in English)
|
79 |
-
tgt_lang=languages[
|
80 |
)[0]["translation_text"]
|
81 |
|
82 |
subtitle_storage["translated"] = translated_subtitle # Store the translated subtitle
|
83 |
|
84 |
-
return "Subtitle translated successfully!"
|
85 |
|
86 |
except Exception as e:
|
87 |
return f"Error in translating subtitle: {e}"
|
@@ -120,8 +121,8 @@ def download_video():
|
|
120 |
# Function to generate subtitle text
|
121 |
generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
|
122 |
|
123 |
-
# Simulated subtitle time intervals for simplicity
|
124 |
-
subs = [(i * 5,
|
125 |
|
126 |
# Create subtitle clips
|
127 |
subtitles = SubtitlesClip(subs, generator)
|
@@ -159,10 +160,11 @@ with gr.Blocks() as demo:
|
|
159 |
|
160 |
# Translate Subtitle
|
161 |
with gr.Row():
|
|
|
162 |
translate_button = gr.Button("Translate Subtitle")
|
163 |
translate_status = gr.Textbox(label="Translation Status")
|
164 |
|
165 |
-
translate_button.click(translate_subtitle, inputs=video_input, outputs=translate_status)
|
166 |
|
167 |
# Download as Word
|
168 |
with gr.Row():
|
|
|
20 |
|
21 |
# Supported languages with their codes
|
22 |
languages = {
|
23 |
+
"Persian": "fa",
|
24 |
+
"French": "fr",
|
25 |
+
"Spanish": "es",
|
26 |
+
"German": "de",
|
27 |
+
"Chinese": "zh",
|
28 |
+
"Arabic": "ar",
|
29 |
+
"Hindi": "hi",
|
30 |
+
"Russian": "ru"
|
31 |
}
|
32 |
|
33 |
def transcribe_audio(chunk):
|
|
|
66 |
except Exception as e:
|
67 |
return f"Error in adding subtitle: {e}"
|
68 |
|
69 |
+
def translate_subtitle(video, language):
|
70 |
try:
|
71 |
# Translate the stored subtitle
|
72 |
original_subtitle = subtitle_storage.get("original")
|
73 |
if not original_subtitle:
|
74 |
return "No subtitle to translate!"
|
75 |
|
76 |
+
# Translate using the selected language
|
77 |
translated_subtitle = translator(
|
78 |
original_subtitle,
|
79 |
src_lang="en", # Source language (assuming the subtitle is in English)
|
80 |
+
tgt_lang=languages[language] # Get the language code from the dropdown selection
|
81 |
)[0]["translation_text"]
|
82 |
|
83 |
subtitle_storage["translated"] = translated_subtitle # Store the translated subtitle
|
84 |
|
85 |
+
return f"Subtitle translated to {language} successfully!"
|
86 |
|
87 |
except Exception as e:
|
88 |
return f"Error in translating subtitle: {e}"
|
|
|
121 |
# Function to generate subtitle text
|
122 |
generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
|
123 |
|
124 |
+
# Simulated subtitle time intervals for simplicity (assumes 5 seconds per subtitle)
|
125 |
+
subs = [(i * 5, translated_subtitle[i:i+50]) for i in range(0, len(translated_subtitle), 50)]
|
126 |
|
127 |
# Create subtitle clips
|
128 |
subtitles = SubtitlesClip(subs, generator)
|
|
|
160 |
|
161 |
# Translate Subtitle
|
162 |
with gr.Row():
|
163 |
+
language_dropdown = gr.Dropdown(choices=list(languages.keys()), label="Choose Target Language", value="Persian")
|
164 |
translate_button = gr.Button("Translate Subtitle")
|
165 |
translate_status = gr.Textbox(label="Translation Status")
|
166 |
|
167 |
+
translate_button.click(translate_subtitle, inputs=[video_input, language_dropdown], outputs=translate_status)
|
168 |
|
169 |
# Download as Word
|
170 |
with gr.Row():
|