import streamlit as st import pandas as pd import joblib import os import xgboost as xgb # Import xgboost for model loading # Set page config st.set_page_config( page_title="Engine Condition Predictor", page_icon="⚙✨", layout="centered", initial_sidebar_state="expanded", ) # --- Paths to model and data --- MODEL_PATH = os.path.join(os.path.dirname(__file__), 'engine1/model', 'tuned_xgboost_model.json') # Update to .json # Create dummy model directory if it doesn't exist (for local testing) # In a real deployment, these paths would be correctly set within the Docker container # or deployment environment. if not os.path.exists(os.path.join(os.path.dirname(__file__), 'engine/model')): os.makedirs(os.path.join(os.path.dirname(__file__), 'engine/model'), exist_ok=True) # --- Load the Model --- @st.cache_resource def load_model(path): try: # Load model using native XGBoost method model = xgb.XGBClassifier() # Initialize an empty model model.load_model(path) # Load the saved parameters return model except Exception as e: st.error(f"Error loading model: {e}") return None model = load_model(MODEL_PATH) # --- Streamlit UI --- st.title("\u2699✨ Predictive Engine Maintenance") st.markdown("\n") st.markdown( "This application predicts whether an engine requires maintenance based on its sensor readings." ) st.markdown("\n") st.header("Engine Sensor Data Input") # Input fields for sensor data engine_rpm = st.slider("Engine RPM", 0, 2500, 750) lub_oil_pressure = st.slider("Lub Oil Pressure (bar/kPa)", 0.0, 10.0, 3.0, 0.1) fuel_pressure = st.slider("Fuel Pressure (bar/kPa)", 0.0, 25.0, 7.0, 0.1) coolant_pressure = st.slider("Coolant Pressure (bar/kPa)", 0.0, 10.0, 2.5, 0.1) lub_oil_temp = st.slider("Lub Oil Temperature (\u00b0C)", 60.0, 100.0, 77.0, 0.1) coolant_temp = st.slider("Coolant Temperature (\u00b0C)", 60.0, 200.0, 78.0, 0.1) # Create a DataFrame for prediction 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, } ] ) st.markdown("\n") if st.button("Predict Engine Condition", help="Click to predict if maintenance is required"): if model is not None: prediction = model.predict(input_data)[0] prediction_proba = model.predict_proba(input_data)[0][1] # Probability of 'Faulty' st.subheader("Prediction Results:") if prediction == 1: st.error(f"**Prediction: Engine requires maintenance!** ({prediction_proba:.2f} probability of being faulty)") st.markdown( "*Proactive intervention is recommended based on sensor readings.*" ) else: st.success(f"**Prediction: Engine is operating normally.** ({prediction_proba:.2f} probability of being faulty)") st.markdown( "*Continue regular monitoring. No immediate maintenance is indicated.*" ) else: st.warning("Model not loaded. Please check the model path and file.") st.sidebar.header("About") st.sidebar.info( "This application uses a trained XGBoost Classifier model to predict engine " "condition. Input sensor data using the sliders to get real-time predictions." ) st.sidebar.caption("Developed by Google Colab AI")