Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from tensorflow.keras.models import load_model | |
import joblib | |
# Load the model | |
model = load_model("Engine_Fault-small.h5") | |
scaler = joblib.load("scaler.pkl") | |
# Fault Type Table | |
fault_table = pd.DataFrame({ | |
"Tipe Kerusakan": [0, 1, 2, 3], | |
"Nama Kerusakan": ["No Fault", "Rich Mixture", "Lean Mixture", "Low Voltage"], | |
"Kondisi": [ | |
"Tidak ada kerusakan", | |
"Performa sensor salah, Tekanan bahan bakar tinggi, Injektor rusak, Regulator tekanan rusak, Filter udara tersumbat, Saluran pengembalian bahan bakar tersumbat", | |
"Performa sensor salah, Tekanan bahan bakar rendah, Injektor rusak, Regulator tekanan rusak", | |
"Busi yang sudah aus, Kabel pengapian rusak, Kumparan rusak, Kabel sensor rusak" | |
] | |
}) | |
# Define input columns required by the model | |
required_columns = [ | |
"MAP", "TPS", "Force", "Power", "RPM", "Consumption L/H", | |
"Consumption L/100KM", "Speed", "CO", "HC", "CO2", "O2", "Lambda", "AFR" | |
] | |
def predict_fault(uploaded_csv): | |
if uploaded_csv is None: | |
return "Please upload a CSV file for prediction.", None | |
df = pd.read_csv(uploaded_csv) | |
missing_cols = set(required_columns) - set(df.columns) | |
if missing_cols: | |
return f"Missing required columns: {', '.join(missing_cols)}", None | |
# Scale input | |
X = df[required_columns] | |
X_scaled = scaler.transform(X) | |
# Predict | |
predictions = model.predict(X_scaled) | |
predicted_faults = np.argmax(predictions, axis=1) | |
# Merge fault descriptions | |
df['Predicted Fault Type'] = predicted_faults | |
df = df.merge(fault_table, left_on='Predicted Fault Type', right_on='Tipe Kerusakan', how='left') | |
# Plot bar chart | |
fig, ax = plt.subplots() | |
fault_counts = df['Predicted Fault Type'].value_counts().sort_index() | |
fault_counts.plot(kind='bar', ax=ax) | |
ax.set_title("Jumlah Tipe Fault yang Diprediksi") | |
ax.set_xlabel("Tipe Fault") | |
ax.set_ylabel("Jumlah") | |
ax.set_xticks(range(4)) | |
ax.set_xticklabels(["No Fault", "Rich", "Lean", "Low Volt"], rotation=0) | |
plt.tight_layout() | |
# Return output | |
return df[['Predicted Fault Type', 'Nama Kerusakan', 'Kondisi']].head(10), fig | |
# Gradio Interface | |
demo = gr.Interface( | |
fn=predict_fault, | |
inputs=[ | |
gr.File(label="Upload CSV File", file_types=[".csv"]) | |
], | |
outputs=[ | |
gr.Dataframe(label="Predicted Faults (Top 10 Rows)"), | |
gr.Plot(label="Bar Chart") | |
], | |
title="Sistem Prediksi Kerusakan Mesin Otomotif", | |
description=( | |
"Upload file CSV yang berisikan data sensor mesin untuk memprediksi kerusakan.\n\n" | |
"Model mengklasifikasi kerusakan berdasarkan kondisi berikut:\n\n" | |
"**Catatan penting:** File CSV *wajib* memiliki kolom-kolom berikut (harus sesuai urutan):\n" | |
"`MAP`, `TPS`, `Force`, `Power`, `RPM`, `Consumption L/H`, `Consumption L/100KM`, `Speed`, `CO`, `HC`, `CO2`, `O2`, `Lambda`, `AFR`\n\n" | |
"| Tipe Kerusakan | Nama Kerusakan | Kondisi |\n" | |
"|------------|----------------|----------------------------------------------------------------------------|\n" | |
"| 0 | No fault | Tidak ada kerusakan |\n" | |
"| 1 | Rich Mixture | Performa sensor salah, Tekanan bahan bakar tinggi, Injektor rusak, Regulator tekanan rusak, Filter udara tersumbat, Saluran pengembalian bahan bakar tersumbat |\n" | |
"| 2 | Lean Mixture | Performa sensor salah, Tekanan bahan bakar rendah, Injektor rusak, Regulator tekanan rusak |\n" | |
"| 3 | Low Voltage | Busi yang sudah aus, Kabel pengapian rusak, Kumparan rusak, Kabel sensor rusak |\n" | |
), | |
examples=[["sample.csv"]] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |