File size: 909 Bytes
3d42d48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
import librosa
import numpy as np
import tensorflow as tf
from tensorflow import keras

# Load trained model
model = keras.models.load_model("engine_sound_model.h5")

# Class labels
labels = ["normal", "faulty", "background_noise", "unknown"]

def predict_engine_sound(audio_file):
    y, sr = librosa.load(audio_file, sr=22050)
    mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
    features = np.mean(mfccs.T, axis=0)
    features = np.expand_dims(features, axis=0)

    prediction = model.predict(features)
    return labels[np.argmax(prediction)]

# Create a Gradio interface
iface = gr.Interface(
    fn=predict_engine_sound,
    inputs=gr.Audio(type="filepath"),
    outputs="text",
    title="Engine Sound Fault Detector",
    description="Upload an engine sound and the model will classify it as normal, faulty, or background noise."
)

# Launch the Gradio app
iface.launch()