Spaces:
Running on Zero
Running on Zero
Added badges for repo and presentation
Browse files
app.py
CHANGED
|
@@ -122,31 +122,22 @@ def preprocess_drive_audio(file_path, target_sample_rate=22050, n_mels=128, nois
|
|
| 122 |
return final_tensor
|
| 123 |
|
| 124 |
|
| 125 |
-
def run_local_inference(
|
| 126 |
"""
|
| 127 |
-
Runs a forward pass
|
| 128 |
-
and a dictionary of all class probabilities.
|
| 129 |
"""
|
|
|
|
| 130 |
emotion_classes = ["Neutral", "Calm", "Happy", "Sad", "Angry", "Fearful", "Disgust", "Surprised"]
|
| 131 |
|
| 132 |
input_tensor = preprocess_drive_audio(file_path, noise_std=noise_std)
|
| 133 |
-
input_tensor = input_tensor.to(
|
| 134 |
|
| 135 |
-
|
| 136 |
with torch.no_grad():
|
| 137 |
-
logits =
|
| 138 |
-
|
| 139 |
-
# Get the top prediction index
|
| 140 |
predicted_index = torch.argmax(logits, dim=1).item()
|
| 141 |
-
top_class = emotion_classes[predicted_index]
|
| 142 |
-
|
| 143 |
-
# Apply softmax to convert logits to probabilities between 0 and 1
|
| 144 |
-
probabilities = torch.softmax(logits, dim=1).squeeze().tolist()
|
| 145 |
|
| 146 |
-
|
| 147 |
-
prob_dict = {emotion_classes[i]: probabilities[i] for i in range(len(emotion_classes))}
|
| 148 |
-
|
| 149 |
-
return top_class, prob_dict
|
| 150 |
|
| 151 |
|
| 152 |
# Load SOTA SupConFN
|
|
@@ -164,13 +155,27 @@ model = model.to(device)
|
|
| 164 |
@spaces.GPU
|
| 165 |
def predict_emotion(audio_path, noise_std):
|
| 166 |
if audio_path is None:
|
| 167 |
-
return "
|
| 168 |
|
| 169 |
try:
|
| 170 |
-
|
| 171 |
-
return
|
| 172 |
except Exception as e:
|
| 173 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
demo = gr.Interface(
|
| 176 |
fn=predict_emotion,
|
|
@@ -183,9 +188,9 @@ demo = gr.Interface(
|
|
| 183 |
gr.Label(num_top_classes=8, label="Probability Distribution")
|
| 184 |
],
|
| 185 |
title="Robust Speech Emotion Recognition (SupCon)",
|
| 186 |
-
description=
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
-
|
| 190 |
if __name__ == "__main__":
|
| 191 |
-
demo.launch(
|
|
|
|
| 122 |
return final_tensor
|
| 123 |
|
| 124 |
|
| 125 |
+
def run_local_inference(model, file_path, device, noise_std=0.0):
|
| 126 |
"""
|
| 127 |
+
Runs a forward pass
|
|
|
|
| 128 |
"""
|
| 129 |
+
|
| 130 |
emotion_classes = ["Neutral", "Calm", "Happy", "Sad", "Angry", "Fearful", "Disgust", "Surprised"]
|
| 131 |
|
| 132 |
input_tensor = preprocess_drive_audio(file_path, noise_std=noise_std)
|
| 133 |
+
input_tensor = input_tensor.to(device)
|
| 134 |
|
| 135 |
+
model.eval()
|
| 136 |
with torch.no_grad():
|
| 137 |
+
logits = model(input_tensor)
|
|
|
|
|
|
|
| 138 |
predicted_index = torch.argmax(logits, dim=1).item()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
return emotion_classes[predicted_index]
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
# Load SOTA SupConFN
|
|
|
|
| 155 |
@spaces.GPU
|
| 156 |
def predict_emotion(audio_path, noise_std):
|
| 157 |
if audio_path is None:
|
| 158 |
+
return "Upload an audio file."
|
| 159 |
|
| 160 |
try:
|
| 161 |
+
pred = run_local_inference(model, audio_path, device, noise_std=noise_std)
|
| 162 |
+
return pred
|
| 163 |
except Exception as e:
|
| 164 |
+
return f"Error processing audio: {str(e)}"
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
description_html = """
|
| 168 |
+
Upload a short audio clip and test the architecture's resilience. The slider injects mathematically controlled Gaussian noise into the normalized Mel-spectrogram before classification.
|
| 169 |
+
|
| 170 |
+
<div style="display: flex; gap: 10px; margin-top: 15px;">
|
| 171 |
+
<a href="https://github.com/gpetrousov/dl_assignment_demokritos" target="_blank">
|
| 172 |
+
<img src="https://img.shields.io/badge/GitHub-View_Repository-181717?style=for-the-badge&logo=github" alt="GitHub Repository" />
|
| 173 |
+
</a>
|
| 174 |
+
<a href="https://docs.google.com/presentation/d/1ShSmXY2OqCEbQQjYiLYCFc8Psu2bioECoFHhliiDEiQ/edit?usp=drive_link" target="_blank">
|
| 175 |
+
<img src="https://img.shields.io/badge/Presentation-View_Slides-F4B400?style=for-the-badge&logo=googleslides&logoColor=white" alt="Presentation Slides" />
|
| 176 |
+
</a>
|
| 177 |
+
</div>
|
| 178 |
+
"""
|
| 179 |
|
| 180 |
demo = gr.Interface(
|
| 181 |
fn=predict_emotion,
|
|
|
|
| 188 |
gr.Label(num_top_classes=8, label="Probability Distribution")
|
| 189 |
],
|
| 190 |
title="Robust Speech Emotion Recognition (SupCon)",
|
| 191 |
+
description=description_html,
|
| 192 |
+
allow_flagging="never"
|
| 193 |
)
|
| 194 |
|
|
|
|
| 195 |
if __name__ == "__main__":
|
| 196 |
+
demo.launch(ssr=False)
|