File size: 12,460 Bytes
b7ba9cf
 
 
 
 
 
 
 
 
5f6a457
 
b7ba9cf
 
 
5f6a457
f746679
ce13a4f
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
5f6a457
b7ba9cf
 
 
 
 
 
 
5f6a457
 
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f746679
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f746679
b7ba9cf
 
5f6a457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
5f6a457
 
 
 
 
 
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f6a457
 
b7ba9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Voxi Lite HTTP / WebSocket server.

    uvicorn server:app --host 0.0.0.0 --port 8080          (one model per process; scale with more processes)

Environment:
    VOXI_MODEL_DIR   model folder (default: this folder)
    VOXI_THREADS     CPU threads per process (default 4)
    VOXI_WARM        JSON file: {"hi": ["नमस्ते, ..."], "en": ["Hello, ..."]} phrases pre-rendered at start-up
    VOXI_WARM_VOICES comma list of voices to warm (default payal,arjun)
    VOXI_API_KEY     if set, every endpoint except /health needs it: header "Authorization: Bearer KEY" or
                     "X-API-Key: KEY" (WebSocket: either header, or ?key=KEY for browsers); otherwise 401

Endpoints:
    GET  /health                 {"status": "ok", "version", "voices"}
    GET  /ui                     browser test console (ui.html; no key needed to load it, the page asks for it)
    GET  /v1/voices              voice names (base voices and their styles; harper, ethan, amelia, oliver: lang "en" only)
    POST /v1/tts                 {"text", "lang": "hi" (hi ta en hinglish tanglish), "voice": "payal", "format": "wav|pcm16|mulaw",
                                  "sample_rate": 8000|16000|24000, "stream": false}
                                 stream=true: chunked raw audio (pcm16 / mulaw) as it is made
    POST /v1/audio/speech        OpenAI-style: {"model", "input", "voice", "response_format": "wav|pcm",
                                  "language": "hi"}  (pcm = 24 kHz 16-bit little-endian, as OpenAI)
    WS   /v1/tts/ws              send {"text", "lang", "voice", "format", "sample_rate", "request_id"};
                                 receive binary audio frames, then {"event": "done", "request_id", ...}.
                                 Send {"event": "cancel"} to stop the current request. Several requests per socket.
Errors: 400 with {"error": "..."} for bad input (unknown voice / language / format, text too long).
Logs: one JSON line per request on the "voxi" logger with lengths, language, voice, timings and status; never the
text or the audio.
"""
import asyncio
import contextlib
import hmac
import json
import logging
import os
import threading
import time
from pathlib import Path

from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse, JSONResponse, Response, StreamingResponse
from pydantic import BaseModel

from voxi import FORMATS, MAX_CHARS, RATES, SR, VoxiLite, encode

HERE = Path(__file__).resolve().parent
log = logging.getLogger("voxi")
if not log.handlers:
    h = logging.StreamHandler(); h.setFormatter(logging.Formatter("%(message)s")); log.addHandler(h); log.setLevel(logging.INFO)

MIME = {"wav": "audio/wav", "pcm16": "audio/L16", "mulaw": "audio/basic"}


class TTSRequest(BaseModel):
    text: str
    lang: str = "hi"
    voice: str = "payal"
    format: str = "wav"
    sample_rate: int = SR
    stream: bool = False


class SpeechRequest(BaseModel):                          # OpenAI /v1/audio/speech shape
    input: str
    voice: str = "payal"
    model: str = "voxi-lite"
    response_format: str = "wav"
    language: str = "hi"
    speed: float = 1.0


class Engine:
    """One VoxiLite per process; synthesis is serialised (the model is not thread-safe), chunk by chunk, so
    concurrent streams interleave instead of waiting for each other's whole reply."""

    def __init__(self):
        self.tts = VoxiLite(os.environ.get("VOXI_MODEL_DIR", HERE), threads=int(os.environ.get("VOXI_THREADS", 4)))
        self.lock = threading.Lock()
        self.version = self.tts.tts.cfg.get("version", "?")
        with self.lock:
            self.tts.synthesize("नमस्ते।", "hi")                     # warm-up
        warm = os.environ.get("VOXI_WARM")
        if warm:
            voices = os.environ.get("VOXI_WARM_VOICES", "payal,arjun").split(",")
            for lang, phrases in json.load(open(warm, encoding="utf-8")).items():
                with self.lock:
                    self.tts.warm(phrases, lang, voices)

    def check(self, text, lang, voice, fmt, rate):
        if fmt not in FORMATS:
            raise ValueError(f"unsupported format {fmt!r}; use one of {list(FORMATS)}")
        if rate not in RATES:
            raise ValueError(f"unsupported sample_rate {rate}; use one of {list(RATES)}")
        self.tts._check_voice(voice, lang)
        self.tts.prepare(text, lang)                     # raises ValueError for a bad language or long text

    def whole(self, text, lang, voice):
        with self.lock:
            return self.tts.synthesize(text, lang, voice)

    def chunks(self, text, lang, voice):
        """Thread-side generator: each chunk is made under the lock."""
        it = self.tts.stream(text, lang, voice)
        while True:
            with self.lock:
                y = next(it, None)
            if y is None:
                return
            yield y


engine: Engine | None = None


@contextlib.asynccontextmanager
async def lifespan(_app):
    global engine
    if engine is None:
        engine = await asyncio.to_thread(Engine)
    yield


app = FastAPI(title="Voxi Lite", version="0.4.0", lifespan=lifespan)


API_KEY = os.environ.get("VOXI_API_KEY", "")


def _key_ok(headers, query=None):
    if not API_KEY:
        return True
    auth = headers.get("authorization", "")
    got = auth[7:] if auth.lower().startswith("bearer ") else headers.get("x-api-key", "") or (query or {}).get("key", "")
    return hmac.compare_digest(got.encode(), API_KEY.encode())


@app.middleware("http")
async def require_key(request: Request, call_next):
    if request.url.path not in ("/health", "/ui") and not _key_ok(request.headers):
        return JSONResponse({"error": "missing or wrong API key"}, status_code=401)
    return await call_next(request)


def _log(**kw):
    log.info(json.dumps({"event": "tts", **kw}, ensure_ascii=False))


def _bad(e):
    return JSONResponse({"error": str(e)}, status_code=400)


@app.get("/health")
def health():
    return {"status": "ok", "version": engine.version, "voices": sum("@" not in v for v in engine.tts.voices)}


@app.get("/ui", response_class=HTMLResponse)
def ui():
    page = HERE / "ui.html"
    return HTMLResponse(page.read_text(encoding="utf-8") if page.exists() else "<p>ui.html not installed</p>")


@app.get("/v1/voices")
def voices():
    return {"voices": engine.tts.voices}


async def _stream_bytes(text, lang, voice, fmt, rate, meta):
    t0, first, n = time.perf_counter(), None, 0
    gen = engine.chunks(text, lang, voice)
    try:
        while True:
            y = await asyncio.to_thread(next, gen, None)
            if y is None:
                break
            first = first if first is not None else time.perf_counter() - t0
            n += len(y)
            yield encode(y, "pcm16" if fmt == "wav" else fmt, rate)
        meta.update(status="ok")
    except Exception as e:                               # noqa: BLE001
        meta.update(status="error", error=type(e).__name__)
        raise
    finally:
        _log(**meta, first_ms=None if first is None else round(first * 1000), total_ms=round((time.perf_counter() - t0) * 1000),
             audio_s=round(n / SR, 2))


@app.post("/v1/tts")
async def tts(req: TTSRequest):
    meta = {"api": "tts", "chars": len(req.text), "lang": req.lang, "voice": req.voice, "format": req.format,
            "sample_rate": req.sample_rate, "stream": req.stream}
    try:
        engine.check(req.text, req.lang, req.voice, req.format, req.sample_rate)
        if req.stream and req.format == "wav":
            raise ValueError("stream=true needs format pcm16 or mulaw (a WAV header needs the total length)")
    except ValueError as e:
        _log(**meta, status="bad_request", error=str(e)[:120]); return _bad(e)
    if req.stream:
        mime = f"{MIME[req.format]};rate={req.sample_rate}"
        return StreamingResponse(_stream_bytes(req.text, req.lang, req.voice, req.format, req.sample_rate, meta), media_type=mime)
    t0 = time.perf_counter()
    y = await asyncio.to_thread(engine.whole, req.text, req.lang, req.voice)
    _log(**meta, status="ok", total_ms=round((time.perf_counter() - t0) * 1000), audio_s=round(len(y) / SR, 2))
    mime = MIME[req.format] + ("" if req.format == "wav" else f";rate={req.sample_rate}")
    return Response(encode(y, req.format, req.sample_rate), media_type=mime)


@app.post("/v1/audio/speech")
async def speech(req: SpeechRequest):
    fmt = {"wav": "wav", "pcm": "pcm16"}.get(req.response_format)
    meta = {"api": "speech", "chars": len(req.input), "lang": req.language, "voice": req.voice, "format": req.response_format}
    try:
        if fmt is None:
            raise ValueError("response_format must be wav or pcm")
        if req.speed != 1.0:
            raise ValueError("speed other than 1.0 is not supported")
        engine.check(req.input, req.language, req.voice, fmt, SR)
    except ValueError as e:
        _log(**meta, status="bad_request", error=str(e)[:120]); return _bad(e)
    t0 = time.perf_counter()
    y = await asyncio.to_thread(engine.whole, req.input, req.language, req.voice)
    _log(**meta, status="ok", total_ms=round((time.perf_counter() - t0) * 1000), audio_s=round(len(y) / SR, 2))
    return Response(encode(y, fmt, SR), media_type=MIME[fmt] + ("" if fmt == "wav" else f";rate={SR}"))


@app.websocket("/v1/tts/ws")
async def tts_ws(ws: WebSocket):
    if not _key_ok(ws.headers, ws.query_params):
        await ws.close(code=1008); return                # policy violation: missing or wrong API key
    await ws.accept()
    cancel = asyncio.Event()
    queue: asyncio.Queue = asyncio.Queue()

    async def reader():                                  # requests and cancels arrive while audio is being sent
        try:
            while True:
                msg = json.loads(await ws.receive_text())
                if msg.get("event") == "cancel":
                    cancel.set()
                else:
                    await queue.put(msg)
        except (WebSocketDisconnect, RuntimeError):
            cancel.set(); await queue.put(None)
        except json.JSONDecodeError:
            await queue.put({"_bad": "messages must be JSON"})

    task = asyncio.create_task(reader())
    try:
        while True:
            msg = await queue.get()
            if msg is None:
                break
            rid = msg.get("request_id")
            if "_bad" in msg:
                await ws.send_text(json.dumps({"event": "error", "request_id": rid, "error": msg["_bad"]})); continue
            text, lang, voice = msg.get("text", ""), msg.get("lang", "hi"), msg.get("voice", "payal")
            fmt, rate = msg.get("format", "pcm16"), int(msg.get("sample_rate", SR))
            meta = {"api": "ws", "chars": len(text), "lang": lang, "voice": voice, "format": fmt, "sample_rate": rate}
            try:
                if fmt == "wav":
                    raise ValueError("use format pcm16 or mulaw on the WebSocket")
                if len(text) > MAX_CHARS:
                    raise ValueError(f"text longer than {MAX_CHARS} characters")
                engine.check(text, lang, voice, fmt, rate)
            except ValueError as e:
                _log(**meta, status="bad_request", error=str(e)[:120])
                await ws.send_text(json.dumps({"event": "error", "request_id": rid, "error": str(e)})); continue
            cancel.clear()
            t0, first, n, status = time.perf_counter(), None, 0, "ok"
            gen = engine.chunks(text, lang, voice)
            while True:
                if cancel.is_set():
                    status = "cancelled"; break
                y = await asyncio.to_thread(next, gen, None)
                if y is None:
                    break
                first = first if first is not None else time.perf_counter() - t0
                n += len(y)
                await ws.send_bytes(encode(y, fmt, rate))
            done = {"event": "done", "request_id": rid, "status": status, "audio_s": round(n / SR, 2),
                    "first_audio_ms": None if first is None else round(first * 1000)}
            _log(**meta, status=status, first_ms=done["first_audio_ms"], total_ms=round((time.perf_counter() - t0) * 1000),
                 audio_s=done["audio_s"])
            await ws.send_text(json.dumps(done))
    except (WebSocketDisconnect, RuntimeError):
        pass
    finally:
        task.cancel()