breeze-tts-2-handler / handler.py
copperscout1's picture
suite Breeze TTS 2 custom handler
6338180 verified
Raw History Blame Contribute Delete
2.01 kB
"""Hugging Face Inference Endpoints custom handler for Breeze TTS 2.
POST JSON:
{"inputs": "Hello from Breeze.", "parameters": {"instruction": "A warm narrator."}}
{"input": "...", "instructions": "...", "voice": "..."} # OpenAI-ish aliases
Returns WAV bytes as base64 plus sampling_rate so the toolkit and suite
wake path can both consume it.
"""
from __future__ import annotations
from typing import Any
from generate import sample_rate, synthesize
class EndpointHandler:
def __init__(self, path: str = "") -> None:
if path:
import os
os.environ.setdefault("BREEZE_MODEL_PATH", path)
# Load at init so HF health is not a cold first request.
sample_rate()
def __call__(self, data: dict[str, Any]) -> dict[str, Any]:
payload = dict(data or {})
parameters = payload.pop("parameters", None) or {}
if not isinstance(parameters, dict):
parameters = {}
text = (
payload.pop("inputs", None)
or payload.pop("input", None)
or payload.pop("text", None)
or parameters.get("text")
or ""
)
instruction = (
payload.pop("instructions", None)
or payload.pop("instruction", None)
or payload.pop("voice", None)
or parameters.get("instruction")
or parameters.get("instructions")
or parameters.get("voice")
)
cfg = payload.pop("cfg_scale", parameters.get("cfg_scale"))
seed = payload.pop("seed", parameters.get("seed", 42))
wav = synthesize(
str(text),
instruction=str(instruction) if instruction else None,
cfg_scale=float(cfg) if cfg is not None else None,
seed=int(seed or 42),
)
import base64
return {
"audio": base64.b64encode(wav).decode("ascii"),
"sampling_rate": sample_rate(),
"content_type": "audio/wav",
}