from fastapi import APIRouter, status, Form, UploadFile, File, Query from typing_extensions import Annotated from .Schemas import UserDetails import aiofiles from celery.result import AsyncResult from App.Worker import transcription_task # from .Model import User # from sqlalchemy import and_ transcription_router = APIRouter(tags=["User"]) @transcription_router.post("/uploadfile/") async def create_file( file: UploadFile, userId: int = 1, model: str = Query( "tiny", enum=["tiny", "small", "medium", "base", "large"], description="Whisper model Sizes", ), ): # Write the file to disk asynchronously try: async with aiofiles.open(file.filename, "wb") as f: while contents := await file.read(1024 * 1024): await f.write(contents) except Exception: return {"message": "There was an error uploading the file"} finally: await file.close() # celery task task = transcription_task.delay(file.filename, model) return {"file_size": file.size, "file_name": file.filename, "task_id": task.id} @transcription_router.get("/tasks/{task_id}") async def get_status(task_id): task_result = AsyncResult(task_id) result = { "task_id": task_id, "task_status": task_result.status, "task_result": task_result.result, } return result