Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
import os | |
import numpy as np | |
from TTS.api import TTS | |
from scipy.io.wavfile import write | |
# Check if the user has agreed to the terms of service | |
tos_gr = gr.Checkbox( | |
label="Agree", | |
value=True, | |
info="I agree to the terms of the CPML: https://coqui.ai/cpml", | |
) | |
tos_agreed = tos_gr.value | |
# Set the COQUI_TOS_AGREED environment variable | |
os.environ["COQUI_TOS_AGREED"] = "1" if tos_agreed else "0" | |
def generate_voice(text, language, sample_voice, tos_agreed): | |
# Check if the user has agreed to the terms of service | |
if not tos_agreed: | |
return "Please agree to the terms of service before using the service." | |
# Initialize TTS | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) | |
# Load the audio sample from the user's upload | |
speaker_wav_path = sample_voice.name | |
# Use the sample voice to convert text to speech | |
wav = tts.tts(text=text, speaker_wav=speaker_wav_path, language=language) | |
# Save the generated audio to a temporary file | |
output_path = "generated_audio.wav" | |
write(output_path, 22050, np.array(wav, dtype=np.int16)) # Use np.int16 | |
# Return the path to the generated audio file | |
return output_path | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=generate_voice, | |
inputs=[ | |
gr.Textbox(lines=3, label="Enter Text"), | |
gr.Dropdown(["es", "en", "fr"], label="Select Language"), | |
gr.File(label="Upload Sample Voice"), | |
tos_gr, | |
], | |
outputs=gr.Audio(label="Generated Audio"), | |
) | |
# Launch the Gradio interface | |
iface.launch(share=True) | |