Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +60 -71
src/app.py
CHANGED
@@ -1,74 +1,63 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
import torch
|
6 |
-
import torchaudio
|
7 |
-
import soundfile as sf
|
8 |
-
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
9 |
|
10 |
-
#
|
11 |
@st.cache_resource
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
audio_buffer = text_to_speech(text, model, processor)
|
69 |
-
|
70 |
-
st.audio(audio_buffer, format="audio/wav")
|
71 |
-
st.markdown(get_download_link(audio_buffer), unsafe_allow_html=True)
|
72 |
-
|
73 |
-
if __name__ == "__main__":
|
74 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from TTS.api import TTS
|
3 |
+
import tempfile
|
4 |
+
import os
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Initialize TTS model (only once)
|
7 |
@st.cache_resource
|
8 |
+
def load_tts_model():
|
9 |
+
return TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)
|
10 |
+
|
11 |
+
tts = load_tts_model()
|
12 |
+
|
13 |
+
# App title
|
14 |
+
st.title("🔊 Voice Cloning with XTTS v2")
|
15 |
+
|
16 |
+
# Text input
|
17 |
+
text_input = st.text_area("Enter the text you want to synthesize", height=150)
|
18 |
+
|
19 |
+
# Speaker file uploader
|
20 |
+
speaker_file = st.file_uploader("Upload a speaker WAV file", type=["wav"])
|
21 |
+
|
22 |
+
# Button to generate
|
23 |
+
if st.button("Generate Speech"):
|
24 |
+
if not text_input:
|
25 |
+
st.error("Please enter text.")
|
26 |
+
elif not speaker_file:
|
27 |
+
st.error("Please upload a speaker WAV file.")
|
28 |
+
else:
|
29 |
+
try:
|
30 |
+
with st.spinner("Generating voice..."):
|
31 |
+
# Save uploaded speaker audio temporarily
|
32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as speaker_temp:
|
33 |
+
speaker_temp.write(speaker_file.read())
|
34 |
+
speaker_path = speaker_temp.name
|
35 |
+
|
36 |
+
# Temporary file to store output
|
37 |
+
output_path = os.path.join(tempfile.gettempdir(), "output.wav")
|
38 |
+
|
39 |
+
# Generate speech
|
40 |
+
tts.tts_to_file(
|
41 |
+
text=text_input,
|
42 |
+
file_path=output_path,
|
43 |
+
speaker_wav=speaker_path,
|
44 |
+
language="en"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Playback
|
48 |
+
st.audio(output_path, format="audio/wav")
|
49 |
+
|
50 |
+
# Download link
|
51 |
+
with open(output_path, "rb") as f:
|
52 |
+
st.download_button(
|
53 |
+
label="Download Audio",
|
54 |
+
data=f,
|
55 |
+
file_name="cloned_voice.wav",
|
56 |
+
mime="audio/wav"
|
57 |
+
)
|
58 |
+
|
59 |
+
# Clean up
|
60 |
+
os.remove(speaker_path)
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|