| import streamlit as st |
| import pandas as pd |
| import joblib |
|
|
| |
| |
| |
| 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") |
|
|
| |
| |
| |
| @st.cache_resource |
| def load_model(): |
| return joblib.load("best_model.pkl") |
|
|
| model = load_model() |
|
|
| st.success("β
Model loaded successfully") |
|
|
| |
| features = [ |
| "Engine_RPM", |
| "Lub_Oil_Pressure", |
| "Fuel_Pressure", |
| "Coolant_Pressure", |
| "Lub_Oil_Temperature", |
| "Coolant_Temperature" |
| ] |
|
|
| |
| |
| |
| 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}%") |
|
|
| |
| |
| |
| 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()) |
|
|
| |
| 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" |
| }) |
|
|
| |
| 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) |
|
|
| |
| 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}") |
|
|
| |
| |
| |
| 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" |
| ) |
|
|
| |
| |
| |
| st.markdown("---") |
| st.caption("Engine Condition: 0 = Normal | 1 = Faulty") |
| st.caption("Built for predictive maintenance monitoring") |
|
|