Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from TTS.api import TTS | |
| import torch | |
| import spaces # Required for Hugging Face GPU Zero | |
| # 1. Setup Device (Hugging Face ZeroGPU handles this via @spaces.GPU) | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # 2. Load the Model (XTTS v2 is the gold standard) | |
| # We do this outside the function so it only loads once | |
| tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) | |
| # This gives you free GPU access on Hugging Face | |
| def clone_voice(text, audio_file): | |
| if audio_file is None: | |
| return None, "Please upload a reference audio file." | |
| output_path = "output.wav" | |
| # The actual cloning process | |
| tts.tts_to_file( | |
| text=text, | |
| speaker_wav=audio_file, | |
| language="en", | |
| file_path=output_path | |
| ) | |
| return output_path | |
| # 3. Create the Interface | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🎙️ Pro Voice Cloner") | |
| gr.Markdown("Upload a 6-10 second clip of a voice and type what you want it to say.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox(label="Text to Speak", placeholder="Hello, I am your cloned voice...", lines=3) | |
| input_audio = gr.Audio(label="Reference Audio (Clone from this)", type="filepath") | |
| submit_btn = gr.Button("Clone Voice", variant="primary") | |
| with gr.Column(): | |
| output_audio = gr.Audio(label="Generated Audio") | |
| submit_btn.click( | |
| fn=clone_voice, | |
| inputs=[input_text, input_audio], | |
| outputs=[output_audio] | |
| ) | |
| demo.launch() | |