|
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect |
|
from fastapi.responses import StreamingResponse, JSONResponse |
|
import outetts |
|
import io |
|
import json |
|
|
|
interface = outetts.Interface( |
|
config=outetts.ModelConfig.auto_config( |
|
model=outetts.Models.VERSION_1_0_SIZE_1B, |
|
|
|
|
|
|
|
|
|
backend=outetts.Backend.HF, |
|
) |
|
) |
|
|
|
|
|
speaker = interface.load_default_speaker("EN-FEMALE-1-NEUTRAL") |
|
|
|
app = FastAPI() |
|
|
|
@app.get("/") |
|
def greet_json(): |
|
return {"Hello": "World!"} |
|
|
|
@app.websocket("/ws/tts") |
|
async def websocket_tts(websocket: WebSocket): |
|
await websocket.accept() |
|
try: |
|
while True: |
|
|
|
data = await websocket.receive_text() |
|
|
|
output = interface.generate( |
|
config=outetts.GenerationConfig( |
|
text=data, |
|
generation_type=outetts.GenerationType.CHUNKED, |
|
speaker=speaker, |
|
sampler_config=outetts.SamplerConfig( |
|
temperature=0.4 |
|
), |
|
) |
|
) |
|
|
|
temp_path = "temp.wav" |
|
output.save(temp_path) |
|
with open(temp_path, "rb") as f: |
|
audio_bytes = f.read() |
|
import os |
|
os.remove(temp_path) |
|
|
|
await websocket.send_bytes(audio_bytes) |
|
except WebSocketDisconnect: |
|
pass |
|
|