Amelia-James commited on
Commit
89c6ab7
·
verified ·
1 Parent(s): da48be6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -48
app.py CHANGED
@@ -1,57 +1,51 @@
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.")
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import os
4
  import soundfile as sf
5
  from TTS.api import TTS
6
+ import torch
7
+ from io import BytesIO
8
 
9
+ # Set up the model for text-to-speech (TTS)
10
+ MODEL_NAME = "tts_models/en/ljspeech/tacotron2-DDC" # Example TTS model; adjust as needed
11
  tts = TTS(model_name=MODEL_NAME, progress_bar=True, gpu=False)
12
 
13
+ # Function to load audio file
14
+ def load_audio(file):
15
+ audio_data, sample_rate = sf.read(file)
16
+ return audio_data, sample_rate
17
+
18
+ # Function to save the generated audio to a file
19
+ def save_audio(output_audio, sample_rate):
20
+ output_path = "output_cloned_voice.wav"
21
+ sf.write(output_path, output_audio, sample_rate)
22
+ return output_path
23
+
24
+ # Streamlit app
25
+ def main():
26
+ st.title("Voice Cloning Tool")
27
+ st.markdown("Upload a voice input, and get the cloned voice output.")
28
+
29
+ # File upload
30
+ audio_file = st.file_uploader("Upload your audio file", type=["wav", "mp3"])
31
+
32
+ if audio_file is not None:
33
+ st.audio(audio_file, format="audio/wav")
34
+
35
+ # Load audio file
36
+ audio_data, sample_rate = load_audio(audio_file)
37
+
38
+ # Perform voice cloning (This assumes your TTS model supports some form of input)
 
 
 
 
 
 
 
 
 
 
 
39
  try:
40
+ st.text("Processing your input...")
41
+ output_audio = tts.tts(audio_data) # Pass the audio to your TTS model for cloning
42
+ output_path = save_audio(output_audio, sample_rate)
43
+
44
+ # Provide download link
45
  st.audio(output_path, format="audio/wav")
46
+ st.markdown(f"[Download Cloned Voice](/{output_path})")
47
  except Exception as e:
48
+ st.error(f"Error processing audio: {e}")
49
+
50
+ if __name__ == "__main__":
51
+ main()