seayala commited on
Commit
bc7c876
verified
1 Parent(s): fb5df18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
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']
@@ -13,19 +14,25 @@ def extract_features(file_name):
13
  mfccsscaled = np.mean(mfccs.T,axis=0)
14
 
15
  except Exception as e:
16
- print("Error encountered while parsing file: ", file_name)
 
17
  return None
18
 
19
  return mfccsscaled
20
 
21
  def classify_audio(audio_file):
22
- # Carga el modelo
23
- model = tf.keras.models.load_model('my_model.h5')
24
 
25
- # Preprocesa el audio
26
- features = extract_features(audio_file)
 
 
 
 
 
 
27
  if features is None:
28
- return "Error al procesar el audio" # Manejo de error
29
 
30
  features = features.reshape(1, -1) # Redimensiona a (1, 40)
31
 
@@ -33,6 +40,7 @@ def classify_audio(audio_file):
33
  # features = features.reshape(1, 40, 1)
34
 
35
  # Realiza la predicci贸n
 
36
  prediction = model.predict(features)
37
  predicted_label_index = np.argmax(prediction)
38
 
 
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']
 
14
  mfccsscaled = np.mean(mfccs.T,axis=0)
15
 
16
  except Exception as e:
17
+ print(f"Error encountered while parsing file: {file_name}")
18
+ print(e) # Imprime la excepci贸n completa
19
  return None
20
 
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
 
 
40
  # features = features.reshape(1, 40, 1)
41
 
42
  # Realiza la predicci贸n
43
+ model = tf.keras.models.load_model('my_model.h5') # Carga del modelo dentro de la funci贸n
44
  prediction = model.predict(features)
45
  predicted_label_index = np.argmax(prediction)
46