import gradio as gr from TTS.api import TTS import os import torch # 1. Environment & License Agreement # XTTS requires agreeing to the terms. os.environ["COQUI_TOS_AGREED"] = "1" # 2. Load Model # Check if CUDA is available device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Loading XTTS v2 on {device}...") try: # Load the model with gpu support if available tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) except Exception as e: print(f"Error loading model: {e}") tts = None def clone_voice(text, audio_filepath, language, speed, temperature, repetition_penalty): """ Generate speech using XTTS v2. Args: text: The text to speak. audio_filepath: Path to the reference audio (wav/mp3/etc). language: Language code (e.g., "en"). speed: Speed factor (default 1.0). temperature: Sampling temperature (lower = more deterministic). repetition_penalty: Penalty to avoid loops. """ print(f"--- NEW REQUEST ---") print(f"Text: {text[:50]}...") print(f"Reference: {audio_filepath}") print(f"Params: Speed={speed}, Temp={temperature}, Rep={repetition_penalty}") if not tts: return None, "Error: Model failed to load at startup." if not audio_filepath: return None, "Error: No audio file provided. Please record or upload a voice sample." output_path = "output.wav" try: # XTTS generation tts.tts_to_file( text=text, file_path=output_path, speaker_wav=audio_filepath, language=language, speed=speed, temperature=temperature, repetition_penalty=repetition_penalty ) return output_path, f"Success! Generated on {device}." except Exception as e: error_msg = str(e) print(f"GENERATION ERROR: {error_msg}") return None, f"Error: {error_msg}" # 3. UI Setup # Defined languages for XTTS v2 LANGUAGES = [ "en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "ko", "hu" ] with gr.Blocks(title="PLOT Voice Engine Pro") as demo: gr.Markdown( """ # 🎙️ PLOT Voice Engine (XTTS v2) **Advanced Voice Cloning** - improved stability and control. """ ) with gr.Row(): with gr.Column(scale=1): # Input Section text_input = gr.Textbox( label="Text to Speak", value="Hello! This is a test of the enhanced voice cloning system.", lines=3 ) # Using 'filepath' handles un/packing of audio automatically audio_input = gr.Audio( label="Reference Voice (Record or Upload)", type="filepath", # <--- Crucial fix: let Gradio handle files sources=["microphone", "upload"] ) with gr.Accordion("Advanced Settings", open=True): language_dropdown = gr.Dropdown( label="Language", choices=LANGUAGES, value="en" ) speed_slider = gr.Slider( label="Speed", minimum=0.5, maximum=2.0, value=1.0, step=0.1 ) temp_slider = gr.Slider( label="Temperature (Stability)", minimum=0.01, maximum=1.0, value=0.75, step=0.05, info="Lower = more stable, Higher = more expressive." ) rep_slider = gr.Slider( label="Repetition Penalty", minimum=1.0, maximum=10.0, value=5.0, step=0.1, info="Increase if the model repeats itself." ) submit_btn = gr.Button("🚀 Generate Voice", variant="primary", size="lg") with gr.Column(scale=1): # Output Section audio_output = gr.Audio(label="Generated Audio", type="filepath", interactive=False) status_output = gr.Textbox(label="Status Log", interactive=False) submit_btn.click( fn=clone_voice, inputs=[ text_input, audio_input, language_dropdown, speed_slider, temp_slider, rep_slider ], outputs=[audio_output, status_output] ) if __name__ == "__main__": demo.launch(ssr_mode=False)