|
import gradio as gr |
|
import tensorflow as tf |
|
import librosa |
|
import numpy as np |
|
|
|
|
|
labels = ['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes'] |
|
|
|
def extract_features(file_name): |
|
try: |
|
audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast') |
|
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40) |
|
mfccsscaled = np.mean(mfccs.T,axis=0) |
|
|
|
except Exception as e: |
|
print(f"Error encountered while parsing file: {file_name}") |
|
print(e) |
|
return None |
|
|
|
return mfccsscaled |
|
|
|
def classify_audio(audio_file): |
|
print(f"Tipo de audio_file: {type(audio_file)}") |
|
|
|
|
|
features = extract_features(audio_file) |
|
|
|
if features is None: |
|
return "Error al procesar el audio" |
|
|
|
features = features.reshape(1, -1) |
|
|
|
|
|
model = tf.keras.models.load_model('my_model.h5', compile=False) |
|
|
|
with tf.device('/CPU:0'): |
|
prediction = model.predict(features) |
|
predicted_label_index = np.argmax(prediction) |
|
|
|
predicted_label = labels[predicted_label_index] |
|
return predicted_label |
|
|
|
iface = gr.Interface( |
|
fn=classify_audio, |
|
inputs=gr.Audio(type="filepath"), |
|
outputs="text", |
|
title="Clasificaci贸n de audio simple", |
|
description="Sube un archivo de audio para clasificarlo." |
|
) |
|
|
|
iface.launch() |