-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathpiper_server.py
More file actions
46 lines (30 loc) · 1.52 KB
/
piper_server.py
File metadata and controls
46 lines (30 loc) · 1.52 KB
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
import subprocess
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.responses import FileResponse
import os
app = FastAPI()
class SynthesisRequest(BaseModel):
text: str
@app.post("/synthesize/")
def synthesize(request: SynthesisRequest):
output_file = "output.wav"
piper_executable = "./piper/piper" #path to the piper binary
model_path = "en_US-lessac-medium.onnx" #path to the .onnx file
if not os.path.isfile(piper_executable) or not os.access(piper_executable, os.X_OK):
raise HTTPException(status_code=500, detail="Piper binary not found or not executable!")
if not os.path.exists(model_path):
raise HTTPException(status_code=500, detail="Piper model file not found!")
command = [piper_executable, "--model", model_path, "--output_file", output_file]
try:
process = subprocess.run(command, input=request.text.encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
if os.path.exists(output_file):
return FileResponse(output_file, media_type="audio/wav")
else:
raise HTTPException(status_code=500, detail="Piper did not generate an audio file.")
except subprocess.CalledProcessError as e:
error_message = e.stderr.decode() if e.stderr else "Unknown error"
raise HTTPException(status_code=500, detail=f"Speech synthesis failed: {error_message}")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5000)