tts: male VCTK fallback until voice-sample.wav lands
Some checks failed
ci / lint (push) Has been cancelled

This commit is contained in:
Timmy 2026-07-08 19:41:02 +00:00
parent d9a4e8dcae
commit e1995bab11

View File

@ -1,23 +1,31 @@
"""Coqui XTTS wrapper: POST /synthesize -> WAV audio bytes."""
import os
import uuid
"""TTS wrapper: clone mode if reference audio is present, else fallback to VCTK male."""
from pathlib import Path
from flask import Flask, request, send_file, jsonify
import os, uuid
app = Flask(__name__)
VOICE_NAME = os.getenv("VOICE_NAME", "wizard")
REFERENCE_DIR = Path("/app/reference")
OUT_DIR = Path("/tmp/tts-out")
OUT_DIR.mkdir(parents=True, exist_ok=True)
_ref = REFERENCE_DIR / "voice-sample.wav"
REF = Path(os.getenv("REFERENCE_AUDIO", "/app/reference/voice-sample.wav"))
from TTS.api import TTS
tts = TTS(model_name="tts_models/en/vctk/vits", progress_bar=False, gpu=False)
def synth_to_file(text: str, out: Path):
if REF.exists():
tts.tts_to_file(text=text, speaker_wav=str(REF), file_path=str(out))
else:
# male VCTK fallback speaker
tts.tts_to_file(text=text, speaker="p267", file_path=str(out))
@app.get("/health")
def health():
return {"status": "ok", "voice": VOICE_NAME}
return {
"status": "ok",
"mode": "clone" if REF.exists() else "fallback-male",
"reference": str(REF),
"model": "tts_models/en/vctk/vits",
}
@app.post("/synthesize")
def synthesize():
@ -26,8 +34,8 @@ def synthesize():
if not text:
return jsonify({"error": "text required"}), 400
out = OUT_DIR / f"{uuid.uuid4().hex}.wav"
tts.tts_to_file(text=text, file_path=str(out))
synth_to_file(text, out)
return send_file(str(out), mimetype="audio/wav")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5002)
app.run(host="0.0.0.0", port=5002)