File size: 2,008 Bytes
6338180 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """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",
}
|