From 2ae6af444d6e01699a590716abce3d461b77ccf0 Mon Sep 17 00:00:00 2001 From: Jaap Blom Date: Wed, 25 Sep 2024 09:56:06 +0200 Subject: [PATCH] readded oddily deleted whisper_api.py --- whisper_api.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 whisper_api.py diff --git a/whisper_api.py b/whisper_api.py new file mode 100644 index 0000000..5b2eede --- /dev/null +++ b/whisper_api.py @@ -0,0 +1,50 @@ +import logging +from uuid import uuid4 +from fastapi import BackgroundTasks, FastAPI +from asr import run + +# from typing import Optional +from pydantic import BaseModel +from base_util import LOG_FORMAT + +logger = logging.getLogger(__name__) +api = FastAPI() + + +class Task(BaseModel): + input_uri: str + output_uri: str + id: str | None = None + + +all_tasks = [] + + +def try_whisper(task: Task): + logger.info(f"Trying to call Whisper for task {task.id}") + try: + run(task.input_uri, task.output_uri) + except Exception: + logger.exception("Failed to run whisper") + logger.info(f"Successfully ran Whisper for task {task.id}") + + +@api.post("/tasks") +async def create_task(task: Task, background_tasks: BackgroundTasks): + background_tasks.add_task(try_whisper, task) + task.id = str(uuid4()) + task_dict = task.dict() + all_tasks.append(task_dict) + return {"data": task_dict, "msg": "Successfully added task", "task_id": task.id} + + +if __name__ == "__main__": + import sys + import uvicorn + + logging.basicConfig( + stream=sys.stdout, + level="INFO", + format=LOG_FORMAT, + ) + uvicorn.run(api, port=8081, host="0.0.0.0")