Artificial-superintelligence
commited on
Commit
•
96d6a67
1
Parent(s):
12e5a2b
Update app.py
Browse files
app.py
CHANGED
@@ -61,24 +61,14 @@ if video_file:
|
|
61 |
|
62 |
return ' '.join(segments)
|
63 |
|
64 |
-
#
|
65 |
-
def
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
current_chunk += " " + word if current_chunk else word
|
73 |
-
else:
|
74 |
-
chunks.append(current_chunk)
|
75 |
-
current_chunk = word
|
76 |
-
|
77 |
-
if current_chunk:
|
78 |
-
chunks.append(current_chunk)
|
79 |
-
|
80 |
-
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
81 |
-
return ' '.join(translated_chunks)
|
82 |
|
83 |
# Transcribe audio using Whisper
|
84 |
try:
|
@@ -87,40 +77,19 @@ if video_file:
|
|
87 |
|
88 |
# Translate text to the target language
|
89 |
translator = Translator(to_lang=LANGUAGES[target_language])
|
90 |
-
translated_text =
|
91 |
st.write(f"Translated Text ({target_language}):", translated_text)
|
92 |
|
93 |
-
# Convert translated text to speech
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
max_length = 200 # Adjust as needed
|
98 |
-
|
99 |
-
for word in words:
|
100 |
-
if len(chunk) + len(word) + 1 > max_length:
|
101 |
-
tts = gTTS(text=chunk, lang=LANGUAGES[target_language])
|
102 |
-
tts_audio_path = tempfile.mktemp(suffix=".mp3")
|
103 |
-
tts.save(tts_audio_path)
|
104 |
-
tts_clips.append(AudioFileClip(tts_audio_path))
|
105 |
-
chunk = word
|
106 |
-
else:
|
107 |
-
chunk += " " + word if chunk else word
|
108 |
-
|
109 |
-
if chunk: # Process last chunk
|
110 |
-
tts = gTTS(text=chunk, lang=LANGUAGES[target_language])
|
111 |
-
tts_audio_path = tempfile.mktemp(suffix=".mp3")
|
112 |
-
tts.save(tts_audio_path)
|
113 |
-
tts_clips.append(AudioFileClip(tts_audio_path))
|
114 |
-
|
115 |
-
# Concatenate all TTS audio chunks
|
116 |
-
final_audio = concatenate_audioclips(tts_clips)
|
117 |
-
translated_audio_path = tempfile.mktemp(suffix=".mp3")
|
118 |
-
final_audio.write_audiofile(translated_audio_path)
|
119 |
|
120 |
# Merge translated audio with the original video
|
121 |
final_video_path = tempfile.mktemp(suffix=".mp4")
|
122 |
original_video = VideoFileClip(temp_video_path)
|
123 |
-
|
|
|
124 |
final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
|
125 |
|
126 |
# Display success message and provide download link
|
@@ -135,9 +104,8 @@ if video_file:
|
|
135 |
st.error(f"Error during transcription/translation: {e}")
|
136 |
|
137 |
# Clean up temporary files
|
138 |
-
for clip in tts_clips:
|
139 |
-
os.remove(clip.filename)
|
140 |
os.remove(temp_video_path)
|
141 |
os.remove(audio_path)
|
142 |
-
os.remove(
|
143 |
-
|
|
|
|
61 |
|
62 |
return ' '.join(segments)
|
63 |
|
64 |
+
# Translate text function with debug info
|
65 |
+
def translate_text(original_text, translator):
|
66 |
+
translated_text = translator.translate(original_text)
|
67 |
+
# Debugging: Check translation results
|
68 |
+
st.write(f"Translated Text Debug: {translated_text}")
|
69 |
+
if translated_text.strip() == original_text.strip():
|
70 |
+
st.warning("The translated text is the same as the original. Check if the target language is appropriate.")
|
71 |
+
return translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# Transcribe audio using Whisper
|
74 |
try:
|
|
|
77 |
|
78 |
# Translate text to the target language
|
79 |
translator = Translator(to_lang=LANGUAGES[target_language])
|
80 |
+
translated_text = translate_text(original_text, translator)
|
81 |
st.write(f"Translated Text ({target_language}):", translated_text)
|
82 |
|
83 |
+
# Convert translated text to speech
|
84 |
+
tts_audio_path = tempfile.mktemp(suffix=".mp3")
|
85 |
+
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language])
|
86 |
+
tts.save(tts_audio_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
# Merge translated audio with the original video
|
89 |
final_video_path = tempfile.mktemp(suffix=".mp4")
|
90 |
original_video = VideoFileClip(temp_video_path)
|
91 |
+
final_audio = AudioFileClip(tts_audio_path)
|
92 |
+
final_video = original_video.set_audio(final_audio)
|
93 |
final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
|
94 |
|
95 |
# Display success message and provide download link
|
|
|
104 |
st.error(f"Error during transcription/translation: {e}")
|
105 |
|
106 |
# Clean up temporary files
|
|
|
|
|
107 |
os.remove(temp_video_path)
|
108 |
os.remove(audio_path)
|
109 |
+
os.remove(tts_audio_path)
|
110 |
+
if 'final_video_path' in locals(): # Check if final_video_path exists
|
111 |
+
os.remove(final_video_path)
|