Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,61 @@ 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']
|
8 |
|
9 |
def extract_features(file_name):
|
10 |
try:
|
11 |
-
audio
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
except Exception as e:
|
16 |
print(f"Error encountered while parsing file: {file_name}")
|
17 |
-
print(e)
|
18 |
return None
|
19 |
-
|
20 |
-
return
|
21 |
|
22 |
def classify_audio(audio_file):
|
23 |
-
print(f"Tipo de audio_file: {type(audio_file)}")
|
24 |
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
if features is None:
|
29 |
-
return "Error al procesar el audio"
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
# Carga el modelo
|
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 |
|
@@ -48,4 +68,4 @@ iface = gr.Interface(
|
|
48 |
description="Sube un archivo de audio para clasificarlo."
|
49 |
)
|
50 |
|
51 |
-
iface.launch()
|
|
|
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']
|
9 |
|
10 |
def extract_features(file_name):
|
11 |
try:
|
12 |
+
# Carga el audio sin cambiar el sample rate
|
13 |
+
audio, sample_rate = librosa.load(file_name, sr=None)
|
14 |
+
|
15 |
+
# Saca el espectrograma de magnitud
|
16 |
+
spectrogram = np.abs(librosa.stft(audio, n_fft=512, hop_length=256))
|
17 |
+
|
18 |
+
# Convierte a escala logarítmica (como normalmente esperan los modelos de audio)
|
19 |
+
log_spectrogram = librosa.amplitude_to_db(spectrogram)
|
20 |
+
|
21 |
+
# Ajusta tamaño exacto
|
22 |
+
log_spectrogram = librosa.util.fix_length(log_spectrogram, size=257, axis=0)
|
23 |
+
log_spectrogram = librosa.util.fix_length(log_spectrogram, size=97, axis=1)
|
24 |
+
|
25 |
+
# Normaliza
|
26 |
+
log_spectrogram = (log_spectrogram - np.mean(log_spectrogram)) / np.std(log_spectrogram)
|
27 |
+
|
28 |
+
# Añade canal para la red convolucional
|
29 |
+
log_spectrogram = log_spectrogram[..., np.newaxis]
|
30 |
|
31 |
except Exception as e:
|
32 |
print(f"Error encountered while parsing file: {file_name}")
|
33 |
+
print(e)
|
34 |
return None
|
35 |
+
|
36 |
+
return log_spectrogram
|
37 |
|
38 |
def classify_audio(audio_file):
|
39 |
+
print(f"Tipo de audio_file: {type(audio_file)}")
|
40 |
|
41 |
+
# El tipo es string (ruta), no hace falta leer ni escribir en temp files
|
42 |
+
file_path = audio_file
|
43 |
+
|
44 |
+
# Extrae características
|
45 |
+
features = extract_features(file_path)
|
46 |
|
47 |
if features is None:
|
48 |
+
return "Error al procesar el audio"
|
49 |
+
|
50 |
+
# Añade batch dimension
|
51 |
+
features = features[np.newaxis, ...] # (1, 97, 257, 1)
|
52 |
|
53 |
+
# Carga el modelo en CPU
|
54 |
model = tf.keras.models.load_model('my_model.h5', compile=False)
|
55 |
|
56 |
with tf.device('/CPU:0'):
|
57 |
prediction = model.predict(features)
|
58 |
predicted_label_index = np.argmax(prediction)
|
59 |
+
|
60 |
predicted_label = labels[predicted_label_index]
|
61 |
return predicted_label
|
62 |
|
|
|
68 |
description="Sube un archivo de audio para clasificarlo."
|
69 |
)
|
70 |
|
71 |
+
iface.launch()
|