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+
return pd.read_csv("data/engine_data.csv")
|
| 8 |
+
except:
|
| 9 |
+
return pd.DataFrame(columns=[
|
| 10 |
+
"Engine rpm","Lub oil pressure","Fuel pressure","Coolant pressure","lub oil temp","Coolant temp","Engine condition"])
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
data=load_data()
|
| 14 |
+
|
| 15 |
+
#renaming columns for easy processing
|
| 16 |
+
data.columns = (data.columns
|
| 17 |
+
.str.strip()
|
| 18 |
+
.str.replace(" ","_")
|
| 19 |
+
.str.replace(r"[^\w]","_",regex=True)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
if data.empty:
|
| 23 |
+
new_id=1
|
| 24 |
+
else:
|
| 25 |
+
new_id=data["CustomerID"].max()+1
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Load Model
|
| 29 |
+
# -----------------------------
|
| 30 |
+
model = joblib.load("best_engine_PM_prediction_v1.joblib")
|
| 31 |
+
|
| 32 |
+
st.set_page_config(page_title="Engine Condition Predictor", layout="centered")
|
| 33 |
+
|
| 34 |
+
st.title("🔧 Engine Health Monitoring System")
|
| 35 |
+
st.write("Enter the engine sensor values below to predict engine condition")
|
| 36 |
+
|
| 37 |
+
# ---- User Inputs ----
|
| 38 |
+
with st.form("engine_input_form"):
|
| 39 |
+
|
| 40 |
+
engine_rpm = st.number_input(
|
| 41 |
+
"Engine RPM",
|
| 42 |
+
min_value=0,
|
| 43 |
+
max_value=10000,
|
| 44 |
+
value=1500,
|
| 45 |
+
step=50
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
lub_oil_pressure = st.number_input(
|
| 49 |
+
"Lub Oil Pressure (bar)",
|
| 50 |
+
min_value=0.0,
|
| 51 |
+
max_value=20.0,
|
| 52 |
+
value=3.5,
|
| 53 |
+
step=0.1
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
fuel_pressure = st.number_input(
|
| 57 |
+
"Fuel Pressure (bar)",
|
| 58 |
+
min_value=0.0,
|
| 59 |
+
max_value=20.0,
|
| 60 |
+
value=4.0,
|
| 61 |
+
step=0.1
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
coolant_pressure = st.number_input(
|
| 65 |
+
"Coolant Pressure (bar)",
|
| 66 |
+
min_value=0.0,
|
| 67 |
+
max_value=10.0,
|
| 68 |
+
value=1.5,
|
| 69 |
+
step=0.1
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
lub_oil_temp = st.number_input(
|
| 73 |
+
"Lub Oil Temperature (°C)",
|
| 74 |
+
min_value=0.0,
|
| 75 |
+
max_value=200.0,
|
| 76 |
+
value=85.0,
|
| 77 |
+
step=1.0
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
coolant_temp = st.number_input(
|
| 81 |
+
"Coolant Temperature (°C)",
|
| 82 |
+
min_value=0.0,
|
| 83 |
+
max_value=200.0,
|
| 84 |
+
value=90.0,
|
| 85 |
+
step=1.0
|
| 86 |
+
)
|
| 87 |
+
submit = st.form_submit_button("🚀 Predict Engine Condition")
|
| 88 |
+
|
| 89 |
+
# -----------------------------
|
| 90 |
+
# Predict Button
|
| 91 |
+
# -----------------------------
|
| 92 |
+
if st.button("Predict"):
|
| 93 |
+
|
| 94 |
+
input_df = pd.DataFrame({
|
| 95 |
+
"Engine_rpm": [engine_rpm],
|
| 96 |
+
"Lub_oil_pressure": [lub_oil_pressure],
|
| 97 |
+
"Fuel_pressure": [fuel_pressure],
|
| 98 |
+
"Coolant_pressure": [coolant_pressure],
|
| 99 |
+
"lub_oil_temp": [lub_oil_temp],
|
| 100 |
+
"Coolant_temp": [coolant_temp]
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
st.success("✅ Input captured successfully")
|
| 104 |
+
st.write("### Input Data")
|
| 105 |
+
st.dataframe(input_df)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# Predict
|
| 109 |
+
prediction = model.predict(input_df)[0]
|
| 110 |
+
prob = model.predict_proba(input_df)[0][1]
|
| 111 |
+
|
| 112 |
+
st.subheader("Prediction Result")
|
| 113 |
+
|
| 114 |
+
if prediction == 1:
|
| 115 |
+
st.success(f"Engine needs Preventive maintenance. Probability: {prob:.2f}")
|
| 116 |
+
else:
|
| 117 |
+
st.error(f"Engine working normal. Probability: {prob:.2f}")
|
| 118 |
+
|
| 119 |
+
# Save prediction to dataframe
|
| 120 |
+
input_df["Predicted_ProdTaken"] = prediction
|
| 121 |
+
input_df["Probability"] = round(prob, 4)
|
| 122 |
+
|
| 123 |
+
st.write("### Record to be saved:")
|
| 124 |
+
st.dataframe(pd.DataFrame([input_df]))
|
| 125 |
+
|
| 126 |
+
# -----------------------------
|
| 127 |
+
# SAVE RECORDS SECTION
|
| 128 |
+
# -----------------------------
|
| 129 |
+
if st.button("Save Record"):
|
| 130 |
+
file_path = "records.csv"
|
| 131 |
+
|
| 132 |
+
# If file exists → append
|
| 133 |
+
if os.path.exists(file_path):
|
| 134 |
+
existing_df = pd.read_csv(file_path)
|
| 135 |
+
updated_df = pd.concat([existing_df, pd.DataFrame([input_df])], ignore_index=True)
|
| 136 |
+
else:
|
| 137 |
+
# Create new CSV
|
| 138 |
+
updated_df = pd.DataFrame([input_data])
|
| 139 |
+
|
| 140 |
+
updated_df.to_csv(file_path, index=False)
|
| 141 |
+
|
| 142 |
+
st.success("Record saved successfully!")
|