Skip to content

A TTS that fits in your CPU (and pocket)

Python

Limit tests: end repeats, numbers, short texts, bursts, pacing, prompt length, cut prompts, unpunctuated text (#351)

manu-lmc95baad7ba85
diff --git a/training/eval/librispeech.py b/training/eval/librispeech.py
index ec3b804..6642ba3 100644
--- a/training/eval/librispeech.py
+++ b/training/eval/librispeech.py
@@ -29,6 +29,7 @@ from typing import Any
import huggingface_hub
import jiwer
+import numpy as np
import numpy.typing as npt
import sphn
import torch
@@ -82,6 +83,10 @@ def eval_dir_name(args: argparse.Namespace, step: int) -> str:
name += f"_n{args.num_items}"
if args.seed:
name += f"_seed{args.seed}"
+ if getattr(args, "prompt_midcut", False):
+ name += "_midcut"
+ if getattr(args, "strip_final_punct", False):
+ name += "_nodot"
if args.asr != DEFAULT_ASR:
name += "_" + re.sub(r"[^a-z0-9]+", "", args.asr.split("/")[-1].lower())[:12]
if args.prompt_root:
@@ -218,6 +223,27 @@ def load_run(
return model, mimi, step
+def cut_mid_last_word(wav: torch.Tensor, sample_rate: int) -> torch.Tensor:
+ """Cut a voice prompt in the middle of its last word, as a user trimming a recording blindly would.
+
+ The last word is the last voiced region (25 ms frames above 5% of the peak energy, gaps of up to
+ 30 ms kept inside the word, at most 0.5 s long); the prompt ends at its midpoint.
+ """
+ hop, win = int(0.01 * sample_rate), int(0.025 * sample_rate)
+ x = wav.numpy()
+ frames = np.lib.stride_tricks.sliding_window_view(x, win)[: len(x) - win : hop]
+ energy = np.sqrt((frames**2).mean(axis=1))
+ voiced = energy > max(energy.max() * 0.05, 1e-4)
+ if not voiced.any():
+ return wav
+ end = int(np.flatnonzero(voiced)[-1])
+ start = end
+ while start > 0 and voiced[max(0, start - 3) : start].any():
+ start -= 1
+ start = max(start, end - int(0.5 * sample_rate / hop))
+ return wav[: int((start + end) / 2 * hop)]
+
+
def load_mono(path: str, sample_rate: int) -> torch.Tensor:
"""Audio file as a mono float tensor at `sample_rate`."""
wav, sr = sphn.read(path)
@@ -291,6 +317,10 @@ def score_items(
def tokenize(text: str) -> list[int]:
return sp_encode(re.sub(r"[^a-z' ]", "", text.lower()).strip())
+ elif args.strip_final_punct:
+
+ def tokenize(text: str) -> list[int]:
+ return sp_encode(re.sub(r"""[.!?;:,'"\u2019\u201d\u2026\s]+$""", "", text))
else:
tokenize = sp_encode
@@ -301,6 +331,8 @@ def score_items(
wav = load_mono(path, mimi.sample_rate)
if args.voice_sec:
wav = wav[: int(args.voice_sec * mimi.sample_rate)]
+ if args.prompt_midcut:
+ wav = cut_mid_last_word(wav, mimi.sample_rate)
return wav
def decode(latents: torch.Tensor) -> torch.Tensor:
@@ -441,6 +473,18 @@ def main():
"lower it if you run out of memory, 1 falls back to the per-item path)",
)
parser.add_argument("--seed", type=int, default=0, help="sampling seed, per shard")
+ parser.add_argument(
+ "--prompt-midcut",
+ action="store_true",
+ help="cut every voice prompt in the middle of its last word (robustness to prompts "
+ "trimmed mid-word)",
+ )
+ parser.add_argument(
+ "--strip-final-punct",
+ action="store_true",
+ help="drop the sentence-final punctuation from the text the model reads (the reference "
+ "transcript is unchanged): robustness to unpunctuated input",
+ )
parser.add_argument(
"--voice-sec",
type=float,
diff --git a/training/eval/release_checks.py b/training/eval/release_checks.py
new file mode 100644
index 0000000..5a0c44e
--- /dev/null
+++ b/training/eval/release_checks.py
@@ -0,0 +1,343 @@
+"""Limit tests that LibriSpeech cannot see, run through the public `TTSModel` API.
+
+Every clip is transcribed by a CTC ASR without language model, which writes repeats down
+literally. Checks (see training/README.md for what each one caught):
+
+- End repeats (#347): sentences whose ending a model can read twice ("... an hour an hour"),
+ numbers in words and short endings, each ending with ".", with nothing (the model really
+ reads unpunctuated text: `append_terminal_punctuation` off), with "!" and with "...".
+- Stutters: an adjacent repeated word group the text does not have ("eighty eighty").
+- Digits: a digit read once too often ("288" -> "two eight eight eight").
+- Short texts (1 to 3 words): share of exact transcripts and mean duration.
+- Bursts: a click or burst in the first 50 ms of a clip, followed by silence.
+- Pacing (#323): articulation rate, pause share and mean inner pause on multi-sentence texts.
+- Prompt length: WER when the voice is cloned from the first 2.5, 5 or 10 s of its recording
+ (models with voice cloning only); it should stay flat.
+
+Usage:
+ python -m training.eval.release_checks --language english --reference english_2026-04
+ python -m training.eval.release_checks --config my.yaml --checkpoint runs/x/checkpoint.pt
+"""
+
+import argparse
+import difflib
+import re
+import statistics
+from collections import defaultdict
+from collections.abc import Callable
+from typing import Any
+
+import jiwer
+import numpy as np
+import numpy.typing as npt
+import sphn
+import torch
+from whisper_normalizer.basic import BasicTextNormalizer
+
+from pocket_tts import TTSModel
+from pocket_tts.data.audio import audio_read
+from pocket_tts.data.audio_utils import convert_audio
+from pocket_tts.utils.utils import _ORIGINS_OF_PREDEFINED_VOICES, download_if_necessary
+
+DEFAULT_ASR = "facebook/wav2vec2-large-960h-lv60-self"
+ALL_ENDINGS = (".", "", "!", "...")
+# group: (texts, sentence endings tried)
+TEXTS: dict[str, tuple[list[str], tuple[str, ...]]] = {
+ "hour": (
+ [
+ "You don't just suddenly go from making twenty dollars to making one thousand "
+ "dollars an hour.",
+ "The new job pays twenty-five dollars an hour.",
+ "We walked along the river for about an hour.",
+ "Call me back in half an hour.",
+ ],
+ ALL_ENDINGS,
+ ),
+ "numbers": (
+ [
+ "The Hermes carries two hundred and eighty-eight SCU, so let me grab the top routes "
+ "at that volume.",
+ "The final count was two hundred and eighty-eight.",
+ "The station is four hundred and twenty kilometers from here.",
+ "Our flight number tomorrow is two hundred and eighty-eight.",
+ ],
+ ALL_ENDINGS,
+ ),
+ "short endings": (
+ [
+ "Whatever you decide, in the end it is up to you.",
+ "I asked him to stay, but he said no.",
+ "I usually drink two or three cups of coffee a day.",
+ "They charge around fifteen dollars a person for the tour.",
+ ],
+ ALL_ENDINGS,
+ ),
+ "digits": (
+ [
+ "The Hermes carries 288 SCU, so let me grab the top routes at that volume.",
+ "Room 316 is on the left.",
+ "She scored 88 points last night.",
+ "Call 911 if anything happens.",
+ ],
+ (".",),
+ ),
+ "short texts": (
+ ["Hello.", "Yes.", "Thanks!", "Blueprint?", "Absolutely.", "See you soon.", "Not today."],
+ (".",),
+ ),
+}
+PACING_TEXTS = [
+ "I checked the drawings twice. Nothing matched the site. Blueprint?",
+ "She opened the door and looked outside. The street was empty. Where had everyone gone?",
+ "We leave at dawn. Pack light, bring water, and keep your phone charged. Any questions?",
+ "The meeting ran long. After two hours, nobody remembered the agenda. Can we stop now?",
+ "Hello there. It has been a while since we last spoke. How have you been?",
+ "First, preheat the oven. Then mix the flour and the eggs. Finally, bake for twenty minutes.",
+ "He paused at the top of the stairs. Something felt wrong. He turned around, slowly.",
+ "The train was late again. Nobody complained; they were used to it. Is this normal here?",
+]
+PROMPT_SECONDS = (2.5, 5.0, 10.0)
+DIGIT_WORDS = "zero one two three four five six seven eight nine".split()
+_normalize = BasicTextNormalizer()
+
+
+def _same_word(a: str, b: str) -> bool:
+ """Equal, or close for words of 4+ letters ("hour"/"hours", "eight"/"eighty")."""
+ if min(len(a), len(b)) < 4:
+ return a == b
+ return difflib.SequenceMatcher(a=a, b=b).ratio() >= 0.75
+
+
+def repeats_ending(text: str, transcript: str, window: int = 5) -> bool:
+ """One of the text's last two words occurs more often near the end of the transcript than
+ near the end of the text."""
+ ref, hyp = _normalize(text).split(), _normalize(transcript).split()
+ if len(ref) < 2 or not hyp:
+ return False
+ count = lambda w, words: sum(_same_word(w, x) for x in words) # noqa: E731
+ return any(count(w, hyp[-window:]) > count(w, ref[-window:]) for w in set(ref[-2:]))
+
+
+def _adjacent_repeats(words: list[str]) -> set[str]:
+ out = set()
+ for n in (1, 2, 3):
+ for i in range(len(words) - 2 * n + 1):
+ a, b = words[i : i + n], words[i + n : i + 2 * n]
+ if a == b:
+ out.add(" ".join(a))
+ return out
+
+
+def stutters(text: str, transcript: str) -> bool:
+ """The transcript repeats an adjacent word group ("eighty eighty") that the text does not."""
+ in_text = _adjacent_repeats(_normalize(text).split())
+ return bool(_adjacent_repeats(_normalize(transcript).split()) - in_text)
+
+
+def extra_digit(text: str, transcript: str) -> bool:
+ """A spoken digit runs longer than in any number of the text ("288" read "two eight eight
+ eight"); reading it as "two eighty-eight" or "two eight eight" is fine."""
+ longest = defaultdict(int)
+ for number in re.findall(r"\d+", text):
+ for m in re.finditer(r"(\d)\1*", number):
+ longest[m.group(1)] = max(longest[m.group(1)], len(m.group(0)))
+ words = _normalize(transcript).split()
+ run = 1
+ for i, w in enumerate(words):
+ run = run + 1 if i and w == words[i - 1] else 1
+ if w in DIGIT_WORDS and run > max(1, longest[str(DIGIT_WORDS.index(w))]):
+ return True
+ return False
+
+
+def has_burst(wav: npt.NDArray[np.float32], sample_rate: int) -> bool:
+ """Loud in the first 50 ms (peak > 0.02) and silent at 80-160 ms (peak < 0.01)."""
+ ms = sample_rate // 1000
+ return bool(
+ np.abs(wav[ms : 50 * ms]).max() > 0.02 and np.abs(wav[80 * ms : 160 * ms]).max() < 0.01
+ )
+
+
+def pacing(wav: npt.NDArray[np.float32], sample_rate: int) -> tuple[float, list[float]]:
+ """(speech span in seconds, inner pauses >= 150 ms) from 10 ms frames under -40 dB."""
+ hop = int(0.01 * sample_rate)
+ n = len(wav) // hop
+ db = 10 * np.log10((wav[: n * hop].reshape(n, hop) ** 2).mean(axis=1) + 1e-12)
+ voiced = db > db.max() - 40
+ idx = np.flatnonzero(voiced)
+ if len(idx) == 0:
+ return 0.0, []
+ pauses, run = [], 0
+ for v in voiced[idx[0] : idx[-1] + 1]:
+ if not v:
+ run += 1
+ elif run:
+ pauses.append(run)
+ run = 0
+ return (idx[-1] - idx[0]) * 0.01, [p * 0.01 for p in pauses if p >= 15]
+
+
+def ctc_transcriber(name: str, device: torch.device) -> Callable[[npt.NDArray[Any]], str]:
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
+
+ processor = Wav2Vec2Processor.from_pretrained(name)
+ model = Wav2Vec2ForCTC.from_pretrained(name).to(device).eval() # ty: ignore[invalid-argument-type]
+
+ def transcribe(wav16: npt.NDArray[Any]) -> str:
+ x = processor(wav16, sampling_rate=16000, return_tensors="pt").input_values.to(device)
+ with torch.no_grad():
+ return processor.batch_decode(model(x).logits.argmax(-1))[0].lower()
+
+ return transcribe
+
+
+def load(args: argparse.Namespace, language: str | None) -> TTSModel:
+ if language is not None:
+ model = TTSModel.load_model(language=language)
+ else:
+ model = TTSModel.load_model(config=args.config)
+ if args.checkpoint:
+ model.load_training_checkpoint(args.checkpoint)
+ return model.to(args.device)
+
+
+def with_ending(text: str, ending: str) -> str:
+ return re.sub(r"[.!?]+$", "", text) + ending if ending != "." else text
+
+
+def generate_all(model: TTSModel, args: argparse.Namespace, released: bool) -> list[dict[str, Any]]:
+ jobs = [(g, e, t) for g, (texts, ends) in TEXTS.items() for e in ends for t in texts]
+ jobs += [("pacing", ".", t) for t in PACING_TEXTS]
+ rows = []
+ for voice in args.voices:
+ # Predefined states only fit the released weights; other weights clone the voice's audio.
+ src = voice if released else _ORIGINS_OF_PREDEFINED_VOICES[voice]
+ state = model.get_state_for_audio_prompt(src)
+ for group, ending, text in jobs:
+ # Without it, the API would add the period back to unpunctuated text.
+ model.append_terminal_punctuation = ending != ""
+ for seed in range(args.seeds):
+ torch.manual_seed(seed)
+ wav = model.generate_audio(state, with_ending(text, ending))
+ wav = wav.detach().float().cpu().numpy()
+ rows.append(dict(group=group, ending=ending, text=text, voice=voice, wav=wav))
+ model.append_terminal_punctuation = True
+ return rows
+
+
+def pacing_summary(rows: list[dict[str, Any]], sample_rate: int) -> str:
+ rates, shares, pauses = [], [], []
+ for r in rows:
+ if r["group"] != "pacing":
+ continue
+ span, inner = pacing(r["wav"], sample_rate)
+ rates.append(len(r["text"].split()) / max(span - sum(inner), 1e-3))
+ shares.append(sum(inner) / max(span, 1e-3))
+ pauses += inner
+ return (
+ f"articulation {np.mean(rates):.2f} words/s, pause share {100 * np.mean(shares):.1f}%, "
+ f"mean inner pause {np.mean(pauses) if pauses else 0:.2f} s"
+ )
+
+
+def prompt_length_wer(
+ model: TTSModel, args: argparse.Namespace, transcribe: Callable[[npt.NDArray[Any]], str]
+) -> str:
+ sr = model.sample_rate
+ texts = TEXTS["short endings"][0] + TEXTS["hour"][0][1:]
+ out = []
+ for seconds in PROMPT_SECONDS:
+ refs, hyps = [], []
+ for voice in args.voices:
+ audio, rate = audio_read(download_if_necessary(_ORIGINS_OF_PREDEFINED_VOICES[voice]))
+ audio = convert_audio(audio, rate, sr, 1)[:, : int(seconds * sr)]
+ state = model.get_state_for_audio_prompt(audio)
+ for text in texts:
+ for seed in range(min(args.seeds, 2)):
+ torch.manual_seed(seed)
+ wav = model.generate_audio(state, text).detach().float().cpu().numpy()
+ refs.append(_normalize(text))
+ hyps.append(
+ _normalize(
+ transcribe(
+ sphn.resample(wav, src_sample_rate=sr, dst_sample_rate=16000)
+ )
+ )
+ )
+ out.append(f"{seconds:g} s {100 * jiwer.wer(refs, hyps):.1f}%")
+ return ", ".join(out)
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ model_arg = parser.add_mutually_exclusive_group(required=True)
+ model_arg.add_argument("--language", help="a released model, e.g. english")
+ model_arg.add_argument(
+ "--config", help="model config (YAML), with --checkpoint for training weights"
+ )
+ parser.add_argument("--checkpoint", default=None)
+ parser.add_argument("--reference", default=None, help="released model for duration ratios")
+ parser.add_argument("--voices", nargs="+", default=["alba", "marius", "cosette"])
+ parser.add_argument("--seeds", type=int, default=10)
+ parser.add_argument("--asr", default=DEFAULT_ASR)
+ parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
+ args = parser.parse_args()
+
+ transcribe = ctc_transcriber(args.asr, torch.device(args.device))
+ model = load(args, args.language)
+ sr = model.sample_rate
+ rows = generate_all(model, args, released=args.language is not None)
+ prompt_wer = prompt_length_wer(model, args, transcribe) if model.has_voice_cloning else "-"
+ del model
+ ref_dur: dict[tuple[str, str, str], float] = {}
+ ref_pacing = ""
+ if args.reference:
+ ref = generate_all(load(args, args.reference), args, released=True)
+ groups = defaultdict(list)
+ for r in ref:
+ groups[(r["ending"], r["text"], r["voice"])].append(len(r["wav"]) / sr)
+ ref_dur = {k: statistics.median(v) for k, v in groups.items()}
+ ref_pacing = pacing_summary(ref, sr)
+
+ # Which detectors apply: digits are spelled out by the ASR, so only the digit check reads them;
+ # exact transcripts only make sense for the short texts.
+ checks: dict[str, Callable[[str, str], bool]] = {
+ "end repeats": repeats_ending,
+ "stutters": stutters,
+ "extra digit": extra_digit,
+ "exact": lambda t, h: _normalize(h).split() == _normalize(t).split(),
+ }
+ applies = {"digits": {"extra digit"}, "short texts": {"exact"}}
+ table = defaultdict(list)
+ for r in rows:
+ if r["group"] == "pacing":
+ continue
+ hyp = transcribe(sphn.resample(r["wav"], src_sample_rate=sr, dst_sample_rate=16000))
+ used = applies.get(r["group"], {"end repeats", "stutters"})
+ dur = len(r["wav"]) / sr
+ ref = ref_dur.get((r["ending"], r["text"], r["voice"]))
+ table[(r["group"], r["ending"])].append(
+ {name: f(r["text"], hyp) for name, f in checks.items() if name in used}
+ | {"dur": dur, "ratio": dur / ref if ref else float("nan")}
+ )
+
+ print(
+ f"{'texts':14s} {'ending':7s} {'n':>4s} "
+ + " ".join(f"{c:>11s}" for c in checks)
+ + " dur ratio"
+ )
+ for (group, ending), v in table.items():
+ counts = " ".join(
+ f"{sum(x[c] for x in v):11d}" if c in v[0] else f"{'-':>11s}" for c in checks
+ )
+ durs = f"{np.mean([x['dur'] for x in v]):5.2f} {np.mean([x['ratio'] for x in v]):5.2f}"
+ print(f"{group:14s} {ending or 'none':7s} {len(v):4d} {counts} {durs}")
+ print(f"bursts at clip starts: {sum(has_burst(r['wav'], sr) for r in rows)}/{len(rows)}")
+ print(f"pacing: {pacing_summary(rows, sr)}")
+ if ref_pacing:
+ print(f"pacing of {args.reference}: {ref_pacing}")
+ print(f"WER by voice-prompt length: {prompt_wer}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/training/tests/test_eval_naming.py b/training/tests/test_eval_naming.py
index 7af9acb..c1aaee7 100644
--- a/training/tests/test_eval_naming.py
+++ b/training/tests/test_eval_naming.py
@@ -73,3 +73,8 @@ def test_hf_prompt_root_tags_by_repo_name_not_snapshot_path():
def test_local_prompt_root_keeps_directory_tag():
a = make_args(prompt_root="/data/libri_prompts_doraclean/")
assert eval_dir_name(a, 400000).endswith("ptsdoraclean")
+
+
+def test_robustness_knobs_mark_the_name():
+ assert eval_dir_name(make_args(prompt_midcut=True), 400000).endswith("_midcut")
+ assert eval_dir_name(make_args(strip_final_punct=True), 400000).endswith("_nodot")
diff --git a/training/tests/test_release_checks.py b/training/tests/test_release_checks.py
new file mode 100644
index 0000000..4042cd4
--- /dev/null
+++ b/training/tests/test_release_checks.py
@@ -0,0 +1,71 @@
+"""The release-check detectors flag the failures they exist for and nothing else."""
+
+import numpy as np
+
+from training.eval.release_checks import (
+ extra_digit,
+ has_burst,
+ pacing,
+ repeats_ending,
+ stutters,
+ with_ending,
+)
+
+TEXT = (
+ "You don't just suddenly go from making twenty dollars to making one thousand dollars an hour."
+)
+
+
+def test_end_repeats_are_flagged():
+ assert repeats_ending(TEXT, "... one thousand dollars an hours an hour")
+ assert repeats_ending(TEXT, "... one thousand dollars an hour an hour")
+ assert repeats_ending(
+ "The final count was eighty-eight.", "the final count was eighty eighty eighty"
+ )
+
+
+def test_clean_or_truncated_endings_are_not():
+ assert not repeats_ending(TEXT, TEXT.lower())
+ assert not repeats_ending("The final count was eighty-eight.", "the final count was eighty")
+ assert not repeats_ending("a b c d", "a b c d e")
+
+
+def test_stutters_ignore_repeats_the_text_has():
+ assert stutters(
+ "It carries two hundred and eighty-eight SCU.", "it carries two hundred eighty eighty a c u"
+ )
+ assert not stutters("It was very very cold.", "it was very very cold")
+ assert not stutters(TEXT, TEXT)
+
+
+def test_extra_digit_only_flags_a_digit_read_too_often():
+ assert extra_digit("It carries 288 SCU.", "it carries two eight eight eight c u")
+ assert not extra_digit("It carries 288 SCU.", "it carries two eight eight c u")
+ assert not extra_digit("It carries 288 SCU.", "it carries two hundred eighty eight c u")
+ assert not extra_digit("Call 911 now.", "call nine one one now")
+ assert extra_digit("Room 316.", "room three one one six")
+
+
+def test_endings():
+ assert with_ending("Call me back.", "") == "Call me back"
+ assert with_ending("Call me back.", "...") == "Call me back..."
+ assert with_ending("Call me back.", ".") == "Call me back."
+
+
+def test_burst_needs_a_loud_start_then_silence():
+ sr = 24000
+ wav = np.zeros(sr, dtype=np.float32)
+ assert not has_burst(wav, sr)
+ wav[100:600] = 0.1
+ assert has_burst(wav, sr)
+ wav[2000:4000] = 0.1 # speech right after: an onset, not a burst
+ assert not has_burst(wav, sr)
+
+
+def test_pacing_finds_inner_pauses_only():
+ sr = 24000
+ tone = np.sin(np.arange(sr // 2) * 0.1).astype(np.float32)
+ gap = np.zeros(sr // 4, dtype=np.float32)
+ span, pauses = pacing(np.concatenate([gap, tone, gap, tone, gap]), sr)
+ assert len(pauses) == 1 and abs(pauses[0] - 0.25) < 0.02
+ assert abs(span - 1.25) < 0.02