import streamlit as st import pandas as pd import joblib # ------------------------------- # PAGE CONFIG # ------------------------------- st.set_page_config( page_title="Engine Predictive Maintenance", page_icon="🚗", layout="wide" ) st.title("🚗 Engine Predictive Maintenance System") st.write("Predict engine health using sensor values") # ------------------------------- # LOAD MODEL (LOCAL FILE) # ------------------------------- @st.cache_resource def load_model(): return joblib.load("best_model.pkl") model = load_model() st.success("✅ Model loaded successfully") # Expected feature order features = [ "Engine_RPM", "Lub_Oil_Pressure", "Fuel_Pressure", "Coolant_Pressure", "Lub_Oil_Temperature", "Coolant_Temperature" ] # ========================================================= # 🔍 SINGLE PREDICTION # ========================================================= st.header("🔍 Single Engine Prediction") col1, col2 = st.columns(2) with col1: rpm = st.number_input("Engine RPM", min_value=0.0) oil_p = st.number_input("Lub Oil Pressure") fuel_p = st.number_input("Fuel Pressure") with col2: cool_p = st.number_input("Coolant Pressure") oil_t = st.number_input("Lub Oil Temperature") cool_t = st.number_input("Coolant Temperature") if st.button("Predict Engine Condition"): input_df = pd.DataFrame([[rpm, oil_p, fuel_p, cool_p, oil_t, cool_t]], columns=features) prediction = model.predict(input_df)[0] try: prob = model.predict_proba(input_df)[0][1] confidence = round(prob * 100, 2) except: confidence = None if prediction == 1: st.error("⚠ Engine Fault Detected") else: st.success("✅ Engine Operating Normally") if confidence is not None: st.write(f"**Fault Probability:** {confidence}%") # ========================================================= # 📂 BATCH PREDICTION # ========================================================= st.markdown("---") st.header("📂 Batch Prediction (CSV Upload)") st.write("Upload a CSV file containing engine sensor values.") st.code(", ".join(features)) uploaded_file = st.file_uploader("Upload CSV File", type=["csv"]) if uploaded_file is not None: try: batch_data = pd.read_csv(uploaded_file) st.subheader("📊 Uploaded Data Preview") st.dataframe(batch_data.head()) # Check required columns if not all(col in batch_data.columns for col in features): st.error("❌ CSV must contain these columns:") st.write(features) else: with st.spinner("Running predictions..."): predictions = model.predict(batch_data[features]) batch_data["Prediction"] = predictions batch_data["Status"] = batch_data["Prediction"].map({ 0: "Normal", 1: "Faulty" }) # Add probabilities if available try: probs = model.predict_proba(batch_data[features])[:, 1] batch_data["Fault_Probability_%"] = (probs * 100).round(2) except: pass st.success("✅ Batch prediction complete!") st.subheader("📈 Prediction Results") st.dataframe(batch_data) # Download results csv = batch_data.to_csv(index=False).encode("utf-8") st.download_button( label="📥 Download Predictions", data=csv, file_name="engine_predictions.csv", mime="text/csv" ) except Exception as e: st.error(f"Error processing file: {e}") # ========================================================= # 📄 SAMPLE CSV FORMAT # ========================================================= st.markdown("---") st.subheader("📄 Sample CSV Format") sample_df = pd.DataFrame(columns=features) st.dataframe(sample_df) csv_sample = sample_df.to_csv(index=False).encode("utf-8") st.download_button( label="Download Sample CSV", data=csv_sample, file_name="sample_format.csv", mime="text/csv" ) # ========================================================= # FOOTER # ========================================================= st.markdown("---") st.caption("Engine Condition: 0 = Normal | 1 = Faulty") st.caption("Built for predictive maintenance monitoring")