Upload folder using huggingface_hub
Browse files- DockerFile +5 -6
- app.py +115 -59
- requirements.txt +1 -1
DockerFile
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
# Use a slim Python base image
|
| 3 |
FROM python:3.9-slim
|
| 4 |
|
| 5 |
# Set the working directory in the container
|
|
@@ -12,9 +12,8 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|
| 12 |
# Copy the application file
|
| 13 |
COPY app.py .
|
| 14 |
|
| 15 |
-
# Expose the port (
|
| 16 |
-
EXPOSE
|
| 17 |
|
| 18 |
-
# Command to run the application
|
| 19 |
-
|
| 20 |
-
CMD ["python", "app.py"]
|
|
|
|
| 1 |
|
| 2 |
+
# Use a slim Python base image
|
| 3 |
FROM python:3.9-slim
|
| 4 |
|
| 5 |
# Set the working directory in the container
|
|
|
|
| 12 |
# Copy the application file
|
| 13 |
COPY app.py .
|
| 14 |
|
| 15 |
+
# Expose the port (Hugging Face Spaces default for Streamlit/Gradio is 8501 or 7860, but leaving it general is fine)
|
| 16 |
+
EXPOSE 8501
|
| 17 |
|
| 18 |
+
# Command to run the Streamlit application
|
| 19 |
+
CMD ["streamlit", "run", "app.py", "--server.port", "8501", "--server.enableCORS", "false", "--server.enableXsrfProtection", "false"]
|
|
|
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
|
| 2 |
-
import
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
# --- Hugging Face Model Repository Details ---
|
| 9 |
-
# Assuming your model and scaler are stored in this repo
|
| 10 |
REPO_ID_MODEL = "RajendrakumarPachaiappan/engine-predictive-model"
|
| 11 |
-
|
|
|
|
| 12 |
SCALER_FILENAME = "standard_scaler.joblib"
|
| 13 |
|
| 14 |
# The six feature columns for input
|
|
@@ -17,68 +17,124 @@ FEATURE_COLS = [
|
|
| 17 |
'Coolant_Pressure', 'Lub_Oil_Temperature', 'Coolant_Temperature'
|
| 18 |
]
|
| 19 |
|
| 20 |
-
# --- Load Model and Scaler ---
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=MODEL_FILENAME, repo_type="model")
|
| 24 |
-
model = joblib.load(model_path)
|
| 25 |
-
print(f"Model loaded successfully from {model_path}")
|
| 26 |
-
|
| 27 |
-
# Download and load the scaler
|
| 28 |
-
scaler_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=SCALER_FILENAME, repo_type="model")
|
| 29 |
-
scaler = joblib.load(scaler_path)
|
| 30 |
-
print(f"Scaler loaded successfully from {scaler_path}")
|
| 31 |
-
|
| 32 |
-
except Exception as e:
|
| 33 |
-
print(f"CRITICAL ERROR: Failed to load model or scaler. Check your repository ID and filenames. Error: {e}")
|
| 34 |
-
# In a real app, you might raise an error or use a dummy model
|
| 35 |
-
model, scaler = None, None
|
| 36 |
-
|
| 37 |
-
# --- Prediction Function ---
|
| 38 |
-
def predict_engine_condition(*args):
|
| 39 |
"""
|
| 40 |
-
|
|
|
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
prediction = model.predict(input_scaled)[0]
|
| 53 |
|
| 54 |
-
#
|
|
|
|
|
|
|
| 55 |
if prediction == 1:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.Number(label="Engine RPM (rev/min)", value=750),
|
| 65 |
-
gr.Number(label="Lub Oil Pressure (bar/kPa)", value=3.0),
|
| 66 |
-
gr.Number(label="Fuel Pressure (bar/kPa)", value=10.0),
|
| 67 |
-
gr.Number(label="Coolant Pressure (bar/kPa)", value=2.5),
|
| 68 |
-
gr.Number(label="Lub Oil Temperature (°C)", value=80.0),
|
| 69 |
-
gr.Number(label="Coolant Temperature (°C)", value=80.0),
|
| 70 |
-
]
|
| 71 |
-
|
| 72 |
-
# Define the Gradio Interface
|
| 73 |
-
iface = gr.Interface(
|
| 74 |
-
fn=predict_engine_condition,
|
| 75 |
-
inputs=input_components,
|
| 76 |
-
outputs=gr.Label(label="Engine Condition Prediction", show_label=True),
|
| 77 |
-
title="Engine Predictive Maintenance Model",
|
| 78 |
-
description="Input the engine sensor readings to predict if the engine is operating normally (0) or is faulty (1). The inputs will be scaled using the pre-trained standard scaler before prediction.",
|
| 79 |
-
allow_flagging="never"
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
# Launch the app
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
-
iface.launch()
|
|
|
|
| 1 |
|
| 2 |
+
import streamlit as st
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
# --- Hugging Face Model Repository Details ---
|
|
|
|
| 9 |
REPO_ID_MODEL = "RajendrakumarPachaiappan/engine-predictive-model"
|
| 10 |
+
# Assuming the best model found in the notebook was Random Forest based on your script
|
| 11 |
+
MODEL_FILENAME = "final_random_forest_model.joblib"
|
| 12 |
SCALER_FILENAME = "standard_scaler.joblib"
|
| 13 |
|
| 14 |
# The six feature columns for input
|
|
|
|
| 17 |
'Coolant_Pressure', 'Lub_Oil_Temperature', 'Coolant_Temperature'
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# --- Caching Function to Load Model and Scaler ---
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_artifacts():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
+
Downloads and loads the model and scaler from the Hugging Face Hub.
|
| 25 |
+
The @st.cache_resource decorator ensures this function only runs once.
|
| 26 |
"""
|
| 27 |
+
try:
|
| 28 |
+
with st.spinner("Downloading and loading model artifacts..."):
|
| 29 |
+
# Download and load the model
|
| 30 |
+
model_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=MODEL_FILENAME, repo_type="model")
|
| 31 |
+
model = joblib.load(model_path)
|
| 32 |
|
| 33 |
+
# Download and load the scaler
|
| 34 |
+
scaler_path = hf_hub_download(repo_id=REPO_ID_MODEL, filename=SCALER_FILENAME, repo_type="model")
|
| 35 |
+
scaler = joblib.load(scaler_path)
|
| 36 |
|
| 37 |
+
return model, scaler
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error(f"CRITICAL ERROR: Failed to load model or scaler. Check repository ID and filenames. Error: {e}")
|
| 40 |
+
return None, None
|
| 41 |
|
| 42 |
+
# Load the model and scaler
|
| 43 |
+
model, scaler = load_artifacts()
|
| 44 |
+
|
| 45 |
+
# --- Streamlit UI Setup ---
|
| 46 |
+
st.set_page_config(
|
| 47 |
+
page_title="Engine Predictive Maintenance",
|
| 48 |
+
layout="centered",
|
| 49 |
+
initial_sidebar_state="expanded"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
st.title("🚗 Engine Predictive Maintenance Model")
|
| 53 |
+
st.markdown(
|
| 54 |
+
"Use the sliders below to input the current engine sensor readings and predict "
|
| 55 |
+
"if the engine is running **NORMAL (0)** or requires **IMMEDIATE MAINTENANCE (1)**. "
|
| 56 |
+
"This model uses a pre-trained Random Forest classifier."
|
| 57 |
+
)
|
| 58 |
+
st.markdown(
|
| 59 |
+
"**Note:** The slider ranges are based on the minimum and maximum values observed in the training dataset (19,535 records)."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if model is None or scaler is None:
|
| 63 |
+
st.stop() # Stop the app if artifacts failed to load
|
| 64 |
+
|
| 65 |
+
# --- Input Components ---
|
| 66 |
+
st.header("Sensor Readings")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Define ranges based on the provided dataset statistics
|
| 70 |
+
# Format: (min_value, max_value, mean_value/default) - all values are floats.
|
| 71 |
+
ranges = {
|
| 72 |
+
'Engine_RPM': (61.0, 2239.0, 791.0),
|
| 73 |
+
'Lub_Oil_Pressure': (0.0, 7.3, 3.3),
|
| 74 |
+
'Fuel_Pressure': (0.0, 21.1, 6.7),
|
| 75 |
+
'Coolant_Pressure': (0.0, 7.5, 2.3),
|
| 76 |
+
'Lub_Oil_Temperature': (71.3, 89.6, 77.6),
|
| 77 |
+
'Coolant_Temperature': (61.7, 195.5, 78.4),
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
input_values = {}
|
| 81 |
+
# Use Streamlit columns for a cleaner, side-by-side layout
|
| 82 |
+
col1, col2 = st.columns(2)
|
| 83 |
+
columns = [col1, col2]
|
| 84 |
+
|
| 85 |
+
for i, col_name in enumerate(FEATURE_COLS):
|
| 86 |
+
# Determine which column to place the widget in
|
| 87 |
+
current_col = columns[i % 2]
|
| 88 |
+
|
| 89 |
+
min_val, max_val, default_val = ranges[col_name]
|
| 90 |
+
|
| 91 |
+
# Clean up names for display in the UI and set units
|
| 92 |
+
label = col_name.replace('_', ' ')
|
| 93 |
+
unit = ""
|
| 94 |
+
if "RPM" in col_name:
|
| 95 |
+
unit = " (rev/min)"
|
| 96 |
+
elif "Pressure" in col_name:
|
| 97 |
+
unit = " (bar/kPa)"
|
| 98 |
+
elif "Temperature" in col_name:
|
| 99 |
+
unit = " (°C)"
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
with current_col:
|
| 103 |
+
input_values[col_name] = st.slider(
|
| 104 |
+
label=f"{label}{unit}",
|
| 105 |
+
min_value=min_val,
|
| 106 |
+
max_value=max_val,
|
| 107 |
+
value=default_val,
|
| 108 |
+
step=0.1,
|
| 109 |
+
help=f"Current reading for {label}. Full data range: [{min_val}, {max_val}]"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# --- Prediction Logic ---
|
| 113 |
+
if st.button("Predict Engine Condition", type="primary"):
|
| 114 |
+
# 1. Prepare data for the model
|
| 115 |
+
# Convert input_values dictionary to a DataFrame row
|
| 116 |
+
input_df = pd.DataFrame([input_values], columns=FEATURE_COLS)
|
| 117 |
+
|
| 118 |
+
# 2. Scale the input data using the pre-trained StandardScaler
|
| 119 |
+
# The scaler must be fitted on the same data distribution as the model
|
| 120 |
+
input_scaled = scaler.transform(input_df)
|
| 121 |
+
|
| 122 |
+
# 3. Make prediction
|
| 123 |
+
# The output is a numpy array, we take the first element (the prediction)
|
| 124 |
prediction = model.predict(input_scaled)[0]
|
| 125 |
|
| 126 |
+
# 4. Display results
|
| 127 |
+
st.subheader("Prediction Result")
|
| 128 |
+
|
| 129 |
if prediction == 1:
|
| 130 |
+
st.error(
|
| 131 |
+
"🔴 FAULTY (1): Immediate Maintenance Required! "
|
| 132 |
+
"High probability of engine failure detected. Check for high RPM, low pressures, or extreme temperatures."
|
| 133 |
+
)
|
| 134 |
else:
|
| 135 |
+
st.success(
|
| 136 |
+
"🟢 NORMAL (0): Operating within expected parameters. "
|
| 137 |
+
"Engine health is currently good."
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
st.caption(f"Raw Model Prediction (0=Normal, 1=Faulty): {prediction}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -2,5 +2,5 @@ pandas
|
|
| 2 |
numpy
|
| 3 |
scikit-learn
|
| 4 |
joblib
|
| 5 |
-
|
| 6 |
huggingface-hub
|
|
|
|
| 2 |
numpy
|
| 3 |
scikit-learn
|
| 4 |
joblib
|
| 5 |
+
streamlit
|
| 6 |
huggingface-hub
|