import os import tempfile from TTS.api import TTS import gradio as gr # Global singleton: model loaded only once tts = None counter = 0 # To auto-number output files MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2" def load_model_once(): global tts if tts is None: tts = TTS(model_name=MODEL_NAME, gpu=False) return tts def generate_audio(text, speaker_wav): global counter if not text or speaker_wav is None: return None tts_model = load_model_once() counter += 1 out_path = os.path.join(tempfile.gettempdir(), f"{counter}.wav") tts_model.tts_to_file( text=text, speaker_wav=speaker_wav, language="hi", file_path=out_path ) return out_path with gr.Blocks(title="XTTS Voice Clone (CPU)") as demo: gr.Markdown("## 🎙️ XTTS Voice Cloning (CPU Space)\nUpload voice + enter text → get WAV") with gr.Row(): text_in = gr.Textbox(label="Enter Text (Hindi / English)", lines=4, placeholder="नमस्ते...") wav_in = gr.Audio(label="Upload Voice Sample (WAV)", type="filepath") out_audio = gr.Audio(label="Generated Voice") btn = gr.Button("Generate Voice") # ⚡ Set concurrency_limit directly instead of deprecated queue() btn.click( fn=generate_audio, inputs=[text_in, wav_in], outputs=out_audio, concurrency_limit=2 # adjust as needed ) demo.launch(max_threads=2) # adjust max_threads if needed