File size: 1,639 Bytes
78f1df3
252d749
 
07fd3f6
252d749
 
 
 
 
 
 
 
 
 
 
 
78f1df3
 
 
 
f61cd1c
4be78c4
f61cd1c
78f1df3
 
252d749
70889e0
 
39a0fff
70889e0
7b68e58
70889e0
 
 
 
252d749
07fd3f6
0b8f470
 
 
 
 
 
252d749
7b68e58
252d749
07fd3f6
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from fastapi import APIRouter, status, Form, UploadFile, File, Query
from typing_extensions import Annotated
from .Schemas import UserDetails
from App import bot
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-v2"],
        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 * 1):
                await f.write(contents)

    except Exception:
        return {"message": "There was an error uploading the file"}
    finally:
        await file.close()

    # telegram
    data = await bot.send_file(
        -1001925049183,
        file_size=file.size,
        caption=file.filename,
        file=f"./{file.filename}",
    )
    # celery task
    task = transcription_task.delay(file.filename, model)

    return {
        "file_size": file.size,
        "file_name": file.filename,
        "task_id": task.id,
        "message_id": data.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