Update app.py
Browse files
app.py
CHANGED
@@ -136,52 +136,29 @@ def decode_emotion_prediction(prediction_tensor, label_encoder):
|
|
136 |
|
137 |
return predicted_emotion, confidence
|
138 |
|
139 |
-
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
140 |
-
# model = model.to(device)
|
141 |
-
|
142 |
def predict(wave):
|
143 |
-
wave
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
# Gradio Interface
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
gr.Markdown("# Emotion Recognition App")
|
164 |
-
gr.Markdown("Upload an audio file or record directly to get a prediction")
|
165 |
-
|
166 |
-
with gr.Row():
|
167 |
-
audio_input = gr.Audio(sources="microphone", type="filepath")
|
168 |
-
audio_output = gr.Audio(label="Processed Audio")
|
169 |
-
|
170 |
-
with gr.Row():
|
171 |
-
submit_btn = gr.Button("Get Prediction", variant="primary")
|
172 |
-
clear_btn = gr.Button("Clear")
|
173 |
-
|
174 |
-
prediction_output = gr.Textbox(label="Prediction")
|
175 |
-
|
176 |
-
submit_btn.click(
|
177 |
-
fn=predict,
|
178 |
-
inputs=[audio_input],
|
179 |
-
outputs=[audio_output, prediction_output]
|
180 |
-
)
|
181 |
-
|
182 |
-
clear_btn.click(
|
183 |
-
fn=lambda: (None, None, ""),
|
184 |
-
outputs=[audio_input, audio_output, prediction_output]
|
185 |
-
)
|
186 |
-
|
187 |
-
demo.launch()
|
|
|
136 |
|
137 |
return predicted_emotion, confidence
|
138 |
|
|
|
|
|
|
|
139 |
def predict(wave):
|
140 |
+
if wave is None or wave == '':
|
141 |
+
return "No audio input provided."
|
142 |
+
try:
|
143 |
+
wave = preprocess_single_audio(wave)
|
144 |
+
le = LabelEncoder()
|
145 |
+
le.classes_ = np.array(['Angry', 'Disgusting', 'Fear', 'Happy', 'Neutral', 'Sad'])
|
146 |
+
wave = wave.unsqueeze(0)
|
147 |
+
with torch.no_grad():
|
148 |
+
prediction = model(wave)
|
149 |
+
predicted_emotion, confidence = decode_emotion_prediction(prediction, le)
|
150 |
+
return f"Predicted emotion: {predicted_emotion} (Confidence: {confidence:.2f})"
|
151 |
+
except Exception as e:
|
152 |
+
return f'Error in processing audio: {str(e)}'
|
153 |
|
154 |
# Gradio Interface
|
155 |
+
iface = gr.Interface(
|
156 |
+
fn=predict,
|
157 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
158 |
+
outputs="text",
|
159 |
+
live=True,
|
160 |
+
title="Speech Emotion Recognition",
|
161 |
+
description="Record your voice and get the predicted emotion."
|
162 |
+
)
|
163 |
+
|
164 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|