Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
dt_app.py
CHANGED
|
@@ -5,135 +5,116 @@ import joblib
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# --- Constants
|
| 9 |
-
# Use the exact repository ID from Code 4
|
| 10 |
HF_MODEL_REPO_ID = "ShaksML/dt-model-predective-maintenance"
|
| 11 |
-
# The model file was saved and uploaded as 'optimized_dt_predictor.pkl' in Code 4
|
| 12 |
HF_MODEL_FILENAME = "optimized_dt_predictor.pkl"
|
| 13 |
-
# Note: Since Code 4 trained a scikit-learn Pipeline (Scaler + DT), the pipeline handles preprocessing.
|
| 14 |
|
| 15 |
# --- Function to Load Model from Hugging Face ---
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model():
|
| 18 |
"""Downloads the model artifact from the Hugging Face Hub and loads it."""
|
| 19 |
try:
|
| 20 |
-
# Download the model file from the Hugging Face repository
|
| 21 |
model_path = hf_hub_download(
|
| 22 |
repo_id=HF_MODEL_REPO_ID,
|
| 23 |
filename=HF_MODEL_FILENAME,
|
| 24 |
-
repo_type="model",
|
| 25 |
local_dir=".",
|
| 26 |
local_dir_use_symlinks=False
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
# Load the model using joblib (which is the format used by Code 4)
|
| 30 |
-
model = joblib.load(model_path)
|
| 31 |
-
return model
|
| 32 |
except Exception as e:
|
| 33 |
-
st.error(f"Error
|
| 34 |
-
st.stop()
|
| 35 |
|
| 36 |
# --- Streamlit Application Layout ---
|
| 37 |
-
|
| 38 |
-
st.set_page_config(
|
| 39 |
-
page_title="DT Predictive Maintenance App",
|
| 40 |
-
layout="wide"
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
st.title("Decision Tree Engine Maintenance Dashboard")
|
| 44 |
-
st.subheader("Forecast potential engine failures using real-time sensor data.")
|
| 45 |
|
| 46 |
# Load the trained model
|
| 47 |
model = load_model()
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
# Assuming the column names are:
|
| 51 |
INPUT_COLUMNS = [
|
| 52 |
'Engine rpm', 'Lub oil pressure', 'Fuel pressure',
|
| 53 |
'Coolant pressure', 'lub oil temp', 'Coolant temp'
|
| 54 |
]
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
st.header("Enter Engine Sensor Readings")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
input_data = {}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
min_value=400, max_value=2000, value=790, step=10, key='rpm'
|
| 72 |
-
)
|
| 73 |
-
# Lub oil pressure (float)
|
| 74 |
-
input_data['Lub oil pressure'] = st.number_input(
|
| 75 |
-
"Lub Oil Pressure (bar)",
|
| 76 |
-
min_value=0.0, max_value=8.0, value=3.30, step=0.1, format="%.2f", key='lop'
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
input_data['Fuel pressure'] = st.number_input(
|
| 82 |
-
"Fuel Pressure (bar)",
|
| 83 |
-
min_value=0.0, max_value=25.0, value=6.60, step=0.1, format="%.2f", key='fp'
|
| 84 |
-
)
|
| 85 |
-
# Coolant pressure (float)
|
| 86 |
-
input_data['Coolant pressure'] = st.number_input(
|
| 87 |
-
"Coolant Pressure (bar)",
|
| 88 |
-
min_value=0.0, max_value=8.0, value=2.30, step=0.1, format="%.2f", key='cp'
|
| 89 |
-
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
"Lub Oil Temperature (°C)",
|
| 95 |
-
min_value=70.0, max_value=100.0, value=78.0, step=0.1, format="%.2f", key='lot'
|
| 96 |
-
)
|
| 97 |
-
# Coolant temp (float)
|
| 98 |
-
input_data['Coolant temp'] = st.number_input(
|
| 99 |
-
"Coolant Temperature (°C)",
|
| 100 |
-
min_value=70.0, max_value=110.0, value=78.0, step=0.1, format="%.2f", key='ct'
|
| 101 |
-
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
if
|
| 105 |
-
# 1.
|
| 106 |
input_df = pd.DataFrame([input_data])
|
| 107 |
|
| 108 |
-
# 2.
|
| 109 |
input_df = input_df[INPUT_COLUMNS]
|
| 110 |
|
| 111 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
try:
|
| 113 |
-
# Predict probability for both classes (0 and 1)
|
| 114 |
prediction_proba = model.predict_proba(input_df)[0]
|
| 115 |
-
# Prediction is the class index (0 or 1)
|
| 116 |
prediction = model.predict(input_df)[0]
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
st.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
-
st.error(f"An error occurred during prediction
|
| 137 |
-
|
|
|
|
| 138 |
else:
|
| 139 |
-
st.warning("
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# --- Constants ---
|
|
|
|
| 9 |
HF_MODEL_REPO_ID = "ShaksML/dt-model-predective-maintenance"
|
|
|
|
| 10 |
HF_MODEL_FILENAME = "optimized_dt_predictor.pkl"
|
|
|
|
| 11 |
|
| 12 |
# --- Function to Load Model from Hugging Face ---
|
| 13 |
@st.cache_resource
|
| 14 |
def load_model():
|
| 15 |
"""Downloads the model artifact from the Hugging Face Hub and loads it."""
|
| 16 |
try:
|
|
|
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id=HF_MODEL_REPO_ID,
|
| 19 |
filename=HF_MODEL_FILENAME,
|
| 20 |
+
repo_type="model",
|
| 21 |
local_dir=".",
|
| 22 |
local_dir_use_symlinks=False
|
| 23 |
)
|
| 24 |
+
return joblib.load(model_path)
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
+
st.error(f"System Error: Could not retrieve model. {e}")
|
| 27 |
+
st.stop()
|
| 28 |
|
| 29 |
# --- Streamlit Application Layout ---
|
| 30 |
+
st.set_page_config(page_title="Engine Health Monitor", layout="wide", page_icon="⚙️")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Load the trained model
|
| 33 |
model = load_model()
|
| 34 |
|
| 35 |
+
# Input columns (Original names as seen in the form)
|
|
|
|
| 36 |
INPUT_COLUMNS = [
|
| 37 |
'Engine rpm', 'Lub oil pressure', 'Fuel pressure',
|
| 38 |
'Coolant pressure', 'lub oil temp', 'Coolant temp'
|
| 39 |
]
|
| 40 |
|
| 41 |
+
# --- Sidebar UI for Inputs ---
|
| 42 |
+
st.sidebar.header("⚙️ Sensor Configuration")
|
| 43 |
+
st.sidebar.info("Adjust the parameters below to simulate engine conditions.")
|
|
|
|
| 44 |
|
| 45 |
+
input_data = {}
|
|
|
|
| 46 |
|
| 47 |
+
# Grouping inputs in the sidebar for a cleaner look
|
| 48 |
+
with st.sidebar.form("sensor_inputs_form"):
|
| 49 |
+
st.subheader("Pressure Readings")
|
| 50 |
+
input_data['Lub oil pressure'] = st.slider("Lub Oil Pressure (bar)", 0.0, 8.0, 3.30, 0.01)
|
| 51 |
+
input_data['Fuel pressure'] = st.slider("Fuel Pressure (bar)", 0.0, 25.0, 6.60, 0.01)
|
| 52 |
+
input_data['Coolant pressure'] = st.slider("Coolant Pressure (bar)", 0.0, 8.0, 2.30, 0.01)
|
| 53 |
|
| 54 |
+
st.subheader("Temperature & RPM")
|
| 55 |
+
input_data['Engine rpm'] = st.number_input("Engine RPM", 400, 2000, 790, 10)
|
| 56 |
+
input_data['lub oil temp'] = st.number_input("Lub Oil Temp (°C)", 70.0, 100.0, 78.0, 0.1)
|
| 57 |
+
input_data['Coolant temp'] = st.number_input("Coolant Temp (°C)", 70.0, 110.0, 78.0, 0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Submit Button
|
| 60 |
+
submit_button = st.form_submit_button("Run Diagnostics")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# --- Main Page UI ---
|
| 63 |
+
st.title("Predictive Maintenance System")
|
| 64 |
+
st.markdown("### 📊 Live Engine Analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
if model is not None:
|
| 67 |
+
if submit_button:
|
| 68 |
+
# 1. Create DataFrame with input data
|
| 69 |
input_df = pd.DataFrame([input_data])
|
| 70 |
|
| 71 |
+
# 2. Organize columns to match original input structure
|
| 72 |
input_df = input_df[INPUT_COLUMNS]
|
| 73 |
|
| 74 |
+
# 3. RENAME COLUMNS TO MATCH MODEL TRAINING DATA (*** CRITICAL FIX ***)
|
| 75 |
+
rename_map = {
|
| 76 |
+
'Engine rpm': 'Engine_RPM',
|
| 77 |
+
'Lub oil pressure': 'Lub_Oil_Pressure',
|
| 78 |
+
'Fuel pressure': 'Fuel_Pressure',
|
| 79 |
+
'Coolant pressure': 'Coolant_Pressure',
|
| 80 |
+
'lub oil temp': 'Lub_Oil_Temperature',
|
| 81 |
+
'Coolant temp': 'Coolant_Temperature'
|
| 82 |
+
}
|
| 83 |
+
input_df.rename(columns=rename_map, inplace=True)
|
| 84 |
+
|
| 85 |
+
# 4. Make Prediction
|
| 86 |
try:
|
|
|
|
| 87 |
prediction_proba = model.predict_proba(input_df)[0]
|
|
|
|
| 88 |
prediction = model.predict(input_df)[0]
|
| 89 |
|
| 90 |
+
st.divider()
|
| 91 |
+
|
| 92 |
+
# Use columns for a dashboard-like result display
|
| 93 |
+
col1, col2 = st.columns([2, 1])
|
| 94 |
+
|
| 95 |
+
with col1:
|
| 96 |
+
st.subheader("Diagnostic Report")
|
| 97 |
+
if prediction == 1:
|
| 98 |
+
st.error("⚠️ **CRITICAL ALERT: Engine Fault Detected**")
|
| 99 |
+
st.markdown("The system analysis indicates a high probability of component failure. Immediate inspection is recommended.")
|
| 100 |
+
else:
|
| 101 |
+
st.success("✅ **STATUS: Optimal**")
|
| 102 |
+
st.markdown("All systems are functioning within normal operational parameters.")
|
| 103 |
+
|
| 104 |
+
with col2:
|
| 105 |
+
st.subheader("Confidence Level")
|
| 106 |
+
if prediction == 1:
|
| 107 |
+
st.metric(label="Failure Probability", value=f"{prediction_proba[1]*100:.2f}%", delta="High Risk", delta_color="inverse")
|
| 108 |
+
else:
|
| 109 |
+
st.metric(label="Health Score", value=f"{prediction_proba[0]*100:.2f}%", delta="Stable")
|
| 110 |
+
|
| 111 |
+
# Expandable section for raw data
|
| 112 |
+
with st.expander("View Processed Sensor Telemetry"):
|
| 113 |
+
st.dataframe(input_df, hide_index=True, use_container_width=True)
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
+
st.error(f"An error occurred during prediction processing: {e}")
|
| 117 |
+
else:
|
| 118 |
+
st.info("👈 Please configure sensors in the sidebar and click 'Run Diagnostics' to begin.")
|
| 119 |
else:
|
| 120 |
+
st.warning("Model definition not found.")
|