Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
p = pipeline("automatic-speech-recognition")
|
2 |
+
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
|
5 |
+
model = load_model('/content/drive/MyDrive/Mis ML Modelos/mymodel_SER_LSTM_RAVDESS.h5')
|
6 |
+
|
7 |
+
def extract_mfcc(wav_file_name):
|
8 |
+
#This function extracts mfcc features and obtain the mean of each dimension
|
9 |
+
y, sr = librosa.load(wav_file_name)
|
10 |
+
mfccs = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40).T,axis=0)
|
11 |
+
return mfccs
|
12 |
+
|
13 |
+
emotions = {1: 'neutral', 2: 'calm', 3: 'happy', 4: 'sad', 5: 'angry', 6: 'fearful', 7: 'disgust', 8: 'surprised'}
|
14 |
+
|
15 |
+
def predict_emotion_from_audio(wav_filepath):
|
16 |
+
test_point = extract_mfcc(wav_filepath)
|
17 |
+
test_point = np.reshape(test_point, newshape=(1, 40, 1))
|
18 |
+
predictions = model.predict(test_point)
|
19 |
+
predicted_emotion_label = np.argmax(predictions[0]) + 1
|
20 |
+
return emotions[predicted_emotion_label]
|
21 |
+
|
22 |
+
# Semantics
|
23 |
+
def sentiment_vader(sentence):
|
24 |
+
|
25 |
+
# Create a SentimentIntensityAnalyzer object.
|
26 |
+
sid_obj = SentimentIntensityAnalyzer()
|
27 |
+
|
28 |
+
sentiment_dict = sid_obj.polarity_scores(sentence)
|
29 |
+
negative = sentiment_dict['neg']
|
30 |
+
neutral = sentiment_dict['neu']
|
31 |
+
positive = sentiment_dict['pos']
|
32 |
+
compound = sentiment_dict['compound']
|
33 |
+
|
34 |
+
if sentiment_dict['compound'] >= 0.05 :
|
35 |
+
overall_sentiment = "Positive"
|
36 |
+
|
37 |
+
elif sentiment_dict['compound'] <= - 0.05 :
|
38 |
+
overall_sentiment = "Negative"
|
39 |
+
|
40 |
+
else :
|
41 |
+
overall_sentiment = "Neutral"
|
42 |
+
|
43 |
+
return overall_sentiment
|
44 |
+
|
45 |
+
def transcribe(audio, state=""):
|
46 |
+
time.sleep(3)
|
47 |
+
text = p(audio)["text"]
|
48 |
+
text = sentiment_vader(text)
|
49 |
+
return text
|