immelstorun commited on
Commit
78fbf94
·
1 Parent(s): f6b1cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -11
app.py CHANGED
@@ -1,19 +1,28 @@
1
  from speechbrain.pretrained.interfaces import foreign_class
2
- import gradio as gr
3
 
4
  import warnings
5
  warnings.filterwarnings("ignore")
6
 
 
 
 
 
 
 
 
 
 
 
7
  # Loading the speechbrain emotion detection model
8
  learner = foreign_class(
9
  source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
10
- pymodule_file="custom_interface.py",
11
  classname="CustomEncoderWav2vec2Classifier"
12
  )
13
 
14
  # Building prediction function for gradio
15
  emotion_dict = {
16
- 'sad': 'Sad',
17
  'hap': 'Happy',
18
  'ang': 'Anger',
19
  'fea': 'Fear',
@@ -21,13 +30,33 @@ emotion_dict = {
21
  'neu': 'Neutral'
22
  }
23
 
24
- def predict_emotion(audio):
25
- out_prob, score, index, text_lab = learner.classify_file(audio.name)
26
- return emotion_dict[text_lab[0]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Loading gradio interface
29
- inputs = gr.inputs.Audio(label="Input Audio", type="file")
30
- outputs = "text"
31
  title = "ML Speech Emotion Detection"
32
- description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
33
- gr.Interface(predict_emotion, inputs, outputs, title=title, description=description).launch()
 
 
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 = 'prerecorded'
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(
18
  source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
19
+ pymodule_file="custom_interface.py",
20
  classname="CustomEncoderWav2vec2Classifier"
21
  )
22
 
23
  # Building prediction function for gradio
24
  emotion_dict = {
25
+ 'sad': 'Sad',
26
  'hap': 'Happy',
27
  'ang': 'Anger',
28
  'fea': 'Fear',
 
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()