Jiraheya commited on
Commit
cc43219
·
1 Parent(s): ee60ffc

Deploy Speech to Emotion model

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. cnn_model2.h5 +3 -0
  3. main.py +100 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["gunicorn","-b","0.0.0.0:7860", "main:app"]
cnn_model2.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8e558903193ebee197ad23f20c7be3d8698bfae03c6fdc012e4f8058558d6f7c
3
+ size 77336
main.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from werkzeug.utils import secure_filename
3
+ import pandas as pd
4
+ import numpy as np
5
+ import os
6
+ import time
7
+ import sys
8
+ from tensorflow import keras
9
+ #from keras.models import model_from_json
10
+ #from tensorflow.python.keras import optimizers
11
+
12
+ import os
13
+ import random
14
+
15
+ import librosa
16
+
17
+ emotions = {0: 'female_angry', 1 : 'female_calm', 2 : 'female_fearful', 3 : 'female_happy', 4 : 'female_sad', 5 : 'male_angry', 6: 'male_calm', 7 : 'male_fearful', 8 : 'male_happy', 9 : 'male_sad'}
18
+
19
+ #model = tf.keras.models.load_model('model.keras')
20
+ def make_predictions(inputfile):
21
+ cnn_model = keras.models.load_model("cnn_model2.h5")
22
+ prediction_data, prediction_sr = librosa.load(
23
+ inputfile,
24
+ res_type="kaiser_fast",
25
+ duration=3,
26
+ sr=22050,
27
+ offset=0.5,
28
+ )
29
+ mfccs = np.mean(librosa.feature.mfcc(y=prediction_data, sr=prediction_sr, n_mfcc=40).T, axis=0)
30
+ x = np.expand_dims(mfccs, axis=1)
31
+ x = np.expand_dims(x, axis=0)
32
+ predictions = cnn_model.predict(x)
33
+
34
+ emotions_dict = {
35
+ "0": "neutral",
36
+ "1": "calm",
37
+ "2": "happy",
38
+ "3": "sad",
39
+ "4": "angry",
40
+ "5": "fearful",
41
+ "6": "disgusted",
42
+ "7": "surprised",
43
+ }
44
+ return emotions_dict[str(predictions[0].argmax())]
45
+ # json_file = open('model.json', 'r')
46
+ # loaded_model_json = json_file.read()
47
+ # json_file.close()
48
+ # loaded_model = model_from_json(loaded_model_json)
49
+ # # load weights into new model
50
+ # loaded_model.load_weights("Emotion_Voice_Detection_Model.h5")
51
+ # print("Loaded model from disk")
52
+ # #opt = optimizers.adam(lr=0.00001)
53
+ # # evaluate loaded model on test data
54
+ # loaded_model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
55
+
56
+ # def data_preprocessing(filename):
57
+ # X, sample_rate = librosa.load(filename,duration=2.5,sr=22050*2,offset=0.5)
58
+ # sample_rate = np.array(sample_rate)
59
+ # mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=13),axis=0)
60
+ # featurelive = mfccs
61
+ # livedf2 = featurelive
62
+ # livedf2= pd.DataFrame(data=livedf2)
63
+ # livedf2 = livedf2.stack().to_frame().T
64
+ # twodim= np.expand_dims(livedf2, axis=2)
65
+ # return twodim
66
+
67
+ # def make_prediction(df):
68
+ # pred = loaded_model.predict(df,
69
+ # batch_size=32,
70
+ # verbose=1)
71
+ # preds1=pred.argmax(axis=1)
72
+ # abc = preds1.astype(int).flatten()
73
+ # return emotions[abc[0]]
74
+
75
+
76
+ app = Flask(__name__)
77
+
78
+
79
+
80
+ @app.route('/')
81
+ def index():
82
+ return jsonify({'message': 'Welcome To Speech to Emotion'})
83
+
84
+ @app.route('/api/media-file', methods=['POST'])
85
+ def predict_emotion():
86
+ audiofile = request.files['audiofile']
87
+ filename = secure_filename(audiofile.filename)
88
+ temp_path = filename
89
+ audiofile.save(temp_path)
90
+
91
+
92
+ #twodim = data_preprocessing(temp_path)
93
+
94
+
95
+ res = jsonify({'result': make_predictions(temp_path)})
96
+ os.remove(temp_path)
97
+ return res
98
+
99
+ if __name__ == '__main__':
100
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask
2
+ gunicorn
3
+ pandas
4
+ numpy
5
+ tensorflow
6
+ librosa