Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import os
|
| 5 |
+
import sklearn # Explicit import prevents some joblib errors
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
# --- 1. PAGE CONFIG (MUST BE FIRST) ---
|
| 9 |
+
st.set_page_config(page_title="Engine Failure Prediction", page_icon="🚛")
|
| 10 |
+
|
| 11 |
+
# --- CONFIGURATION ---
|
| 12 |
+
HF_USERNAME = os.getenv("HF_USERNAME", "iStillWaters")
|
| 13 |
+
MODEL_REPO_NAME = os.getenv("MODEL_REPO_NAME", "auto_predictive_maintenance_model")
|
| 14 |
+
MODEL_REPO_ID = f"{HF_USERNAME}/{MODEL_REPO_NAME}"
|
| 15 |
+
|
| 16 |
+
MODEL_FILENAME = "best_engine_model.pkl"
|
| 17 |
+
SCALER_FILENAME = "scaler.joblib"
|
| 18 |
+
|
| 19 |
+
# CRITICAL: Must match the order in process_data.py exactly!
|
| 20 |
+
EXPECTED_FEATURES = [
|
| 21 |
+
'Engine rpm',
|
| 22 |
+
'Lub oil pressure',
|
| 23 |
+
'Fuel pressure',
|
| 24 |
+
'Coolant pressure',
|
| 25 |
+
'lub oil temp',
|
| 26 |
+
'Coolant temp'
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# --- LOAD MODEL & SCALER ---
|
| 30 |
+
@st.cache_resource
|
| 31 |
+
def load_artifacts():
|
| 32 |
+
print(f"Loading artifacts from {MODEL_REPO_ID}...")
|
| 33 |
+
try:
|
| 34 |
+
# Download Model
|
| 35 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=MODEL_FILENAME)
|
| 36 |
+
model = joblib.load(model_path)
|
| 37 |
+
|
| 38 |
+
# Download Scaler
|
| 39 |
+
scaler_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=SCALER_FILENAME)
|
| 40 |
+
scaler = joblib.load(scaler_path)
|
| 41 |
+
|
| 42 |
+
return model, scaler
|
| 43 |
+
except Exception as e:
|
| 44 |
+
# We cannot use st.error here easily if it's cached, so we print to logs
|
| 45 |
+
print(f"❌ Error loading artifacts: {e}")
|
| 46 |
+
return None, None
|
| 47 |
+
|
| 48 |
+
# Load them now
|
| 49 |
+
model, scaler = load_artifacts()
|
| 50 |
+
|
| 51 |
+
# --- UI LAYOUT ---
|
| 52 |
+
st.title("🚛 Engine Failure Prediction System")
|
| 53 |
+
st.markdown(f"**Model Source:** `{MODEL_REPO_ID}`")
|
| 54 |
+
st.markdown("Enter real-time sensor data to predict engine health status.")
|
| 55 |
+
|
| 56 |
+
# --- INPUT FORM ---
|
| 57 |
+
with st.form("prediction_form"):
|
| 58 |
+
st.subheader("Sensor Telemetry")
|
| 59 |
+
col1, col2 = st.columns(2)
|
| 60 |
+
|
| 61 |
+
with col1:
|
| 62 |
+
rpm = st.number_input("Engine RPM", min_value=0, max_value=10000, value=2000)
|
| 63 |
+
lub_oil_p = st.number_input("Lub Oil Pressure", min_value=0.0, max_value=10.0, value=4.5)
|
| 64 |
+
fuel_p = st.number_input("Fuel Pressure", min_value=0.0, max_value=20.0, value=7.0)
|
| 65 |
+
|
| 66 |
+
with col2:
|
| 67 |
+
coolant_p = st.number_input("Coolant Pressure", min_value=0.0, max_value=10.0, value=3.0)
|
| 68 |
+
lub_oil_t = st.number_input("Lub Oil Temp (°C)", min_value=0.0, max_value=150.0, value=75.0)
|
| 69 |
+
coolant_t = st.number_input("Coolant Temp (°C)", min_value=0.0, max_value=150.0, value=80.0)
|
| 70 |
+
|
| 71 |
+
submit_button = st.form_submit_button("Predict Engine Status")
|
| 72 |
+
|
| 73 |
+
# --- PREDICTION LOGIC ---
|
| 74 |
+
if submit_button:
|
| 75 |
+
if model is None or scaler is None:
|
| 76 |
+
st.error("Cannot predict: Model or Scaler not loaded. Check HF Space Logs.")
|
| 77 |
+
else:
|
| 78 |
+
# 1. Create Dataframe
|
| 79 |
+
input_data = pd.DataFrame({
|
| 80 |
+
'Engine rpm': [rpm],
|
| 81 |
+
'Lub oil pressure': [lub_oil_p],
|
| 82 |
+
'Fuel pressure': [fuel_p],
|
| 83 |
+
'Coolant pressure': [coolant_p],
|
| 84 |
+
'lub oil temp': [lub_oil_t],
|
| 85 |
+
'Coolant temp': [coolant_t]
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
# 2. Reorder Columns
|
| 89 |
+
input_data = input_data[EXPECTED_FEATURES]
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# 3. Scale
|
| 93 |
+
scaled_data = scaler.transform(input_data)
|
| 94 |
+
|
| 95 |
+
# 4. Predict
|
| 96 |
+
prediction = model.predict(scaled_data)[0]
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
probability = model.predict_proba(scaled_data)[0][1]
|
| 100 |
+
except:
|
| 101 |
+
probability = 0.0
|
| 102 |
+
|
| 103 |
+
# 5. Display
|
| 104 |
+
st.divider()
|
| 105 |
+
if prediction == 1:
|
| 106 |
+
st.error(f"🚨 CRITICAL WARNING: Engine Failure Predicted!")
|
| 107 |
+
st.write(f"**Confidence Level:** {probability:.2%}")
|
| 108 |
+
st.warning("Recommendation: Stop vehicle immediately and inspect cooling system.")
|
| 109 |
+
else:
|
| 110 |
+
st.success(f"✅ SYSTEM NORMAL: Engine is Healthy.")
|
| 111 |
+
st.write(f"**Failure Probability:** {probability:.2%}")
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
st.error(f"Prediction Error: {e}")
|