Certainly! Below is the Python code that sets up a Gradio interface using Coqui TTS v2. This application allows users to input text, select a language, and optionally upload a reference audio file (e.g., an MP3 file) to clone the voice. The synthesized speech is then generated in the selected language and played back to the user.
Before running the code, make sure to install the required packages:
bash
pip install TTS gradio
You should also ensure that the Coqui TTS model you are using supports multiple languages and voice cloning. Adjust the model_name in the code to match a model that fits your needs.
Here's the code:
python
# Import necessary libraries
from TTS.api import TTS
import gradio as gr
import tempfile
# Initialize the TTS model
tts = TTS(
model_name="tts_models/multilingual/multi-dataset/your_tts_model", # Replace with your model
progress_bar=False,
gpu=False
)
# Get the list of supported languages
languages = tts.languages
def tts_synthesize(text, language, reference_audio):
# Handle reference audio
if reference_audio is not None:
# Save the uploaded file to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_file.write(reference_audio.read())
reference_audio_path = tmp_file.name
else:
reference_audio_path = None
# Generate the speech as a NumPy array
audio_output = tts.tts(text=text, speaker_wav=reference_audio_path, language=language)
# Return the sample rate and audio data
return (tts.output_sample_rate, audio_output)
# Create Gradio interface
iface = gr.Interface(
fn=tts_synthesize,
inputs=[
gr.inputs.Textbox(label="Text to Synthesize"),
gr.inputs.Dropdown(choices=languages, label="Language"),
gr.inputs.Audio(source="upload", type="file", optional=True, label="Reference Audio (Optional)"),
],
outputs=gr.outputs.Audio(label="Generated Speech"),
title="Coqui TTS v2 with Gradio",
description="Enter text, select language, and optionally upload a reference audio to clone the voice.",
)
# Launch the interface
iface.launch()
Explanation of the Code:
Imports:
TTS from TTS.api: This is the Coqui TTS API for text-to-speech synthesis.
gradio as gr: Gradio library for creating the web interface.
tempfile: Used to handle temporary files for the uploaded reference audio.
Initialize the TTS Model:
python
tts = TTS(
model_name="tts_models/multilingual/multi-dataset/your_tts_model", # Replace with an appropriate model
progress_bar=False,
gpu=False
)
Replace "tts_models/multilingual/multi-dataset/your_tts_model" with the actual model name that supports multiple languages and voice cloning. You can find available models by running TTS.list_models().
Retrieve Supported Languages:
python
languages = tts.languages
Retrieves the list of languages supported by the loaded TTS model.
Define the Synthesis Function:
python
def tts_synthesize(text, language, reference_audio):
# Handle reference audio
if reference_audio is not None:
# Save the uploaded file to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_file.write(reference_audio.read())
reference_audio_path = tmp_file.name
else:
reference_audio_path = None
# Generate the speech as a NumPy array
audio_output = tts.tts(text=text, speaker_wav=reference_audio_path, language=language)
# Return the sample rate and audio data
return (tts.output_sample_rate, audio_output)
Parameters:
Additional Notes:
Model Selection: Ensure that the model_name you provide supports both multiple languages and speaker cloning. Coqui TTS models have specific capabilities, and not all models support all features. You can list available models using:
Reference Audio Format: The reference audio should preferably be in WAV format for compatibility. If you upload an MP3 file, the code writes it as a WAV file in the temporary directory.
Temporary Files: The uploaded reference audio is written to a temporary file that is not automatically deleted (delete=False). If you're concerned about disk space or security, you may want to implement a mechanism to delete these temporary files after they're no longer needed.
How to Run the Application:
Install Dependencies:
bash
pip install TTS gradio
Save the Code: Save the above code to a Python file, e.g., coqui_tts_gradio_app.py.
Run the Application:
bash
python coqui_tts_gradio_app.py
Access the Interface:
Open your web browser and navigate to http://127.0.0.1:7860/ (or the URL provided in the terminal).
Using the Interface:
Text to Synthesize: Enter the text you want the model to speak.
Language: Select the desired language from the dropdown menu.
Reference Audio: (Optional) Upload an audio file of a speaker whose voice you want to clone.
Click Submit to generate and play back the synthesized speech.
Feel free to customize and enhance the application according to your specific requirements!