Add app.py for Streamlit app deployment
Browse files
app.py
CHANGED
|
@@ -1 +1,102 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# ---------------------------------------
|
| 7 |
+
# CONFIG
|
| 8 |
+
# ---------------------------------------
|
| 9 |
+
MODEL_REPO_ID = "SunnyShaurya1981/engine-predictive-maintenance-model"
|
| 10 |
+
|
| 11 |
+
FILES = {
|
| 12 |
+
"model": "random_forest_model.joblib",
|
| 13 |
+
# No scaler, cat_cols, num_cols, processed_cols for this model as it's a simple RF without complex preprocessing artifacts
|
| 14 |
+
# If the chosen model requires them, these paths should be uncommented and defined.
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# ---------------------------------------
|
| 18 |
+
# LOAD ARTIFACTS
|
| 19 |
+
# ---------------------------------------
|
| 20 |
+
@st.cache_resource
|
| 21 |
+
def load_artifacts():
|
| 22 |
+
model = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["model"]))
|
| 23 |
+
# Uncomment and load other artifacts if they were part of your model pipeline
|
| 24 |
+
# scaler = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["scaler"]))
|
| 25 |
+
# categorical_cols = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["cat_cols"]))
|
| 26 |
+
# numerical_cols = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["num_cols"]))
|
| 27 |
+
# processed_columns = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["processed_cols"]))
|
| 28 |
+
return model # , scaler, categorical_cols, numerical_cols, processed_columns
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
model = load_artifacts() # Unpack other artifacts if returned
|
| 32 |
+
|
| 33 |
+
# ---------------------------------------
|
| 34 |
+
# STREAMLIT UI
|
| 35 |
+
# ---------------------------------------
|
| 36 |
+
st.set_page_config(page_title="Engine Predictive Maintenance", layout="centered")
|
| 37 |
+
st.title("⚙️ Engine Predictive Maintenance")
|
| 38 |
+
st.write("Predict if an engine is in a healthy or unhealthy condition based on sensor readings.")
|
| 39 |
+
|
| 40 |
+
with st.form("prediction_form"):
|
| 41 |
+
col1, col2 = st.columns(2)
|
| 42 |
+
|
| 43 |
+
with col1:
|
| 44 |
+
engine_rpm = st.number_input("Engine RPM", min_value=0, max_value=3000, value=750)
|
| 45 |
+
lub_oil_pressure = st.number_input("Lub Oil Pressure (MPa)", min_value=0.0, max_value=10.0, value=3.0, format="%.6f")
|
| 46 |
+
fuel_pressure = st.number_input("Fuel Pressure (MPa)", min_value=0.0, max_value=30.0, value=6.0, format="%.6f")
|
| 47 |
+
|
| 48 |
+
with col2:
|
| 49 |
+
coolant_pressure = st.number_input("Coolant Pressure (MPa)", min_value=0.0, max_value=10.0, value=2.5, format="%.6f")
|
| 50 |
+
lub_oil_temp = st.number_input("Lub Oil Temperature (C)", min_value=50.0, max_value=150.0, value=75.0, format="%.6f")
|
| 51 |
+
coolant_temp = st.number_input("Coolant Temperature (C)", min_value=50.0, max_value=150.0, value=80.0, format="%.6f")
|
| 52 |
+
|
| 53 |
+
submitted = st.form_submit_button("Predict Engine Condition")
|
| 54 |
+
|
| 55 |
+
# ---------------------------------------
|
| 56 |
+
# PREDICTION
|
| 57 |
+
# ---------------------------------------
|
| 58 |
+
if submitted:
|
| 59 |
+
# Create a DataFrame from the input values
|
| 60 |
+
input_data = pd.DataFrame([{
|
| 61 |
+
'Engine rpm': engine_rpm,
|
| 62 |
+
'Lub oil pressure': lub_oil_pressure,
|
| 63 |
+
'Fuel pressure': fuel_pressure,
|
| 64 |
+
'Coolant pressure': coolant_pressure,
|
| 65 |
+
'lub oil temp': lub_oil_temp,
|
| 66 |
+
'Coolant temp': coolant_temp
|
| 67 |
+
}])
|
| 68 |
+
|
| 69 |
+
# Note: For this specific Random Forest model, if no scaling or one-hot encoding was applied in training,
|
| 70 |
+
# then these steps are not needed here. Otherwise, include them based on your model_build_eval.py.
|
| 71 |
+
# Example of how you would apply preprocessing if needed:
|
| 72 |
+
# if scaler is not None:
|
| 73 |
+
# input_data[numerical_cols] = scaler.transform(input_data[numerical_cols])
|
| 74 |
+
# if categorical_cols is not None:
|
| 75 |
+
# input_data_encoded = pd.get_dummies(input_data, columns=categorical_cols, drop_first=True)
|
| 76 |
+
# input_data_processed = input_data_encoded.reindex(columns=processed_columns, fill_value=0)
|
| 77 |
+
# else:
|
| 78 |
+
# input_data_processed = input_data
|
| 79 |
+
|
| 80 |
+
# In this case, assuming the RF model takes raw numerical input directly (after data preparation steps)
|
| 81 |
+
# If your model_build_eval.py involved scaling, you would need to load and apply the scaler here.
|
| 82 |
+
# For this simplified Random Forest, we assume direct input without further preprocessing artifacts.
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
prediction = model.predict(input_data)[0]
|
| 86 |
+
prediction_proba = model.predict_proba(input_data)[0]
|
| 87 |
+
|
| 88 |
+
st.subheader("Prediction Result")
|
| 89 |
+
if prediction == 0:
|
| 90 |
+
st.success("✅ The engine is predicted to be in **Healthy** condition.")
|
| 91 |
+
st.metric("Confidence (Healthy)", f"{prediction_proba[0]*100:.2f}%")
|
| 92 |
+
st.metric("Confidence (Unhealthy)", f"{prediction_proba[1]*100:.2f}%")
|
| 93 |
+
else:
|
| 94 |
+
st.warning("⚠️ The engine is predicted to be in **Unhealthy** condition.")
|
| 95 |
+
st.metric("Confidence (Unhealthy)", f"{prediction_proba[1]*100:.2f}%")
|
| 96 |
+
st.metric("Confidence (Healthy)", f"{prediction_proba[0]*100:.2f}%")
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"An error occurred during prediction: {e}")
|
| 100 |
+
st.write("Please ensure all model artifacts are correctly loaded and input features match the model's expectations.")
|
| 101 |
+
|
| 102 |
+
st.caption("⚠️ ML-based prediction for decision support only.")
|