Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
|
| 7 |
+
# Load trained model
|
| 8 |
+
model = keras.models.load_model("engine_sound_model.h5")
|
| 9 |
+
|
| 10 |
+
# Class labels
|
| 11 |
+
labels = ["normal", "faulty", "background_noise", "unknown"]
|
| 12 |
+
|
| 13 |
+
def predict_engine_sound(audio_file):
|
| 14 |
+
y, sr = librosa.load(audio_file, sr=22050)
|
| 15 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
| 16 |
+
features = np.mean(mfccs.T, axis=0)
|
| 17 |
+
features = np.expand_dims(features, axis=0)
|
| 18 |
+
|
| 19 |
+
prediction = model.predict(features)
|
| 20 |
+
return labels[np.argmax(prediction)]
|
| 21 |
+
|
| 22 |
+
# Create a Gradio interface
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=predict_engine_sound,
|
| 25 |
+
inputs=gr.Audio(type="filepath"),
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="Engine Sound Fault Detector",
|
| 28 |
+
description="Upload an engine sound and the model will classify it as normal, faulty, or background noise."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Launch the Gradio app
|
| 32 |
+
iface.launch()
|