Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,17 @@
|
|
1 |
import streamlit as st
|
2 |
from moviepy.editor import VideoFileClip
|
3 |
import whisper
|
4 |
-
from
|
5 |
from gtts import gTTS
|
6 |
import tempfile
|
7 |
import os
|
8 |
import numpy as np
|
9 |
|
10 |
# Initialize Whisper model
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
# Language options
|
14 |
LANGUAGES = {
|
@@ -44,22 +47,28 @@ if video_file:
|
|
44 |
os.remove(temp_video_path)
|
45 |
st.stop()
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
for segment in audio_segments:
|
55 |
-
result = whisper_model.transcribe(segment)
|
56 |
-
original_text += result["text"] + " " # Concatenate transcriptions
|
57 |
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
# Translate text to the target language
|
61 |
-
translator = Translator()
|
62 |
-
translated_text = translator.translate(original_text
|
63 |
st.write(f"Translated Text ({target_language}):", translated_text)
|
64 |
|
65 |
# Convert translated text to speech
|
@@ -77,13 +86,3 @@ if video_file:
|
|
77 |
os.remove(temp_video_path)
|
78 |
os.remove(audio_path)
|
79 |
os.remove(audio_output_path)
|
80 |
-
|
81 |
-
def split_audio(audio, segment_length=30):
|
82 |
-
"""Split audio into segments of specified length in seconds."""
|
83 |
-
total_length = audio.shape[1] # Total length in seconds
|
84 |
-
segments = []
|
85 |
-
for start in range(0, total_length, segment_length):
|
86 |
-
end = min(start + segment_length, total_length)
|
87 |
-
segment = audio[:, start:end] # Append the segment
|
88 |
-
segments.append(segment)
|
89 |
-
return segments
|
|
|
1 |
import streamlit as st
|
2 |
from moviepy.editor import VideoFileClip
|
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:
|
12 |
+
whisper_model = whisper.load_model("base") # Ensure the model is installed from the correct Whisper library
|
13 |
+
except AttributeError:
|
14 |
+
st.error("Whisper model could not be loaded. Ensure that Whisper is installed from GitHub.")
|
15 |
|
16 |
# Language options
|
17 |
LANGUAGES = {
|
|
|
47 |
os.remove(temp_video_path)
|
48 |
st.stop()
|
49 |
|
50 |
+
# Function to transcribe audio in chunks
|
51 |
+
def transcribe_audio_in_chunks(audio_path, model, chunk_length=30):
|
52 |
+
audio_clip = whisper.load_audio(audio_path)
|
53 |
+
audio_duration = whisper.get_duration(audio_clip)
|
54 |
+
segments = []
|
55 |
+
|
56 |
+
for start in np.arange(0, audio_duration, chunk_length):
|
57 |
+
end = min(start + chunk_length, audio_duration)
|
58 |
+
segment = audio_clip[int(start * 16000):int(end * 16000)] # Convert to the right format
|
59 |
+
result = model.transcribe(segment)
|
60 |
+
segments.append(result['text'])
|
61 |
|
62 |
+
return ' '.join(segments)
|
|
|
|
|
|
|
63 |
|
64 |
+
# Transcribe audio using Whisper
|
65 |
+
try:
|
66 |
+
original_text = transcribe_audio_in_chunks(audio_path, whisper_model)
|
67 |
+
st.write("Original Transcription:", original_text)
|
68 |
|
69 |
# Translate text to the target language
|
70 |
+
translator = Translator(to_lang=LANGUAGES[target_language])
|
71 |
+
translated_text = translator.translate(original_text)
|
72 |
st.write(f"Translated Text ({target_language}):", translated_text)
|
73 |
|
74 |
# Convert translated text to speech
|
|
|
86 |
os.remove(temp_video_path)
|
87 |
os.remove(audio_path)
|
88 |
os.remove(audio_output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|