Ani-Voice-API / api.py
beleata74's picture
Initial release of Ani-Voice-API (created by Ani-Antigravity)
695fb87 verified
Raw
History Blame Contribute Delete
2.28 kB
from fastapi import FastAPI, HTTPException
from fastapi.responses import Response, StreamingResponse
from pydantic import BaseModel
import uvicorn
# Импортираме tts_engine - това автоматично ще зареди моделите в паметта при стартиране!
from tts_engine import engine
app = FastAPI(title="Ani Voice API", version="1.0.0")
class SynthesizeRequest(BaseModel):
text: str
voice_style: str = "F5"
speed: float = 1.6
@app.post("/api/v1/synthesize")
def synthesize_full_audio(request: SynthesizeRequest):
"""
Генерира аудио за целия текст и го връща като един WAV файл.
Подходящо за кратки съобщения.
"""
try:
audio_bytes = engine.synthesize_full(request.text, request.voice_style, request.speed)
if not audio_bytes:
raise HTTPException(status_code=400, detail="Неуспешно генериране на аудио (празен текст?).")
return Response(content=audio_bytes, media_type="audio/wav")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
import base64
import json
@app.post("/api/v1/synthesize/stream")
def synthesize_stream_audio(request: SynthesizeRequest):
"""
Стрийминг endpoint, който връща аудио на парчета (chunks).
Всеки ред е JSON обект: {"chunk_index": i, "audio_base64": "..."}
"""
def generate():
try:
for i, audio_bytes in enumerate(engine.synthesize_stream(request.text, request.voice_style, request.speed)):
encoded = base64.b64encode(audio_bytes).decode("utf-8")
yield json.dumps({"chunk_index": i, "audio_base64": encoded}) + "\n"
except Exception as e:
print(f"Грешка по време на стрийминг: {e}")
yield json.dumps({"error": str(e)}) + "\n"
return StreamingResponse(generate(), media_type="application/x-ndjson")
if __name__ == "__main__":
print("Стартиране на Ani Voice API сървър на порт 8000...")
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=False)