voxi-lite / tests /test_server.py
Joshuant's picture
Voxi Lite 0.4.0: American and British English voices (harper, ethan, amelia, oliver on Kokoro-82M), CPU comparison on the card, English-accents samples
f746679 verified
Raw History Blame Contribute Delete
7.55 kB
"""Voxi Lite server checks (in-process, no network): every endpoint, format and error path, and the WebSocket
(stream, cancel, error, two requests on one socket).
python tests/test_server.py
"""
import io
import json
import sys
from pathlib import Path
import soundfile as sf
from fastapi.testclient import TestClient
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import server # noqa: E402
LINE = "नमस्ते, आपका ऑर्डर कल शाम तक पहुँच जाएगा। क्या मैं और कोई मदद कर सकती हूँ?"
LONG = " ".join(["आपका ऑर्डर कल शाम तक पहुँच जाएगा, और ट्रैकिंग लिंक एसएमएस पर भेज दिया गया है।"] * 6)
results = []
def check(name, ok, detail=""):
results.append(ok)
print(("PASS " if ok else "FAIL ") + name + (f" ({detail})" if detail else ""), flush=True)
def main():
with TestClient(server.app) as c:
r = c.get("/health"); j = r.json()
check("health", r.status_code == 200 and j["status"] == "ok" and j["voices"] >= 4, json.dumps(j))
v = c.get("/v1/voices").json()["voices"]
check("voices", all(x in v for x in ("payal", "arjun", "sanjana", "vishnu", "payal@happy", "arjun@sad-soft",
"harper", "ethan", "amelia", "oliver")), f"{len(v)} names")
r = c.post("/v1/tts", json={"text": LINE, "lang": "hi", "voice": "payal"})
y, sr = sf.read(io.BytesIO(r.content))
check("tts wav 24k", r.status_code == 200 and r.headers["content-type"].startswith("audio/wav") and sr == 24000 and len(y) > sr,
f"{len(y) / sr:.2f} s")
dur = len(y) / sr
r = c.post("/v1/tts", json={"text": LINE, "voice": "arjun", "format": "pcm16", "sample_rate": 16000})
check("tts pcm16 16k", r.status_code == 200 and "rate=16000" in r.headers["content-type"] and len(r.content) % 2 == 0
and len(r.content) / 2 / 16000 > 1, f"{len(r.content) / 32000:.2f} s")
r = c.post("/v1/tts", json={"text": LINE, "voice": "payal@happy", "format": "mulaw", "sample_rate": 8000, "stream": True})
check("tts mulaw 8k streamed", r.status_code == 200 and r.headers["content-type"].startswith("audio/basic")
and len(r.content) / 8000 > 1, f"{len(r.content) / 8000:.2f} s")
r = c.post("/v1/tts", json={"text": "Your refund has been processed.", "lang": "en", "voice": "sanjana", "format": "wav",
"sample_rate": 8000})
y8, sr8 = sf.read(io.BytesIO(r.content)); check("tts wav 8k", r.status_code == 200 and sr8 == 8000 and len(y8) > 4000)
r = c.post("/v1/tts", json={"text": "Aapka bill kal tak pay kar dijiye.", "lang": "hinglish", "voice": "vishnu"})
check("tts hinglish", r.status_code == 200)
r = c.post("/v1/tts", json={"text": "Unga order naalaikku varum, link SMS-la anuppirukom.", "lang": "tanglish", "voice": "payal"})
check("tts tanglish", r.status_code == 200 and len(sf.read(io.BytesIO(r.content))[0]) > 24000)
for voice in ("harper", "amelia"):
r = c.post("/v1/tts", json={"text": "Your refund has been processed.", "lang": "en", "voice": voice, "format": "mulaw",
"sample_rate": 8000, "stream": True})
check(f"tts english {voice} mulaw streamed", r.status_code == 200 and len(r.content) / 8000 > 1, f"{len(r.content) / 8000:.2f} s")
r = c.post("/v1/tts", json={"text": "नमस्ते", "lang": "hi", "voice": "oliver"})
check("400 english voice with hi", r.status_code == 400 and "English only" in r.json().get("error", ""), r.text[:80])
r = c.post("/v1/tts", json={"text": "", "lang": "hi"})
check("tts empty text -> empty audio", r.status_code == 200 and len(sf.read(io.BytesIO(r.content))[0]) == 0)
for name, body, frag in [("bad voice", {"text": "hi", "voice": "nobody"}, "unknown voice"),
("bad lang", {"text": "hi", "lang": "xx"}, "unsupported lang"),
("bad format", {"text": "hi", "format": "mp3"}, "unsupported format"),
("bad rate", {"text": "hi", "sample_rate": 44100}, "sample_rate"),
("text too long", {"text": "क" * 2001}, "longer than"),
("wav stream", {"text": "hi", "stream": True}, "stream=true")]:
r = c.post("/v1/tts", json=body)
check(f"400 {name}", r.status_code == 400 and frag in r.json().get("error", ""), r.text[:80])
r = c.post("/v1/audio/speech", json={"model": "voxi-lite", "input": LINE, "voice": "payal", "response_format": "wav"})
check("openai speech wav", r.status_code == 200 and sf.read(io.BytesIO(r.content))[1] == 24000)
r = c.post("/v1/audio/speech", json={"input": "Hello there, how can I help?", "voice": "arjun", "response_format": "pcm",
"language": "en"})
check("openai speech pcm", r.status_code == 200 and len(r.content) / 48000 > 0.5)
r = c.post("/v1/audio/speech", json={"input": "hi", "response_format": "mp3"})
check("openai speech 400", r.status_code == 400)
with c.websocket_connect("/v1/tts/ws") as ws:
ws.send_text(json.dumps({"text": LINE, "lang": "hi", "voice": "payal", "format": "mulaw", "sample_rate": 8000,
"request_id": "a"}))
n, frames = 0, 0
while True:
m = ws.receive()
if m.get("bytes") is not None:
n += len(m["bytes"]); frames += 1
else:
done = json.loads(m["text"]); break
check("ws stream", done["event"] == "done" and done["request_id"] == "a" and done["status"] == "ok" and frames >= 2
and abs(n / 8000 - done["audio_s"]) < 0.1, f"{frames} frames, {n / 8000:.2f} s, first {done['first_audio_ms']} ms")
ws.send_text(json.dumps({"text": LONG, "lang": "hi", "voice": "arjun", "format": "pcm16", "sample_rate": 16000,
"request_id": "b"}))
first = ws.receive()
ws.send_text(json.dumps({"event": "cancel"}))
while True:
m = ws.receive()
if m.get("text") is not None:
done = json.loads(m["text"]); break
check("ws cancel", first.get("bytes") is not None and done["request_id"] == "b" and done["status"] == "cancelled"
and done["audio_s"] < 30, json.dumps(done))
ws.send_text(json.dumps({"text": "hi", "voice": "nobody", "request_id": "c"}))
e = json.loads(ws.receive_text())
check("ws error", e["event"] == "error" and e["request_id"] == "c" and "unknown voice" in e["error"])
ws.send_text(json.dumps({"text": "जी, बिल्कुल।", "voice": "sanjana", "request_id": "d"}))
while True:
m = ws.receive()
if m.get("text") is not None:
done = json.loads(m["text"]); break
check("ws second request", done["request_id"] == "d" and done["status"] == "ok")
print(f"SERVER_TESTS {sum(results)}/{len(results)} passed", flush=True)
sys.exit(0 if all(results) else 1)
if __name__ == "__main__":
main()