File size: 1,503 Bytes
db2bd37 3a02521 db2bd37 3a02521 db2bd37 3a02521 db2bd37 3a02521 db2bd37 3a02521 db2bd37 35af385 db2bd37 35af385 db2bd37 35af385 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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
|