Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -212,18 +212,33 @@ def post_test_data():
|
|
| 212 |
|
| 213 |
@app.route('/api/voice', methods=['POST'])
|
| 214 |
def post_api_voice():
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
@app.route('/api/voice', methods=['POST'])
|
| 214 |
def post_api_voice():
|
| 215 |
+
try:
|
| 216 |
+
data = request.json
|
| 217 |
+
if not data or 'base64' not in data:
|
| 218 |
+
return jsonify({'error': 'Invalid input. No base64 data provided.'}), 400
|
| 219 |
+
|
| 220 |
+
encoded_audio = data['base64']
|
| 221 |
+
|
| 222 |
+
# Process the audio to get Mel spectrogram and length
|
| 223 |
+
mel_spectrogram, length = process_audio(encoded_audio)
|
| 224 |
+
|
| 225 |
+
# Ensure the model and input dimensions are correct
|
| 226 |
+
with torch.no_grad():
|
| 227 |
+
output = voice_binary_model(mel_spectrogram, length)
|
| 228 |
+
prob = torch.nn.functional.softmax(output[0], dim=0)
|
| 229 |
+
pred = torch.argmax(prob).item()
|
| 230 |
+
category = binary_labels[pred]
|
| 231 |
+
probs_dict = {binary_labels[i]: prob[i].item() for i in range(len(binary_labels))}
|
| 232 |
+
|
| 233 |
+
response = {
|
| 234 |
+
'message': 'Data received!',
|
| 235 |
+
'class': category,
|
| 236 |
+
'probs': probs_dict
|
| 237 |
+
}
|
| 238 |
+
return jsonify(response), 201
|
| 239 |
+
|
| 240 |
+
except KeyError as e:
|
| 241 |
+
return jsonify({'error': f'Missing key: {str(e)}'}), 400
|
| 242 |
+
|
| 243 |
+
except Exception as e:
|
| 244 |
+
return jsonify({'error': str(e)}), 500
|