Spaces:
Runtime error
Runtime error
Commit
·
9524ef8
1
Parent(s):
49e0159
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow import keras
|
2 |
+
import os
|
3 |
+
import soundfile as sf
|
4 |
+
import numpy as np
|
5 |
+
import librosa
|
6 |
+
import gradio as gr
|
7 |
+
import seaborn as sns
|
8 |
+
import pandas as pd
|
9 |
+
import plotly.express as px
|
10 |
+
model = keras.models.load_model('/kaggle/input/emotions/emotion (1).h5')
|
11 |
+
labels = ['Angry', 'Disgusted', 'Fearful', 'Happy', 'Neutral', 'Sad', 'Suprised']
|
12 |
+
# def load()
|
13 |
+
|
14 |
+
def predict(audio):
|
15 |
+
# audio_dir = ('/kaggle/input/sample-audios/')
|
16 |
+
# audio = os.path.join(audio_dir,audio_link)
|
17 |
+
wave, sr = librosa.load(audio, sr=None)
|
18 |
+
segment_dur_secs = 3
|
19 |
+
segment_length = sr * segment_dur_secs
|
20 |
+
num_sections = int(np.ceil(len(wave) / segment_length))
|
21 |
+
split = []
|
22 |
+
paths =[]
|
23 |
+
for i in range(num_sections):
|
24 |
+
t = wave[i * segment_length: (i + 1) * segment_length]
|
25 |
+
split.append(t)
|
26 |
+
|
27 |
+
out_dir = ('audio_data/splits/')
|
28 |
+
os.makedirs(out_dir, exist_ok=True)
|
29 |
+
for i in range(num_sections):
|
30 |
+
recording_name = os.path.basename(audio[:-4])
|
31 |
+
out_file = f"{recording_name}_{str(i)}.wav"
|
32 |
+
sf.write(os.path.join(out_dir, out_file), split[i], sr)
|
33 |
+
paths.append(os.path.join(out_dir, out_file))
|
34 |
+
|
35 |
+
|
36 |
+
predicted_features = pd.DataFrame(columns=['features'])
|
37 |
+
counter=0
|
38 |
+
for path in paths:
|
39 |
+
X, sample_rate = librosa.load(path
|
40 |
+
,duration=2.5
|
41 |
+
,sr=44100
|
42 |
+
,offset=0.5
|
43 |
+
)
|
44 |
+
sample_rate = np.array(sample_rate)
|
45 |
+
|
46 |
+
# mean as the feature. Could do min and max etc as well.
|
47 |
+
mfccs = np.mean(librosa.feature.mfcc(y=X,
|
48 |
+
sr=sample_rate,
|
49 |
+
n_mfcc=13),
|
50 |
+
axis=0)
|
51 |
+
predicted_features.loc[counter] = [mfccs]
|
52 |
+
counter=counter+1
|
53 |
+
predicted_features = pd.DataFrame(predicted_features['features'].values.tolist())
|
54 |
+
predicted_features.dropna(inplace=True)
|
55 |
+
preds = model.predict(predicted_features)
|
56 |
+
|
57 |
+
preds=preds.argmax(axis=1)
|
58 |
+
df_preds = pd.DataFrame(preds,columns = ['prediction'])
|
59 |
+
emotions = []
|
60 |
+
for i in df_preds['prediction']:
|
61 |
+
emotion = labels[int(i)]
|
62 |
+
emotions.append(emotion)
|
63 |
+
df_preds['emotion'] = emotions
|
64 |
+
df_preds = df_preds.reset_index()
|
65 |
+
import plotly.io as pio
|
66 |
+
fig = px.line(df_preds, x="index", y="emotion", title='Life expectancy in Canada')
|
67 |
+
# plt = sns.lineplot(df_preds,x='index',y='emotion');
|
68 |
+
# plt.set_xlabel('samples(each in 3s interval)');
|
69 |
+
return fig
|
70 |
+
|
71 |
+
# outputs = gr.Plot()
|
72 |
+
title = "Emotion recognition"
|
73 |
+
description = "This model can shows how speaker emotion changes over the speech"
|
74 |
+
|
75 |
+
infr = gr.Interface(fn=predict,
|
76 |
+
inputs=gr.Audio(type="filepath",),
|
77 |
+
outputs=outputs,
|
78 |
+
title=title,description=description,interpretation='default',)
|
79 |
+
infr.launch()
|