File size: 1,464 Bytes
a8bbd3d 30499da 8eb7cd7 a8bbd3d feab8eb a8bbd3d dcf17fd 8e56947 a8bbd3d 30499da 282a8f6 a8bbd3d 30499da 282a8f6 30499da 282a8f6 30499da 282a8f6 30499da 282a8f6 30499da 282a8f6 a8bbd3d 282a8f6 30499da 282a8f6 30499da a8bbd3d 282a8f6 a8bbd3d feab8eb 282a8f6 3f9ecf2 a8bbd3d 282a8f6 a8bbd3d 282a8f6 a8bbd3d 3f9ecf2 d9f8eb6 3f9ecf2 282a8f6 | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import os
import tempfile
import threading
import gradio as gr
from TTS.api import TTS
os.environ["COQUI_TOS_AGREED"] = "1"
MODEL_PATH = "models/xtts"
CONFIG_PATH = "models/xtts/config.json"
VOCAB_PATH = "models/xtts/vocab.json"
SPEAKERS_PATH = "models/xtts/speakers_xtts.pth"
for p in [MODEL_PATH, CONFIG_PATH, VOCAB_PATH, SPEAKERS_PATH]:
if not os.path.exists(p):
raise RuntimeError(f"Missing file: {p}")
tts = None
model_ready = False
def load_model():
global tts, model_ready
print("🔁 Loading XTTS model once...")
tts = TTS(
model_path=MODEL_PATH,
config_path=CONFIG_PATH,
progress_bar=False,
gpu=False
)
model_ready = True
print("✅ XTTS model loaded!")
threading.Thread(target=load_model, daemon=True).start()
def tts_api(text, speaker_wav):
if not model_ready:
raise gr.Error("Model loading... wait 30 seconds")
if not text or speaker_wav is None:
raise gr.Error("Provide text and speaker audio")
out_path = os.path.join(tempfile.gettempdir(), "out.wav")
tts.tts_to_file(
text=text,
speaker_wav=speaker_wav,
language="en",
file_path=out_path
)
return out_path
demo = gr.Interface(
fn=tts_api,
inputs=[
gr.Textbox(label="Text"),
gr.Audio(type="filepath", label="Speaker WAV"),
],
outputs=gr.Audio(type="filepath"),
api_name="tts"
)
demo.queue()
demo.launch() |