Mohamed41 commited on
Commit
4cff6f8
·
1 Parent(s): 07e7b1e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +50 -35
main.py CHANGED
@@ -1,12 +1,13 @@
1
  import tensorflow as tf
2
- import gradio as gr
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 = from_pretrained_keras('Mohamed41/MODEL_EMOTION_AR_TEXT_72P')
45
-
46
-
47
- def feat_ext(data):
48
- # Time_domain_features
49
- # ZCR Persody features or Low level ascoustic features
50
- result = np.array([])
51
- zcr = np.mean(librosa.feature.zero_crossing_rate(y=data).T, axis=0)
52
- result = np.hstack((result, zcr)) # stacking horizontally
53
- # Frequency_domain_features
54
- # Spectral and wavelet Features
55
- # MFCC
56
- mfcc = np.mean(librosa.feature.mfcc(y=data, sr=22050, n_mfcc=40).T, axis=0)
57
- result = np.hstack((result, mfcc)) # stacking horizontally
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = librosa.load(path, duration=2.5, offset=0.6)
67
- res = feat_ext(d)
68
- result = np.array(res)
69
- result = np.reshape(result, newshape=(1, 41))
70
- i_result = scaler.transform(result)
71
- final_result = np.expand_dims(i_result, axis=2)
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 = get_predict_feat(path1)
82
- predictions = model.predict(res)
83
- y_pred = encoder.inverse_transform(predictions)
84
- return y_pred[0][0]
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
- return {"filename": file.filename, "filepath": f"/app/{file.filename}"}
 
 
 
 
 
 
 
 
 
 
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}