Download api_server.py from Cyrax321/tui-voice-mini-ml: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/Cyrax321/tui-voice-mini-ml/resolve/main/api_server.py
- Command line
-
hf download hf://Cyrax321/tui-voice-mini-ml/api_server.py
-
curl -L -o api_server.py https://huggingface.co/Cyrax321/tui-voice-mini-ml/resolve/main/api_server.py
1.42 kB
| from fastapi import FastAPI, Response | |
| from pydantic import BaseModel | |
| import onnxruntime as ort | |
| import numpy as np | |
| import io, json | |
| import soundfile as sf | |
| import re | |
| app = FastAPI(title="Tui Voice Mini Malayalam TTS API") | |
| class SpeechRequest(BaseModel): | |
| input: str | |
| speed: float = 1.0 | |
| voice: int = 0 | |
| pitch_shift: float = 0.15 | |
| pitch_range: float = 0.85 | |
| # Load ONNX session and vocab | |
| session = ort.InferenceSession("onnx/tui-voice-mini-ml.onnx") | |
| cfg = json.load(open("config.json")) | |
| vocab = cfg.get("vocab", {}) | |
| def generate_speech(req: SpeechRequest): | |
| # Normalize & synthesize | |
| from tui_voice import phonemes as P | |
| tokens = P.tokenize_phonemes(P.text_to_phonemes(req.input)) | |
| token_ids = [vocab.get(t, 0) for t in tokens] | |
| one = lambda v: np.array([v], dtype=np.float32) | |
| audio, _ = session.run(None, { | |
| "tokens": np.array([token_ids], dtype=np.int64), | |
| "stress": np.zeros((1, len(token_ids)), dtype=np.int64), | |
| "voice": np.array([req.voice], dtype=np.int64), | |
| "speed": one(req.speed), | |
| "pitch_shift": one(req.pitch_shift), | |
| "pitch_range": one(req.pitch_range), | |
| }) | |
| out = io.BytesIO() | |
| sf.write(out, audio[0], 24000, format='WAV') | |
| return Response(content=out.getvalue(), media_type="audio/wav") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |