File size: 3,563 Bytes
591d823 252d749 07fd3f6 af5c58a 385a3d3 252d749 385a3d3 61061b5 591d823 61061b5 4ac9f00 dd2695b 591d823 252d749 62b5ac4 252d749 385a3d3 fec71d6 c51d144 591d823 fec71d6 af5c58a fbe8b8e dd2695b af5c58a 6878fd1 af5c58a 385a3d3 af5c58a 385a3d3 af5c58a c51d144 fec71d6 591d823 385a3d3 252d749 78f1df3 4ac9f00 78f1df3 f61cd1c 4be78c4 f61cd1c 78f1df3 591d823 78f1df3 252d749 70889e0 39a0fff 70889e0 7b68e58 70889e0 252d749 7b68e58 252d749 61061b5 ee482de 4ac9f00 07fd3f6 4ada698 07fd3f6 252d749 c1f12e1 252d749 4ac9f00 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
from fastapi import APIRouter, status, Depends, UploadFile, File, Query, BackgroundTasks
from typing_extensions import Annotated
from .Schemas import UserDetails
from App import bot
import aiofiles, os, re
import tempfile
from celery.result import AsyncResult
from App.Worker import transcription_task, downloadfile
from App.Users.Model import User
from App.Users.UserRoutes import get_token_owner
from App.Users.Schemas import UserSchema
from .Model import Transcriptions
from .Utils.fastapi_tasks import perform_background_task
import yt_dlp
from fastapi_jwt_auth import AuthJWT
# from .Model import User
# from sqlalchemy import and_
transcription_router = APIRouter(tags=["Transcription"])
@transcription_router.get("/download-audio")
async def download_audio(
url: str,
model: str = Query(
"tiny",
enum=["tiny", "small", "medium", "base", "large-v2"],
description="Whisper model Sizes",
),
user: UserSchema = Depends(get_token_owner),
):
if user == None:
return {"code": 400, "message": "doesn't exist", "payload": None}
ydl_opts_info = {
"quiet": True,
}
with yt_dlp.YoutubeDL(ydl_opts_info) as ydl:
info_dict = ydl.extract_info(url, download=False)
video_title = info_dict.get("title", None)
sanitized_title = re.sub(
r"(?u)[^-\w.]", "", video_title
) # Ensure the title is file-friendly
filename = f"{sanitized_title}.mp3"
file_path = os.path.join("/srv/App/", "Downloads", filename)
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": file_path,
}
task = downloadfile.delay(url, ydl_opts, model)
transcription_enrty = await Transcriptions.objects.create(
task_id=task.id, user=user
)
return {"task_id": task.id, "file_name": filename}
@transcription_router.post("/uploadfile/")
async def create_file(
background_tasks: BackgroundTasks,
file: UploadFile,
model: str = Query(
"tiny",
enum=["tiny", "small", "medium", "base", "large-v2"],
description="Whisper model Sizes",
),
user: UserSchema = Depends(get_token_owner),
):
# 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()
# celery task
task = transcription_task.delay(file.filename, model)
# create a transcription entry
transcription_enrty = await Transcriptions.objects.create(
task_id=task.id, user=user
)
background_tasks.add_task(perform_background_task, file=file, task_id=task.id)
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)
entry: Transcriptions = await Transcriptions.objects.filter(task_id=task_id).first()
if task_result.status == "SUCCESS":
await entry.update(
content=task_result.result, transcription_state=task_result.status
)
else:
await entry.update(transcription_state=task_result.status)
result = {
"task_id": task_id,
"task_status": task_result.status,
"task_result": task_result.result,
"id": entry.tl_file_id,
}
return result
|