Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -8,14 +8,53 @@ st.set_page_config(page_title="Engine Predictive Maintenance", layout="centered"
|
|
| 8 |
st.title("Engine Predictive Maintenance")
|
| 9 |
st.write("Enter sensor values to predict whether maintenance is needed.")
|
| 10 |
|
| 11 |
-
# Model repo (public)
|
| 12 |
MODEL_REPO = "vinayakdnrdd/engine-pm-model"
|
| 13 |
MODEL_FILE = "engine_pm_model.joblib"
|
| 14 |
|
|
|
|
| 15 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 16 |
model = joblib.load(model_path)
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
engine_rpm = st.number_input("Engine RPM", value=1500.0)
|
| 20 |
lub_oil_pressure = st.number_input("Lub Oil Pressure", value=2.5)
|
| 21 |
fuel_pressure = st.number_input("Fuel Pressure", value=3.0)
|
|
@@ -23,26 +62,70 @@ coolant_pressure = st.number_input("Coolant Pressure", value=1.2)
|
|
| 23 |
lub_oil_temp = st.number_input("Lub Oil Temperature", value=85.0)
|
| 24 |
coolant_temp = st.number_input("Coolant Temperature", value=90.0)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if st.button("Predict"):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
proba = float(model.predict_proba(X)[0, 1])
|
| 42 |
pred = int(proba >= 0.5)
|
| 43 |
|
| 44 |
st.write(f"Probability (Maintenance Needed): **{proba:.2f}**")
|
| 45 |
-
|
| 46 |
if pred == 1:
|
| 47 |
st.error("⚠️ Prediction: **Maintenance Needed**")
|
| 48 |
else:
|
|
|
|
| 8 |
st.title("Engine Predictive Maintenance")
|
| 9 |
st.write("Enter sensor values to predict whether maintenance is needed.")
|
| 10 |
|
|
|
|
| 11 |
MODEL_REPO = "vinayakdnrdd/engine-pm-model"
|
| 12 |
MODEL_FILE = "engine_pm_model.joblib"
|
| 13 |
|
| 14 |
+
# Load model
|
| 15 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 16 |
model = joblib.load(model_path)
|
| 17 |
|
| 18 |
+
# ---- Helper: get expected input columns from sklearn Pipeline ----
|
| 19 |
+
def get_expected_columns(m):
|
| 20 |
+
# Best case: pipeline has feature_names_in_
|
| 21 |
+
cols = getattr(m, "feature_names_in_", None)
|
| 22 |
+
if cols is not None:
|
| 23 |
+
return list(cols)
|
| 24 |
+
|
| 25 |
+
# Typical: pipeline.named_steps["preprocess"] has feature_names_in_
|
| 26 |
+
pre = getattr(m, "named_steps", {}).get("preprocess", None)
|
| 27 |
+
if pre is not None:
|
| 28 |
+
cols = getattr(pre, "feature_names_in_", None)
|
| 29 |
+
if cols is not None:
|
| 30 |
+
return list(cols)
|
| 31 |
+
|
| 32 |
+
# Fallback: derive from transformers definition
|
| 33 |
+
exp = []
|
| 34 |
+
try:
|
| 35 |
+
for _, _, c in pre.transformers:
|
| 36 |
+
if isinstance(c, list):
|
| 37 |
+
exp.extend(c)
|
| 38 |
+
except Exception:
|
| 39 |
+
pass
|
| 40 |
+
# keep unique order
|
| 41 |
+
seen = set()
|
| 42 |
+
exp2 = []
|
| 43 |
+
for c in exp:
|
| 44 |
+
if c not in seen:
|
| 45 |
+
exp2.append(c); seen.add(c)
|
| 46 |
+
if exp2:
|
| 47 |
+
return exp2
|
| 48 |
+
|
| 49 |
+
# Last fallback: assume these common names
|
| 50 |
+
return ["Engine rpm", "Lub oil pressure", "Fuel pressure", "Coolant pressure", "Lub oil temp", "Coolant temp"]
|
| 51 |
+
|
| 52 |
+
expected_cols = get_expected_columns(model)
|
| 53 |
+
|
| 54 |
+
with st.expander("Debug (Expected input columns from model)"):
|
| 55 |
+
st.write(expected_cols)
|
| 56 |
+
|
| 57 |
+
# ---- UI inputs ----
|
| 58 |
engine_rpm = st.number_input("Engine RPM", value=1500.0)
|
| 59 |
lub_oil_pressure = st.number_input("Lub Oil Pressure", value=2.5)
|
| 60 |
fuel_pressure = st.number_input("Fuel Pressure", value=3.0)
|
|
|
|
| 62 |
lub_oil_temp = st.number_input("Lub Oil Temperature", value=85.0)
|
| 63 |
coolant_temp = st.number_input("Coolant Temperature", value=90.0)
|
| 64 |
|
| 65 |
+
# We store inputs with MANY aliases (case-insensitive matching)
|
| 66 |
+
inputs = {
|
| 67 |
+
"Engine rpm": engine_rpm,
|
| 68 |
+
"engine rpm": engine_rpm,
|
| 69 |
+
"Engine_RPM": engine_rpm,
|
| 70 |
+
"Engine_Rpm": engine_rpm,
|
| 71 |
+
|
| 72 |
+
"Lub oil pressure": lub_oil_pressure,
|
| 73 |
+
"lub oil pressure": lub_oil_pressure,
|
| 74 |
+
"Lub_Oil_Pressure": lub_oil_pressure,
|
| 75 |
+
|
| 76 |
+
"Fuel pressure": fuel_pressure,
|
| 77 |
+
"fuel pressure": fuel_pressure,
|
| 78 |
+
"Fuel_Pressure": fuel_pressure,
|
| 79 |
+
|
| 80 |
+
"Coolant pressure": coolant_pressure,
|
| 81 |
+
"coolant pressure": coolant_pressure,
|
| 82 |
+
"Coolant_Pressure": coolant_pressure,
|
| 83 |
+
|
| 84 |
+
"Lub oil temp": lub_oil_temp,
|
| 85 |
+
"lub oil temp": lub_oil_temp,
|
| 86 |
+
"Lub oil temperature": lub_oil_temp,
|
| 87 |
+
"lub oil temperature": lub_oil_temp,
|
| 88 |
+
"Lub_Oil_Temperature": lub_oil_temp,
|
| 89 |
+
|
| 90 |
+
"Coolant temp": coolant_temp,
|
| 91 |
+
"coolant temp": coolant_temp,
|
| 92 |
+
"Coolant temperature": coolant_temp,
|
| 93 |
+
"coolant temperature": coolant_temp,
|
| 94 |
+
"Coolant_Temperature": coolant_temp,
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
def find_value_for_col(colname: str):
|
| 98 |
+
# exact match
|
| 99 |
+
if colname in inputs:
|
| 100 |
+
return inputs[colname]
|
| 101 |
+
# normalized match
|
| 102 |
+
key = colname.strip().lower()
|
| 103 |
+
for k, v in inputs.items():
|
| 104 |
+
if k.strip().lower() == key:
|
| 105 |
+
return v
|
| 106 |
+
return None
|
| 107 |
+
|
| 108 |
if st.button("Predict"):
|
| 109 |
+
row = {}
|
| 110 |
+
missing = []
|
| 111 |
+
for col in expected_cols:
|
| 112 |
+
v = find_value_for_col(col)
|
| 113 |
+
if v is None:
|
| 114 |
+
missing.append(col)
|
| 115 |
+
else:
|
| 116 |
+
row[col] = v
|
| 117 |
+
|
| 118 |
+
if missing:
|
| 119 |
+
st.error("Model expects these columns but app couldn't map them:")
|
| 120 |
+
st.write(missing)
|
| 121 |
+
st.stop()
|
| 122 |
+
|
| 123 |
+
X = pd.DataFrame([row], columns=expected_cols)
|
| 124 |
+
|
| 125 |
proba = float(model.predict_proba(X)[0, 1])
|
| 126 |
pred = int(proba >= 0.5)
|
| 127 |
|
| 128 |
st.write(f"Probability (Maintenance Needed): **{proba:.2f}**")
|
|
|
|
| 129 |
if pred == 1:
|
| 130 |
st.error("⚠️ Prediction: **Maintenance Needed**")
|
| 131 |
else:
|