File size: 4,799 Bytes
591d823 252d749 e666955 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 b8952b7 fec71d6 b8952b7 fec71d6 b8952b7 385a3d3 e666955 5425bdf e666955 b560dd6 252d749 78f1df3 4ac9f00 78f1df3 f61cd1c 4be78c4 f61cd1c 78f1df3 591d823 78f1df3 252d749 5600a35 70889e0 39a0fff 70889e0 7b68e58 8240ca0 70889e0 252d749 7b68e58 252d749 61061b5 ee482de efa77ee ee482de 4ac9f00 07fd3f6 4ada698 07fd3f6 252d749 b8952b7 09ded1f c1f12e1 b8952b7 d58dbfa c1f12e1 b8952b7 852de8c b8952b7 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
from fastapi import APIRouter, status, Depends, UploadFile, File, Query, BackgroundTasks
from typing_extensions import Annotated
from .Schemas import (
UserDetails,
TranscriptionMetadata,
TranscriptionResult,
BaseTranscription,
)
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)
response = {"task_id": task.id, "file_name": f"{video_title}.mp3"}
transcription_enrty = await Transcriptions.objects.create(
user=user, youtubeLink=url, **response
)
return response
@transcription_router.get("/transcriptions")
async def get_user_transcriptions(
user: UserSchema = Depends(get_token_owner),
):
transcriptions = await Transcriptions.objects.filter(user=user.id).all()
objects = [BaseTranscription(**obj.__dict__) for obj in transcriptions]
return objects
@transcription_router.post("/delete/{task_id}")
async def delete_transcription(
task_id: str,
user: UserSchema = Depends(get_token_owner),
):
transcript = await Transcriptions.objects.filter(task_id=task_id).first()
await transcript.delete()
return {"code": 200, "message": f"deleted {task_id}", "payload": None}
@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
Upload_dir = ""
try:
async with aiofiles.open(file.filename, "wb") as f:
while contents := await file.read(1024 * 1):
await f.write(contents)
except Exception as e:
return {
"message": f"There was an error uploading the file, error message {str(e)} "
}
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, file_name=file.filename
)
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)
if task_result.result == None:
return {
"task_id": task_id,
"task_status": task_result.status,
"task_result": task_result.result,
}
entry: Transcriptions = await Transcriptions.objects.filter(task_id=task_id).first()
if task_result.status == "SUCCESS":
trans = TranscriptionResult(**task_result.result)
trans
await entry.update(
content=trans.content,
**trans.dict(
exclude={"transcript"},
),
)
else:
_trans = TranscriptionMetadata(**task_result.result)
await entry.update(**_trans.dict(exclude={"transcription"}))
result = {
"task_id": task_id,
"task_status": task_result.status,
"task_result": task_result.result,
"id": entry.tl_file_id,
}
return result
|