Spaces:
Runtime error
Runtime error
Commit
·
c9ca9ad
1
Parent(s):
9f42e58
Update app.py
Browse files
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 |
-
#
|
| 24 |
emotion_dict = {
|
| 25 |
'sad': 'Sad',
|
| 26 |
'hap': 'Happy',
|
|
@@ -30,33 +22,48 @@ emotion_dict = {
|
|
| 30 |
'neu': 'Neutral'
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
inputs =
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|