From ade9a049c2b9c8b5015927bad816acebc96c6f3f Mon Sep 17 00:00:00 2001 From: j Date: Fri, 24 Jan 2025 19:43:09 +0530 Subject: [PATCH] api/tasks --- .gitignore | 2 ++ tasks.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ views.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tasks.py create mode 100644 views.py diff --git a/.gitignore b/.gitignore index 0d20b64..aeec1ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.pyc +*.swp +*.swo diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..7e8847b --- /dev/null +++ b/tasks.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +import logging + +from app.celery import app +from django.conf import settings + +from item import utils +from user.models import User +from item.models import Item + +from . import transcribe + +logger = logging.getLogger('pandora.' + __name__) + +@app.task(queue='encoding') +def transcribe(**kwargs): + user = User.objects.get(username=kwargs["user"]) + item = Item.objects.get(public_id=kwargs["item"]) + subtitles = utils.get_by_key(layers, 'isSubtitles', True) + + gpu = getattr(settings, "TRANSCRIBE_GPU", False) + join_sentences = getattr(settings, "TRANSCRIBE_JOIN", False) + model = getattr(settings, "TRANSCRIBE_MODEL", "small") + translate = getattr(settings, "TRANSCRIBE_TRANSLATE", "") + logger.error( + "transcribe %s %s %s %s %s %s %s", + item, + user, + subtitles, + translate, + gpu, + join_sentences, + model + ) + + transcribe.extract_subtitles( + item, + user, + subtitles, + translate, + gpu, + join_sentences=join_sentences, + model=model + ) diff --git a/views.py b/views.py new file mode 100644 index 0000000..68ff6e0 --- /dev/null +++ b/views.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- + +from oxdjango.decorators import login_required_json +from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response + +from item.models import Item +from oxdjango.api import actions +from user.models import has_capability + +from . import tasks + + +@login_required_json +def transcribeAudio(request, data): + ''' + Transcribe audio and add to subtitles layer + takes { + item: string // item id + } + returns { + item: id // item id + } + ''' + response = {} + item = get_object_or_404_json(Item, public_id=data["item"]) + if has_capability(request.user, 'canTranscribeAudio') and item.editalbe(request.user): + t = tasks.transcribe.delay(item=data["item"], user=request.user.username) + response["taskId"] = t.task_id + else: + response = json_response(status=403, text='permission denied') + return render_to_json_response(response) +actions.register(transcribeAudio, cache=False)