seikin_alexey commited on
Commit
a87a2e7
·
1 Parent(s): d5b924f
Files changed (1) hide show
  1. app2.py +47 -0
app2.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from speechbrain.pretrained.interfaces import foreign_class
2
+ import gradio as gr
3
+ import os
4
+ import warnings
5
+ warnings.filterwarnings("ignore")
6
+
7
+ # Function to get the list of audio files in the 'rec/' directory
8
+ def get_audio_files_list(directory="rec"):
9
+ try:
10
+ return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
11
+ except FileNotFoundError:
12
+ print("The 'rec' directory does not exist. Please make sure it is the correct path.")
13
+ return []
14
+
15
+ # Loading the speechbrain emotion detection model
16
+ learner = foreign_class(
17
+ source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
18
+ pymodule_file="custom_interface.py",
19
+ classname="CustomEncoderWav2vec2Classifier"
20
+ )
21
+
22
+ # Building prediction function for Gradio
23
+ emotion_dict = {
24
+ 'sad': 'Sad',
25
+ 'hap': 'Happy',
26
+ 'ang': 'Anger',
27
+ 'fea': 'Fear',
28
+ 'sur': 'Surprised',
29
+ 'neu': 'Neutral'
30
+ }
31
+
32
+ def predict_emotion(selected_audio):
33
+ file_path = os.path.join("rec", selected_audio)
34
+ out_prob, score, index, text_lab = learner.classify_file(file_path)
35
+ return emotion_dict[text_lab[0]]
36
+
37
+ # Get the list of audio files for the dropdown
38
+ audio_files_list = get_audio_files_list()
39
+
40
+ # Loading Gradio interface
41
+ inputs = gr.Dropdown(label="Select Audio", choices=audio_files_list)
42
+ outputs = "text"
43
+ title = "ML Speech Emotion Detection"
44
+ description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
45
+
46
+ interface = gr.Interface(fn=predict_emotion, inputs=inputs, outputs=outputs, title=title, description=description)
47
+ interface.launch()