Update main.py
Browse files
main.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import tensorflow as tf
|
2 |
-
|
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 |
-
|
|
|
10 |
# 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.
|
11 |
import librosa
|
12 |
import librosa.display
|
@@ -41,34 +42,42 @@ if not sys.warnoptions:
|
|
41 |
warnings.simplefilter("ignore")
|
42 |
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
43 |
|
44 |
-
model
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
return result
|
59 |
|
60 |
-
|
61 |
-
scaler = joblib.load('scaler.joblib')
|
62 |
-
encoder = joblib.load('encoder.joblib')
|
63 |
-
|
64 |
-
|
65 |
def get_predict_feat(path):
|
66 |
-
d, s_rate
|
67 |
-
res
|
68 |
-
result
|
69 |
-
result
|
70 |
-
i_result =
|
71 |
-
final_result
|
72 |
|
73 |
return final_result
|
74 |
|
@@ -78,17 +87,23 @@ emotions1 = {1: 'Neutral', 2: 'Calm', 3: 'Happy', 4: 'Sad',
|
|
78 |
|
79 |
|
80 |
def prediction(path1):
|
81 |
-
res
|
82 |
-
predictions
|
83 |
-
y_pred =
|
84 |
-
|
85 |
|
86 |
|
87 |
app = FastAPI()
|
88 |
|
89 |
|
90 |
-
@app.post("/")
|
91 |
-
async def read_root(request: Request, file: UploadFile = File(...)):
|
92 |
-
json_data = await request.json()
|
93 |
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
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 |
|
|
|
87 |
|
88 |
|
89 |
def prediction(path1):
|
90 |
+
res=get_predict_feat(path1)
|
91 |
+
predictions=loaded_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}
|