Artificial-superintelligence
commited on
Commit
•
dc36981
1
Parent(s):
96d6a67
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
-
from moviepy.editor import VideoFileClip, AudioFileClip
|
3 |
import whisper
|
4 |
from translate import Translator
|
5 |
from gtts import gTTS
|
6 |
import tempfile
|
7 |
import os
|
8 |
import numpy as np
|
|
|
9 |
|
10 |
# Initialize Whisper model
|
11 |
try:
|
@@ -61,14 +62,24 @@ if video_file:
|
|
61 |
|
62 |
return ' '.join(segments)
|
63 |
|
64 |
-
#
|
65 |
-
def
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# Transcribe audio using Whisper
|
74 |
try:
|
@@ -77,19 +88,20 @@ if video_file:
|
|
77 |
|
78 |
# Translate text to the target language
|
79 |
translator = Translator(to_lang=LANGUAGES[target_language])
|
80 |
-
translated_text =
|
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 |
-
|
|
|
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 |
-
|
92 |
-
|
|
|
93 |
final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
|
94 |
|
95 |
# Display success message and provide download link
|
@@ -102,10 +114,13 @@ if video_file:
|
|
102 |
|
103 |
except Exception as e:
|
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 |
-
|
110 |
-
|
111 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
3 |
import whisper
|
4 |
from translate import Translator
|
5 |
from gtts import gTTS
|
6 |
import tempfile
|
7 |
import os
|
8 |
import numpy as np
|
9 |
+
import time
|
10 |
|
11 |
# Initialize Whisper model
|
12 |
try:
|
|
|
62 |
|
63 |
return ' '.join(segments)
|
64 |
|
65 |
+
# Function to translate text in chunks
|
66 |
+
def translate_in_chunks(text, translator, max_length=500):
|
67 |
+
words = text.split()
|
68 |
+
chunks = []
|
69 |
+
current_chunk = ""
|
70 |
+
|
71 |
+
for word in words:
|
72 |
+
if len(current_chunk) + len(word) + 1 <= max_length:
|
73 |
+
current_chunk += " " + word if current_chunk else word
|
74 |
+
else:
|
75 |
+
chunks.append(current_chunk)
|
76 |
+
current_chunk = word
|
77 |
+
|
78 |
+
if current_chunk:
|
79 |
+
chunks.append(current_chunk)
|
80 |
+
|
81 |
+
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
82 |
+
return ' '.join(translated_chunks)
|
83 |
|
84 |
# Transcribe audio using Whisper
|
85 |
try:
|
|
|
88 |
|
89 |
# Translate text to the target language
|
90 |
translator = Translator(to_lang=LANGUAGES[target_language])
|
91 |
+
translated_text = translate_in_chunks(original_text, translator)
|
92 |
st.write(f"Translated Text ({target_language}):", translated_text)
|
93 |
|
94 |
# Convert translated text to speech
|
|
|
95 |
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language])
|
96 |
+
translated_audio_path = tempfile.mktemp(suffix=".mp3")
|
97 |
+
tts.save(translated_audio_path)
|
98 |
|
99 |
# Merge translated audio with the original video
|
100 |
final_video_path = tempfile.mktemp(suffix=".mp4")
|
101 |
original_video = VideoFileClip(temp_video_path)
|
102 |
+
translated_audio = AudioFileClip(translated_audio_path)
|
103 |
+
|
104 |
+
final_video = original_video.set_audio(translated_audio)
|
105 |
final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
|
106 |
|
107 |
# Display success message and provide download link
|
|
|
114 |
|
115 |
except Exception as e:
|
116 |
st.error(f"Error during transcription/translation: {e}")
|
117 |
+
translated_audio_path = None # Ensure this variable is defined
|
118 |
+
final_video_path = None # Ensure this variable is defined
|
119 |
|
120 |
# Clean up temporary files
|
121 |
os.remove(temp_video_path)
|
122 |
os.remove(audio_path)
|
123 |
+
if translated_audio_path: # Only remove if it was created
|
124 |
+
os.remove(translated_audio_path)
|
125 |
+
if final_video_path: # Only remove if it was created
|
126 |
+
os.remove(final_video_path)
|