File size: 3,183 Bytes
c1929b2
 
 
 
 
304b1ed
c1929b2
35ba780
 
 
 
 
 
 
 
304b1ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1929b2
 
 
 
304b1ed
c1929b2
 
 
304b1ed
 
c1929b2
 
304b1ed
 
 
c1929b2
 
 
304b1ed
 
 
 
 
c1929b2
304b1ed
 
 
c1929b2
304b1ed
 
 
 
 
 
c1929b2
 
304b1ed
 
 
 
 
 
 
c1929b2
304b1ed
28db3a6
c1929b2
304b1ed
 
 
c1929b2
304b1ed
 
c1929b2
 
304b1ed
c1929b2
 
304b1ed
 
c1929b2
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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.")