Spaces:
Runtime error
Runtime error
seikin_alexey
commited on
Commit
·
8f07fb4
1
Parent(s):
08c021b
app2.py
CHANGED
|
@@ -37,9 +37,17 @@ def predict_emotion(selected_audio):
|
|
| 37 |
# Get the list of audio files for the dropdown
|
| 38 |
audio_files_list = get_audio_files_list()
|
| 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."
|
|
|
|
| 37 |
# Get the list of audio files for the dropdown
|
| 38 |
audio_files_list = get_audio_files_list()
|
| 39 |
|
| 40 |
+
def return_audio_clip(audio_text):
|
| 41 |
+
post_file_name = audio_text.lower() + '.wav'
|
| 42 |
+
filepath = os.path.join("pre_recoreded",post_file_name)
|
| 43 |
+
return filepath
|
| 44 |
+
|
| 45 |
+
|
| 46 |
# Loading Gradio interface
|
| 47 |
inputs = gr.Dropdown(label="Select Audio", choices=audio_files_list)
|
| 48 |
audio_ui=gr.Audio()
|
| 49 |
+
inputs.change(return_audio_clip,inputs,audio_ui)
|
| 50 |
+
|
| 51 |
outputs = "text"
|
| 52 |
title = "ML Speech Emotion Detection"
|
| 53 |
description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
|
app3.py
CHANGED
|
@@ -1,55 +0,0 @@
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|