immelstorun commited on
Commit
73bfdef
·
1 Parent(s): e7fc258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
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
- import warnings
6
- warnings.filterwarnings("ignore")
 
 
 
 
 
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 gradio
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
- # Assuming emotion_dict and learner are defined elsewhere in your code
28
- # and learner.classify_file is a method that classifies the audio file
29
-
30
- def predict_emotion(audio, rec_file):
31
- rec_path = os.path.join("rec", rec_file.name)
32
- # Assuming you want to use the audio file from the 'rec' directory for prediction
33
- out_prob, score, index, text_lab = learner.classify_file(rec_path)
34
- return emotion_dict[text_lab[0]]
35
-
36
- # Loading gradio interface
37
- inputs = [
38
- gr.inputs.Audio(label="Input Audio", type="file"),
39
- gr.inputs.File(label="Choose file from rec directory", type="file", default="rec/")
40
- ]
41
- outputs = "text"
42
- title = "ML Speech Emotion Detection"
43
- description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
44
-
45
- gr.Interface(fn=predict_emotion, inputs=inputs, outputs=outputs, title=title, description=description).launch()
 
 
 
 
 
 
 
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()