Open-source State-of-the-art TTS model which runs on a CPU 😻
Python
Simplify normalization API
Dewan Shakil7eeae3003f3d
diff --git a/README.md b/README.mdindex 0cdc270..6d8bb73 100644--- a/README.md+++ b/README.md@@ -132,8 +132,6 @@ Synthesize speech from text, returning a NumPy array of audio samples at 24 kHz.| `voice` | `str` | `"expr-voice-5-m"` | Voice name (see available voices) || `speed` | `float` | `1.0` | Speech speed multiplier || `clean_text` | `bool` | `False` | Preprocess text (expand numbers, currencies, etc.) |-| `normalize` | `bool` | `None` | Use read-aloud text normalization. When unset, follows `clean_text` for backward compatibility |-| `locale` | `str` | `"en-US"` | Normalization locale. Currently supports English |### `model.generate_to_file(text, output_path, voice, speed, sample_rate, clean_text)`@@ -148,9 +146,9 @@ Synthesize speech and write directly to an audio file.| `sample_rate` | `int` | `24000` | Audio sample rate in Hz || `clean_text` | `bool` | `True` | Preprocess text (expand numbers, currencies, etc.) |-### `normalize_text(text, locale="en-US", domain="general-read-aloud", return_spans=False)`+### `normalize_text(text, locale="en-US", return_spans=False)`-Normalize text for read-aloud use without generating audio.+Normalize text for TTS without generating audio.```pythonfrom kittentts import normalize_textdiff --git a/kittentts/get_model.py b/kittentts/get_model.pyindex 73c3a37..d8d2225 100644--- a/kittentts/get_model.py+++ b/kittentts/get_model.py@@ -24,12 +24,11 @@ class KittenTTS:self.model = download_from_huggingface(repo_id=repo_id, cache_dir=cache_dir, backend=backend)- def normalize_text(self, text, locale="en-US", domain="general-read-aloud", return_spans=False):- """Normalize text for read-aloud synthesis without generating audio."""- return normalize_text(text, locale=locale, domain=domain, return_spans=return_spans)+ def normalize_text(self, text, locale="en-US", return_spans=False):+ """Normalize text for TTS without generating audio."""+ return normalize_text(text, locale=locale, return_spans=return_spans)- def generate(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False,- normalize=None, locale="en-US", domain="general-read-aloud"):+ def generate(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False):"""Generate audio from text.Args:@@ -41,35 +40,17 @@ class KittenTTS:Audio data as numpy array"""print(f"Generating audio for text: {text}")- return self.model.generate(- text,- voice=voice,- speed=speed,- clean_text=clean_text,- normalize=normalize,- locale=locale,- domain=domain,- )+ return self.model.generate(text, voice=voice, speed=speed, clean_text=clean_text)- def generate_stream(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False,- normalize=None, locale="en-US", domain="general-read-aloud"):+ def generate_stream(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False):"""Generate audio as a stream of chunks.Yields:numpy.ndarray: Audio data for each text chunk."""- yield from self.model.generate_stream(- text,- voice=voice,- speed=speed,- clean_text=clean_text,- normalize=normalize,- locale=locale,- domain=domain,- )+ yield from self.model.generate_stream(text, voice=voice, speed=speed, clean_text=clean_text)- def generate_to_file(self, text, output_path, voice="expr-voice-5-m", speed=1.0, sample_rate=24000,- clean_text=True, normalize=None, locale="en-US", domain="general-read-aloud"):+ def generate_to_file(self, text, output_path, voice="expr-voice-5-m", speed=1.0, sample_rate=24000):"""Generate audio from text and save to file.Args:@@ -79,17 +60,7 @@ class KittenTTS:speed: Speech speed (1.0 = normal)sample_rate: Audio sample rate"""- return self.model.generate_to_file(- text,- output_path,- voice=voice,- speed=speed,- sample_rate=sample_rate,- clean_text=clean_text,- normalize=normalize,- locale=locale,- domain=domain,- )+ return self.model.generate_to_file(text, output_path, voice=voice, speed=speed, sample_rate=sample_rate)@propertydef available_voices(self):diff --git a/kittentts/onnx_model.py b/kittentts/onnx_model.pyindex 09138b8..b368e4f 100644--- a/kittentts/onnx_model.py+++ b/kittentts/onnx_model.py@@ -114,29 +114,25 @@ class KittenTTS_1_Onnx:"speed": np.array([speed], dtype=np.float32),}- def normalize_text(self, text: str, locale: str = "en-US", domain: str = "general-read-aloud", return_spans: bool = False):- return normalize_text(text, locale=locale, domain=domain, return_spans=return_spans)+ def normalize_text(self, text: str, locale: str = "en-US", return_spans: bool = False):+ return normalize_text(text, locale=locale, return_spans=return_spans)- def generate(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0, clean_text: bool=True,- normalize: bool = None, locale: str = "en-US", domain: str = "general-read-aloud") -> np.ndarray:+ def generate(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0, clean_text: bool=True) -> np.ndarray:out_chunks = []- should_normalize = clean_text if normalize is None else normalize- if should_normalize:- text = normalize_text(text, locale=locale, domain=domain)+ if clean_text:+ text = self.preprocessor(text)for text_chunk in chunk_text(text):out_chunks.append(self.generate_single_chunk(text_chunk, voice, speed))return np.concatenate(out_chunks, axis=-1)- def generate_stream(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0, clean_text: bool = True,- normalize: bool = None, locale: str = "en-US", domain: str = "general-read-aloud"):+ def generate_stream(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0, clean_text: bool = True):"""Generate audio chunk-by-chunk as a generator.Yields:numpy.ndarray: Audio data for each text chunk."""- should_normalize = clean_text if normalize is None else normalize- if should_normalize:- text = normalize_text(text, locale=locale, domain=domain)+ if clean_text:+ text = self.preprocessor(text)for text_chunk in chunk_text(text):yield self.generate_single_chunk(text_chunk, voice, speed)@@ -161,8 +157,7 @@ class KittenTTS_1_Onnx:return audiodef generate_to_file(self, text: str, output_path: str, voice: str = "expr-voice-5-m",- speed: float = 1.0, sample_rate: int = 24000, clean_text: bool=True,- normalize: bool = None, locale: str = "en-US", domain: str = "general-read-aloud") -> None:+ speed: float = 1.0, sample_rate: int = 24000, clean_text: bool=True) -> None:"""Synthesize speech and save to file.Args:@@ -173,6 +168,6 @@ class KittenTTS_1_Onnx:sample_rate: Audio sample rateclean_text: If true, it will cleanup the text. Eg. replace numbers with words."""- audio = self.generate(text, voice, speed, clean_text=clean_text, normalize=normalize, locale=locale, domain=domain)+ audio = self.generate(text, voice, speed, clean_text=clean_text)sf.write(output_path, audio, sample_rate)print(f"Audio saved to {output_path}")diff --git a/kittentts/preprocess.py b/kittentts/preprocess.pyindex 56700dc..b31ed00 100644--- a/kittentts/preprocess.py+++ b/kittentts/preprocess.py@@ -650,7 +650,7 @@ class NormalizedSpan:@dataclassclass NormalizedTextResult:- """Normalized read-aloud text plus optional original-to-normalized spans."""+ """Normalized text plus optional original-to-normalized spans."""text: strspans: List[NormalizedSpan]@@ -916,31 +916,26 @@ def _sub_with_spans(def normalize_text(text: str,locale: str = "en-US",- domain: str = "general-read-aloud",return_spans: bool = False,) -> Union[str, NormalizedTextResult]:- """Normalize English text for read-aloud TTS use cases.+ """Normalize English text for TTS use cases.Args:text: Input text to normalize.locale: Currently only "en-US" is supported.- domain: Currently only "general-read-aloud" is supported.return_spans: When true, return NormalizedTextResult instead of text."""- result = normalize_text_result(text, locale=locale, domain=domain)+ result = normalize_text_result(text, locale=locale)return result if return_spans else result.textdef normalize_text_result(text: str,locale: str = "en-US",- domain: str = "general-read-aloud",) -> NormalizedTextResult:"""Normalize English text and return span metadata for changed segments."""if locale.lower() not in {"en-us", "en"}:raise ValueError("Only en-US text normalization is currently supported")- if domain != "general-read-aloud":- raise ValueError("Only the general-read-aloud normalization domain is currently supported")text = normalize_unicode(text)origins = list(range(len(text)))diff --git a/tests/test_text_normalization.py b/tests/test_text_normalization.pyindex 9b1487b..6a9eb92 100644--- a/tests/test_text_normalization.py+++ b/tests/test_text_normalization.py@@ -5,7 +5,7 @@ from kittentts.preprocess import chunk_textclass TextNormalizationTests(unittest.TestCase):- def test_issue_examples_normalize_for_read_aloud(self):+ def test_issue_examples_normalize_for_tts(self):cases = {"Smith et al. 2024, pp. 31-35": "Smith et al twenty twenty-four pages thirty-one to thirty-five","Fig. 2": "Figure two",