Create v1.txt
Browse files
v1.txt
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import numpy as np
|
| 4 |
+
from scipy.io import wavfile
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load the YourTTS model once at startup
|
| 9 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False)
|
| 10 |
+
sample_rate = tts.synthesizer.output_sample_rate
|
| 11 |
+
|
| 12 |
+
def generate_speech(reference_audio, text):
|
| 13 |
+
"""
|
| 14 |
+
Generate speech audio mimicking the voice from the reference audio.
|
| 15 |
+
|
| 16 |
+
Parameters:
|
| 17 |
+
reference_audio (str): Filepath to the uploaded voice sample.
|
| 18 |
+
text (str): Text to convert to speech.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
str: Path to the generated audio file
|
| 22 |
+
"""
|
| 23 |
+
# Generate speech using the reference audio and text
|
| 24 |
+
wav = tts.tts(text=text, speaker_wav=reference_audio, language="en")
|
| 25 |
+
# Convert list to numpy array
|
| 26 |
+
wav_np = np.array(wav, dtype=np.float32)
|
| 27 |
+
|
| 28 |
+
# Create a temporary file to save the audio
|
| 29 |
+
temp_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 30 |
+
temp_file_path = temp_file.name
|
| 31 |
+
# Save the audio to the temporary file
|
| 32 |
+
wavfile.write(temp_file_path, sample_rate, wav_np)
|
| 33 |
+
temp_file.close()
|
| 34 |
+
|
| 35 |
+
return temp_file_path
|
| 36 |
+
|
| 37 |
+
# Build the Gradio interface
|
| 38 |
+
with gr.Blocks(title="Voice Cloning TTS") as app:
|
| 39 |
+
gr.Markdown("## Voice Cloning Text-to-Speech")
|
| 40 |
+
gr.Markdown("Upload a short voice sample in English, then enter text to hear it in your voice!")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
audio_input = gr.Audio(type="filepath", label="Upload Your Voice Sample (English)")
|
| 44 |
+
text_input = gr.Textbox(label="Enter Text to Convert to Speech", placeholder="e.g., I love chocolate")
|
| 45 |
+
|
| 46 |
+
generate_btn = gr.Button("Generate Speech")
|
| 47 |
+
audio_output = gr.Audio(label="Generated Speech", interactive=False)
|
| 48 |
+
|
| 49 |
+
# Connect the button to the generation function
|
| 50 |
+
generate_btn.click(
|
| 51 |
+
fn=generate_speech,
|
| 52 |
+
inputs=[audio_input, text_input],
|
| 53 |
+
outputs=audio_output
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Launch the application
|
| 57 |
+
app.launch()
|