File size: 1,384 Bytes
78f1df3
252d749
 
 
 
 
 
 
 
 
 
 
 
 
 
78f1df3
 
 
 
f61cd1c
 
 
78f1df3
 
252d749
70889e0
 
 
 
 
 
 
 
252d749
 
78f1df3
252d749
 
 
 
 
 
 
 
 
 
 
 
 
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
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