NERDDISCO's picture
kale-tts-1
f21d6eb
Raw History Blame Contribute Delete
3.24 kB
// transformers.js (node) loads kale-tts-1 as-is: StyleTextToSpeech2Model (our config.json says style_text_to_speech_2) passes every
// graph input through, so the extra voice inputs just ride along. usage: node tjs_node.mjs <nano|tiny> [bundled|inject] [bench]
// inject = a newer onnxruntime-node handed to transformers.js via globalThis[Symbol.for("onnxruntime")] (its documented seam)
import { readFileSync, writeFileSync } from "node:fs";
const [, , size = "nano", mode = "bundled", doBench] = process.argv;
let injected = null;
if (mode === "inject") { injected = await import("onnxruntime-node"); globalThis[Symbol.for("onnxruntime")] = injected; }
const { StyleTextToSpeech2Model, Tensor, env } = await import("@huggingface/transformers");
const RD = new URL("../../", import.meta.url).pathname; // the release folder
const dir = `${RD}models/kale-tts-1-${size}/`;
// --- snippet (the model card's): text -> the model's inputs with our front end (KaleTTS.prepare, no onnxruntime of its own) ----------
env.allowRemoteModels = false; env.localModelPath = "";
const model = await StyleTextToSpeech2Model.from_pretrained(dir, { subfolder: "", dtype: "fp32" });
const { KaleTTS } = await import(RD + "js/kale-tts.mjs");
const prep = await KaleTTS.prepare(dir); // numbers, dates, the voice's style + voice inputs, and for tiny the Ettin word inputs
const voice = process.env.KALE_VOICE ?? "kenzie"; // a name or an id from voices.json
const feedsOf = (inputs) => Object.fromEntries(Object.entries(inputs).map(([k, t]) => [k, new Tensor(t.type, t.data, t.dims)]));
const parts = [];
for await (const { inputs } of prep.inputs("A volcano is a place.", { voice })) parts.push((await model(feedsOf(inputs))).waveform.data);
const wav = Float32Array.from(parts.flatMap((p) => [...p]));
// ids in (the bench lines, nano): the voice's feeds with these input_ids; tiny also needs Ettin word inputs per sentence (prep.inputs)
const ettin = !!prep.ettinTok, vs = await prep._loadVoice(voice);
async function say(ids) { return (await model(feedsOf(prep._feeds(vs, ids)))).waveform.data; }
// ---------------------------------------------------------------------------------------------------------------------------
const { toWav } = await import(RD + "js/kale-tts.mjs");
const { createRequire } = await import("node:module"); const { dirname } = await import("node:path");
const ver = (name, from) => { let d = dirname(createRequire(from).resolve(name)); while (!d.endsWith("/" + name)) d = dirname(d); return JSON.parse(readFileSync(d + "/package.json")).version; };
const tjsMain = import.meta.resolve("@huggingface/transformers");
const ortv = injected ? ver("onnxruntime-node", import.meta.url) : ver("onnxruntime-node", tjsMain), tv = ver("@huggingface/transformers", import.meta.url);
console.log(JSON.stringify({ size, mode, transformers: tv, ort_node: ortv, samples: wav.length, outputs: model.sessions.model.outputNames }));
if (doBench && ettin) console.log("bench: ids-only lines skip tiny (it also needs Ettin word inputs per sentence)");
else if (doBench) { const { bench } = await import(RD + "runtimes/_bench/bench.mjs"); console.log(JSON.stringify({ size, mode, ort_node: ortv, ...(await bench((ids) => say(ids))) })); }