Spaces:
Running
Running
Add tiny-voca TTS model support
Browse files- MODEL_ATTRIBUTION.md +8 -0
- README.md +8 -0
- app.py +117 -2
- requirements.txt +1 -0
MODEL_ATTRIBUTION.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model attribution
|
| 2 |
+
|
| 3 |
+
## tiny-voca
|
| 4 |
+
|
| 5 |
+
- Model: [n4ze3m/tiny-voca](https://huggingface.co/n4ze3m/tiny-voca)
|
| 6 |
+
- License: Apache-2.0. Downloaded model and phonemizer files retain their upstream license notices.
|
| 7 |
+
- Voices: English F2 and M2, 24 kHz mono.
|
| 8 |
+
- Phonemizer: [dizione](https://github.com/n4ze3m/dizione).
|
README.md
CHANGED
|
@@ -8,3 +8,11 @@ pinned: false
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 11 |
+
|
| 12 |
+
## Text-to-speech models
|
| 13 |
+
|
| 14 |
+
This Space defaults to tiny-voca with the M2 voice. It downloads the ONNX model and phonemizer assets from Hugging Face on first use. tiny-voca produces 24 kHz audio and supports English F2/M2 voices.
|
| 15 |
+
|
| 16 |
+
Set `KOKORO_ONNX_MODE` to `auto`, `legacy`, `stts2`, or `piper` to select an existing backend instead. For tiny-voca, `TINY_VOCA_VOICE` accepts `F2` or `M2`; `TINY_VOCA_SPEED`, `TINY_VOCA_PITCH_SHIFT`, and `TINY_VOCA_PITCH_RANGE` configure synthesis. Model source and download path can be changed with `TINY_VOCA_REPO_ID` and `TINY_VOCA_MODEL_DIR`.
|
| 17 |
+
|
| 18 |
+
The tiny-voca phonemizer requires Python 3.10 or newer and is installed from [dizione](https://github.com/n4ze3m/dizione).
|
app.py
CHANGED
|
@@ -149,7 +149,7 @@ ASR_ONNX_REPO = os.environ.get("ASR_ONNX_REPO", "onnx-community/wav2vec2-base-96
|
|
| 149 |
PUNCTUATE_TEXT = os.environ.get("PUNCTUATE_TEXT", "0").lower() in {"1", "true", "yes", "on"}
|
| 150 |
TECH_NORMALIZE = os.environ.get("TECH_NORMALIZE", "0").lower() in {"1", "true", "yes", "on"}
|
| 151 |
PUNCTUATION_MODEL = os.environ.get("PUNCTUATION_MODEL", "kredor/punctuate-all")
|
| 152 |
-
KOKORO_ONNX_MODE = os.environ.get("KOKORO_ONNX_MODE", "
|
| 153 |
KOKORO_SPEAKER_ID = int(os.environ.get("KOKORO_SPEAKER_ID", "0"))
|
| 154 |
KOKORO_MAX_PHONEME_TOKENS = int(os.environ.get("KOKORO_MAX_PHONEME_TOKENS", "510"))
|
| 155 |
KOKORO_STTS2_NOISE_SCALE = float(os.environ.get("KOKORO_STTS2_NOISE_SCALE", "0.667"))
|
|
@@ -163,6 +163,13 @@ PIPER_REPO_ID = os.environ.get("PIPER_REPO_ID", "campwill/HAL-9000-Piper-TTS")
|
|
| 163 |
PIPER_REPO_MODEL_FILE = os.environ.get("PIPER_REPO_MODEL_FILE", "hal.onnx")
|
| 164 |
PIPER_REPO_CONFIG_FILE = os.environ.get("PIPER_REPO_CONFIG_FILE", "hal.onnx.json")
|
| 165 |
PIPER_DOWNLOAD_DIR = os.environ.get("PIPER_DOWNLOAD_DIR", os.path.join(model_path, "piper_repo"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
MODEL_INIT_MODE = os.environ.get("MODEL_INIT_MODE", "lazy").lower() # lazy | startup
|
| 167 |
PIPER_PREFETCH_ON_STARTUP = os.environ.get("PIPER_PREFETCH_ON_STARTUP", "0").lower() in {"1", "true", "yes", "on"}
|
| 168 |
REQUEST_LOCK_TIMEOUT_SEC = float(os.environ.get("REQUEST_LOCK_TIMEOUT_SEC", "15"))
|
|
@@ -240,6 +247,34 @@ def initialize_models(init_tts=True, init_asr=True):
|
|
| 240 |
sess = None
|
| 241 |
kokoro_input_names = set()
|
| 242 |
kokoro_tts_mode = "piper"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
else:
|
| 244 |
kokoro_dir = model_path if os.path.exists(model_path) else None
|
| 245 |
onnx_path = find_onnx(kokoro_dir) if kokoro_dir else None
|
|
@@ -269,7 +304,7 @@ def initialize_models(init_tts=True, init_asr=True):
|
|
| 269 |
elif KOKORO_ONNX_MODE in {"legacy", "stts2"}:
|
| 270 |
kokoro_tts_mode = KOKORO_ONNX_MODE
|
| 271 |
else:
|
| 272 |
-
raise ValueError("KOKORO_ONNX_MODE must be one of: auto, legacy, stts2, piper")
|
| 273 |
|
| 274 |
logger.info(f"Kokoro ONNX input names: {sorted(kokoro_input_names) if kokoro_input_names else []}")
|
| 275 |
logger.info(f"Kokoro ONNX mode: {kokoro_tts_mode}")
|
|
@@ -520,6 +555,84 @@ def synthesize_with_piper(text, output_path):
|
|
| 520 |
if not os.path.isfile(output_path) or os.path.getsize(output_path) == 0:
|
| 521 |
raise RuntimeError("Piper did not produce an output wav file.")
|
| 522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
def restore_punctuation(text, max_words=120):
|
| 524 |
if not PUNCTUATE_TEXT:
|
| 525 |
return text
|
|
@@ -720,6 +833,8 @@ def generate_audio():
|
|
| 720 |
|
| 721 |
if kokoro_tts_mode == "piper":
|
| 722 |
synthesize_with_piper(text_for_tts, cached_file_path)
|
|
|
|
|
|
|
| 723 |
else:
|
| 724 |
# Tokenize
|
| 725 |
from kokoro import phonemize, tokenize # lazy import is fine
|
|
|
|
| 149 |
PUNCTUATE_TEXT = os.environ.get("PUNCTUATE_TEXT", "0").lower() in {"1", "true", "yes", "on"}
|
| 150 |
TECH_NORMALIZE = os.environ.get("TECH_NORMALIZE", "0").lower() in {"1", "true", "yes", "on"}
|
| 151 |
PUNCTUATION_MODEL = os.environ.get("PUNCTUATION_MODEL", "kredor/punctuate-all")
|
| 152 |
+
KOKORO_ONNX_MODE = os.environ.get("KOKORO_ONNX_MODE", "tiny-voca").lower() # auto | legacy | stts2 | piper | tiny-voca
|
| 153 |
KOKORO_SPEAKER_ID = int(os.environ.get("KOKORO_SPEAKER_ID", "0"))
|
| 154 |
KOKORO_MAX_PHONEME_TOKENS = int(os.environ.get("KOKORO_MAX_PHONEME_TOKENS", "510"))
|
| 155 |
KOKORO_STTS2_NOISE_SCALE = float(os.environ.get("KOKORO_STTS2_NOISE_SCALE", "0.667"))
|
|
|
|
| 163 |
PIPER_REPO_MODEL_FILE = os.environ.get("PIPER_REPO_MODEL_FILE", "hal.onnx")
|
| 164 |
PIPER_REPO_CONFIG_FILE = os.environ.get("PIPER_REPO_CONFIG_FILE", "hal.onnx.json")
|
| 165 |
PIPER_DOWNLOAD_DIR = os.environ.get("PIPER_DOWNLOAD_DIR", os.path.join(model_path, "piper_repo"))
|
| 166 |
+
TINY_VOCA_REPO_ID = os.environ.get("TINY_VOCA_REPO_ID", "n4ze3m/tiny-voca")
|
| 167 |
+
TINY_VOCA_MODEL_DIR = os.environ.get("TINY_VOCA_MODEL_DIR", "tiny_voca_model")
|
| 168 |
+
TINY_VOCA_VOICE = os.environ.get("TINY_VOCA_VOICE", "M2").upper()
|
| 169 |
+
TINY_VOCA_SPEED = float(os.environ.get("TINY_VOCA_SPEED", "1.0"))
|
| 170 |
+
TINY_VOCA_PITCH_SHIFT = float(os.environ.get("TINY_VOCA_PITCH_SHIFT", "0.0"))
|
| 171 |
+
TINY_VOCA_PITCH_RANGE = float(os.environ.get("TINY_VOCA_PITCH_RANGE", "1.0"))
|
| 172 |
+
TINY_VOCA_MAX_TOKENS = int(os.environ.get("TINY_VOCA_MAX_TOKENS", "512"))
|
| 173 |
MODEL_INIT_MODE = os.environ.get("MODEL_INIT_MODE", "lazy").lower() # lazy | startup
|
| 174 |
PIPER_PREFETCH_ON_STARTUP = os.environ.get("PIPER_PREFETCH_ON_STARTUP", "0").lower() in {"1", "true", "yes", "on"}
|
| 175 |
REQUEST_LOCK_TIMEOUT_SEC = float(os.environ.get("REQUEST_LOCK_TIMEOUT_SEC", "15"))
|
|
|
|
| 247 |
sess = None
|
| 248 |
kokoro_input_names = set()
|
| 249 |
kokoro_tts_mode = "piper"
|
| 250 |
+
elif KOKORO_ONNX_MODE == "tiny-voca":
|
| 251 |
+
if sys.version_info < (3, 10):
|
| 252 |
+
raise RuntimeError("tiny-voca mode requires Python 3.10 or newer")
|
| 253 |
+
if TINY_VOCA_VOICE not in {"F2", "M2"}:
|
| 254 |
+
raise ValueError("TINY_VOCA_VOICE must be F2 or M2")
|
| 255 |
+
tiny_voca_dir = snapshot_download(
|
| 256 |
+
TINY_VOCA_REPO_ID,
|
| 257 |
+
local_dir=TINY_VOCA_MODEL_DIR,
|
| 258 |
+
allow_patterns=["onnx/model.onnx", "config.json", "phonemizer/**"],
|
| 259 |
+
resume_download=True,
|
| 260 |
+
)
|
| 261 |
+
onnx_path = os.path.join(tiny_voca_dir, "onnx", "model.onnx")
|
| 262 |
+
tiny_voca_data = os.path.join(tiny_voca_dir, "phonemizer")
|
| 263 |
+
if not os.path.isfile(onnx_path):
|
| 264 |
+
raise FileNotFoundError(f"tiny-voca ONNX model not found at {onnx_path}")
|
| 265 |
+
if not os.path.isdir(tiny_voca_data):
|
| 266 |
+
raise FileNotFoundError(f"tiny-voca phonemizer data not found at {tiny_voca_data}")
|
| 267 |
+
os.environ["DIZIONE_DATA"] = tiny_voca_data
|
| 268 |
+
import dizione
|
| 269 |
+
|
| 270 |
+
sess = InferenceSession(onnx_path, sess_options)
|
| 271 |
+
kokoro_input_names = {i.name for i in sess.get_inputs()}
|
| 272 |
+
required_inputs = {"tokens", "stress", "voice", "speed", "pitch_shift", "pitch_range"}
|
| 273 |
+
if not required_inputs.issubset(kokoro_input_names):
|
| 274 |
+
raise RuntimeError(
|
| 275 |
+
f"Unexpected tiny-voca ONNX inputs: {sorted(kokoro_input_names)}"
|
| 276 |
+
)
|
| 277 |
+
kokoro_tts_mode = "tiny-voca"
|
| 278 |
else:
|
| 279 |
kokoro_dir = model_path if os.path.exists(model_path) else None
|
| 280 |
onnx_path = find_onnx(kokoro_dir) if kokoro_dir else None
|
|
|
|
| 304 |
elif KOKORO_ONNX_MODE in {"legacy", "stts2"}:
|
| 305 |
kokoro_tts_mode = KOKORO_ONNX_MODE
|
| 306 |
else:
|
| 307 |
+
raise ValueError("KOKORO_ONNX_MODE must be one of: auto, legacy, stts2, piper, tiny-voca")
|
| 308 |
|
| 309 |
logger.info(f"Kokoro ONNX input names: {sorted(kokoro_input_names) if kokoro_input_names else []}")
|
| 310 |
logger.info(f"Kokoro ONNX mode: {kokoro_tts_mode}")
|
|
|
|
| 555 |
if not os.path.isfile(output_path) or os.path.getsize(output_path) == 0:
|
| 556 |
raise RuntimeError("Piper did not produce an output wav file.")
|
| 557 |
|
| 558 |
+
|
| 559 |
+
def synthesize_with_tiny_voca(text, output_path):
|
| 560 |
+
"""Synthesize text with tiny-voca's ONNX graph and dizione phonemizer."""
|
| 561 |
+
import dizione
|
| 562 |
+
|
| 563 |
+
if (
|
| 564 |
+
TINY_VOCA_SPEED <= 0
|
| 565 |
+
or TINY_VOCA_PITCH_RANGE < 0
|
| 566 |
+
or not 3 <= TINY_VOCA_MAX_TOKENS <= 512
|
| 567 |
+
):
|
| 568 |
+
raise ValueError("Invalid tiny-voca speed, pitch range, or token limit")
|
| 569 |
+
|
| 570 |
+
sentences = [part.strip() for part in re.split(r"(?<=[.!?])\s+", text.strip()) if part.strip()]
|
| 571 |
+
if not sentences:
|
| 572 |
+
raise ValueError("No text to synthesize")
|
| 573 |
+
|
| 574 |
+
audio_parts = []
|
| 575 |
+
voice_id = 0 if TINY_VOCA_VOICE == "F2" else 1
|
| 576 |
+
|
| 577 |
+
def synthesize_chunk(chunk):
|
| 578 |
+
phones = dizione.phonemize(chunk)
|
| 579 |
+
tokens = np.asarray([phones.ids], dtype=np.int64)
|
| 580 |
+
stress = np.asarray([phones.stress], dtype=np.int64)
|
| 581 |
+
if tokens.shape[1] > TINY_VOCA_MAX_TOKENS:
|
| 582 |
+
raise ValueError("tiny-voca phoneme sequence exceeds the configured token limit")
|
| 583 |
+
outputs = sess.run(None, {
|
| 584 |
+
"tokens": tokens,
|
| 585 |
+
"stress": stress,
|
| 586 |
+
"voice": np.asarray([voice_id], dtype=np.int64),
|
| 587 |
+
"speed": np.asarray([TINY_VOCA_SPEED], dtype=np.float32),
|
| 588 |
+
"pitch_shift": np.asarray([TINY_VOCA_PITCH_SHIFT], dtype=np.float32),
|
| 589 |
+
"pitch_range": np.asarray([TINY_VOCA_PITCH_RANGE], dtype=np.float32),
|
| 590 |
+
})
|
| 591 |
+
predicted_frames = int(np.asarray(outputs[1]).sum())
|
| 592 |
+
if predicted_frames > 3000:
|
| 593 |
+
words = chunk.split()
|
| 594 |
+
if len(words) < 2:
|
| 595 |
+
raise ValueError("A tiny-voca utterance exceeds the 32-second frame limit")
|
| 596 |
+
split_at = len(words) // 2
|
| 597 |
+
first = " ".join(words[:split_at]).rstrip(".,;:") + ","
|
| 598 |
+
second = " ".join(words[split_at:])
|
| 599 |
+
synthesize_chunk(first)
|
| 600 |
+
synthesize_chunk(second)
|
| 601 |
+
return
|
| 602 |
+
audio_parts.append(np.squeeze(outputs[0]).astype(np.float32))
|
| 603 |
+
|
| 604 |
+
for sentence in sentences:
|
| 605 |
+
if sentence[-1] not in ".!?;:":
|
| 606 |
+
sentence += "."
|
| 607 |
+
|
| 608 |
+
# Keep each ONNX call under the model's token limit. Split long
|
| 609 |
+
# sentences at word boundaries and add a short pause at each split.
|
| 610 |
+
chunks = []
|
| 611 |
+
current = ""
|
| 612 |
+
for word in sentence.split():
|
| 613 |
+
candidate = f"{current} {word}".strip()
|
| 614 |
+
phones = dizione.phonemize(candidate)
|
| 615 |
+
if len(phones.ids) <= TINY_VOCA_MAX_TOKENS:
|
| 616 |
+
current = candidate
|
| 617 |
+
continue
|
| 618 |
+
if not current:
|
| 619 |
+
raise ValueError("A single word exceeds tiny-voca's token limit")
|
| 620 |
+
chunks.append(current.rstrip(".,;:") + ",")
|
| 621 |
+
current = word
|
| 622 |
+
if current:
|
| 623 |
+
chunks.append(current)
|
| 624 |
+
|
| 625 |
+
for chunk in chunks:
|
| 626 |
+
synthesize_chunk(chunk)
|
| 627 |
+
|
| 628 |
+
if not audio_parts:
|
| 629 |
+
raise RuntimeError("tiny-voca produced no audio for the supplied text")
|
| 630 |
+
audio = np.concatenate(audio_parts)
|
| 631 |
+
peak = float(np.max(np.abs(audio))) if audio.size else 0.0
|
| 632 |
+
if peak > 0.99:
|
| 633 |
+
audio *= 0.99 / peak
|
| 634 |
+
sf.write(output_path, audio, 24000)
|
| 635 |
+
|
| 636 |
def restore_punctuation(text, max_words=120):
|
| 637 |
if not PUNCTUATE_TEXT:
|
| 638 |
return text
|
|
|
|
| 833 |
|
| 834 |
if kokoro_tts_mode == "piper":
|
| 835 |
synthesize_with_piper(text_for_tts, cached_file_path)
|
| 836 |
+
elif kokoro_tts_mode == "tiny-voca":
|
| 837 |
+
synthesize_with_tiny_voca(text_for_tts, cached_file_path)
|
| 838 |
else:
|
| 839 |
# Tokenize
|
| 840 |
from kokoro import phonemize, tokenize # lazy import is fine
|
requirements.txt
CHANGED
|
@@ -15,3 +15,4 @@ ftfy
|
|
| 15 |
sentencepiece
|
| 16 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 17 |
onnxruntime
|
|
|
|
|
|
| 15 |
sentencepiece
|
| 16 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 17 |
onnxruntime
|
| 18 |
+
dizione @ git+https://github.com/n4ze3m/dizione
|