Mohamed41 commited on
Commit
06b5484
·
1 Parent(s): 22040cd

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +109 -0
main.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ #from transformers import pipeline
3
+ from huggingface_hub import from_pretrained_keras
4
+ import pandas as pd
5
+ import numpy as np
6
+ import joblib
7
+ import os
8
+ import sys
9
+ import pickle
10
+ import shutil
11
+ # librosa is a Python library for analyzing audio and music. It can be used to extract the data from the audio files we will see it later.
12
+ import librosa
13
+ import librosa.display
14
+ import seaborn as sns
15
+ import matplotlib.pyplot as plt
16
+
17
+ from sklearn.preprocessing import StandardScaler, OneHotEncoder
18
+ from sklearn.metrics import confusion_matrix, classification_report
19
+ from sklearn.model_selection import train_test_split
20
+
21
+ # to play the audio files
22
+
23
+
24
+ import keras
25
+ from keras.preprocessing import sequence
26
+ from keras.models import Sequential, model_from_json
27
+ from keras.layers import Dense, Embedding
28
+ from keras.layers import LSTM, BatchNormalization, GRU
29
+ from keras.preprocessing.text import Tokenizer
30
+
31
+ from tensorflow.keras.utils import to_categorical
32
+ from keras.layers import Input, Flatten, Dropout, Activation
33
+ from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D
34
+ from keras.models import Model
35
+ from keras.callbacks import ModelCheckpoint
36
+ from tensorflow.keras.optimizers import SGD
37
+ from fastapi import FastAPI, Request, UploadFile, File
38
+
39
+
40
+ import warnings
41
+ if not sys.warnoptions:
42
+ warnings.simplefilter("ignore")
43
+ warnings.filterwarnings("ignore", category=DeprecationWarning)
44
+
45
+ model=from_pretrained_keras( 'Mohamed41/MODEL_EMOTION_AR_TEXT_72P')
46
+
47
+
48
+ with open('scaler3.pickle', 'rb') as f:
49
+ scaler3 = pickle.load(f)
50
+
51
+ with open('encoder3.pickle', 'rb') as f:
52
+ encoder3 = pickle.load(f)
53
+
54
+ def zcr(data,frame_length,hop_length):
55
+ zcr=librosa.feature.zero_crossing_rate(data,frame_length=frame_length,hop_length=hop_length)
56
+ return np.squeeze(zcr)
57
+ def rmse(data,frame_length=2048,hop_length=512):
58
+ rmse=librosa.feature.rms(y=data,frame_length=frame_length,hop_length=hop_length)
59
+ return np.squeeze(rmse)
60
+ def mfcc(data,sr,frame_length=2048,hop_length=512,flatten:bool=True):
61
+ mfcc=librosa.feature.mfcc(y=data,sr=sr)
62
+ return np.squeeze(mfcc.T)if not flatten else np.ravel(mfcc.T)
63
+
64
+ def extract_features(data,sr=22050,frame_length=2048,hop_length=512):
65
+ result=np.array([])
66
+
67
+ result=np.hstack((result,
68
+ zcr(data,frame_length,hop_length),
69
+ rmse(data,frame_length,hop_length),
70
+ mfcc(data,sr,frame_length,hop_length)
71
+ ))
72
+ return result
73
+
74
+ def get_predict_feat(path):
75
+ d, s_rate= librosa.load(path, duration=2.5, offset=0.6)
76
+ res=extract_features(d)
77
+ result=np.array(res)
78
+ result=np.reshape(result,newshape=(1,2376))
79
+ i_result = scaler3.transform(result)
80
+ final_result=np.expand_dims(i_result, axis=2)
81
+
82
+ return final_result
83
+
84
+
85
+ emotions1 = {1: 'Neutral', 2: 'Calm', 3: 'Happy', 4: 'Sad',
86
+ 5: 'Angry', 6: 'Fear', 7: 'Disgust', 8: 'Surprise'}
87
+
88
+
89
+ def prediction(path1):
90
+ res=get_predict_feat(path1)
91
+ predictions=model.predict(res)
92
+ y_pred = encoder3.inverse_transform(predictions)
93
+ print(y_pred[0][0])
94
+
95
+
96
+ app = FastAPI()
97
+
98
+
99
+
100
+
101
+ @app.post("/")
102
+ async def read_root( file: UploadFile = File(...)):
103
+ file_extension = os.path.splitext(file.filename)[1]
104
+ with open("tmp"+file_extension, "wb") as buffer:
105
+ shutil.copyfileobj(file.file, buffer)
106
+
107
+
108
+ x = prediction("tmp"+file_extension)
109
+ return {"filename": file.filename, "filepath": f"/app/{file.filename}","prediction":x}