up10
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
|
|
|
| 2 |
import outetts
|
| 3 |
import io
|
| 4 |
-
|
| 5 |
# Initialize the interface
|
| 6 |
interface = outetts.Interface(
|
| 7 |
config=outetts.ModelConfig.auto_config(
|
|
@@ -23,29 +24,37 @@ app = FastAPI()
|
|
| 23 |
def greet_json():
|
| 24 |
return {"Hello": "World!"}
|
| 25 |
|
| 26 |
-
@app.
|
| 27 |
-
async def
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
import outetts
|
| 4 |
import io
|
| 5 |
+
import json
|
| 6 |
# Initialize the interface
|
| 7 |
interface = outetts.Interface(
|
| 8 |
config=outetts.ModelConfig.auto_config(
|
|
|
|
| 24 |
def greet_json():
|
| 25 |
return {"Hello": "World!"}
|
| 26 |
|
| 27 |
+
@app.post("/tts")
|
| 28 |
+
async def tts_endpoint(request: Request):
|
| 29 |
+
"""
|
| 30 |
+
Accepts JSON {"text": "..."} and streams the generated audio as WAV.
|
| 31 |
+
"""
|
| 32 |
try:
|
| 33 |
+
data = await request.json()
|
| 34 |
+
text = data.get("text")
|
| 35 |
+
if not text:
|
| 36 |
+
return JSONResponse({"error": "Missing 'text' in request"}, status_code=400)
|
| 37 |
+
|
| 38 |
+
# Generate audio from text
|
| 39 |
+
output = interface.generate(
|
| 40 |
+
config=outetts.GenerationConfig(
|
| 41 |
+
text=text,
|
| 42 |
+
generation_type=outetts.GenerationType.CHUNKED,
|
| 43 |
+
speaker=speaker,
|
| 44 |
+
sampler_config=outetts.SamplerConfig(
|
| 45 |
+
temperature=0.4
|
| 46 |
+
),
|
| 47 |
)
|
| 48 |
+
)
|
| 49 |
+
audio_buffer = io.BytesIO()
|
| 50 |
+
output.save(audio_buffer)
|
| 51 |
+
audio_buffer.seek(0)
|
| 52 |
+
|
| 53 |
+
def audio_stream():
|
| 54 |
+
yield audio_buffer.read()
|
| 55 |
+
|
| 56 |
+
return StreamingResponse(audio_stream(), media_type="audio/wav")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 59 |
+
|
| 60 |
+
# WebSocket endpoint removed; use POST /tts for TTS requests.
|