Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,10 +18,22 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
18 |
model.to(device)
|
19 |
model.eval()
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def recognize_emotion(audio):
|
22 |
try:
|
23 |
if audio is None:
|
24 |
-
return {emotion: 0
|
25 |
|
26 |
audio_path = audio if isinstance(audio, str) else audio.name
|
27 |
speech_array, sampling_rate = torchaudio.load(audio_path)
|
@@ -30,7 +42,7 @@ def recognize_emotion(audio):
|
|
30 |
if duration > 60:
|
31 |
return {
|
32 |
"Error": "Audio too long (max 1 minute)",
|
33 |
-
**{emotion: 0
|
34 |
}
|
35 |
|
36 |
if sampling_rate != 16000:
|
@@ -52,7 +64,7 @@ def recognize_emotion(audio):
|
|
52 |
probs = F.softmax(logits, dim=-1)[0].cpu().numpy()
|
53 |
|
54 |
confidence_scores = {
|
55 |
-
emotion: round(float(prob) * 100
|
56 |
for emotion, prob in zip(emotion_labels, probs)
|
57 |
}
|
58 |
|
@@ -67,51 +79,42 @@ def recognize_emotion(audio):
|
|
67 |
except Exception as e:
|
68 |
return {
|
69 |
"Error": str(e),
|
70 |
-
**{emotion: 0
|
71 |
}
|
72 |
|
|
|
|
|
|
|
73 |
interface = gr.Interface(
|
74 |
fn=recognize_emotion,
|
75 |
inputs=gr.Audio(
|
76 |
sources=["microphone", "upload"],
|
77 |
type="filepath",
|
78 |
-
label="
|
79 |
-
max_length=60
|
80 |
),
|
81 |
outputs=gr.Label(
|
82 |
num_top_classes=len(emotion_labels),
|
83 |
-
label="Emotion
|
84 |
),
|
85 |
title="Speech Emotion Recognition",
|
86 |
-
description="""
|
87 |
-
|
88 |
-
|
89 |
-
This model recognizes emotions from speech audio in the following categories:
|
90 |
-
- Angry π
|
91 |
-
- Calm π
|
92 |
-
- Disgust π€’
|
93 |
-
- Fearful π¨
|
94 |
-
- Happy π
|
95 |
-
- Neutral π
|
96 |
-
- Sad π’
|
97 |
-
- Surprised π²
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
- Confidence scores are shown as percentages
|
108 |
"""
|
109 |
)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
)
|
|
|
18 |
model.to(device)
|
19 |
model.eval()
|
20 |
|
21 |
+
# At the top with other global variables
|
22 |
+
emotion_icons = {
|
23 |
+
"angry": "π ",
|
24 |
+
"calm": "π",
|
25 |
+
"disgust": "π€’",
|
26 |
+
"fearful": "π¨",
|
27 |
+
"happy": "π",
|
28 |
+
"neutral": "π",
|
29 |
+
"sad": "π’",
|
30 |
+
"surprised": "π²"
|
31 |
+
}
|
32 |
+
|
33 |
def recognize_emotion(audio):
|
34 |
try:
|
35 |
if audio is None:
|
36 |
+
return {f"{emotion} {emotion_icons[emotion]}": 0 for emotion in emotion_labels}
|
37 |
|
38 |
audio_path = audio if isinstance(audio, str) else audio.name
|
39 |
speech_array, sampling_rate = torchaudio.load(audio_path)
|
|
|
42 |
if duration > 60:
|
43 |
return {
|
44 |
"Error": "Audio too long (max 1 minute)",
|
45 |
+
**{f"{emotion} {emotion_icons[emotion]}": 0 for emotion in emotion_labels}
|
46 |
}
|
47 |
|
48 |
if sampling_rate != 16000:
|
|
|
64 |
probs = F.softmax(logits, dim=-1)[0].cpu().numpy()
|
65 |
|
66 |
confidence_scores = {
|
67 |
+
f"{emotion} {emotion_icons[emotion]}": int(round(float(prob) * 100))
|
68 |
for emotion, prob in zip(emotion_labels, probs)
|
69 |
}
|
70 |
|
|
|
79 |
except Exception as e:
|
80 |
return {
|
81 |
"Error": str(e),
|
82 |
+
**{f"{emotion} {emotion_icons[emotion]}": 0 for emotion in emotion_labels}
|
83 |
}
|
84 |
|
85 |
+
# Create a formatted string of supported emotions
|
86 |
+
supported_emotions = " | ".join([f"{emotion_icons[emotion]} {emotion}" for emotion in emotion_labels])
|
87 |
+
|
88 |
interface = gr.Interface(
|
89 |
fn=recognize_emotion,
|
90 |
inputs=gr.Audio(
|
91 |
sources=["microphone", "upload"],
|
92 |
type="filepath",
|
93 |
+
label="Record or Upload Audio"
|
|
|
94 |
),
|
95 |
outputs=gr.Label(
|
96 |
num_top_classes=len(emotion_labels),
|
97 |
+
label="Detected Emotion"
|
98 |
),
|
99 |
title="Speech Emotion Recognition",
|
100 |
+
description=f"""
|
101 |
+
### Supported Emotions:
|
102 |
+
{supported_emotions}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
Maximum audio length: 1 minute""",
|
105 |
+
theme=gr.themes.Soft(
|
106 |
+
primary_hue="orange",
|
107 |
+
secondary_hue="blue"
|
108 |
+
),
|
109 |
+
css="""
|
110 |
+
.gradio-container {max-width: 800px}
|
111 |
+
.label {font-size: 18px}
|
|
|
112 |
"""
|
113 |
)
|
114 |
|
115 |
+
interface.launch(
|
116 |
+
share=True,
|
117 |
+
debug=True,
|
118 |
+
server_name="0.0.0.0",
|
119 |
+
server_port=7860
|
120 |
+
)
|
|