Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
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({
|
| 11 |
+
"Fault Type": [0, 1, 2, 3],
|
| 12 |
+
"Fault Name": ["No Fault", "Rich Mixture", "Lean Mixture", "Low Voltage"],
|
| 13 |
+
"Conditions": [
|
| 14 |
+
"No Fault",
|
| 15 |
+
"Incorrect sensor performance, High fuel pressure, Defective injector, faulty pressure regulator, clogged air filter, clogged fuel return line",
|
| 16 |
+
"Incorrect sensor performance, low fuel pressure, defective injector, faulty pressure regulator",
|
| 17 |
+
"Worn spark plugs, faulty ignition cables, defective coil, faulty sensor wiring"
|
| 18 |
+
]
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
# Define input columns required by the model
|
| 22 |
+
required_columns = [
|
| 23 |
+
"MAP", "TPS", "Force", "Power", "RPM", "Fuel consumption L/H",
|
| 24 |
+
"Fuel consumption L/100KM", "Speed (km/h)", "CO", "HC", "CO2", "O2", "Lambda", "AFR"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def predict_fault(input_text, uploaded_csv):
|
| 28 |
+
if uploaded_csv is not None:
|
| 29 |
+
df = pd.read_csv(uploaded_csv)
|
| 30 |
+
else:
|
| 31 |
+
# Simulate the case where the text input would describe the dataset
|
| 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 |
+
# Predict
|
| 39 |
+
X = df[required_columns]
|
| 40 |
+
predictions = model.predict(X)
|
| 41 |
+
predicted_faults = np.argmax(predictions, axis=1)
|
| 42 |
+
|
| 43 |
+
# Attach fault description
|
| 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 |
+
return df[['Predicted Fault Type', 'Fault Name', 'Conditions']].head(10)
|
| 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"
|
| 62 |
+
"The model classifies faults into the following types:\n\n"
|
| 63 |
+
"| Fault Type | Fault Name | Conditions |\n"
|
| 64 |
+
"|------------|----------------|----------------------------------------------------------------------------|\n"
|
| 65 |
+
"| 0 | No fault | No Fault |\n"
|
| 66 |
+
"| 1 | Rich Mixture | Incorrect sensor performance, High fuel pressure, etc. |\n"
|
| 67 |
+
"| 2 | Lean Mixture | Incorrect sensor performance, low fuel pressure, etc. |\n"
|
| 68 |
+
"| 3 | Low Voltage | Worn spark plugs, faulty ignition cables, etc. |\n"
|
| 69 |
+
)
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
tensorflow
|