import gradio as gr from TTS.api import TTS import os os.environ["COQUI_TOS_AGREED"] = "1" # Load the XTTS-v2 model and set it to use CPU tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2") tts.to("cpu") # Set the model to run on CPU # Define the function for voice cloning def generate_voice(text, speaker_audio): output_path = "output.wav" tts.tts_to_file( text=text, speaker_wav=speaker_audio.name, file_path=output_path, language="en" ) return output_path # Gradio Interface with gr.Blocks() as demo: gr.Markdown("# 🗣️ Voice Cloning with Coqui XTTS-v2") with gr.Row(): text_input = gr.Textbox(label="Enter Text", placeholder="Type the text you want to synthesize...") speaker_audio_input = gr.Audio(label="Upload Speaker Audio (WAV)", type="file") output_audio = gr.Audio(label="Generated Voice", type="filepath") generate_button = gr.Button("Generate Voice") generate_button.click( fn=generate_voice, inputs=[text_input, speaker_audio_input], outputs=output_audio ) # Launch the Gradio app demo.launch()