from contextlib import asynccontextmanager from fastapi import FastAPI, Response from fastapi.concurrency import run_in_threadpool from pydantic import BaseModel from EngineABC import ModelDescription from EnginesController import EnginesController def play_bytes(bytes_sound: bytes) -> None: import winsound winsound.PlaySound(bytes_sound, winsound.SND_MEMORY) engines_controller: EnginesController preview_cache: dict[str, bytes] = dict() class BodyText(BaseModel): text: str @asynccontextmanager async def lifespan(_app: FastAPI): # on_startup global engines_controller engines_controller = EnginesController() yield app = FastAPI(lifespan=lifespan) @app.get('/discovery') async def discovery() -> list[ModelDescription]: return await run_in_threadpool(engines_controller.discovery) @app.post('/synth/{engine}/model/{model}') async def synth(engine: str, model: str, text: BodyText): res = await run_in_threadpool( engines_controller.synth, engine, model, text.text) # preview_cache[engine + model] = res return Response(content=res, media_type='audio/wav') # @app.get('/synth/{engine}/model/{model}') # async def synth_get(engine: str, model: str): # return Response(content=preview_cache[engine+model], media_type='audio/wav') def main(): c = EnginesController() c.discovery() c.synth_all() if __name__ == '__main__': main()