File size: 4,091 Bytes
3508743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import streamlit as st
import pandas as pd
import joblib
import matplotlib.pyplot as plt

# -----------------------------
# PAGE CONFIG
# -----------------------------
st.set_page_config(page_title="Engine Health Monitor", layout="wide")

st.title("πŸš— Engine Predictive Maintenance Dashboard")

st.write(
    "Predict engine health using sensor data. "
    "Adjust the decision threshold to balance false alarms vs missed failures."
)

# -----------------------------
# LOAD MODEL
# -----------------------------
@st.cache_resource
def load_model():
    return joblib.load("best_model.pkl")

model = load_model()
feature_names = model.feature_names_in_

# -----------------------------
# SIDEBAR SETTINGS
# -----------------------------
st.sidebar.header("βš™ Prediction Settings")

threshold = st.sidebar.slider(
    "Failure Decision Threshold",
    min_value=0.10,
    max_value=0.90,
    value=0.50,
    step=0.01,
    help="Lower β†’ detect more faults\nHigher β†’ reduce false alarms"
)

st.sidebar.markdown("---")
st.sidebar.write("**Engine Condition Meaning**")
st.sidebar.write("0 β†’ Normal")
st.sidebar.write("1 β†’ Faulty")

# -----------------------------
# SEVERITY FUNCTION
# -----------------------------
def get_severity(prob):
    if prob < 0.4:
        return "🟒 Low Risk"
    elif prob < 0.7:
        return "🟑 Moderate Risk"
    else:
        return "πŸ”΄ High Risk"

# -----------------------------
# MANUAL PREDICTION
# -----------------------------
st.header("πŸ”§ Manual Prediction")

cols = st.columns(3)
inputs = []

for i, feature in enumerate(feature_names):
    with cols[i % 3]:
        val = st.number_input(feature, value=0.0)
        inputs.append(val)

if st.button("Predict Engine Condition"):

    input_df = pd.DataFrame([inputs], columns=feature_names)

    prob = model.predict_proba(input_df)[0][1]
    prediction = 1 if prob >= threshold else 0
    severity = get_severity(prob)

    # Result
    if prediction == 1:
        st.error("⚠ Engine Likely Faulty")
    else:
        st.success("βœ… Engine Operating Normally")

    st.write(f"### Failure Probability: **{prob:.3f}**")
    st.write(f"### Severity Level: {severity}")

    # Gauge Chart
    fig, ax = plt.subplots()
    ax.barh(["Risk"], [prob])
    ax.set_xlim(0,1)
    ax.set_title("Failure Risk Level")
    st.pyplot(fig)

# -----------------------------
# BATCH PREDICTION
# -----------------------------
st.header("πŸ“‚ Batch Prediction")

uploaded_file = st.file_uploader("Upload CSV", type=["csv"])

if uploaded_file is not None:
    try:
        df = pd.read_csv(uploaded_file)

        if len(df) > 20000:
            st.warning("⚠ Maximum 10,000 rows allowed.")
        else:
            missing_cols = [col for col in feature_names if col not in df.columns]

            if missing_cols:
                st.error(f"Missing columns: {missing_cols}")
            else:
                df = df[feature_names]

                probs = model.predict_proba(df)[:, 1]
                df["Failure_Probability"] = probs
                df["Prediction"] = (probs >= threshold).astype(int)
                df["Severity"] = df["Failure_Probability"].apply(get_severity)

                st.success("βœ… Predictions completed")

                st.dataframe(df.head())

                csv = df.to_csv(index=False).encode("utf-8")

                st.download_button(
                    "Download Results",
                    csv,
                    "engine_predictions.csv",
                    "text/csv"
                )

    except Exception as e:
        st.error(f"Error: {e}")

# -----------------------------
# MODEL INFO
# -----------------------------
st.markdown("---")
st.subheader("πŸ“Š Model Information")

st.write("βœ” Algorithm: Random Forest")
st.write("βœ” Handles feature correlation & non-linearity")
st.write("βœ” Optimized for predictive maintenance")

st.info(
    "Tip: Lower threshold if missing failures is costly.\n"
    "Raise threshold if false alarms are costly."
)

st.markdown("---")
st.caption("Built for predictive maintenance monitoring")