Spaces:
Runtime error
Runtime error
seikin_alexey
commited on
Commit
·
4745aa4
1
Parent(s):
cb8d014
app4
Browse files
README.md
CHANGED
|
@@ -5,7 +5,7 @@ colorFrom: green
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.0.19
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
duplicated_from: harish3110/emotion_detection
|
| 11 |
---
|
|
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.0.19
|
| 8 |
+
app_file: app4.py
|
| 9 |
pinned: false
|
| 10 |
duplicated_from: harish3110/emotion_detection
|
| 11 |
---
|
app4.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from speechbrain.pretrained.interfaces import foreign_class
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import warnings
|
| 5 |
+
warnings.filterwarnings("ignore")
|
| 6 |
+
|
| 7 |
+
# Function to get the list of audio files in the 'rec/' directory
|
| 8 |
+
def get_audio_files_list(directory="rec"):
|
| 9 |
+
try:
|
| 10 |
+
return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
| 11 |
+
except FileNotFoundError:
|
| 12 |
+
print("The 'rec' directory does not exist. Please make sure it is the correct path.")
|
| 13 |
+
return []
|
| 14 |
+
|
| 15 |
+
# Loading the speechbrain emotion detection model
|
| 16 |
+
learner = foreign_class(
|
| 17 |
+
source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
|
| 18 |
+
pymodule_file="custom_interface.py",
|
| 19 |
+
classname="CustomEncoderWav2vec2Classifier"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Building prediction function for Gradio
|
| 23 |
+
emotion_dict = {
|
| 24 |
+
'sad': 'Sad',
|
| 25 |
+
'hap': 'Happy',
|
| 26 |
+
'ang': 'Anger',
|
| 27 |
+
'fea': 'Fear',
|
| 28 |
+
'sur': 'Surprised',
|
| 29 |
+
'neu': 'Neutral'
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
def predict_emotion(selected_audio):
|
| 33 |
+
if selected_audio is None: # Check if an audio file is selected
|
| 34 |
+
return "Please select an audio file.", None
|
| 35 |
+
file_path = os.path.join("rec", selected_audio)
|
| 36 |
+
out_prob, score, index, text_lab = learner.classify_file(file_path)
|
| 37 |
+
emotion = emotion_dict[text_lab[0]]
|
| 38 |
+
return emotion, file_path # Return both emotion and file path
|
| 39 |
+
|
| 40 |
+
# Get the list of audio files for the dropdown
|
| 41 |
+
audio_files_list = get_audio_files_list()
|
| 42 |
+
|
| 43 |
+
# Loading Gradio interface
|
| 44 |
+
dropdown = gr.Dropdown(label="Select Audio", choices=audio_files_list)
|
| 45 |
+
button = gr.Button("Detect emotion")
|
| 46 |
+
outputs = [gr.outputs.Textbox(label="Predicted Emotion"), gr.outputs.Audio(label="Play Audio")]
|
| 47 |
+
|
| 48 |
+
def button_click(selected_audio):
|
| 49 |
+
return predict_emotion(selected_audio) # Call predict_emotion when button is clicked
|
| 50 |
+
|
| 51 |
+
title = "ML Speech Emotion Detection"
|
| 52 |
+
description = "Speechbrain powered wav2vec 2.0 pretrained model on IEMOCAP dataset using Gradio."
|
| 53 |
+
|
| 54 |
+
# Create the Gradio interface
|
| 55 |
+
interface = gr.Interface(
|
| 56 |
+
fn=button_click, # Use the button_click function for the interface
|
| 57 |
+
inputs=[dropdown, button],
|
| 58 |
+
outputs=outputs,
|
| 59 |
+
title=title,
|
| 60 |
+
description=description
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
interface.launch()
|