Spaces:
Runtime error
Runtime error
immelstorun
commited on
Commit
·
73bfdef
1
Parent(s):
e7fc258
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,25 @@
|
|
1 |
-
from speechbrain.pretrained.interfaces import foreign_class
|
2 |
import gradio as gr
|
|
|
3 |
import os
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Loading the speechbrain emotion detection model
|
9 |
learner = foreign_class(
|
10 |
source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
|
11 |
-
pymodule_file="custom_interface.py",
|
12 |
classname="CustomEncoderWav2vec2Classifier"
|
13 |
)
|
14 |
|
15 |
-
# Building prediction function for
|
16 |
emotion_dict = {
|
17 |
-
'sad': 'Sad',
|
18 |
'hap': 'Happy',
|
19 |
'ang': 'Anger',
|
20 |
'fea': 'Fear',
|
@@ -22,24 +27,29 @@ emotion_dict = {
|
|
22 |
'neu': 'Neutral'
|
23 |
}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
gr.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from speechbrain.pretrained.interfaces import foreign_class
|
3 |
import os
|
4 |
|
5 |
+
# Function to get the list of audio files in the 'rec/' directory
|
6 |
+
def get_audio_files_list(directory="rec"):
|
7 |
+
try:
|
8 |
+
return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
9 |
+
except FileNotFoundError:
|
10 |
+
print("The 'rec' directory does not exist. Please make sure it is the correct path.")
|
11 |
+
return []
|
12 |
|
13 |
# Loading the speechbrain emotion detection model
|
14 |
learner = foreign_class(
|
15 |
source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
|
16 |
+
pymodule_file="custom_interface.py",
|
17 |
classname="CustomEncoderWav2vec2Classifier"
|
18 |
)
|
19 |
|
20 |
+
# Building prediction function for Gradio
|
21 |
emotion_dict = {
|
22 |
+
'sad': 'Sad',
|
23 |
'hap': 'Happy',
|
24 |
'ang': 'Anger',
|
25 |
'fea': 'Fear',
|
|
|
27 |
'neu': 'Neutral'
|
28 |
}
|
29 |
|
30 |
+
def selected_audio(audio_file):
|
31 |
+
if audio_file is None:
|
32 |
+
return None, "Please select an audio file."
|
33 |
+
file_path = os.path.join("rec", audio_file)
|
34 |
+
audio_data = gr.Audio(file=file_path)
|
35 |
+
out_prob, score, index, text_lab = learner.classify_file(file_path)
|
36 |
+
emotion = emotion_dict[text_lab[0]]
|
37 |
+
return audio_data, emotion
|
38 |
+
|
39 |
+
# Get the list of audio files for the dropdown
|
40 |
+
audio_files_list = get_audio_files_list()
|
41 |
+
|
42 |
+
# Define Gradio blocks
|
43 |
+
with gr.Blocks() as blocks:
|
44 |
+
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>" +
|
45 |
+
"Audio Emotion Detection" +
|
46 |
+
"</h1>")
|
47 |
+
with gr.Column():
|
48 |
+
input_audio_dropdown = gr.Dropdown(label="Select Audio", choices=audio_files_list)
|
49 |
+
audio_ui = gr.Audio()
|
50 |
+
output_text = gr.Textbox(label="Detected Emotion!")
|
51 |
+
detect_btn = gr.Button("Detect Emotion")
|
52 |
+
detect_btn.click(selected_audio, inputs=input_audio_dropdown, outputs=[audio_ui, output_text])
|
53 |
+
|
54 |
+
# Launch the Gradio blocks interface
|
55 |
+
blocks.launch()
|