Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,63 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
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 |
-
with open(output_audio_file, 'rb') as audio_file:
|
65 |
-
audio_bytes = audio_file.read()
|
66 |
-
st.markdown(get_binary_file_downloader_html(audio_bytes, output_audio_file), unsafe_allow_html=True)
|
67 |
-
|
68 |
-
except Exception as e:
|
69 |
-
st.error(f"An error occurred: {str(e)}")
|
70 |
-
|
71 |
-
# Function to create a download link
|
72 |
-
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
73 |
-
with st.spinner("Preparing download link..."):
|
74 |
-
data = bin_file
|
75 |
-
b64 = base64.b64encode(data).decode()
|
76 |
-
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}" target="_blank">Download {file_label}</a>'
|
77 |
-
return href
|
78 |
-
|
79 |
-
if __name__ == '__main__':
|
80 |
-
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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|