import streamlit as st import pandas as pd from huggingface_hub import hf_hub_download import joblib # Download and load the best engine model from Hugging Face Model Hub. # The model is a scikit-learn compatible estimator saved with joblib. model_path = hf_hub_download( repo_id='Garg06/Predictive-Maintenance-Model', filename='best_engine_model.joblib' ) model = joblib.load(model_path) # ── App header ──────────────────────────────────────────────────────────── st.title('Engine Predictive Maintenance App') st.write(""" This application predicts whether an engine is **Faulty** or **Normal** based on real-time sensor readings. Enter the current sensor values below to get a prediction. """) # ── Sensor input fields ─────────────────────────────────────────────────── st.header('Engine Sensor Readings') # Engine RPM: observed range 61–2239, typical idle around 750–800 RPM. engine_rpm = st.number_input( 'Engine RPM', min_value=0.0, max_value=2500.0, value=800.0, step=1.0, help='Engine revolutions per minute. Typical idle: ~750 RPM.' ) # Lub oil pressure: observed range 0–7.27 bar. lub_oil_pressure = st.number_input( 'Lub Oil Pressure (bar)', min_value=0.0, max_value=10.0, value=3.3, step=0.01, help='Lubrication oil pressure in bar. Healthy range: 2.5–4.5 bar.' ) # Fuel pressure: observed range 0–21.14 bar. fuel_pressure = st.number_input( 'Fuel Pressure (bar)', min_value=0.0, max_value=25.0, value=6.7, step=0.01, help='Fuel system pressure in bar. Healthy range: 4.9–7.7 bar.' ) # Coolant pressure: observed range 0–7.48 bar. coolant_pressure = st.number_input( 'Coolant Pressure (bar)', min_value=0.0, max_value=10.0, value=2.3, step=0.01, help='Engine coolant system pressure in bar. Healthy range: 1.6–2.8 bar.' ) # Lub oil temperature: observed range 71–90 °C. lub_oil_temp = st.number_input( 'Lub Oil Temperature (°C)', min_value=60.0, max_value=100.0, value=77.6, step=0.1, help='Lubrication oil temperature in °C. Normal operating range: 75–80 °C.' ) # Coolant temperature: observed range 62–196 °C. coolant_temp = st.number_input( 'Coolant Temperature (°C)', min_value=60.0, max_value=200.0, value=78.4, step=0.1, help='Engine coolant temperature in °C. Normal range: 74–83 °C.' ) # ── Assemble raw sensor input ───────────────────────────────────────────── # Column names must exactly match those used during model training. 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, }]) # ── Feature engineering — must match prep.py exactly ───────────────────── # The model was trained on 12 features (6 raw + 6 engineered). # The saved Pipeline handles StandardScaling internally. eps = 1e-6 input_data['pressure_ratio_fuel_coolant'] = ( input_data['Fuel pressure'] / (input_data['Coolant pressure'] + eps) ) input_data['temp_delta_oil_coolant'] = ( input_data['lub oil temp'] - input_data['Coolant temp'] ) input_data['lub_pressure_per_rpm'] = ( (input_data['Lub oil pressure'] / (input_data['Engine rpm'] + eps)) * 1000 ) input_data['coolant_pressure_per_rpm'] = ( (input_data['Coolant pressure'] / (input_data['Engine rpm'] + eps)) * 1000 ) input_data['thermal_stress_index'] = ( (input_data['lub oil temp'] + input_data['Coolant temp']) / 2 ) input_data['pressure_diff_fuel_lub'] = ( input_data['Fuel pressure'] - input_data['Lub oil pressure'] ) # ── Prediction ──────────────────────────────────────────────────────────── if st.button('Predict Engine Condition'): prediction = model.predict(input_data)[0] probability = model.predict_proba(input_data)[0] if prediction == 1: st.error( f'⚠️ **Faulty Engine Detected!** ' f'Confidence: {probability[1]:.1%} ' f'\n\nImmediate inspection is recommended to prevent breakdown.' ) else: st.success( f'✅ **Engine is Normal.** ' f'Confidence: {probability[0]:.1%} ' f'\n\nAll sensor readings are within acceptable limits.' ) st.subheader('Input Summary') st.dataframe(input_data)