"""Persistent, single-worker Modalix inference with immediate PCM callbacks.""" from __future__ import annotations import json import logging import time import threading import tempfile from pathlib import Path from runtime import pipeline import numpy as np import torch from runtime.neat_backend import ModalixBackend from runtime.common import MODELS, LANGUAGES, SAMPLES, options, split_sentences, validate_request from runtime.text import create_g2p from runtime.audio import pcm16, wav_writer class Cancelled(Exception): pass class Kokoro: def __init__(self, *, languages=None, threads=16): if not 1 <= threads <= 16: raise ValueError('threads must be between 1 and 16') languages = list(LANGUAGES) if languages is None else list(dict.fromkeys(languages)) if not languages or any(language not in LANGUAGES for language in languages): raise ValueError('Select supported language tags from runtime.common.LANGUAGES') self.lock = threading.Lock() torch.set_num_threads(threads) try: torch.set_num_interop_threads(1) except RuntimeError: if torch.get_num_interop_threads() != 1: raise self.backend = None self.g2p = {} self.language_errors = {} self.vocab = json.loads((MODELS / "vocab.json").read_text()) self.styles = {} try: for language in languages: try: processor = create_g2p(language) processor(SAMPLES[language]) # Resolve dictionaries during startup. self.g2p[language] = processor except Exception as error: self.language_errors[language] = str(error) if not self.g2p: raise RuntimeError('No text frontend initialized: ' + str(self.language_errors)) # Initialize native text libraries before Neat loads its runtime libraries. # Loading spaCy/srsly afterward crashes with the validated target SDK. logging.getLogger("kokoro.web").info("Text frontends ready: %s", ", ".join(self.g2p)) language = next(iter(self.g2p)) voice = next(v['id'] for v in options()['voices'] if v['language'] == language) ids, style = self._inputs(split_sentences(SAMPLES[language])[0], language, voice) self.backend = ModalixBackend(MODELS, threads) pipeline.synthesize(self.backend, ids, style) except BaseException: self.close() raise def style(self, voice, index): if voice not in self.styles: self.styles[voice] = np.load(MODELS / "voices" / (voice + ".npy"), allow_pickle=False) pack = self.styles[voice] if not 0 <= index < len(pack): raise ValueError("This sentence is too long. Split it with . ? or ! and try again.") return pack[index].reshape(1, 256).astype(np.float32, copy=False) def _inputs(self, text, language, voice): ps, _ = self.g2p[language](text) ids = [self.vocab[p] for p in ps if p in self.vocab] if not ids: raise ValueError('This sentence produced no supported speech sounds.') if len(ids) + 2 > 480: raise ValueError('This sentence is too long. Add . ? or ! to split it.') return np.asarray([[0, *ids, 0]], dtype=np.int64), self.style(voice, len(ps) - 1) def generate(self, request, emit, cancel): """Generate validated sentence requests; callbacks receive PCM immediately.""" with self.lock: return self._generate(request, emit, cancel) def _generate(self, request, emit, cancel): language = request["language"] if language not in self.g2p: raise ValueError(f"{LANGUAGES[language]} is unavailable: {self.language_errors.get(language, 'G2P not initialized')}") started = time.perf_counter() reports, first = [], None for index, text in enumerate(request["sentences"]): if cancel.is_set(): raise Cancelled() emit("sentence", {"index": index, "text": text, "total": len(request["sentences"])}) g2p_started = time.perf_counter() ids, style = self._inputs(text, language, request["voice"]) g2p_seconds = time.perf_counter() - g2p_started if cancel.is_set(): raise Cancelled() self.backend.timings = {} def pcm(value): nonlocal first if cancel.is_set(): raise Cancelled() if first is None: first = time.perf_counter() - started emit("first_audio", {"seconds": first}) emit("pcm", np.asarray(value, dtype="