A TTS that fits in your CPU (and pocket)
Python
French: rewrite characters the models never saw in training (quotes, curly apostrophes) (#325)
manu-lm497f399c09fd
diff --git a/pocket_tts/config/french.yaml b/pocket_tts/config/french.yamlindex e88f2e6..4ec2e0f 100644--- a/pocket_tts/config/french.yaml+++ b/pocket_tts/config/french.yaml@@ -2,6 +2,21 @@ weights_path: hf://kyutai/pocket-tts/languages/french/model.safetensors@d0ebc5deweights_path_without_voice_cloning: hf://kyutai/pocket-tts-without-voice-cloning/languages/french/model.safetensors@8843db76457a91db32077edf8dfcd1c0e3e755fdremove_semicolons: true+# Replace characters that are absent from the training data.+replace_characters:+ '"': ''+ '“': ''+ '”': ''+ '„': ''+ '«': ''+ '»': ''+ '’': "'"+ '‘': "'"+ ':': ','+ '(': ''+ ')': ''+ '[': ''+ ']': ''flow_lm:insert_bos_before_voice: truediff --git a/pocket_tts/config/french_24l.yaml b/pocket_tts/config/french_24l.yamlindex d8564ed..f5b858f 100644--- a/pocket_tts/config/french_24l.yaml+++ b/pocket_tts/config/french_24l.yaml@@ -2,6 +2,21 @@ weights_path: hf://kyutai/pocket-tts/languages/french_24l/model.safetensors@d0ebweights_path_without_voice_cloning: hf://kyutai/pocket-tts-without-voice-cloning/languages/french_24l/model.safetensors@8843db76457a91db32077edf8dfcd1c0e3e755fdremove_semicolons: true+# Replace characters that are absent from the training data.+replace_characters:+ '"': ''+ '“': ''+ '”': ''+ '„': ''+ '«': ''+ '»': ''+ '’': "'"+ '‘': "'"+ ':': ','+ '(': ''+ ')': ''+ '[': ''+ ']': ''flow_lm:insert_bos_before_voice: truediff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.pyindex 07fd001..cf1b903 100644--- a/pocket_tts/models/text_chunking.py+++ b/pocket_tts/models/text_chunking.py@@ -5,6 +5,7 @@ boundaries and regrouped into chunks that fit `max_tokens`."""import logging+import refrom pocket_tts.modules.text_conditioner import Tokenizer@@ -17,8 +18,14 @@ def prepare_text_prompt(remove_semicolons: bool,append_terminal_punctuation: bool = True,capitalize_first_letter: bool = True,+ replace_characters: dict[str, str] | None = None,) -> tuple[str, int]:text = text.strip()+ if replace_characters:+ text = " ".join(text.translate(str.maketrans(replace_characters)).split())+ # Deleted quotes leave '"Hi?", she said' as 'Hi?, she said', which reads as a sentence end+ # followed by a stray comma; keep the sentence mark only.+ text = re.sub(r"([.!?\u2026])\s*[,;:]", r"\1", text)if text == "":raise ValueError("Text prompt cannot be empty")text = text.replace("\n", " ").replace("\r", " ").replace(" ", " ")@@ -141,6 +148,7 @@ def split_into_best_sentences(remove_semicolons: bool,append_terminal_punctuation: bool = True,capitalize_first_letter: bool = True,+ replace_characters: dict[str, str] | None = None,) -> list[str]:text_to_generate, _ = prepare_text_prompt(text_to_generate,@@ -148,6 +156,7 @@ def split_into_best_sentences(remove_semicolons,append_terminal_punctuation,capitalize_first_letter,+ replace_characters,)text_to_generate = text_to_generate.strip()tokens = tokenizer(text_to_generate)diff --git a/pocket_tts/models/tts_model.py b/pocket_tts/models/tts_model.pyindex d8df48e..706e259 100644--- a/pocket_tts/models/tts_model.py+++ b/pocket_tts/models/tts_model.py@@ -102,6 +102,7 @@ class TTSModel(nn.Module):remove_semicolons: bool = False,append_terminal_punctuation: bool = True,capitalize_first_letter: bool = True,+ replace_characters: dict[str, str] | None = None,):super().__init__()self.flow_lm = flow_lm@@ -119,6 +120,7 @@ class TTSModel(nn.Module):self.remove_semicolons = remove_semicolonsself.append_terminal_punctuation = append_terminal_punctuationself.capitalize_first_letter = capitalize_first_letter+ self.replace_characters = replace_characters or {}@propertydef device(self) -> torch.device:@@ -156,6 +158,7 @@ class TTSModel(nn.Module):remove_semicolons=config.remove_semicolons,append_terminal_punctuation=config.append_terminal_punctuation,capitalize_first_letter=config.capitalize_first_letter,+ replace_characters=config.replace_characters,)return tts_model@@ -708,6 +711,7 @@ class TTSModel(nn.Module):remove_semicolons=self.remove_semicolons,append_terminal_punctuation=self.append_terminal_punctuation,capitalize_first_letter=self.capitalize_first_letter,+ replace_characters=self.replace_characters,)for chunk in chunks:@@ -719,6 +723,7 @@ class TTSModel(nn.Module):self.remove_semicolons,self.append_terminal_punctuation,self.capitalize_first_letter,+ self.replace_characters,)frames_after_eos_guess += 2effective_frames = (diff --git a/pocket_tts/utils/config.py b/pocket_tts/utils/config.pyindex c7f94c4..d539147 100644--- a/pocket_tts/utils/config.py+++ b/pocket_tts/utils/config.py@@ -128,6 +128,10 @@ class Config(StrictModel):# a capital *is* a phoneme it silently changes the sound ("salAm" -> "SalAm"# is /salaam/ -> /shalaam/).capitalize_first_letter: bool = True+ # Per-character rewrites applied before tokenization ("" deletes). For characters the model's+ # training text never contained (straight quotes, curly apostrophes in CML-TTS/MLS): their+ # embeddings are untrained and the model speaks filler syllables where they occur.+ replace_characters: dict[str, str] = {}model_recommended_frames_after_eos: int | None = None# 0.3 beats 0.7 on WER and UTMOS for every shipped model (human evals agreed for English, #223);# a config sets its own value only if it was tuned elsewhere.diff --git a/tests/test_generation_regressions.py b/tests/test_generation_regressions.pyindex 2238709..287d12c 100644--- a/tests/test_generation_regressions.py+++ b/tests/test_generation_regressions.py@@ -23,6 +23,7 @@ def test_generate_audio_stream_uses_prepared_chunk_text(monkeypatch: pytest.Monkremove_semicolons: bool,append_terminal_punctuation: bool,capitalize_first_letter: bool,+ replace_characters: dict[str, str],) -> list[str]:assert text_to_generate == "hi"assert pad_with_spaces_for_short_inputs is True@@ -46,6 +47,7 @@ def test_generate_audio_stream_uses_prepared_chunk_text(monkeypatch: pytest.Monkremove_semicolons=False,append_terminal_punctuation=True,capitalize_first_letter=True,+ replace_characters={},_generate_audio_stream_short_text=fake_generate_audio_stream_short_text,),)diff --git a/tests/test_split_sentences.py b/tests/test_split_sentences.pyindex dd5f2a2..7ee5286 100644--- a/tests/test_split_sentences.py+++ b/tests/test_split_sentences.py@@ -244,14 +244,37 @@ def test_capitalization_can_be_switched_off():Both were observed in a Persian model whose alphabet uses "S" for sh: "salAm"was capitalized to "SalAm" and came out as /shalaam/."""- kwargs = dict(pad_with_spaces_for_short_inputs=False, remove_semicolons=False)-- on, _ = prepare_text_prompt("salAm hAle SomA", **kwargs)+ on, _ = prepare_text_prompt("salAm hAle SomA", False, False)assert on.startswith("SalAm")- off, _ = prepare_text_prompt("salAm hAle SomA", capitalize_first_letter=False, **kwargs)+ off, _ = prepare_text_prompt("salAm hAle SomA", False, False, capitalize_first_letter=False)assert off.startswith("salAm")# The default is unchanged, so nothing moves for the shipped languages.- default, _ = prepare_text_prompt("hello there", **kwargs)+ default, _ = prepare_text_prompt("hello there", False, False)assert default == "Hello there."+++def test_replace_characters_rewrites_unseen_characters_before_capitalizing():+ """Characters a model never saw in training are rewritten, and the text is re-tidied."""+ drop = {c: "" for c in '"¡¿«»'}++ got, _ = prepare_text_prompt(+ '"¡Venid a mí, hombres!" Alzó la voz.', False, False, replace_characters=drop+ )+ assert got == "Venid a mí, hombres! Alzó la voz."++ got, _ = prepare_text_prompt(+ "il a dit « l’homme »", False, False, replace_characters={**drop, "’": "'"}+ )+ assert got == "Il a dit l'homme."++ got, _ = prepare_text_prompt('"Vieni stasera?", chiese.', False, False, replace_characters=drop)+ assert got == "Vieni stasera? chiese."++ with pytest.raises(ValueError):+ prepare_text_prompt('" "', False, False, replace_characters=drop)++ # Empty by default: shipped configs that don't set it are unchanged.+ got, _ = prepare_text_prompt('"Yes," she said.', False, False)+ assert got == '"Yes," she said.'