Download code/tk/tts.py from Lucasgitguy-co/Turbo-Kev-Dictate-1.4B: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/tts.py
- Command line
-
hf download hf://Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/code/tk/tts.py
-
curl -L -o tts.py https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/tts.py
2.74 kB
| """Spoken dictation prompts (macOS `say`) so the audio model learns to transcribe questions/commands, never answer them. | |
| Train clips use LvW-Dictate part-D train prompts (OASST2 train, with disfluencies) and 15 voices; test clips use the | |
| held-out part-D test prompts and 4 OTHER voices. data/audio/say-{train,test}/<id>.flac + data/manifest/say-*.jsonl | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import random | |
| import subprocess | |
| import sys | |
| import tempfile | |
| from pathlib import Path | |
| import numpy as np | |
| import soundfile as sf | |
| from tk import paths | |
| from tk.wer import read_jsonl | |
| TRAIN_VOICES = ["Daniel", "Karen", "Moira", "Fred", "Ralph", "Kathy", "Albert", "Aman", "Rishi", "Eddy (Engels (VS))", | |
| "Flo (Engels (VK))", "Reed (Engels (VS))", "Rocko (Engels (VK))", "Sandy (Engels (VS))", | |
| "Shelley (Engels (VK))"] | |
| TEST_VOICES = ["Tessa", "Tara", "Samantha (Engels (VS))", "Grandpa (Engels (VK))"] | |
| def speak(text: str, voice: str, rate: int) -> np.ndarray: | |
| with tempfile.TemporaryDirectory() as d: | |
| aiff, wav = Path(d) / "a.aiff", Path(d) / "a.wav" | |
| subprocess.run(["say", "-v", voice, "-r", str(rate), "-o", str(aiff), text], check=True) | |
| subprocess.run(["ffmpeg", "-v", "quiet", "-y", "-i", str(aiff), "-ar", "16000", "-ac", "1", str(wav)], check=True) | |
| w, _ = sf.read(wav, dtype="float32") | |
| return w | |
| def make(split: str, rows: list[dict], voices: list[str], n: int, seed: int) -> None: | |
| rng = random.Random(seed) | |
| rows = rows[:] | |
| rng.shuffle(rows) | |
| adir = paths.AUDIO / f"say-{split}" | |
| adir.mkdir(parents=True, exist_ok=True) | |
| man = paths.DATA / "manifest" / f"say-{split}.jsonl" | |
| done = {r["id"] for r in read_jsonl(man)} | |
| with man.open("a") as f: | |
| for r in rows[:n]: | |
| rid = r["id"] | |
| if rid in done: | |
| continue | |
| v = rng.choice(voices) | |
| w = speak(r["input"], v, rng.randint(160, 210)) | |
| if not (0.5 <= len(w) / 16000 <= 29.5): | |
| continue | |
| sf.write(adir / f"{rid}.flac", w, 16000, subtype="PCM_16") | |
| f.write(json.dumps({"id": rid, "set": f"say-{split}", "lang": "en", "dur": round(len(w) / 16000, 2), | |
| "ref": r["input"], "target": r["target"], "voice": v}, ensure_ascii=False) + "\n") | |
| print(split, len(read_jsonl(man))) | |
| def main() -> None: | |
| pairs = paths.DATA / "lvw" / "pairs" | |
| d_train = [r for r in read_jsonl(pairs / "train.jsonl") if r["part"] == "D"] | |
| d_test = [r for r in read_jsonl(pairs / "test.jsonl") if r["part"] == "D"] | |
| make("test", d_test, TEST_VOICES, len(d_test), 7) | |
| make("train", d_train, TRAIN_VOICES, 1200, 8) | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |