kale-tts-1-tiny / recipe /bench /make_ids.py
NERDDISCO's picture
kale-tts-1
f21d6eb
Raw History Blame Contribute Delete
2.38 kB
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Kale
"""make_ids.py: the bench's text -> ids step. The 192 lines (script.jsonl) and the guard lines (config/bench.json) go through the
RELEASE text front end (../../python/frontend_cli.mjs: sentences + normaliser + lexicon/g2p -> Kokoro-table ids), once per accent,
and are written as ids-<accent>.json = {"guard": [{name, sents}], "bench": [{id, sents}]} (sents = one id list per sentence, no 0 pads).
bench.py renders each voice with the ids of its accent (config/bench.json "accent": d03 / d13 en-gb, all others en-us).
Same role as the id steps (en-us and en-gb) used for the release tables;
the release tables' en-us ids came from our earlier Python copy of the same lexicon + g2p (not part of this release), so a few lines
may differ by an id or two from what this script writes.
usage: python3 make_ids.py [--frontend ../../python/frontend_cli.mjs] [--node node] (needs node >= 18, no npm packages)"""
import argparse
import json
import shutil
import subprocess
from pathlib import Path
HERE = Path(__file__).resolve().parent
ap = argparse.ArgumentParser()
ap.add_argument("--frontend", default=str(HERE.parent.parent / "python" / "frontend_cli.mjs"))
ap.add_argument("--node", default=shutil.which("node") or "node")
ap.add_argument("--config", default=str(HERE / "config" / "bench.json"))
a = ap.parse_args()
cfg = json.loads(Path(a.config).read_text())
lines = [r for r in map(json.loads, (HERE / "script.jsonl").read_text().splitlines()) if "id" in r]
p = subprocess.Popen([a.node, a.frontend], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1)
def sents(text, accent):
p.stdin.write(json.dumps({"text": text, "accent": accent}) + "\n"); p.stdin.flush()
r = json.loads(p.stdout.readline())
if "error" in r:
raise ValueError(f"{text[:60]}: {r['error']}")
return [s["ids"] for s in r["sentences"]]
for accent in sorted(set(cfg["accent"].values())):
out = dict(guard=[dict(name=n, sents=sents(t, accent)) for n, t in cfg["guard_lines"]],
bench=[dict(id=l["id"], sents=sents(l["text"], accent)) for l in lines])
(HERE / f"ids-{accent}.json").write_text(json.dumps(out))
print(accent, len(out["guard"]), "guard lines,", len(out["bench"]), "bench lines,",
sum(len(s) for l in out["bench"] for s in l["sents"]), "ids")
p.terminate()