Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
-
import os
|
3 |
-
from TTS.api import TTS
|
4 |
import moviepy.editor as mp
|
5 |
-
from io import BytesIO
|
6 |
import soundfile as sf
|
|
|
|
|
7 |
|
8 |
# Set up the model for text-to-speech (TTS)
|
9 |
MODEL_NAME = "tts_models/en/ljspeech/tacotron2-DDC" # Example TTS model; adjust as needed
|
@@ -11,16 +10,24 @@ tts = TTS(model_name=MODEL_NAME, progress_bar=True, gpu=False)
|
|
11 |
|
12 |
# Function to extract audio from MP4 file
|
13 |
def extract_audio_from_mp4(mp4_file):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Function to load audio file
|
21 |
def load_audio(file):
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Function to save the generated audio to a file
|
26 |
def save_audio(output_audio, sample_rate):
|
@@ -45,10 +52,11 @@ def main():
|
|
45 |
|
46 |
# Extract audio from MP4
|
47 |
audio_path = extract_audio_from_mp4("uploaded_video.mp4")
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
else:
|
53 |
# For audio files directly
|
54 |
st.audio(audio_file, format=f"audio/{audio_file.type.split('/')[1]}")
|
@@ -59,17 +67,20 @@ def main():
|
|
59 |
|
60 |
audio_data, sample_rate = load_audio("temp_audio.wav")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
main()
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import moviepy.editor as mp
|
|
|
3 |
import soundfile as sf
|
4 |
+
from io import BytesIO
|
5 |
+
from TTS.api import TTS
|
6 |
|
7 |
# Set up the model for text-to-speech (TTS)
|
8 |
MODEL_NAME = "tts_models/en/ljspeech/tacotron2-DDC" # Example TTS model; adjust as needed
|
|
|
10 |
|
11 |
# Function to extract audio from MP4 file
|
12 |
def extract_audio_from_mp4(mp4_file):
|
13 |
+
try:
|
14 |
+
video = mp.VideoFileClip(mp4_file)
|
15 |
+
audio = video.audio
|
16 |
+
audio_file = "temp_audio.wav"
|
17 |
+
audio.write_audiofile(audio_file)
|
18 |
+
return audio_file
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Error extracting audio from MP4: {e}")
|
21 |
+
return None
|
22 |
|
23 |
# Function to load audio file
|
24 |
def load_audio(file):
|
25 |
+
try:
|
26 |
+
audio_data, sample_rate = sf.read(file)
|
27 |
+
return audio_data, sample_rate
|
28 |
+
except Exception as e:
|
29 |
+
st.error(f"Error loading audio: {e}")
|
30 |
+
return None, None
|
31 |
|
32 |
# Function to save the generated audio to a file
|
33 |
def save_audio(output_audio, sample_rate):
|
|
|
52 |
|
53 |
# Extract audio from MP4
|
54 |
audio_path = extract_audio_from_mp4("uploaded_video.mp4")
|
55 |
+
if audio_path:
|
56 |
+
st.audio(audio_path, format="audio/wav")
|
57 |
+
|
58 |
+
# Load audio for TTS processing
|
59 |
+
audio_data, sample_rate = load_audio(audio_path)
|
60 |
else:
|
61 |
# For audio files directly
|
62 |
st.audio(audio_file, format=f"audio/{audio_file.type.split('/')[1]}")
|
|
|
67 |
|
68 |
audio_data, sample_rate = load_audio("temp_audio.wav")
|
69 |
|
70 |
+
if audio_data is not None:
|
71 |
+
# Perform voice cloning (This assumes your TTS model supports some form of input)
|
72 |
+
try:
|
73 |
+
st.text("Processing your input...")
|
74 |
+
output_audio = tts.tts(audio_data) # Pass the audio to your TTS model for cloning
|
75 |
+
output_path = save_audio(output_audio, sample_rate)
|
76 |
|
77 |
+
# Provide download link
|
78 |
+
st.audio(output_path, format="audio/wav")
|
79 |
+
st.markdown(f"[Download Cloned Voice](/{output_path})")
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Error processing audio: {e}")
|
82 |
+
else:
|
83 |
+
st.error("No audio data to process.")
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
main()
|