kale-tts-1-tiny / server /server.mjs
NERDDISCO's picture
Clear file names: kale-tts-1-tiny.onnx at the root, PyTorch weights in pytorch/
6901100 verified
Raw History Blame Contribute Delete
5.51 kB
// kale-tts self-hosted HTTP API (OpenAI-shaped). POST /v1/audio/speech {model, input, voice, response_format: "wav"|"pcm", stream}
// model: kale-tts-1-nano (default) | kale-tts-1-tiny; voice by name: kenzie simone amy frankie darlene owen toby jack earl magnus (GET /v1/voices)
// stream: true (or response_format "pcm") -> chunked raw 16-bit 24 kHz mono PCM, one chunk per sentence as it is ready
// otherwise one WAV. With TTS_API_TOKEN set (>= 20 chars) every route except /health needs "Authorization: Bearer <token>";
// without it the server only listens on 127.0.0.1 (local use).
// run (in the release folder or a downloaded model repo): npm i onnxruntime-node && node server/server.mjs (env: PORT=8000, HOST, KALE_TTS_WORKERS=<cores, max 8>, KALE_TTS_THREADS=1,
// KALE_TTS_ROOT=<release folder>/)
import http from "node:http";
import { timingSafeEqual, createHash } from "node:crypto";
import cluster from "node:cluster";
import os from "node:os";
// onnxruntime-node runs a session on the JS thread, so one process renders one request at a time (a session pool doesn't help).
// Parallel requests: KALE_TTS_WORKERS processes on the same port (node:cluster), default = CPU cores (max 8), 1 onnxruntime thread each.
// Each process loads both models (~0.3 GB). Measured on a Mac: 1 process nano 152x, tiny 82x real time (18 Nov); 10 processes 465 s of audio/s (16 Nov)
// with 10 parallel clients.
const WORKERS = Number(process.env.KALE_TTS_WORKERS ?? Math.min(8, os.availableParallelism?.() ?? os.cpus().length));
if (cluster.isPrimary && WORKERS > 1) {
for (let i = 0; i < WORKERS; i++) cluster.fork();
cluster.on("exit", (w, code) => { if (code !== 0) { console.error(`kale-tts worker ${w.process.pid} exited (${code}), restarting`); cluster.fork(); } });
await new Promise(() => {});
}
const T0 = performance.now();
const ROOT = process.env.KALE_TTS_ROOT ?? new URL("../", import.meta.url).pathname; // the release folder (js/, models/, frontend/) or one downloaded model repo
const { KaleTTS, toWav } = await import(ROOT + "js/kale-tts.mjs");
const ort = await import("onnxruntime-node");
const threads = Number(process.env.KALE_TTS_THREADS ?? 1);
// a downloaded model repo (voices.json at its root) serves that one model; the release folder serves both
const { existsSync } = await import("node:fs");
const ONE = existsSync(ROOT + "voices.json") ? (process.env.KALE_TTS_MODEL ?? (existsSync(ROOT + "ettin-tokenizer.json") ? "kale-tts-1-tiny" : "kale-tts-1-nano")) : null;
const MODELS = ONE ? [ONE] : ["kale-tts-1-nano", "kale-tts-1-tiny"];
const tts = {}; for (const m of MODELS) tts[m] = await KaleTTS.load(ONE ? ROOT : m, { ort, threads });
for (const m of MODELS) { await tts[m].preload(); for (const v of tts[m].listVoices()) await tts[m].speak("Hi.", { voice: v.id }); } // load voices + warm
const TOKEN = process.env.TTS_API_TOKEN ?? ""; if (TOKEN && TOKEN.length < 20) throw new Error("TTS_API_TOKEN must be at least 20 characters");
const h = (s) => createHash("sha256").update(s).digest();
const authed = (req) => { if (!TOKEN) return true; const a = req.headers.authorization ?? ""; return a.startsWith("Bearer ") && timingSafeEqual(h(a.slice(7)), h(TOKEN)); };
const json = (res, code, obj) => { res.writeHead(code, { "content-type": "application/json" }); res.end(JSON.stringify(obj)); };
http.createServer(async (req, res) => {
try {
if (req.method === "GET" && req.url === "/health") return res.end("ok");
if (!authed(req)) return json(res, 401, { error: "missing or wrong bearer token" });
if (req.method === "GET" && req.url === "/v1/models") return json(res, 200, { data: MODELS.map((id) => ({ id, object: "model" })) });
if (req.method === "GET" && req.url === "/v1/voices") return json(res, 200, { data: tts[MODELS[0]].listVoices() });
if (req.method !== "POST" || req.url !== "/v1/audio/speech") return json(res, 404, { error: "not found" });
let body = ""; for await (const c of req) { body += c; if (body.length > 20000) return json(res, 413, { error: "input too long (max ~20k chars)" }); }
const b = JSON.parse(body || "{}"); const input = String(b.input ?? "").trim();
if (!input) return json(res, 400, { error: "input is empty" });
const model = MODELS.includes(b.model) ? b.model : MODELS[0]; const t = tts[model];
let voice; try { voice = t.resolveVoice(b.voice ?? t.listVoices()[0].id); } catch { return json(res, 400, { error: `unknown voice ${b.voice}`, voices: t.listVoices().map((v) => ({ name: (v.name ?? v.id).toLowerCase(), id: v.id })) }); } // public name (any case) or id
const t0 = performance.now();
if (b.stream || b.response_format === "pcm") {
res.writeHead(200, { "content-type": "audio/pcm", "x-sample-rate": "24000", "x-channels": "1", "x-sample-format": "s16le", "cache-control": "no-store" });
for await (const c of t.stream(input, { voice })) res.write(toWav(c.audio).subarray(44));
return res.end();
}
const { audio } = await t.speak(input, { voice }); const w = toWav(audio);
res.writeHead(200, { "content-type": "audio/wav", "content-length": w.length, "x-compute-ms": Math.round(performance.now() - t0) }); res.end(w);
} catch (e) { if (!res.headersSent) json(res, 400, { error: String(e?.message ?? e) }); else res.end(); }
}).listen(Number(process.env.PORT ?? 8000), process.env.HOST ?? (TOKEN ? "0.0.0.0" : "127.0.0.1"), () => console.log(`kale-tts api ready after ${((performance.now() - T0) / 1000).toFixed(2)} s`));