seikin_alexey commited on
Commit
595bf80
·
1 Parent(s): ec70b4e
Files changed (2) hide show
  1. app2.py +1 -2
  2. app3.py +55 -0
app2.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  import os
4
  import warnings
5
  warnings.filterwarnings("ignore")
6
- import IPython.display as ipd
7
 
8
  # Function to get the list of audio files in the 'rec/' directory
9
  def get_audio_files_list(directory="rec"):
@@ -32,7 +31,6 @@ emotion_dict = {
32
 
33
  def predict_emotion(selected_audio):
34
  file_path = os.path.join("rec", selected_audio)
35
- ipd.display(ipd.Audio(file_path))
36
  out_prob, score, index, text_lab = learner.classify_file(file_path)
37
  return emotion_dict[text_lab[0]]
38
 
@@ -41,6 +39,7 @@ audio_files_list = get_audio_files_list()
41
 
42
  # Loading Gradio interface
43
  inputs = gr.Dropdown(label="Select Audio", choices=audio_files_list)
 
44
  outputs = "text"
45
  title = "ML Speech Emotion Detection"
46
  description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
 
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"):
 
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
 
 
39
 
40
  # Loading Gradio interface
41
  inputs = gr.Dropdown(label="Select Audio", choices=audio_files_list)
42
+ audio_ui=gr.Audio()
43
  outputs = "text"
44
  title = "ML Speech Emotion Detection"
45
  description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
app3.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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',
26
+ 'sur': 'Surprised',
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()