/api/upload: synchronously uploads audio, returns file_uuid, classifies audio, returns audio_class

This commit is contained in:
Joel Mathew Thomas
2025-02-25 22:53:49 +05:30
parent 26367e3497
commit 4f86f7a94b
2 changed files with 26 additions and 13 deletions
+12 -2
View File
@@ -1,7 +1,17 @@
from celery import shared_task from celery import shared_task
from freqsplit.input.file_reader import read_audio
from freqsplit.preprocessing.classify import classify_audio
@shared_task @shared_task
def save_uploaded_file(file_path, file_content): def save_and_classify(file_path, file_content):
"""Save uploaded file asynchronously""" """Save uploaded file asynchronously and classify the audio file"""
with open(file_path, 'wb') as destination: with open(file_path, 'wb') as destination:
destination.write(file_content) destination.write(file_content)
# Read the saved audio file
waveform, sr = read_audio(file_path, 16000, mono=True)
# Classify the audio
audio_class = classify_audio(waveform, sr)
return audio_class
+12 -9
View File
@@ -3,7 +3,7 @@ import uuid
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from .tasks import save_uploaded_file from .tasks import save_and_classify
UPLOAD_DIR = "/tmp/freqsplit" UPLOAD_DIR = "/tmp/freqsplit"
@@ -28,12 +28,15 @@ def upload_audio(request):
file_path = os.path.join(upload_dir, audio_file.name) file_path = os.path.join(upload_dir, audio_file.name)
# Save the uploaded file # Save the uploaded file
save_uploaded_file.delay(file_path, audio_file.read()) task = save_and_classify.apply(args=(file_path, audio_file.read()))
return Response( if task.successful():
{ audio_class = task.result
"Status": "File uploaded successfully", return Response(
"file_uuid": file_uuid, {
}, "Status": "File uploaded successfully",
status=status.HTTP_201_CREATED, "file_uuid": file_uuid,
) "audio_class": audio_class
},
status=status.HTTP_201_CREATED,
)