Commit c78c6955 authored by Ridma Dilshan's avatar Ridma Dilshan

Fix the audio emotion detection Backend

parent d504a0fd
......@@ -37,3 +37,109 @@
2023-08-03 06:00:41,626 - INFO - Error.
2023-08-03 06:00:41,626 - INFO - Error.
2023-08-03 06:00:41,626 - INFO - Error.
2023-09-02 15:24:16,102 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:16,102 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:16,102 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:16,553 - INFO - Error.
2023-09-02 15:24:16,553 - INFO - Error.
2023-09-02 15:24:16,553 - INFO - Error.
2023-09-02 15:24:53,767 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:53,767 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:53,767 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/E6.mp4'
2023-09-02 15:24:53,768 - INFO - Error.
2023-09-02 15:24:53,768 - INFO - Error.
2023-09-02 15:24:53,768 - INFO - Error.
2023-09-02 15:25:37,381 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/Y2Mate.is - React Native Tutorial for Beginners - Build a React Native App-0-S5a0eXPoc-360p-1657268484983.mp4'
2023-09-02 15:25:37,381 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/Y2Mate.is - React Native Tutorial for Beginners - Build a React Native App-0-S5a0eXPoc-360p-1657268484983.mp4'
2023-09-02 15:25:37,381 - INFO - Failed to make predictions. [Errno 2] No such file or directory: 'files/Y2Mate.is - React Native Tutorial for Beginners - Build a React Native App-0-S5a0eXPoc-360p-1657268484983.mp4'
2023-09-02 15:25:37,384 - INFO - Error.
2023-09-02 15:25:37,384 - INFO - Error.
2023-09-02 15:25:37,384 - INFO - Error.
2023-09-02 18:55:18,412 - INFO - Failed to upload file. [Errno 2] No such file or directory: 'files/videoplayback.mp4'
2023-09-02 18:55:18,412 - INFO - Failed to upload file. [Errno 2] No such file or directory: 'files/videoplayback.mp4'
2023-09-02 18:55:18,412 - INFO - Failed to upload file. [Errno 2] No such file or directory: 'files/videoplayback.mp4'
2023-09-03 03:00:21,346 - INFO - Error. name 'audio_request' is not defined
2023-09-03 03:00:21,346 - INFO - Error. name 'audio_request' is not defined
2023-09-03 03:00:21,346 - INFO - Error. name 'audio_request' is not defined
2023-09-03 03:00:21,346 - INFO - Error. name 'audio_request' is not defined
2023-09-03 03:00:21,346 - INFO - Error. name 'audio_request' is not defined
2023-09-03 04:43:22,666 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:43:22,666 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:43:22,666 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:43:22,666 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:43:22,666 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:43:22,669 - INFO - Error.
2023-09-03 04:43:22,669 - INFO - Error.
2023-09-03 04:43:22,669 - INFO - Error.
2023-09-03 04:43:22,669 - INFO - Error.
2023-09-03 04:43:22,669 - INFO - Error.
2023-09-03 04:49:57,058 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:49:57,058 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:49:57,058 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:49:57,058 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:49:57,058 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:49:57,059 - INFO - Error.
2023-09-03 04:49:57,059 - INFO - Error.
2023-09-03 04:49:57,059 - INFO - Error.
2023-09-03 04:49:57,059 - INFO - Error.
2023-09-03 04:49:57,059 - INFO - Error.
2023-09-03 04:55:20,430 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:55:20,430 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:55:20,430 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:55:20,430 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:55:20,430 - INFO - Failed to make predictions. type object 'dict' has no attribute 'predict'
2023-09-03 04:55:20,740 - INFO - Error.
2023-09-03 04:55:20,740 - INFO - Error.
2023-09-03 04:55:20,740 - INFO - Error.
2023-09-03 04:55:20,740 - INFO - Error.
2023-09-03 04:55:20,740 - INFO - Error.
2023-09-03 05:06:41,091 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:41,091 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:41,091 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:41,091 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:41,091 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:41,093 - INFO - Error.
2023-09-03 05:06:41,093 - INFO - Error.
2023-09-03 05:06:41,093 - INFO - Error.
2023-09-03 05:06:41,093 - INFO - Error.
2023-09-03 05:06:41,093 - INFO - Error.
2023-09-03 05:06:43,311 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:43,311 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:43,311 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:43,311 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:43,311 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:43,313 - INFO - Error.
2023-09-03 05:06:43,313 - INFO - Error.
2023-09-03 05:06:43,313 - INFO - Error.
2023-09-03 05:06:43,313 - INFO - Error.
2023-09-03 05:06:43,313 - INFO - Error.
2023-09-03 05:06:53,621 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:53,621 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:53,621 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:53,621 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:53,621 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:06:53,623 - INFO - Error.
2023-09-03 05:06:53,623 - INFO - Error.
2023-09-03 05:06:53,623 - INFO - Error.
2023-09-03 05:06:53,623 - INFO - Error.
2023-09-03 05:06:53,623 - INFO - Error.
2023-09-03 05:07:04,712 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:07:04,712 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:07:04,712 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:07:04,712 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:07:04,712 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:07:04,714 - INFO - Error.
2023-09-03 05:07:04,714 - INFO - Error.
2023-09-03 05:07:04,714 - INFO - Error.
2023-09-03 05:07:04,714 - INFO - Error.
2023-09-03 05:07:04,714 - INFO - Error.
2023-09-03 05:19:00,190 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:19:00,190 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:19:00,190 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:19:00,190 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:19:00,190 - ERROR - Failed to make predictions. 'SpooledTemporaryFile' object has no attribute 'file'
2023-09-03 05:19:00,196 - INFO - Error.
2023-09-03 05:19:00,196 - INFO - Error.
2023-09-03 05:19:00,196 - INFO - Error.
2023-09-03 05:19:00,196 - INFO - Error.
2023-09-03 05:19:00,196 - INFO - Error.
from fastapi import APIRouter,FastAPI, UploadFile ,File,HTTPException
from fastapi.responses import FileResponse
import os
from core.logger import setup_logger
from services.audio_detect_service import EmotionPredictionService
import tensorflow as tf
app = FastAPI()
router = APIRouter()
audio: UploadFile
logger = setup_logger()
model = tf.keras.models.load_model('../ML_Models/Emotion_Detection_Model/mymodel.h5')
prediction_service = EmotionPredictionService(model)
@router.post("/upload/audio", tags=["Emotion Detection"])
async def upload_audio(audio: UploadFile = File(...)):
try:
file_location = f"files/{audio.filename}"
with open(file_location, "wb") as file:
file.write(audio.file.read())
return {"text": "OK"}
except Exception as e:
logger.info(f"Failed to upload file. {e}")
raise HTTPException(
status_code=500,
detail="Failed to upload the audio"
)
@router.post('/predict_emotion/audio', tags=["Emotion Detection"])
def predict_using_video(audio_request: UploadFile = File(...)):
try:
return prediction_service.predict_emotion_detection_audio_new(audio_request)
except Exception as e:
logger.info(f"Error. {e}")
raise HTTPException(
status_code=500,
detail="Request Failed."
)
from fastapi import FastAPI
from controllers import translate_controler, users_controller
# from Project.Backend.Server_Python.controllers import audio_detect_controler
from controllers import translate_controler, users_controller, audio_detect_controler
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
......@@ -13,6 +14,7 @@ logger = setup_logger()
app.include_router(users_controller.router)
app.include_router(translate_controler.router)
app.include_router(audio_detect_controler.router)
# Add cores middleware
......
from fastapi.types import ModelNameMap
from sklearn import model_selection
import tensorflow as tf
import numpy as np
import librosa
from fastapi import HTTPException, UploadFile
from typing import Dict
import os
from core.logger import setup_logger
logger = setup_logger()
class EmotionPredictionService:
def __init__(self, model):
self.model = model
def predict_emotion_detection_audio(self,audio_request: UploadFile) -> Dict[str, str]:
try:
# Create a temporary file to save the audio
audio_location = f"files/{audio_request.filename}"
with open(audio_location, "wb") as file:
file.write(audio_request.file.read())
# Load the audio data from the saved file
y, sr = librosa.load(audio_location)
mfccs = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40).T, axis=0)
test_point = np.reshape(mfccs, newshape=(1, 40, 1))
predictions = self.model.predict(test_point)
emotions = {
1: 'neutral', 2: 'calm', 3: 'happy', 4: 'sad',
5: 'angry', 6: 'fearful', 7: 'disgust', 8: 'surprised'
}
predicted_emotion = emotions[np.argmax(predictions[0]) + 1]
return {"predicted_emotion": predicted_emotion}
except Exception as e:
logger.error(f"Failed to make predictions. {str(e)}")
raise HTTPException(
status_code=500,
detail=f"Failed to make predictions. Error: {str(e)}"
)
def predict_emotion_detection_audio_new(self, audio_request: UploadFile) -> Dict[str, str]:
try:
# Create a temporary file to save the audio
audio_location = f"files/{audio_request.filename}"
with open(audio_location, "wb") as file:
file.write(audio_request.file.read())
# Load the audio data from the saved file
y, sr = librosa.load(audio_location)
mfccs = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40).T, axis=0)
test_point = np.reshape(mfccs, newshape=(1, 40, 1))
predictions = self.model.predict(test_point)
emotions = {
1: 'neutral', 2: 'calm', 3: 'happy', 4: 'sad',
5: 'angry', 6: 'fearful', 7: 'disgust', 8: 'surprised'
}
predicted_emotion = emotions[np.argmax(predictions[0]) + 1]
return {"predicted_emotion": predicted_emotion}
except Exception as e:
logger.error(f"Failed to make predictions. {str(e)}")
raise HTTPException(
status_code=500,
detail=f"Failed to make predictions. Error: {str(e)}"
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment