immelstorun commited on
Commit
c9ca9ad
·
1 Parent(s): 9f42e58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -42
app.py CHANGED
@@ -1,17 +1,9 @@
1
  from speechbrain.pretrained.interfaces import foreign_class
2
-
3
- import warnings
4
- warnings.filterwarnings("ignore")
5
-
6
- import os
7
  import gradio as gr
 
 
8
 
9
- # Путь к каталогу с предзаписанными аудиофайлами
10
- prerecorded_audio_path = 'prerecordered'
11
- # Список файлов в каталоге prerecorded
12
- prerecorded_audio_files = os.listdir(prerecorded_audio_path)
13
- # Полные пути к файлам для Dropdown
14
- prerecorded_audio_files_full_path = [os.path.join(prerecorded_audio_path, file) for file in prerecorded_audio_files]
15
 
16
  # Loading the speechbrain emotion detection model
17
  learner = foreign_class(
@@ -20,7 +12,7 @@ learner = foreign_class(
20
  classname="CustomEncoderWav2vec2Classifier"
21
  )
22
 
23
- # Building prediction function for gradio
24
  emotion_dict = {
25
  'sad': 'Sad',
26
  'hap': 'Happy',
@@ -30,33 +22,48 @@ emotion_dict = {
30
  'neu': 'Neutral'
31
  }
32
 
33
- def predict_emotion(uploaded_audio=None, prerecorded_audio=None):
34
- # Если выбран аудиофайл из выпадающего списка, использовать его
35
- if prerecorded_audio is not None:
36
- audio_file_path = prerecorded_audio
37
- elif uploaded_audio is not None:
38
- # Иначе, если загружен файл, использовать его
39
- audio_file_path = uploaded_audio.name
40
- else:
41
- # Если нет файла, вернуть сообщение об ошибке
42
- return "No audio file provided", 0
43
-
44
- out_prob, score, index, text_lab = learner.classify_file(audio_file_path)
45
- emotion_probability = out_prob[0][index[0]].item()
46
-
47
- # Возвращаем словарь с эмоцией и вероятностью
48
- return {"Emotion": emotion_dict[text_lab[0]], "Probability": f"{emotion_probability:.2f}"}
49
-
50
- # Модифицированный Gradio interface
51
- inputs = [
52
- gr.inputs.Dropdown(list(prerecorded_audio_files_full_path), label="Select Prerecorded Audio", default=None),
53
- gr.inputs.Audio(label="Or Upload Audio", type="file", source="upload", optional=True),
54
- gr.inputs.Audio(label="Or Record Audio", type="file", source="microphone", optional=True)
55
- ]
56
-
57
- outputs = gr.outputs.Label(num_top_classes=2)
58
-
59
- title = "ML Speech Emotion Detection"
60
- description = "Detect emotions from speech using a Speechbrain powered model."
61
-
62
- gr.Interface(fn=predict_emotion, inputs=inputs, outputs=outputs, title=title, description=description).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from speechbrain.pretrained.interfaces import foreign_class
 
 
 
 
 
2
  import gradio as gr
3
+ import os
4
+ import warnings
5
 
6
+ warnings.filterwarnings("ignore")
 
 
 
 
 
7
 
8
  # Loading the speechbrain emotion detection model
9
  learner = foreign_class(
 
12
  classname="CustomEncoderWav2vec2Classifier"
13
  )
14
 
15
+ # Emotion dictionary
16
  emotion_dict = {
17
  'sad': 'Sad',
18
  'hap': 'Happy',
 
22
  'neu': 'Neutral'
23
  }
24
 
25
+ # Function for classification of uploaded files
26
+ def predict_emotion_upload(audio):
27
+ out_prob, score, index, text_lab = learner.classify_file(audio.name)
28
+ return emotion_dict[text_lab[0]]
29
+
30
+ # Function for classification of selected files from the dropdown
31
+ def predict_emotion_select(filename):
32
+ file_path = os.path.join('rec', filename)
33
+ out_prob, score, index, text_lab = learner.classify_file(file_path)
34
+ return emotion_dict[text_lab[0]]
35
+
36
+ # Function to create an audio player component
37
+ def create_audio_player(filename):
38
+ file_path = os.path.join('rec', filename)
39
+ return file_path
40
+
41
+ # Retrieve a list of audio file names from the 'rec' directory
42
+ audio_files = os.listdir('rec')
43
+ audio_files_dropdown = gr.inputs.Dropdown(choices=audio_files, label="Select Audio File")
44
+
45
+ # Define Gradio interface components for both tabs
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("## ML Speech Emotion Detection")
48
+ gr.Markdown("Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset.")
49
+
50
+ with gr.Tabs():
51
+ with gr.TabItem("Upload Audio"):
52
+ with gr.Group():
53
+ audio_upload = gr.Audio(label="Upload Audio", type="file")
54
+ submit_btn_1 = gr.Button("Classify Uploaded Audio")
55
+ audio_player_1 = gr.Audio(label="Uploaded Audio Player", interactive=True)
56
+ output_text_1 = gr.Textbox(label="Prediction")
57
+
58
+ submit_btn_1.click(predict_emotion_upload, inputs=audio_upload, outputs=[output_text_1, audio_player_1])
59
+
60
+ with gr.TabItem("Select from List"):
61
+ with gr.Group():
62
+ submit_btn_2 = gr.Button("Classify Selected Audio")
63
+ audio_player_2 = gr.Audio(label="Selected Audio Player", interactive=True)
64
+ output_text_2 = gr.Textbox(label="Prediction")
65
+
66
+ audio_files_dropdown.change(create_audio_player, inputs=audio_files_dropdown, outputs=audio_player_2)
67
+ submit_btn_2.click(predict_emotion_select, inputs=audio_files_dropdown, outputs=output_text_2)
68
+
69
+ demo.launch()