/api/upload: synchronously uploads audio, returns file_uuid, classifies audio, returns audio_class
This commit is contained in:
+12
-2
@@ -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
|
||||||
+5
-2
@@ -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()))
|
||||||
|
|
||||||
|
if task.successful():
|
||||||
|
audio_class = task.result
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
"Status": "File uploaded successfully",
|
"Status": "File uploaded successfully",
|
||||||
"file_uuid": file_uuid,
|
"file_uuid": file_uuid,
|
||||||
|
"audio_class": audio_class
|
||||||
},
|
},
|
||||||
status=status.HTTP_201_CREATED,
|
status=status.HTTP_201_CREATED,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user