Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from tensorflow.keras.models import load_model
|
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model = load_model("Engine_Fault-small.h5")
|
|
|
|
| 8 |
|
| 9 |
# Fault Type Table
|
| 10 |
fault_table = pd.DataFrame({
|
|
@@ -24,38 +25,45 @@ required_columns = [
|
|
| 24 |
"Consumption L/100KM", "Speed", "CO", "HC", "CO2", "O2", "Lambda", "AFR"
|
| 25 |
]
|
| 26 |
|
| 27 |
-
def predict_fault(
|
| 28 |
-
if uploaded_csv is
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return "Please upload a CSV file for prediction. Input text is only for description."
|
| 33 |
|
| 34 |
missing_cols = set(required_columns) - set(df.columns)
|
| 35 |
if missing_cols:
|
| 36 |
-
return f"Missing required columns: {', '.join(missing_cols)}"
|
| 37 |
|
| 38 |
-
#
|
| 39 |
X = df[required_columns]
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
predicted_faults = np.argmax(predictions, axis=1)
|
| 42 |
|
| 43 |
-
# Attach fault
|
| 44 |
df['Predicted Fault Type'] = predicted_faults
|
| 45 |
df = df.merge(fault_table, left_on='Predicted Fault Type', right_on='Fault Type', how='left')
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Gradio Interface
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=predict_fault,
|
| 51 |
inputs=[
|
| 52 |
-
gr.Textbox(label="Dataset Description (Optional)", placeholder="Describe the dataset here..."),
|
| 53 |
gr.File(label="Upload CSV File", file_types=[".csv"])
|
| 54 |
],
|
| 55 |
outputs=[
|
| 56 |
-
gr.Dataframe(label="Predicted Faults (Top 10 Rows)")
|
|
|
|
| 57 |
],
|
| 58 |
-
examples=[["This dataset consists of...", "sample.csv"]],
|
| 59 |
title="Engine Fault Prediction System",
|
| 60 |
description=(
|
| 61 |
"Upload a CSV file containing engine sensor data to predict engine fault types.\n\n"
|
|
|
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model = load_model("Engine_Fault-small.h5")
|
| 8 |
+
scaler = joblib.load("scaler.pkl")
|
| 9 |
|
| 10 |
# Fault Type Table
|
| 11 |
fault_table = pd.DataFrame({
|
|
|
|
| 25 |
"Consumption L/100KM", "Speed", "CO", "HC", "CO2", "O2", "Lambda", "AFR"
|
| 26 |
]
|
| 27 |
|
| 28 |
+
def predict_fault(uploaded_csv):
|
| 29 |
+
if uploaded_csv is None:
|
| 30 |
+
return "Please upload a CSV file for prediction.", None
|
| 31 |
+
|
| 32 |
+
df = pd.read_csv(uploaded_csv)
|
|
|
|
| 33 |
|
| 34 |
missing_cols = set(required_columns) - set(df.columns)
|
| 35 |
if missing_cols:
|
| 36 |
+
return f"Missing required columns: {', '.join(missing_cols)}", None
|
| 37 |
|
| 38 |
+
# Apply scaler
|
| 39 |
X = df[required_columns]
|
| 40 |
+
X_scaled = scaler.transform(X)
|
| 41 |
+
|
| 42 |
+
# Predict
|
| 43 |
+
predictions = model.predict(X_scaled)
|
| 44 |
predicted_faults = np.argmax(predictions, axis=1)
|
| 45 |
|
| 46 |
+
# Attach fault descriptions
|
| 47 |
df['Predicted Fault Type'] = predicted_faults
|
| 48 |
df = df.merge(fault_table, left_on='Predicted Fault Type', right_on='Fault Type', how='left')
|
| 49 |
+
|
| 50 |
+
# Create boxplot
|
| 51 |
+
fig, ax = plt.subplots(figsize=(12, 5))
|
| 52 |
+
df[required_columns].boxplot(ax=ax, rot=90)
|
| 53 |
+
plt.tight_layout()
|
| 54 |
+
|
| 55 |
+
return df[['Predicted Fault Type', 'Fault Name', 'Conditions']].head(10), fig
|
| 56 |
|
| 57 |
# Gradio Interface
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=predict_fault,
|
| 60 |
inputs=[
|
|
|
|
| 61 |
gr.File(label="Upload CSV File", file_types=[".csv"])
|
| 62 |
],
|
| 63 |
outputs=[
|
| 64 |
+
gr.Dataframe(label="Predicted Faults (Top 10 Rows)"),
|
| 65 |
+
gr.Plot(label="Sensor Data Boxplot")
|
| 66 |
],
|
|
|
|
| 67 |
title="Engine Fault Prediction System",
|
| 68 |
description=(
|
| 69 |
"Upload a CSV file containing engine sensor data to predict engine fault types.\n\n"
|