from fastapi import FastAPI
from fastapi.responses import Response
import edge_tts

app = FastAPI()

@app.get("/")
async def mainapp(question):
    voices = await edge_tts.VoicesManager.create()
    voices = voices.find(ShortName="zh-CN-XiaoyiNeural")
    communicate = edge_tts.Communicate(question, voices[0]["Name"])
    out = bytes()
    async for chunk in communicate.stream():
        if chunk["type"] == "audio":
            out += chunk["data"]
        elif chunk["type"] == "WordBoundary":
            pass
    return Response(out, media_type="audio/mpeg")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8088)
