seayala commited on
Commit
21e2400
verified
1 Parent(s): cf08fbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -17
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import tensorflow as tf
3
  import librosa
4
  import numpy as np
5
- import tempfile
6
 
7
  # Diccionario de etiquetas
8
  labels = ['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']
@@ -21,33 +20,23 @@ def extract_features(file_name):
21
  return mfccsscaled
22
 
23
  def classify_audio(audio_file):
24
- print(f"Tipo de audio_file: {type(audio_file)}")
25
 
26
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
27
- tmp_file.write(audio_file.read())
28
- tmp_file_path = tmp_file.name
29
 
30
- # Preprocesa el audio (con extract_features())
31
- features = extract_features(tmp_file_path)
32
-
33
- # Si features es None, hubo un error en extract_features
34
  if features is None:
35
  return "Error al procesar el audio"
36
 
37
- features = features.reshape(1, -1) # Redimensiona a (1, 40)
38
-
39
- # Si tu modelo necesita 3 dimensiones, redimensiona a (1, 40, 1)
40
- # features = features.reshape(1, 40, 1)
41
 
42
- # Carga del modelo para usar la CPU
43
  model = tf.keras.models.load_model('my_model.h5', compile=False)
44
 
45
- # Realiza la predicci贸n en la CPU
46
  with tf.device('/CPU:0'):
47
  prediction = model.predict(features)
48
- predicted_label_index = np.argmax(prediction)
49
 
50
- # Devuelve la etiqueta predicha
51
  predicted_label = labels[predicted_label_index]
52
  return predicted_label
53
 
 
2
  import tensorflow as tf
3
  import librosa
4
  import numpy as np
 
5
 
6
  # Diccionario de etiquetas
7
  labels = ['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']
 
20
  return mfccsscaled
21
 
22
  def classify_audio(audio_file):
23
+ print(f"Tipo de audio_file: {type(audio_file)}") # Deber铆a imprimir <class 'str'>
24
 
25
+ # Preprocesa el audio directamente
26
+ features = extract_features(audio_file)
 
27
 
 
 
 
 
28
  if features is None:
29
  return "Error al procesar el audio"
30
 
31
+ features = features.reshape(1, -1)
 
 
 
32
 
33
+ # Carga el modelo (aseg煤rate que 'my_model.h5' est茅 en el mismo directorio)
34
  model = tf.keras.models.load_model('my_model.h5', compile=False)
35
 
 
36
  with tf.device('/CPU:0'):
37
  prediction = model.predict(features)
38
+ predicted_label_index = np.argmax(prediction)
39
 
 
40
  predicted_label = labels[predicted_label_index]
41
  return predicted_label
42