|
|
|
|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import joblib |
|
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
|
|
|
REPO_ID_MODEL = "RajendrakumarPachaiappan/engine-predictive-model" |
|
|
MODEL_FILE = "final_random_forest_model.joblib" |
|
|
SCALER_FILE = "standard_scaler.joblib" |
|
|
|
|
|
|
|
|
FEATURE_COLS = ['Engine_RPM', 'Lub_Oil_Pressure', 'Fuel_Pressure', |
|
|
'Coolant_Pressure', 'Lub_Oil_Temperature', 'Coolant_Temperature'] |
|
|
|
|
|
|
|
|
@st.cache_resource(show_spinner=False) |
|
|
def load_model_and_scaler(): |
|
|
""" |
|
|
Downloads and loads the model and scaler from Hugging Face Hub. |
|
|
Does NOT use st. commands inside to avoid initial warnings. |
|
|
""" |
|
|
try: |
|
|
|
|
|
model_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=MODEL_FILE) |
|
|
model = joblib.load(model_path) |
|
|
scaler_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=SCALER_FILE) |
|
|
scaler = joblib.load(scaler_path) |
|
|
return model, scaler |
|
|
except Exception as e: |
|
|
|
|
|
raise Exception(f"Failed to load required artifacts: {e}") |
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Predictive Maintenance", layout="wide") |
|
|
st.title("Engine Health Predictor ⚙️") |
|
|
|
|
|
|
|
|
st.info("Loading predictive model and scaler from Hugging Face Hub...") |
|
|
try: |
|
|
|
|
|
model, scaler = load_model_and_scaler() |
|
|
st.success("Artifacts loaded successfully! Ready for prediction.") |
|
|
|
|
|
st.markdown("Use the sliders to simulate real-time sensor data and predict the **Engine Condition** (0=Healthy, 1=Faulty).") |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
st.error(f"🔴 Error loading resources: {e}") |
|
|
st.stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
|
|
with col1: |
|
|
Engine_RPM = st.slider("Engine RPM (rev/min)", min_value=60, max_value=2300, value=791, step=10) |
|
|
Lub_Oil_Pressure = st.slider("Lub Oil Pressure (bar)", min_value=0.0, max_value=7.3, value=3.3, step=0.1) |
|
|
Fuel_Pressure = st.slider("Fuel Pressure (bar)", min_value=0.0, max_value=22.0, value=6.7, step=0.1) |
|
|
|
|
|
with col2: |
|
|
Coolant_Pressure = st.slider("Coolant Pressure (bar)", min_value=0.0, max_value=7.5, value=2.3, step=0.1) |
|
|
Lub_Oil_Temperature = st.slider("Lub Oil Temp (°C)", min_value=71.0, max_value=90.0, value=78.0, step=0.1) |
|
|
Coolant_Temperature = st.slider("Coolant Temp (°C)", min_value=60.0, max_value=200.0, value=78.5, step=0.5) |
|
|
|
|
|
|
|
|
if st.button("Predict Engine Condition", type="primary"): |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame({ |
|
|
'Engine_RPM': [Engine_RPM], |
|
|
'Lub_Oil_Pressure': [Lub_Oil_Pressure], |
|
|
'Fuel_Pressure': [Fuel_Pressure], |
|
|
'Coolant_Pressure': [Coolant_Pressure], |
|
|
'Lub_Oil_Temperature': [Lub_Oil_Temperature], |
|
|
'Coolant_Temperature': [Coolant_Temperature] |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input_scaled = scaler.transform(input_data[FEATURE_COLS]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_scaled)[0] |
|
|
|
|
|
prediction_proba = model.predict_proba(input_scaled)[:, 1][0] |
|
|
|
|
|
|
|
|
st.divider() |
|
|
st.subheader("Prediction Result:") |
|
|
|
|
|
if prediction == 1: |
|
|
st.error(f"Engine Condition: **FAULTY** (Probability of Fault: {prediction_proba:.2f}) ⚠️") |
|
|
st.write("Immediate maintenance is recommended to prevent breakdown.") |
|
|
else: |
|
|
st.success(f"Engine Condition: **HEALTHY** (Probability of Fault: {prediction_proba:.2f}) ✅") |
|
|
st.write("Engine is operating normally. Continue regular monitoring.") |
|
|
|
|
|
st.caption("Note: Probability of fault close to 0.5 indicates uncertainty.") |
|
|
|