File size: 3,032 Bytes
65700a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import asyncio
import base64
import os
import uuid
from contextlib import asynccontextmanager
from pathlib import Path

from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, Response
from loguru import logger

from app.runtime import TTSRuntime, SAMPLE_RATE
from app.schemas import HealthResponse, OptionsResponse, ReadyResponse, TTSJSONResponse, TTSRequest
from app.speaker_map import SUPPORTED_SPEAKERS, validate_language_user

MAX_INFLIGHT = int(os.getenv("MAX_INFLIGHT_REQUESTS", "2"))
runtime = TTSRuntime()
semaphore = asyncio.Semaphore(MAX_INFLIGHT)


@asynccontextmanager
async def lifespan(app: FastAPI):
    runtime.load()
    yield


app = FastAPI(title="snorTTS Hosting API", version="1.0.0", lifespan=lifespan)


@app.get("/")
def root() -> dict:
    return {
        "service": "snorTTS Hosting API",
        "routes": ["/health", "/ready", "/v1/options", "/v1/tts", "/ui"],
    }


@app.get("/ui")
def ui() -> FileResponse:
    return FileResponse(Path(__file__).with_name("ui.html"))


@app.get("/health", response_model=HealthResponse)
def health() -> HealthResponse:
    return HealthResponse()


@app.get("/ready", response_model=ReadyResponse)
def ready() -> ReadyResponse:
    return ReadyResponse(ready=runtime.is_ready)


@app.get("/v1/options", response_model=OptionsResponse)
def options() -> OptionsResponse:
    defaults = {
        "temperature": float(os.getenv("TTS_TEMPERATURE", "0.4")),
        "top_p": float(os.getenv("TTS_TOP_P", "0.9")),
        "repetition_penalty": float(os.getenv("TTS_REPETITION_PENALTY", "1.05")),
    }
    return OptionsResponse(speakers=SUPPORTED_SPEAKERS, defaults=defaults)


@app.post("/v1/tts", response_class=Response)
async def tts(req: TTSRequest, response_mode: str = "wav"):
    if not validate_language_user(req.language, req.user_id):
        raise HTTPException(
            status_code=400,
            detail=f"Invalid user_id '{req.user_id}' for language '{req.language}'",
        )

    async with semaphore:
        try:
            wav_bytes, duration_ms = await asyncio.to_thread(
                runtime.synthesize_wav_bytes,
                req.utterance,
                req.language,
                req.user_id,
            )
        except RuntimeError as exc:
            raise HTTPException(status_code=422, detail=str(exc)) from exc
        except Exception as exc:
            logger.exception("TTS inference failed")
            raise HTTPException(status_code=500, detail=f"Inference failed: {exc}") from exc

    request_id = str(uuid.uuid4())
    if response_mode == "json":
        payload = TTSJSONResponse(
            request_id=request_id,
            sample_rate=SAMPLE_RATE,
            duration_ms=duration_ms,
            audio_base64=base64.b64encode(wav_bytes).decode("ascii"),
        )
        return payload

    headers = {
        "X-Request-Id": request_id,
        "X-Duration-Ms": str(duration_ms),
    }
    return Response(content=wav_bytes, media_type="audio/wav", headers=headers)