import streamlit as st import pandas as pd import numpy as np import joblib from huggingface_hub import hf_hub_download import os MODEL_REPO_ID = "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 temp', 'Coolant temp'] @st.cache_resource def load_model_and_scaler(): """Downloads and loads the model and scaler from Hugging Face Hub.""" st.info("Loading model and scaler from Hugging Face Hub...") try: model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=MODEL_FILE) model = joblib.load(model_path) scaler_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=SCALER_FILE) scaler = joblib.load(scaler_path) st.success("Artifacts loaded successfully!") return model, scaler except Exception as e: st.error(f"Error loading artifacts from Hugging Face Hub: {e}") return None, None model, scaler = load_model_and_scaler() # Streamlit UI and Prediction Logic st.set_page_config(page_title="Predictive Maintenance", layout="wide") st.title("Engine Health Predictor") st.markdown("Use the sliders to simulate real-time sensor data and predict the **Engine Condition** (0=Healthy, 1=Faulty).") 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_temp = st.slider("Lub Oil Temp (°C)", min_value=71.0, max_value=90.0, value=78.0, step=0.1) Coolant_temp = st.slider("Coolant Temp (°C)", min_value=60.0, max_value=200.0, value=78.5, step=0.5) # Prediction if st.button("Predict Engine Condition", type="primary"): if model and scaler: input_data = 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] }, index=[0]) scaled_data = scaler.transform(input_data) prediction = model.predict(scaled_data)[0] prediction_proba = model.predict_proba(scaled_data)[0] # Display Results st.subheader("Prediction Result:") if prediction == 1: st.error(f"**FAULTY (Requires Maintenance)**") st.markdown(f"**Confidence (Faulty):** `{prediction_proba[1]*100:.2f}%`") st.warning("**Actionable Insight:** The model predicts a high risk of failure. Schedule maintenance immediately.") else: st.success(f"**HEALTHY (Normal Operation)**") st.markdown(f"**Confidence (Healthy):** `{prediction_proba[0]*100:.2f}%`") st.info("Engine is operating within normal parameters. Continue monitoring.")