File size: 2,809 Bytes
2c2f868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
import os
import joblib
from sound_classifier import SoundClassifier
import numpy as np

# Get list of available models and their friendly names
MODELS_DIR = 'models'
MODEL_NAMES = {
    'lr_sound_classifier_model.joblib': 'Logistic Regression',
    'nn_sound_classifier_model.joblib': 'Neural Network',
    'rf_sound_classifier_model.joblib': 'Random Forest',
    'svm_sound_classifier_model.joblib': 'Support Vector Machine'
}

model_files = [f for f in os.listdir(MODELS_DIR) if f.endswith('_model.joblib')]
model_choices = {MODEL_NAMES[file]: file for file in model_files}

def load_model(model_file):
    """Load a saved model and its associated scaler and label encoder"""
    model_path = os.path.join(MODELS_DIR, model_file)
    saved_data = joblib.load(model_path)
    return saved_data['model'], saved_data['scaler'], saved_data['label_encoder']

def format_issue(issue_text):
    """Format the issue text to be more readable"""
    # Replace underscores with spaces and title case the text
    formatted = issue_text.replace('_', ' ').title()
    return formatted

def predict_sound(audio_file, model_name):
    """
    Function to make predictions on uploaded audio files using the selected model
    """
    # Get the actual model filename from the friendly name
    model_file = model_choices[model_name]
    
    # Load the selected model
    model, scaler, le = load_model(model_file)
    
    # Initialize classifier for feature extraction only
    classifier = SoundClassifier(data_dir='data')
    
    # Extract features and predict
    features = classifier.extract_features(audio_file)
    features = features.reshape(1, -1)
    features_scaled = scaler.transform(features)
    prediction = model.predict(features_scaled)
    
    # Get the predicted label and format it
    predicted_label = le.inverse_transform(prediction)[0]
    formatted_label = format_issue(predicted_label)
    
    return f"Predicted Issue: {formatted_label}"

# Create Gradio interface
iface = gr.Interface(
    fn=predict_sound,
    inputs=[
        gr.Audio(type="filepath", label="Upload Sound File"),
        gr.Dropdown(choices=list(model_choices.keys()), label="Select Model Type", value=list(model_choices.keys())[0])
    ],
    outputs=gr.Textbox(label="Prediction"),
    title="Engine Sound Issue Classifier",
    description="Upload an audio file of engine sound to identify potential issues. Choose from different machine learning models.",
    examples=[
        [os.path.join("test_data", "air_filter_sample_5.wav"), list(model_choices.keys())[0]],
        [os.path.join("test_data", "cd_sample_16.wav"), list(model_choices.keys())[1]],
        [os.path.join("test_data", "vl_sample_4.wav"), list(model_choices.keys())[2]]
    ]
)

if __name__ == "__main__":
    iface.launch()