File size: 1,424 Bytes
769a950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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", {})

@app.post("/v1/audio/speech")
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)