diff --git a/services/tts/server.py b/services/tts/server.py index a6fa863..86c2a8d 100644 --- a/services/tts/server.py +++ b/services/tts/server.py @@ -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) \ No newline at end of file + app.run(host="0.0.0.0", port=5002)