workspace commited on
Commit
1acd416
β€’
1 Parent(s): 67d72d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import wave
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from extract_feature import *
6
+ import pickle
7
+
8
+ classifier = pickle.load(open('finalized_rf.sav', 'rb'))
9
+
10
+ def emotion_predict(input):
11
+ input_features = extract_feature(input, mfcc=True, chroma=True, mel=True, contrast=True, tonnetz=True)
12
+ rf_prediction = classifier.predict(input_features.reshape(1,-1))
13
+ if rf_prediction == 'happy':
14
+ return 'Happy 😎'
15
+ elif rf_prediction == 'neutral':
16
+ return 'Neutral 😐'
17
+ elif rf_prediction == 'sad':
18
+ return 'Sad 😒'
19
+ else:
20
+ return 'Angry 😀'
21
+
22
+
23
+ def plot_fig(input):
24
+ wav = wave.open(input, 'r')
25
+
26
+ raw = wav.readframes(-1)
27
+ raw = np.frombuffer(raw, "int16")
28
+ sampleRate = wav.getframerate()
29
+
30
+ Time = np.linspace(0, len(raw)/sampleRate, num=len(raw))
31
+
32
+ fig = plt.figure()
33
+
34
+ plt.rcParams["figure.figsize"] = (100,30)
35
+
36
+ plt.title("Waveform Of the Audio", fontsize=100)
37
+
38
+ plt.xticks(fontsize=50)
39
+
40
+ plt.yticks(fontsize=50)
41
+
42
+ plt.ylabel("Amplitude", fontsize=100)
43
+
44
+ plt.plot(Time, raw, color='red')
45
+
46
+ return fig
47
+
48
+
49
+ with gr.Blocks() as app:
50
+ gr.Markdown(
51
+ """
52
+ # Speech Emotion Detector 🎡😍
53
+ This application classifies inputted audio πŸ”Š according to the verbal emotion into four categories:
54
+ 1. Happy 😎
55
+ 1. Neutral 😐
56
+ 1. Sad 😒
57
+ 1. Angry 😀
58
+ """
59
+ )
60
+ with gr.Tab("Record Audio"):
61
+ record_input = gr.Audio(source="microphone", type="filepath")
62
+
63
+ with gr.Accordion("Audio Visualization"):
64
+ plot_record = gr.Button("Display Audio Signal")
65
+ plot_record_c = gr.Plot(label='Waveform Of the Audio')
66
+
67
+ record_button = gr.Button("Detect Emotion")
68
+ record_output = gr.Text(label = 'Emotion Detected')
69
+
70
+ with gr.Tab("Upload Audio File"):
71
+ gr.Markdown(
72
+ """
73
+ ## Uploaded Audio should be of .wav format
74
+ """
75
+ )
76
+
77
+ upload_input = gr.Audio(type="filepath")
78
+
79
+ with gr.Accordion("Audio Visualization"):
80
+ plot_upload = gr.Button("Display Audio Signal")
81
+ plot_upload_c = gr.Plot(label='Waveform Of the Audio')
82
+
83
+ upload_button = gr.Button("Detect Emotion")
84
+ upload_output = gr.Text(label = 'Emotion Detected')
85
+
86
+ record_button.click(emotion_predict, inputs=record_input, outputs=record_output)
87
+ upload_button.click(emotion_predict, inputs=upload_input, outputs=upload_output)
88
+ plot_record.click(plot_fig, inputs=record_input, outputs=plot_record_c)
89
+ plot_upload.click(plot_fig, inputs=upload_input, outputs=plot_upload_c)
90
+
91
+ app.launch()