cbendale10's picture
Upload folder using huggingface_hub
28db3a6 verified
import pandas as pd
import streamlit as st
import joblib
from huggingface_hub import hf_hub_download
st.set_page_config(page_title="Predictive Maintenance", layout="centered")
st.markdown(
"""
<h2 style="font-size:24px; font-weight:600;">
🔧 Engine Predictive Maintenance – Fault Prediction
</h2>
""",
unsafe_allow_html=True
)
st.write(
"Enter live engine sensor readings to predict whether the engine is "
"**Normal (0)** or **Faulty (1)**."
)
# -------------------
# CONFIG (EDIT THIS)
# -------------------
MODEL_REPO_ID = "cbendale10/Capstone-Predictive-Maintenance-model" # <-- model repo
MODEL_FILENAME = "best_predictive_maintenance_model_v1.joblib" # <-- best model file name
# ✅ Must match training feature names EXACTLY
FEATURES = [
"engine_rpm",
"lub_oil_pressure",
"fuel_pressure",
"coolant_pressure",
"lub_oil_temp",
"coolant_temp",
]
@st.cache_resource
def load_model():
"""Download model from HF and load once per app session."""
model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=MODEL_FILENAME)
return joblib.load(model_path)
# Load model
model = load_model()
# -------------------
# INPUT UI
# -------------------
st.subheader("Sensor Inputs")
engine_rpm = st.number_input("Engine RPM", min_value=0.0, value=750.0, step=1.0)
lub_oil_pressure = st.number_input("Lub Oil Pressure", min_value=0.0, value=3.10, step=0.01)
fuel_pressure = st.number_input("Fuel Pressure", min_value=0.0, value=6.20, step=0.01)
coolant_pressure = st.number_input("Coolant Pressure", min_value=0.0, value=2.10, step=0.01)
lub_oil_temp = st.number_input("Lub Oil Temperature (°C)", min_value=0.0, value=76.80, step=0.01)
coolant_temp = st.number_input("Coolant Temperature (°C)", min_value=0.0, value=78.30, step=0.01)
# -------------------
# BUILD INPUT DF (Rubric requirement)
# -------------------
input_df = pd.DataFrame([{
"engine_rpm": engine_rpm,
"lub_oil_pressure": lub_oil_pressure,
"fuel_pressure": fuel_pressure,
"coolant_pressure": coolant_pressure,
"lub_oil_temp": lub_oil_temp,
"coolant_temp": coolant_temp,
}])
# Force correct column order (prevents feature-name mismatch)
input_df = input_df[FEATURES]
# Extra-safety: if model stores expected columns, align to them
if hasattr(model, "feature_names_in_"):
input_df = input_df.reindex(columns=list(model.feature_names_in_))
st.write("### Input DataFrame")
st.dataframe(input_df, use_container_width=True)
# -------------------
# PREDICT
# -------------------
if st.button("Predict Engine Condition"):
pred = int(model.predict(input_df)[0])
proba = None
if hasattr(model, "predict_proba"):
proba = float(model.predict_proba(input_df)[0, 1])
st.write("### Prediction Result")
if pred == 1:
st.error("⚠️ Engine Condition: **Faulty (1)** — Maintenance Recommended")
else:
st.success("✅ Engine Condition: **Normal (0)** — No Maintenance Required")
if proba is not None:
st.info(f"Fault probability: **{proba:.2f}**")
st.caption("Model loaded from Hugging Face Model Hub. Built for Capstone Predictive Maintenance.")