abdullahsajid commited on
Commit
b64f6b8
·
verified ·
1 Parent(s): 190dda7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
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
- data = request.json
216
- encoded_audio = data['base64']
217
- mel_spectrogram, length = process_audio(encoded_audio)
218
- with torch.no_grad():
219
- output = voice_binary_model(mel_spectrogram,length)
220
- prob = torch.nn.functional.softmax(output[0], dim=0)
221
- pred = torch.argmax(prob).item()
222
- category = binary_labels[pred]
223
- probs_dict = {binary_labels[i]:prob[i] for i in range(len(binary_labels))}
224
- response = {
225
- 'message': 'Data received!',
226
- 'class' : category,
227
- 'probs' : probs_dict
228
- }
229
- return jsonify(response), 201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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