truegleai commited on
Commit
97f0fe1
·
verified ·
1 Parent(s): 1374545

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io, base64, tempfile, os
2
+ from fastapi import FastAPI
3
+ from fastapi.responses import StreamingResponse
4
+ from pydantic import BaseModel
5
+ import torchaudio as ta
6
+ from chatterbox.tts import ChatterboxTTS
7
+
8
+ app = FastAPI()
9
+
10
+ # Load model once at startup
11
+ print("Loading ChatterboxTTS model...")
12
+ model = ChatterboxTTS.from_pretrained(device="cpu")
13
+ print("Model ready.")
14
+
15
+
16
+ class TTSRequest(BaseModel):
17
+ text: str
18
+ ref_audio: str | None = None # base64-encoded audio file
19
+ exaggeration: float = 0.5
20
+ cfg_weight: float = 0.5
21
+ temperature: float = 0.8
22
+
23
+
24
+ @app.get("/health")
25
+ def health():
26
+ return {"status": "ok"}
27
+
28
+
29
+ @app.post("/synthesize")
30
+ def synthesize(req: TTSRequest):
31
+ ref_path = None
32
+
33
+ # Write ref audio to a temp file if provided
34
+ if req.ref_audio:
35
+ audio_bytes = base64.b64decode(req.ref_audio)
36
+ tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
37
+ tmp.write(audio_bytes)
38
+ tmp.close()
39
+ ref_path = tmp.name
40
+
41
+ try:
42
+ wav = model.generate(
43
+ req.text,
44
+ audio_prompt_path=ref_path,
45
+ exaggeration=req.exaggeration,
46
+ cfg_weight=req.cfg_weight,
47
+ temperature=req.temperature,
48
+ )
49
+ finally:
50
+ if ref_path and os.path.exists(ref_path):
51
+ os.unlink(ref_path)
52
+
53
+ # Write wav to buffer and return as audio/wav
54
+ buf = io.BytesIO()
55
+ ta.save(buf, wav, model.sr, format="wav")
56
+ buf.seek(0)
57
+
58
+ return StreamingResponse(buf, media_type="audio/wav")