Skip to content

Open-source State-of-the-art TTS model which runs on a CPU 😻

Python

Support for CUDA

Your Name14b4a2647cd6
diff --git a/README.md b/README.md
index 29a2747..88fa577 100644
--- a/README.md
+++ b/README.md
@@ -99,6 +99,18 @@ print(model.available_voices)
# ['Bella', 'Jasper', 'Luna', 'Bruno', 'Rosie', 'Hugo', 'Kiki', 'Leo']
```
+### Using with GPU
+
+```
+pip install -r requirements_gpu.txt
+```
+
+```python
+m = KittenTTS("KittenML/kitten-tts-mini-0.8", backend="cuda")
+```
+
+Check out `example_cuda.py`
+
## API Reference
### `KittenTTS(model_name, cache_dir=None)`
diff --git a/example_cuda.py b/example_cuda.py
new file mode 100644
index 0000000..1cca8d6
--- /dev/null
+++ b/example_cuda.py
@@ -0,0 +1,27 @@
+from kittentts import KittenTTS
+
+# This example will runs on GPU.
+
+# Step 1: Load the model
+m = KittenTTS("KittenML/kitten-tts-mini-0.8", backend="cuda") # 80M version (highest quality)
+# m = KittenTTS("KittenML/kitten-tts-micro-0.8") # 40M version (balances speed and quality )
+# m = KittenTTS("KittenML/kitten-tts-nano-0.8") # 15M version (tiny and faster )
+
+
+# Step 2: Generate the audio
+
+# this is a sample from the TinyStories dataset.
+text ="""One day, a little girl named Lily found a needle in her room. She knew it was difficult to play with it because it was sharp. """
+
+
+# available_voices : ['Bella', 'Jasper', 'Luna', 'Bruno', 'Rosie', 'Hugo', 'Kiki', 'Leo']
+voice = 'Bruno'
+
+
+
+audio = m.generate(text=text, voice=voice )
+
+# Save the audio
+import soundfile as sf
+sf.write('output.wav', audio, 24000)
+print(f"Audio saved to output.wav")
\ No newline at end of file
diff --git a/kittentts/get_model.py b/kittentts/get_model.py
index e9b346d..12d2ef3 100644
--- a/kittentts/get_model.py
+++ b/kittentts/get_model.py
@@ -7,7 +7,7 @@ from .onnx_model import KittenTTS_1_Onnx
class KittenTTS:
"""Main KittenTTS class for text-to-speech synthesis."""
- def __init__(self, model_name="KittenML/kitten-tts-nano-0.8", cache_dir=None):
+ def __init__(self, model_name="KittenML/kitten-tts-nano-0.8", cache_dir=None, backend=None):
"""Initialize KittenTTS with a model from Hugging Face.
Args:
@@ -21,7 +21,7 @@ class KittenTTS:
else:
repo_id = model_name
- self.model = download_from_huggingface(repo_id=repo_id, cache_dir=cache_dir)
+ self.model = download_from_huggingface(repo_id=repo_id, cache_dir=cache_dir, backend=backend)
def generate(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False):
"""Generate audio from text.
@@ -55,7 +55,7 @@ class KittenTTS:
return self.model.all_voice_names
-def download_from_huggingface(repo_id="KittenML/kitten-tts-nano-0.1", cache_dir=None):
+def download_from_huggingface(repo_id="KittenML/kitten-tts-nano-0.1", cache_dir=None, backend=None):
"""Download model files from Hugging Face repository.
Args:
@@ -93,11 +93,11 @@ def download_from_huggingface(repo_id="KittenML/kitten-tts-nano-0.1", cache_dir=
)
# Instantiate and return model
- model = KittenTTS_1_Onnx(model_path=model_path, voices_path=voices_path, speed_priors=config.get("speed_priors", {}) , voice_aliases=config.get("voice_aliases", {}))
+ model = KittenTTS_1_Onnx(model_path=model_path, voices_path=voices_path, speed_priors=config.get("speed_priors", {}) , voice_aliases=config.get("voice_aliases", {}), backend=backend)
return model
-def get_model(repo_id="KittenML/kitten-tts-nano-0.1", cache_dir=None):
+def get_model(repo_id="KittenML/kitten-tts-nano-0.1", cache_dir=None, backend=None):
"""Get a KittenTTS model (legacy function for backward compatibility)."""
- return KittenTTS(repo_id, cache_dir)
+ return KittenTTS(repo_id, cache_dir, backend=backend)
diff --git a/kittentts/onnx_model.py b/kittentts/onnx_model.py
index 88009fc..56d2027 100644
--- a/kittentts/onnx_model.py
+++ b/kittentts/onnx_model.py
@@ -78,7 +78,7 @@ class TextCleaner:
class KittenTTS_1_Onnx:
- def __init__(self, model_path="kitten_tts_nano_preview.onnx", voices_path="voices.npz", speed_priors={}, voice_aliases={}):
+ def __init__(self, model_path="kitten_tts_nano_preview.onnx", voices_path="voices.npz", speed_priors={}, voice_aliases={}, backend=None):
"""Initialize KittenTTS with model and voice data.
Args:
@@ -87,7 +87,19 @@ class KittenTTS_1_Onnx:
"""
self.model_path = model_path
self.voices = np.load(voices_path)
- self.session = ort.InferenceSession(model_path)
+ providers = []
+ if backend == "cuda":
+ providers = ["CUDAExecutionProvider"]
+ elif backend == "amd_gpu":
+ providers = ["ROCMExecutionProvider"]
+ elif backend == "cpu":
+ providers = ["CPUExecutionProvider"]
+ elif backend is None:
+ providers = []
+ else:
+ raise ValueError("Unsupported backend")
+
+ self.session = ort.InferenceSession(model_path, providers=providers)
self.phonemizer = phonemizer.backend.EspeakBackend(
language="en-us", preserve_punctuation=True, with_stress=True
diff --git a/requirements_gpu.txt b/requirements_gpu.txt
new file mode 100644
index 0000000..5186daa
--- /dev/null
+++ b/requirements_gpu.txt
@@ -0,0 +1,8 @@
+num2words
+spacy
+espeakng_loader
+misaki[en]>=0.9.4
+onnxruntime-gpu
+soundfile
+numpy
+huggingface_hub