Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import librosa
|
3 |
+
import soundfile as sf
|
4 |
+
from TTS.api import TTS
|
5 |
+
|
6 |
+
# Load the pre-trained TTS model
|
7 |
+
MODEL_NAME = "tts_models/en/vctk/vits" # Change this to other models if desired
|
8 |
+
tts = TTS(model_name=MODEL_NAME, progress_bar=True, gpu=False)
|
9 |
+
|
10 |
+
# Title and description
|
11 |
+
st.title("Voice Cloning Tool")
|
12 |
+
st.markdown("""
|
13 |
+
Upload a sample of your voice and type text to generate a cloned output.
|
14 |
+
This tool uses a pre-trained voice synthesis model.
|
15 |
+
""")
|
16 |
+
|
17 |
+
# Step 1: Upload an audio file
|
18 |
+
uploaded_file = st.file_uploader("Upload your voice sample (WAV format preferred):", type=["wav", "mp3"])
|
19 |
+
if uploaded_file:
|
20 |
+
st.audio(uploaded_file, format="audio/wav", start_time=0)
|
21 |
+
|
22 |
+
# Step 2: Enter text for synthesis
|
23 |
+
text_input = st.text_area("Enter text to synthesize with the cloned voice:")
|
24 |
+
if not text_input:
|
25 |
+
st.warning("Please enter text to generate cloned voice output.")
|
26 |
+
|
27 |
+
# Process the audio input (convert to mono WAV)
|
28 |
+
def preprocess_audio(file):
|
29 |
+
"""Converts audio to mono WAV with a sampling rate of 16kHz."""
|
30 |
+
y, sr = librosa.load(file, sr=16000, mono=True)
|
31 |
+
return y, sr
|
32 |
+
|
33 |
+
# Save processed audio for the model
|
34 |
+
if uploaded_file:
|
35 |
+
with open("input_audio.wav", "wb") as f:
|
36 |
+
f.write(uploaded_file.read())
|
37 |
+
input_audio_path = "input_audio.wav"
|
38 |
+
|
39 |
+
# Step 3: Clone voice and synthesize speech
|
40 |
+
if st.button("Clone Voice"):
|
41 |
+
if uploaded_file and text_input:
|
42 |
+
# Process the input audio
|
43 |
+
audio, sr = preprocess_audio(input_audio_path)
|
44 |
+
sf.write("processed_audio.wav", audio, sr)
|
45 |
+
|
46 |
+
# Clone the voice and synthesize speech
|
47 |
+
try:
|
48 |
+
output_audio = tts.tts(text=text_input, speaker_wav="processed_audio.wav")
|
49 |
+
output_path = "cloned_output.wav"
|
50 |
+
sf.write(output_path, output_audio, samplerate=16000)
|
51 |
+
st.success("Voice cloning complete! Listen to the output below:")
|
52 |
+
st.audio(output_path, format="audio/wav")
|
53 |
+
st.download_button("Download Cloned Voice", data=open(output_path, "rb"), file_name="cloned_output.wav")
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Error during voice cloning: {e}")
|
56 |
+
else:
|
57 |
+
st.error("Please upload a voice sample and enter text for synthesis.")
|