Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,57 +2,76 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
model
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.title("🚢 Engine Predictive Maintenance")
|
| 10 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
fuel_pressure = st.slider("Fuel Pressure", 2.0, 10.0, 5.0)
|
| 17 |
-
coolant_temp = st.slider("Coolant Temp", 60.0, 110.0, 80.0)
|
| 18 |
-
coolant_pressure = st.slider("Coolant Pressure", 1.0, 5.0, 2.5)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
eps = 1e-6
|
| 22 |
coolant_temp_pressure_interaction = coolant_temp * coolant_pressure
|
| 23 |
coolant_temp_pressure_ratio = coolant_temp / (coolant_pressure + eps)
|
| 24 |
lub_oil_temp_engine_rpm_interaction = lub_oil_temp * engine_rpm
|
| 25 |
fuel_pressure_engine_rpm_ratio = fuel_pressure / (engine_rpm + eps)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
# lub_oil_temp_engine_rpm_interaction, fuel_pressure_engine_rpm_ratio
|
| 31 |
-
input_df = pd.DataFrame([[
|
| 32 |
-
engine_rpm,
|
| 33 |
-
lub_oil_temp,
|
| 34 |
-
lub_oil_pressure,
|
| 35 |
-
fuel_pressure,
|
| 36 |
-
coolant_temp,
|
| 37 |
-
coolant_pressure,
|
| 38 |
-
coolant_temp_pressure_interaction,
|
| 39 |
-
coolant_temp_pressure_ratio,
|
| 40 |
-
lub_oil_temp_engine_rpm_interaction,
|
| 41 |
-
fuel_pressure_engine_rpm_ratio
|
| 42 |
-
]], columns=[
|
| 43 |
'engine_rpm', 'lub_oil_temp', 'lub_oil_pressure', 'fuel_pressure',
|
| 44 |
'coolant_temp', 'coolant_pressure', 'coolant_temp_pressure_interaction',
|
| 45 |
'coolant_temp_pressure_ratio', 'lub_oil_temp_engine_rpm_interaction',
|
| 46 |
'fuel_pressure_engine_rpm_ratio'
|
| 47 |
-
]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if prediction[0] == 1:
|
| 54 |
-
st.error("
|
|
|
|
| 55 |
else:
|
| 56 |
-
st.success("✅
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
+
# --- MODEL LOADING ---
|
| 8 |
+
# Fetches the model directly from your Hugging Face model repository
|
| 9 |
+
REPO_ID = "P-Mishra/engine-predictive-maintenance"
|
| 10 |
+
MODEL_FILENAME = "rf_predictive_maintenance.pkl"
|
| 11 |
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model():
|
| 14 |
+
try:
|
| 15 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 16 |
+
return joblib.load(model_path)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
st.error(f"Error loading model from Hub: {e}")
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
model = load_model()
|
| 22 |
+
|
| 23 |
+
# --- UI SETUP ---
|
| 24 |
+
st.set_page_config(page_title="Engine Health Monitor", page_icon="🚢")
|
| 25 |
st.title("🚢 Engine Predictive Maintenance")
|
| 26 |
+
st.write("Input sensor data to predict potential engine failure.")
|
| 27 |
+
|
| 28 |
+
# --- USER INPUTS ---
|
| 29 |
+
col1, col2 = st.columns(2)
|
| 30 |
+
|
| 31 |
+
with col1:
|
| 32 |
+
engine_rpm = st.number_input("Engine RPM", value=1200.0)
|
| 33 |
+
lub_oil_temp = st.number_input("Lubricating Oil Temp (°C)", value=85.0)
|
| 34 |
+
lub_oil_pressure = st.number_input("Lubricating Oil Pressure (bar)", value=4.5)
|
| 35 |
|
| 36 |
+
with col2:
|
| 37 |
+
fuel_pressure = st.number_input("Fuel Pressure (bar)", value=5.0)
|
| 38 |
+
coolant_temp = st.number_input("Coolant Temp (°C)", value=80.0)
|
| 39 |
+
coolant_pressure = st.number_input("Coolant Pressure (bar)", value=2.5)
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# --- FEATURE ENGINEERING (Must match notebook logic) ---
|
| 42 |
eps = 1e-6
|
| 43 |
coolant_temp_pressure_interaction = coolant_temp * coolant_pressure
|
| 44 |
coolant_temp_pressure_ratio = coolant_temp / (coolant_pressure + eps)
|
| 45 |
lub_oil_temp_engine_rpm_interaction = lub_oil_temp * engine_rpm
|
| 46 |
fuel_pressure_engine_rpm_ratio = fuel_pressure / (engine_rpm + eps)
|
| 47 |
|
| 48 |
+
# --- DATAFRAME CONSTRUCTION ---
|
| 49 |
+
# Important: Features must be in the exact order they were trained in your notebook
|
| 50 |
+
feature_columns = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
'engine_rpm', 'lub_oil_temp', 'lub_oil_pressure', 'fuel_pressure',
|
| 52 |
'coolant_temp', 'coolant_pressure', 'coolant_temp_pressure_interaction',
|
| 53 |
'coolant_temp_pressure_ratio', 'lub_oil_temp_engine_rpm_interaction',
|
| 54 |
'fuel_pressure_engine_rpm_ratio'
|
| 55 |
+
]
|
| 56 |
|
| 57 |
+
input_data = pd.DataFrame([[
|
| 58 |
+
engine_rpm, lub_oil_temp, lub_oil_pressure, fuel_pressure,
|
| 59 |
+
coolant_temp, coolant_pressure, coolant_temp_pressure_interaction,
|
| 60 |
+
coolant_temp_pressure_ratio, lub_oil_temp_engine_rpm_interaction,
|
| 61 |
+
fuel_pressure_engine_rpm_ratio
|
| 62 |
+
]], columns=feature_columns)
|
| 63 |
+
|
| 64 |
+
# --- PREDICTION ---
|
| 65 |
+
if st.button("Run Diagnostic"):
|
| 66 |
+
if model is not None:
|
| 67 |
+
prediction = model.predict(input_data)
|
| 68 |
+
|
| 69 |
+
st.divider()
|
| 70 |
if prediction[0] == 1:
|
| 71 |
+
st.error("### 🚨 Result: Maintenance Required")
|
| 72 |
+
st.write("The model has detected patterns indicating a high probability of engine failure.")
|
| 73 |
else:
|
| 74 |
+
st.success("### ✅ Result: Normal Operation")
|
| 75 |
+
st.write("Engine sensors are within safe operating parameters.")
|
| 76 |
+
else:
|
| 77 |
+
st.error("Model not available.")
|