Percy3822 commited on
Commit
3a99d34
·
verified ·
1 Parent(s): c090f46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io, math, wave, uuid
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from fastapi.responses import FileResponse
5
+
6
+ app = FastAPI(title="TTS Space (stub)")
7
+
8
+ class SpeakIn(BaseModel):
9
+ text: str
10
+
11
+ def _beep_wav(duration_s=0.4, sr=16000, freq=440):
12
+ n = int(duration_s*sr)
13
+ buf = bytearray()
14
+ for i in range(n):
15
+ val = int(32767*0.2*math.sin(2*math.pi*freq*i/sr))
16
+ buf += val.to_bytes(2, byteorder="little", signed=True)
17
+ path = f"/tmp/{uuid.uuid4().hex}.wav"
18
+ with wave.open(path, "wb") as w:
19
+ w.setnchannels(1)
20
+ w.setsampwidth(2)
21
+ w.setframerate(sr)
22
+ w.writeframes(buf)
23
+ return path
24
+
25
+ @app.post("/speak")
26
+ def speak(inp: SpeakIn):
27
+ # Stub: ignore text, return a short beep so Brain can play audio.
28
+ path = _beep_wav()
29
+ return {"audio_path": path}
30
+
31
+ @app.get("/file/{name}")
32
+ def file(name: str):
33
+ return FileResponse(f"/tmp/{name}")