tiny-swara / inference.py
Xeven7's picture
Upload folder using huggingface_hub
bbac2cd verified
Raw History Blame Contribute Delete
7.82 kB
"""
tiny-swara ONNX inference.
Expected repository layout:
tiny-swara/
inference.py
config.json
inventory.json
lexicon.json
tiny_voca/
__init__.py
text.py # patched Bengali tiny-voca frontend
onnx/
tiny-swara-int8.onnx
tiny-swara.onnx # optional FP32 model
System dependency: espeak-ng
Python dependencies: numpy, onnxruntime
The frontend module must be the Bengali-patched tiny_voca.text from the
training/export environment. The ONNX graph itself accepts phoneme IDs,
not raw text.
"""
from __future__ import annotations
import argparse
import json
import os
import re
import wave
from pathlib import Path
from typing import Iterator
import numpy as np
import onnxruntime as ort
ROOT = Path(__file__).resolve().parent
# Set these BEFORE importing tiny_voca.text because it loads the inventory
# and lexicon when the module is imported.
os.environ["TINY_VOCA_INVENTORY"] = str(ROOT / "inventory.json")
os.environ["TINY_VOCA_LEXICON"] = str(ROOT / "lexicon.json")
os.environ.setdefault("TINY_VOCA_FRONTEND", "espeak")
from tiny_voca import text as frontend # noqa: E402
def load_config() -> dict:
path = ROOT / "config.json"
if not path.exists():
raise FileNotFoundError(f"Missing model config: {path}")
return json.loads(path.read_text(encoding="utf-8"))
CONFIG = load_config()
SPEAKERS = CONFIG.get("speakers", ["BF", "BM"])
SAMPLE_RATE = int(CONFIG.get("sample_rate", 24000))
def resolve_model(model: str | Path | None = None) -> Path:
if model:
path = Path(model)
if not path.is_absolute():
path = ROOT / path
if path.is_file():
return path
raise FileNotFoundError(f"Model file not found: {path}")
candidates = [
ROOT / "onnx" / "tiny-swara-int8.onnx",
ROOT / "tiny-swara-int8.onnx",
ROOT / "onnx" / "tiny-swara.onnx",
ROOT / "tiny-swara.onnx",
ROOT / "tiny-bn-int8.onnx",
ROOT / "tiny-bn.onnx",
]
for path in candidates:
if path.is_file():
return path
raise FileNotFoundError(
"Could not find an ONNX model. Put tiny-swara-int8.onnx in onnx/ "
"or pass --model PATH."
)
def split_sentences(text: str) -> list[str]:
"""Split at sentence endings while retaining punctuation."""
text = re.sub(r"\s+", " ", text).strip()
if not text:
return []
parts = re.split(r"(?<=[.!?।…])\s+", text)
result = []
for part in parts:
part = part.strip()
if not part:
continue
if not re.search(r"[.!?।…][\"'’”)\]]*$", part):
part += "."
result.append(part)
return result
class TinySwara:
def __init__(
self,
model: str | Path | None = None,
voice: str = "BF",
speed: float = 1.0,
pitch_shift: float = 0.0,
pitch_range: float = 1.0,
providers: list[str] | None = None,
):
if voice not in SPEAKERS:
raise ValueError(
f"Unknown voice {voice!r}. Available voices: {', '.join(SPEAKERS)}"
)
self.voice = voice
self.voice_id = SPEAKERS.index(voice)
self.speed = float(speed)
self.pitch_shift = float(pitch_shift)
self.pitch_range = float(pitch_range)
self.model_path = resolve_model(model)
self.session = ort.InferenceSession(
str(self.model_path),
providers=providers or ["CPUExecutionProvider"],
)
required_inputs = {
"tokens", "stress", "voice", "speed", "pitch_shift", "pitch_range"
}
actual_inputs = {item.name for item in self.session.get_inputs()}
missing = required_inputs - actual_inputs
if missing:
raise RuntimeError(
f"ONNX model is missing expected inputs: {sorted(missing)}. "
f"Found: {sorted(actual_inputs)}"
)
def synthesize_chunk(self, text: str) -> np.ndarray:
"""Generate one sentence/utterance and return mono float32 audio."""
phones = frontend.phonemize(text)
if phones.unknown:
raise ValueError(
f"Unmapped phonemes in {text!r}: {phones.unknown}. "
"Check inventory.json and lexicon.json."
)
inputs = {
"tokens": np.asarray([phones.ids], dtype=np.int64),
"stress": np.asarray([phones.stress], dtype=np.int64),
"voice": np.asarray([self.voice_id], dtype=np.int64),
"speed": np.asarray([self.speed], dtype=np.float32),
"pitch_shift": np.asarray([self.pitch_shift], dtype=np.float32),
"pitch_range": np.asarray([self.pitch_range], dtype=np.float32),
}
audio, _durations = self.session.run(["audio", "durations"], inputs)
wave = np.asarray(audio[0], dtype=np.float32).reshape(-1)
if wave.size == 0 or not np.isfinite(wave).all():
raise RuntimeError("ONNX model returned empty or non-finite audio.")
peak = float(np.max(np.abs(wave)))
if peak > 0:
wave = wave * (0.9 / peak)
return wave
def stream(self, text: str) -> Iterator[np.ndarray]:
"""
Yield one generated sentence at a time.
This is sentence-level streaming: each sentence is generated fully
before it is yielded; it is not frame-by-frame neural streaming.
"""
sentences = split_sentences(text)
if not sentences:
raise ValueError("Text is empty.")
for sentence in sentences:
yield self.synthesize_chunk(sentence)
def synthesize(self, text: str) -> np.ndarray:
"""Generate a complete utterance by joining sentence audio chunks."""
chunks = list(self.stream(text))
return np.concatenate(chunks) if len(chunks) > 1 else chunks[0]
def save(self, text: str, output_path: str | Path) -> Path:
output_path = Path(output_path)
if not output_path.is_absolute():
output_path = Path.cwd() / output_path
output_path.parent.mkdir(parents=True, exist_ok=True)
audio = self.synthesize(text)
pcm = np.clip(audio * 32767.0, -32768, 32767).astype("<i2")
with wave.open(str(output_path), "wb") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(SAMPLE_RATE)
f.writeframes(pcm.tobytes())
return output_path
def main() -> None:
parser = argparse.ArgumentParser(description="tiny-swara Bengali ONNX TTS")
parser.add_argument("--text", help="Text to synthesize")
parser.add_argument("--text-file", help="UTF-8 text file to synthesize")
parser.add_argument("--output", default="tiny-swara.wav")
parser.add_argument("--model", help="Optional ONNX model path")
parser.add_argument("--voice", default="BF", choices=SPEAKERS)
parser.add_argument("--speed", type=float, default=1.0)
parser.add_argument("--pitch-shift", type=float, default=0.0)
parser.add_argument("--pitch-range", type=float, default=1.0)
args = parser.parse_args()
if bool(args.text) == bool(args.text_file):
parser.error("Provide exactly one of --text or --text-file.")
text = (
Path(args.text_file).read_text(encoding="utf-8")
if args.text_file
else args.text
)
tts = TinySwara(
model=args.model,
voice=args.voice,
speed=args.speed,
pitch_shift=args.pitch_shift,
pitch_range=args.pitch_range,
)
saved = tts.save(text, args.output)
print(f"Saved: {saved} ({SAMPLE_RATE} Hz, voice={args.voice})")
print(f"Model: {tts.model_path}")
if __name__ == "__main__":
main()