File size: 2,276 Bytes
695fb87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
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)