Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import joblib
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
import json
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
# =============================================================================
|
| 11 |
# STREAMLIT CONFIG
|
|
@@ -27,38 +28,21 @@ def load_artifacts():
|
|
| 27 |
repo_id = "SharleyK/predictive-maintenance-model"
|
| 28 |
token = os.getenv("HF_TOKEN")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
filename="best_model.pkl",
|
| 33 |
-
token=token
|
| 34 |
-
)
|
| 35 |
-
model = joblib.load(model_path)
|
| 36 |
-
|
| 37 |
-
scaler_path = hf_hub_download(
|
| 38 |
-
repo_id=repo_id,
|
| 39 |
-
filename="scaler.pkl",
|
| 40 |
-
token=token
|
| 41 |
-
)
|
| 42 |
-
scaler = joblib.load(scaler_path)
|
| 43 |
|
| 44 |
-
metadata_path = hf_hub_download(
|
| 45 |
-
|
| 46 |
-
filename="metadata.json",
|
| 47 |
-
token=token
|
| 48 |
-
)
|
| 49 |
-
with open(metadata_path, "r") as f:
|
| 50 |
-
metadata = json.load(f)
|
| 51 |
|
| 52 |
return model, scaler, metadata, True
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
st.error(f"❌ Error loading model
|
| 56 |
return None, None, {}, False
|
| 57 |
-
model, scaler, metadata, MODEL_LOADED = load_artifacts()
|
| 58 |
|
|
|
|
| 59 |
MODEL_NAME = metadata.get("model_name", "Predictive Maintenance Model")
|
| 60 |
|
| 61 |
-
|
| 62 |
# =============================================================================
|
| 63 |
# FEATURE ENGINEERING
|
| 64 |
# =============================================================================
|
|
@@ -93,8 +77,10 @@ def engineer_features(df):
|
|
| 93 |
# =============================================================================
|
| 94 |
|
| 95 |
def predict(df):
|
|
|
|
| 96 |
df_eng = engineer_features(df)
|
| 97 |
X_scaled = scaler.transform(df_eng)
|
|
|
|
| 98 |
pred = model.predict(X_scaled)[0]
|
| 99 |
|
| 100 |
if hasattr(model, "predict_proba"):
|
|
@@ -104,33 +90,129 @@ def predict(df):
|
|
| 104 |
|
| 105 |
return pred, confidence
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# =============================================================================
|
| 108 |
# UI
|
| 109 |
# =============================================================================
|
| 110 |
|
| 111 |
-
st.title("🛠️ Predictive Maintenance
|
| 112 |
-
st.caption("
|
| 113 |
|
| 114 |
-
st.
|
| 115 |
-
**Model:** {MODEL_NAME}
|
| 116 |
-
**Accuracy:** 66.68% | **Recall:** 87.13%
|
| 117 |
-
""")
|
| 118 |
|
| 119 |
st.divider()
|
| 120 |
|
| 121 |
-
col1, col2 = st.columns(
|
| 122 |
|
| 123 |
with col1:
|
| 124 |
-
engine_rpm = st.slider("Engine RPM", 50, 2500, 800
|
|
|
|
|
|
|
| 125 |
lub_oil_pressure = st.slider("Lub Oil Pressure", 0.0, 8.0, 3.3)
|
|
|
|
|
|
|
| 126 |
fuel_pressure = st.slider("Fuel Pressure", 0.0, 22.0, 6.5)
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
coolant_pressure = st.slider("Coolant Pressure", 0.0, 8.0, 2.3)
|
| 130 |
-
lub_oil_temp = st.slider("Lub Oil Temp (°C)", 70.0, 95.0, 77.0)
|
| 131 |
-
coolant_temp = st.slider("Coolant Temp (°C)", 60.0, 200.0, 78.0)
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
df = pd.DataFrame([{
|
| 135 |
"engine_rpm": engine_rpm,
|
| 136 |
"lub_oil_pressure": lub_oil_pressure,
|
|
@@ -142,9 +224,41 @@ if st.button("Predict Engine Condition"):
|
|
| 142 |
|
| 143 |
pred, conf = predict(df)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
st.metric("Confidence", f"{conf*100:.1f}%")
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
import json
|
| 8 |
import os
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
|
| 11 |
# =============================================================================
|
| 12 |
# STREAMLIT CONFIG
|
|
|
|
| 28 |
repo_id = "SharleyK/predictive-maintenance-model"
|
| 29 |
token = os.getenv("HF_TOKEN")
|
| 30 |
|
| 31 |
+
model = joblib.load(hf_hub_download(repo_id, "best_model.pkl", token=token))
|
| 32 |
+
scaler = joblib.load(hf_hub_download(repo_id, "scaler.pkl", token=token))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
metadata_path = hf_hub_download(repo_id, "metadata.json", token=token)
|
| 35 |
+
metadata = json.load(open(metadata_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
return model, scaler, metadata, True
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
+
st.error(f"❌ Error loading model: {e}")
|
| 41 |
return None, None, {}, False
|
|
|
|
| 42 |
|
| 43 |
+
model, scaler, metadata, MODEL_LOADED = load_artifacts()
|
| 44 |
MODEL_NAME = metadata.get("model_name", "Predictive Maintenance Model")
|
| 45 |
|
|
|
|
| 46 |
# =============================================================================
|
| 47 |
# FEATURE ENGINEERING
|
| 48 |
# =============================================================================
|
|
|
|
| 77 |
# =============================================================================
|
| 78 |
|
| 79 |
def predict(df):
|
| 80 |
+
|
| 81 |
df_eng = engineer_features(df)
|
| 82 |
X_scaled = scaler.transform(df_eng)
|
| 83 |
+
|
| 84 |
pred = model.predict(X_scaled)[0]
|
| 85 |
|
| 86 |
if hasattr(model, "predict_proba"):
|
|
|
|
| 90 |
|
| 91 |
return pred, confidence
|
| 92 |
|
| 93 |
+
# =============================================================================
|
| 94 |
+
# VISUALS
|
| 95 |
+
# =============================================================================
|
| 96 |
+
|
| 97 |
+
def gauge_chart(confidence):
|
| 98 |
+
|
| 99 |
+
fig = go.Figure(go.Indicator(
|
| 100 |
+
mode="gauge+number",
|
| 101 |
+
value=confidence * 100,
|
| 102 |
+
title={'text': "Confidence %"},
|
| 103 |
+
gauge={
|
| 104 |
+
'axis': {'range': [0, 100]},
|
| 105 |
+
'steps': [
|
| 106 |
+
{'range': [0, 50], 'color': 'lightgreen'},
|
| 107 |
+
{'range': [50, 70], 'color': 'yellow'},
|
| 108 |
+
{'range': [70, 85], 'color': 'orange'},
|
| 109 |
+
{'range': [85, 100], 'color': 'red'}
|
| 110 |
+
],
|
| 111 |
+
}
|
| 112 |
+
))
|
| 113 |
+
|
| 114 |
+
fig.update_layout(height=300)
|
| 115 |
+
return fig
|
| 116 |
+
|
| 117 |
+
def radar_chart(params):
|
| 118 |
+
|
| 119 |
+
categories = [
|
| 120 |
+
'RPM',
|
| 121 |
+
'Oil Pressure',
|
| 122 |
+
'Fuel Pressure',
|
| 123 |
+
'Coolant Pressure',
|
| 124 |
+
'Oil Temp',
|
| 125 |
+
'Coolant Temp'
|
| 126 |
+
]
|
| 127 |
+
|
| 128 |
+
values = [
|
| 129 |
+
params["engine_rpm"]/25,
|
| 130 |
+
params["lub_oil_pressure"]/0.08,
|
| 131 |
+
params["fuel_pressure"]/0.22,
|
| 132 |
+
params["coolant_pressure"]/0.08,
|
| 133 |
+
params["lub_oil_temp"],
|
| 134 |
+
params["coolant_temp"]/2
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
fig = go.Figure()
|
| 138 |
+
|
| 139 |
+
fig.add_trace(go.Scatterpolar(
|
| 140 |
+
r=values,
|
| 141 |
+
theta=categories,
|
| 142 |
+
fill='toself'
|
| 143 |
+
))
|
| 144 |
+
|
| 145 |
+
return fig
|
| 146 |
+
|
| 147 |
+
# =============================================================================
|
| 148 |
+
# RECOMMENDATIONS
|
| 149 |
+
# =============================================================================
|
| 150 |
+
|
| 151 |
+
def recommendations(pred, rpm, oil_p, fuel_p, coolant_p, oil_t, coolant_t):
|
| 152 |
+
|
| 153 |
+
rec = []
|
| 154 |
+
|
| 155 |
+
if pred == 1:
|
| 156 |
+
rec.append("### 🚨 Maintenance Recommended")
|
| 157 |
+
|
| 158 |
+
if oil_p < 2.5:
|
| 159 |
+
rec.append("• Check oil pump / leaks")
|
| 160 |
+
|
| 161 |
+
if coolant_t > 85:
|
| 162 |
+
rec.append("• Cooling system inspection needed")
|
| 163 |
+
|
| 164 |
+
if rpm < 600:
|
| 165 |
+
rec.append("• Engine load or fuel intake check")
|
| 166 |
+
|
| 167 |
+
if fuel_p > 8:
|
| 168 |
+
rec.append("• Possible fuel regulator issue")
|
| 169 |
+
|
| 170 |
+
else:
|
| 171 |
+
rec.append("### ✅ Engine Healthy")
|
| 172 |
+
rec.append("• Continue routine maintenance")
|
| 173 |
+
rec.append("• Monitor coolant weekly")
|
| 174 |
+
|
| 175 |
+
return "\n".join(rec)
|
| 176 |
+
|
| 177 |
# =============================================================================
|
| 178 |
# UI
|
| 179 |
# =============================================================================
|
| 180 |
|
| 181 |
+
st.title("🛠️ Predictive Maintenance Dashboard")
|
| 182 |
+
st.caption("Real-time AI Engine Health Monitoring")
|
| 183 |
|
| 184 |
+
st.info(f"Model: {MODEL_NAME}")
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
st.divider()
|
| 187 |
|
| 188 |
+
col1, col2, col3 = st.columns(3)
|
| 189 |
|
| 190 |
with col1:
|
| 191 |
+
engine_rpm = st.slider("Engine RPM", 50, 2500, 800)
|
| 192 |
+
|
| 193 |
+
with col2:
|
| 194 |
lub_oil_pressure = st.slider("Lub Oil Pressure", 0.0, 8.0, 3.3)
|
| 195 |
+
|
| 196 |
+
with col3:
|
| 197 |
fuel_pressure = st.slider("Fuel Pressure", 0.0, 22.0, 6.5)
|
| 198 |
|
| 199 |
+
col4, col5, col6 = st.columns(3)
|
| 200 |
+
|
| 201 |
+
with col4:
|
| 202 |
coolant_pressure = st.slider("Coolant Pressure", 0.0, 8.0, 2.3)
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
with col5:
|
| 205 |
+
lub_oil_temp = st.slider("Lub Oil Temp", 70.0, 95.0, 77.0)
|
| 206 |
+
|
| 207 |
+
with col6:
|
| 208 |
+
coolant_temp = st.slider("Coolant Temp", 60.0, 200.0, 78.0)
|
| 209 |
+
|
| 210 |
+
# =============================================================================
|
| 211 |
+
# PREDICT BUTTON
|
| 212 |
+
# =============================================================================
|
| 213 |
+
|
| 214 |
+
if st.button("🔮 Predict Engine Condition"):
|
| 215 |
+
|
| 216 |
df = pd.DataFrame([{
|
| 217 |
"engine_rpm": engine_rpm,
|
| 218 |
"lub_oil_pressure": lub_oil_pressure,
|
|
|
|
| 224 |
|
| 225 |
pred, conf = predict(df)
|
| 226 |
|
| 227 |
+
colA, colB = st.columns(2)
|
| 228 |
+
|
| 229 |
+
with colA:
|
| 230 |
+
if pred == 1:
|
| 231 |
+
st.error("🚨 Faulty Engine Detected")
|
| 232 |
+
else:
|
| 233 |
+
st.success("✅ Engine Healthy")
|
| 234 |
+
|
| 235 |
+
st.metric("Confidence", f"{conf*100:.2f}%")
|
| 236 |
+
|
| 237 |
+
with colB:
|
| 238 |
+
st.plotly_chart(gauge_chart(conf), use_container_width=True)
|
| 239 |
+
|
| 240 |
+
st.divider()
|
| 241 |
+
|
| 242 |
+
st.subheader("📊 Engine Parameter Profile")
|
| 243 |
+
|
| 244 |
+
st.plotly_chart(
|
| 245 |
+
radar_chart(df.iloc[0]),
|
| 246 |
+
use_container_width=True
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
st.divider()
|
| 250 |
+
|
| 251 |
+
st.subheader("💡 Recommendations")
|
| 252 |
+
|
| 253 |
+
st.markdown(
|
| 254 |
+
recommendations(
|
| 255 |
+
pred,
|
| 256 |
+
engine_rpm,
|
| 257 |
+
lub_oil_pressure,
|
| 258 |
+
fuel_pressure,
|
| 259 |
+
coolant_pressure,
|
| 260 |
+
lub_oil_temp,
|
| 261 |
+
coolant_temp
|
| 262 |
+
)
|
| 263 |
+
)
|
| 264 |
|
|
|