Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
import whisper
|
4 |
+
from googletrans import Translator
|
5 |
+
from gtts import gTTS
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Initialize Whisper model and translator
|
10 |
+
whisper_model = whisper.load_model("base") # Choose model size: 'tiny', 'base', 'small', 'medium', 'large'
|
11 |
+
translator = Translator()
|
12 |
+
|
13 |
+
# Language options
|
14 |
+
LANGUAGES = {
|
15 |
+
'English': 'en',
|
16 |
+
'Tamil': 'ta',
|
17 |
+
'Sinhala': 'si',
|
18 |
+
'French': 'fr', # Add more languages as needed
|
19 |
+
}
|
20 |
+
|
21 |
+
st.title("AI Video Translator with Whisper and GTTS")
|
22 |
+
|
23 |
+
# Step 1: Upload video file
|
24 |
+
video_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
|
25 |
+
|
26 |
+
if video_file:
|
27 |
+
# Step 2: Select translation language
|
28 |
+
target_language = st.selectbox("Select the target language for translation", list(LANGUAGES.keys()))
|
29 |
+
|
30 |
+
# Process when user clicks translate
|
31 |
+
if st.button("Translate Video"):
|
32 |
+
# Save video to a temporary file
|
33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
34 |
+
temp_video.write(video_file.read())
|
35 |
+
temp_video_path = temp_video.name
|
36 |
+
|
37 |
+
# Extract audio from video
|
38 |
+
try:
|
39 |
+
video = VideoFileClip(temp_video_path)
|
40 |
+
audio_path = tempfile.mktemp(suffix=".wav")
|
41 |
+
video.audio.write_audiofile(audio_path)
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f"Error extracting audio from video: {e}")
|
44 |
+
os.remove(temp_video_path)
|
45 |
+
st.stop()
|
46 |
+
|
47 |
+
# Transcribe audio using Whisper
|
48 |
+
try:
|
49 |
+
result = whisper_model.transcribe(audio_path)
|
50 |
+
original_text = result["text"]
|
51 |
+
st.write("Original Transcription:", original_text)
|
52 |
+
|
53 |
+
# Translate text to the target language
|
54 |
+
translated_text = translator.translate(original_text, dest=LANGUAGES[target_language]).text
|
55 |
+
st.write(f"Translated Text ({target_language}):", translated_text)
|
56 |
+
|
57 |
+
# Convert translated text to speech
|
58 |
+
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language])
|
59 |
+
audio_output_path = tempfile.mktemp(suffix=".mp3")
|
60 |
+
tts.save(audio_output_path)
|
61 |
+
|
62 |
+
# Display translated text and audio
|
63 |
+
st.success("Translation successful!")
|
64 |
+
st.audio(audio_output_path, format="audio/mp3")
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"Error during transcription/translation: {e}")
|
67 |
+
|
68 |
+
# Clean up temporary files
|
69 |
+
os.remove(temp_video_path)
|
70 |
+
os.remove(audio_path)
|
71 |
+
os.remove(audio_output_path)
|