Deploy predictive maintenance Streamlit application
Browse files- Dockerfile +15 -0
- app.py +82 -0
- hf_streamlit_space/Dockerfile +15 -0
- hf_streamlit_space/README.md +30 -0
- hf_streamlit_space/app.py +71 -0
- hf_streamlit_space/deployment_config.json +19 -0
- hf_streamlit_space/push_to_hf_space.py +16 -0
- hf_streamlit_space/requirements.txt +7 -0
- push_to_hf_space.py +32 -0
- requirements.txt +7 -0
- streamlit_hf_space/.streamlit/config.toml +7 -0
- streamlit_hf_space/Dockerfile +17 -0
- streamlit_hf_space/README.md +31 -0
- streamlit_hf_space/app.py +95 -0
- streamlit_hf_space/deployment_config.json +23 -0
- streamlit_hf_space/push_to_hf_space.py +33 -0
- streamlit_hf_space/requirements.txt +7 -0
- streamlit_hf_space/sample_input.csv +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt /app/requirements.txt
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r /app/requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY app.py /app/app.py
|
| 12 |
+
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
HF_MODEL_REPO_ID = os.getenv("HF_MODEL_REPO_ID", "premswan/engine-predictive-maintenance-model")
|
| 8 |
+
MODEL_FILENAME = "best_engine_maintenance_model.joblib"
|
| 9 |
+
FEATURE_COLUMNS = ["Engine_RPM", "Lub_Oil_Pressure", "Fuel_Pressure", "Coolant_Pressure", "Lub_Oil_Temperature", "Coolant_Temperature"]
|
| 10 |
+
|
| 11 |
+
LABEL_MAP = {
|
| 12 |
+
0: "Normal / Healthy",
|
| 13 |
+
1: "Maintenance Required / Faulty"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_model():
|
| 19 |
+
# Download and load the trained model from Hugging Face Model Hub.
|
| 20 |
+
token = os.getenv("HF_TOKEN")
|
| 21 |
+
model_path = hf_hub_download(
|
| 22 |
+
repo_id=HF_MODEL_REPO_ID,
|
| 23 |
+
filename=MODEL_FILENAME,
|
| 24 |
+
token=token
|
| 25 |
+
)
|
| 26 |
+
return joblib.load(model_path)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
MODEL = load_model()
|
| 30 |
+
|
| 31 |
+
st.set_page_config(
|
| 32 |
+
page_title="Engine Predictive Maintenance",
|
| 33 |
+
page_icon="🔧",
|
| 34 |
+
layout="wide"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
st.title("Engine Predictive Maintenance Classifier")
|
| 38 |
+
st.write(
|
| 39 |
+
"Enter engine sensor readings. The app loads the registered model from "
|
| 40 |
+
"Hugging Face Model Hub and predicts whether maintenance is required."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
with st.form("prediction_form"):
|
| 44 |
+
st.subheader("Sensor Inputs")
|
| 45 |
+
sensor_values = {}
|
| 46 |
+
cols = st.columns(2)
|
| 47 |
+
for idx, feature in enumerate(FEATURE_COLUMNS):
|
| 48 |
+
with cols[idx % 2]:
|
| 49 |
+
sensor_values[feature] = st.number_input(
|
| 50 |
+
label=feature,
|
| 51 |
+
value=0.0,
|
| 52 |
+
format="%.6f"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
submitted = st.form_submit_button("Predict Engine Condition")
|
| 56 |
+
|
| 57 |
+
if submitted:
|
| 58 |
+
# Rubric requirement: get inputs and save them into a DataFrame.
|
| 59 |
+
input_df = pd.DataFrame([sensor_values], columns=FEATURE_COLUMNS)
|
| 60 |
+
|
| 61 |
+
prediction = int(MODEL.predict(input_df)[0])
|
| 62 |
+
|
| 63 |
+
if hasattr(MODEL, "predict_proba"):
|
| 64 |
+
probability_maintenance = float(MODEL.predict_proba(input_df)[0, 1])
|
| 65 |
+
else:
|
| 66 |
+
probability_maintenance = None
|
| 67 |
+
|
| 68 |
+
prediction_label = LABEL_MAP.get(prediction, str(prediction))
|
| 69 |
+
|
| 70 |
+
st.subheader("Prediction Output")
|
| 71 |
+
st.metric("Predicted Engine Condition", prediction_label)
|
| 72 |
+
|
| 73 |
+
if probability_maintenance is not None:
|
| 74 |
+
st.metric("Probability of Maintenance/Faulty Class", "%.4f" % probability_maintenance)
|
| 75 |
+
|
| 76 |
+
if prediction == 1:
|
| 77 |
+
st.error("Recommended action: Schedule inspection or preventive maintenance before continued operation.")
|
| 78 |
+
else:
|
| 79 |
+
st.success("Recommended action: Continue normal operation and keep monitoring sensor readings.")
|
| 80 |
+
|
| 81 |
+
st.subheader("Input DataFrame Used for Inference")
|
| 82 |
+
st.dataframe(input_df, use_container_width=True)
|
hf_streamlit_space/Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt /app/requirements.txt
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r /app/requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY . /app
|
| 12 |
+
EXPOSE 8501
|
| 13 |
+
|
| 14 |
+
HEALTHCHECK CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8501/_stcore/health')" || exit 1
|
| 15 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
hf_streamlit_space/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Engine Predictive Maintenance
|
| 3 |
+
emoji: W
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.37.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Engine Predictive Maintenance Streamlit App
|
| 13 |
+
|
| 14 |
+
This Hugging Face Space hosts a Streamlit application for the predictive maintenance model.
|
| 15 |
+
|
| 16 |
+
## What the app does
|
| 17 |
+
|
| 18 |
+
1. Loads the trained model from `premswan/engine-predictive-maintenance-model`.
|
| 19 |
+
2. Accepts engine sensor inputs.
|
| 20 |
+
3. Converts the inputs into a pandas dataframe.
|
| 21 |
+
4. Predicts whether the engine is normal or requires maintenance.
|
| 22 |
+
5. Displays the predicted class and maintenance probability, if available.
|
| 23 |
+
|
| 24 |
+
## Model repository
|
| 25 |
+
|
| 26 |
+
`premswan/engine-predictive-maintenance-model`
|
| 27 |
+
|
| 28 |
+
## Dataset repository
|
| 29 |
+
|
| 30 |
+
`premswan/engine-predictive-maintenance-data`
|
hf_streamlit_space/app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
DEFAULT_MODEL_REPO_ID = "premswan/engine-predictive-maintenance-model"
|
| 8 |
+
MODEL_FILE = "best_engine_maintenance_model.joblib"
|
| 9 |
+
METADATA_FILE = "model_metadata.json"
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Engine Predictive Maintenance", page_icon="W", layout="centered")
|
| 12 |
+
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model_and_metadata(model_repo_id: str = DEFAULT_MODEL_REPO_ID):
|
| 15 |
+
# Load model and metadata from Hugging Face Model Hub.
|
| 16 |
+
model_path = hf_hub_download(repo_id=model_repo_id, filename=MODEL_FILE)
|
| 17 |
+
metadata_path = hf_hub_download(repo_id=model_repo_id, filename=METADATA_FILE)
|
| 18 |
+
model = joblib.load(model_path)
|
| 19 |
+
with open(metadata_path, "r", encoding="utf-8") as file:
|
| 20 |
+
metadata = json.load(file)
|
| 21 |
+
return model, metadata
|
| 22 |
+
|
| 23 |
+
model, metadata = load_model_and_metadata()
|
| 24 |
+
feature_columns = metadata.get("feature_columns", ['Engine_RPM', 'Lub_Oil_Pressure', 'Fuel_Pressure', 'Coolant_Pressure', 'Lub_Oil_Temperature', 'Coolant_Temperature'])
|
| 25 |
+
|
| 26 |
+
st.title("Engine Predictive Maintenance App")
|
| 27 |
+
st.write("Enter engine sensor readings to predict whether the engine is normal or needs maintenance attention.")
|
| 28 |
+
st.subheader("Sensor Inputs")
|
| 29 |
+
|
| 30 |
+
default_values = {
|
| 31 |
+
"Engine_RPM": 800.0,
|
| 32 |
+
"Lub_Oil_Pressure": 3.2,
|
| 33 |
+
"Fuel_Pressure": 6.5,
|
| 34 |
+
"Coolant_Pressure": 2.4,
|
| 35 |
+
"Lub_Oil_Temperature": 78.0,
|
| 36 |
+
"Coolant_Temperature": 80.0,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
user_inputs = {}
|
| 40 |
+
for feature in feature_columns:
|
| 41 |
+
user_inputs[feature] = st.number_input(
|
| 42 |
+
label=feature,
|
| 43 |
+
value=float(default_values.get(feature, 0.0)),
|
| 44 |
+
step=0.1,
|
| 45 |
+
format="%.4f"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Save inputs into a dataframe as required by the deployment rubric.
|
| 49 |
+
input_df = pd.DataFrame([user_inputs], columns=feature_columns)
|
| 50 |
+
st.subheader("Input DataFrame")
|
| 51 |
+
st.dataframe(input_df, use_container_width=True)
|
| 52 |
+
|
| 53 |
+
if st.button("Predict Engine Condition"):
|
| 54 |
+
prediction = int(model.predict(input_df)[0])
|
| 55 |
+
probability_maintenance = None
|
| 56 |
+
if hasattr(model, "predict_proba"):
|
| 57 |
+
probability_maintenance = float(model.predict_proba(input_df)[0, 1])
|
| 58 |
+
|
| 59 |
+
if prediction == 1:
|
| 60 |
+
st.error("Prediction: Maintenance / Faulty condition")
|
| 61 |
+
st.write("Recommended action: inspect engine health and schedule preventive maintenance.")
|
| 62 |
+
else:
|
| 63 |
+
st.success("Prediction: Normal / Healthy condition")
|
| 64 |
+
st.write("Recommended action: continue normal monitoring.")
|
| 65 |
+
|
| 66 |
+
if probability_maintenance is not None:
|
| 67 |
+
st.metric("Maintenance Probability", f"{probability_maintenance:.2%}")
|
| 68 |
+
|
| 69 |
+
st.write("Raw prediction output:", prediction)
|
| 70 |
+
|
| 71 |
+
st.caption("Model loaded from Hugging Face Model Hub: " + DEFAULT_MODEL_REPO_ID)
|
hf_streamlit_space/deployment_config.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_repo_id": "premswan/engine-predictive-maintenance-model",
|
| 3 |
+
"space_repo_id": "premswan/engine-predictive-maintenance-streamlit",
|
| 4 |
+
"dataset_repo_id": "premswan/engine-predictive-maintenance-data",
|
| 5 |
+
"app_file": "app.py",
|
| 6 |
+
"docker_base_image": "python:3.10-slim",
|
| 7 |
+
"streamlit_port": 8501,
|
| 8 |
+
"feature_columns": [
|
| 9 |
+
"Engine_RPM",
|
| 10 |
+
"Lub_Oil_Pressure",
|
| 11 |
+
"Fuel_Pressure",
|
| 12 |
+
"Coolant_Pressure",
|
| 13 |
+
"Lub_Oil_Temperature",
|
| 14 |
+
"Coolant_Temperature"
|
| 15 |
+
],
|
| 16 |
+
"target_column": "Engine_Condition",
|
| 17 |
+
"model_file": "best_engine_maintenance_model.joblib",
|
| 18 |
+
"metadata_file": "model_metadata.json"
|
| 19 |
+
}
|
hf_streamlit_space/push_to_hf_space.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import HfApi, login
|
| 4 |
+
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
HF_SPACE_ID = os.getenv("HF_SPACE_ID", "premswan/engine-predictive-maintenance-streamlit")
|
| 7 |
+
SPACE_FOLDER = Path(__file__).resolve().parent
|
| 8 |
+
|
| 9 |
+
if not HF_TOKEN:
|
| 10 |
+
raise ValueError("HF_TOKEN environment variable is required to push the Hugging Face Space.")
|
| 11 |
+
|
| 12 |
+
login(token=HF_TOKEN, add_to_git_credential=True)
|
| 13 |
+
api = HfApi(token=HF_TOKEN)
|
| 14 |
+
api.create_repo(repo_id=HF_SPACE_ID, repo_type="space", space_sdk="streamlit", exist_ok=True, private=False, token=HF_TOKEN)
|
| 15 |
+
api.upload_folder(folder_path=str(SPACE_FOLDER), repo_id=HF_SPACE_ID, repo_type="space", path_in_repo=".", commit_message="Deploy Streamlit predictive maintenance app", token=HF_TOKEN)
|
| 16 |
+
print(f"Deployment completed: https://huggingface.co/spaces/{HF_SPACE_ID}")
|
hf_streamlit_space/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
xgboost
|
| 6 |
+
joblib
|
| 7 |
+
huggingface_hub
|
push_to_hf_space.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import HfApi
|
| 4 |
+
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
SPACE_REPO_ID = os.getenv("HF_SPACE_REPO_ID", "premswan/engine-predictive-maintenance-space")
|
| 7 |
+
DEPLOYMENT_DIR = Path(__file__).resolve().parent
|
| 8 |
+
|
| 9 |
+
if not HF_TOKEN:
|
| 10 |
+
raise ValueError("HF_TOKEN environment variable is required to push files to Hugging Face Space.")
|
| 11 |
+
|
| 12 |
+
api = HfApi(token=HF_TOKEN)
|
| 13 |
+
|
| 14 |
+
api.create_repo(
|
| 15 |
+
repo_id=SPACE_REPO_ID,
|
| 16 |
+
repo_type="space",
|
| 17 |
+
space_sdk="docker",
|
| 18 |
+
exist_ok=True,
|
| 19 |
+
private=False,
|
| 20 |
+
token=HF_TOKEN
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
api.upload_folder(
|
| 24 |
+
folder_path=str(DEPLOYMENT_DIR),
|
| 25 |
+
repo_id=SPACE_REPO_ID,
|
| 26 |
+
repo_type="space",
|
| 27 |
+
path_in_repo=".",
|
| 28 |
+
commit_message="Deploy predictive maintenance Streamlit app",
|
| 29 |
+
token=HF_TOKEN
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
print(f"Deployment files pushed to: https://huggingface.co/spaces/{SPACE_REPO_ID}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
joblib
|
| 6 |
+
huggingface_hub
|
| 7 |
+
xgboost
|
streamlit_hf_space/.streamlit/config.toml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[server]
|
| 2 |
+
headless = true
|
| 3 |
+
enableCORS = false
|
| 4 |
+
enableXsrfProtection = false
|
| 5 |
+
|
| 6 |
+
[browser]
|
| 7 |
+
gatherUsageStats = false
|
streamlit_hf_space/Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 6 |
+
ENV PYTHONUNBUFFERED=1
|
| 7 |
+
ENV STREAMLIT_SERVER_PORT=7860
|
| 8 |
+
ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
|
| 9 |
+
|
| 10 |
+
COPY requirements.txt /app/requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r /app/requirements.txt
|
| 12 |
+
|
| 13 |
+
COPY . /app
|
| 14 |
+
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
streamlit_hf_space/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Engine Predictive Maintenance
|
| 3 |
+
emoji: 🛠️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.35.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Engine Predictive Maintenance Streamlit App
|
| 13 |
+
|
| 14 |
+
This Streamlit app loads the registered model from `premswan/engine-predictive-maintenance-model` and predicts whether an engine is operating normally or may require maintenance.
|
| 15 |
+
|
| 16 |
+
## Inputs
|
| 17 |
+
|
| 18 |
+
The app accepts these sensor readings:
|
| 19 |
+
|
| 20 |
+
- `Engine_RPM`
|
| 21 |
+
- `Lub_Oil_Pressure`
|
| 22 |
+
- `Fuel_Pressure`
|
| 23 |
+
- `Coolant_Pressure`
|
| 24 |
+
- `Lub_Oil_Temperature`
|
| 25 |
+
- `Coolant_Temperature`
|
| 26 |
+
|
| 27 |
+
## Output
|
| 28 |
+
|
| 29 |
+
- Predicted engine condition
|
| 30 |
+
- Maintenance probability, when supported by the model
|
| 31 |
+
- Operational recommendation
|
streamlit_hf_space/app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# ------------------------------------------------------------
|
| 8 |
+
# Deployment configuration
|
| 9 |
+
# ------------------------------------------------------------
|
| 10 |
+
# MODEL_REPO_ID can be overridden in Hugging Face Space secrets/variables.
|
| 11 |
+
MODEL_REPO_ID = os.getenv("MODEL_REPO_ID", "premswan/engine-predictive-maintenance-model")
|
| 12 |
+
MODEL_FILENAME = "best_engine_maintenance_model.joblib"
|
| 13 |
+
FEATURE_COLUMNS = [
|
| 14 |
+
"Engine_RPM",
|
| 15 |
+
"Lub_Oil_Pressure",
|
| 16 |
+
"Fuel_Pressure",
|
| 17 |
+
"Coolant_Pressure",
|
| 18 |
+
"Lub_Oil_Temperature",
|
| 19 |
+
"Coolant_Temperature"
|
| 20 |
+
]
|
| 21 |
+
FEATURE_DEFAULTS = {
|
| 22 |
+
"Engine_RPM": 800.0,
|
| 23 |
+
"Lub_Oil_Pressure": 3.2,
|
| 24 |
+
"Fuel_Pressure": 6.5,
|
| 25 |
+
"Coolant_Pressure": 2.4,
|
| 26 |
+
"Lub_Oil_Temperature": 78.0,
|
| 27 |
+
"Coolant_Temperature": 80.0
|
| 28 |
+
}
|
| 29 |
+
FEATURE_HELP = {
|
| 30 |
+
"Engine_RPM": "Engine speed in revolutions per minute.",
|
| 31 |
+
"Lub_Oil_Pressure": "Lubricating oil pressure reading.",
|
| 32 |
+
"Fuel_Pressure": "Fuel pressure reading.",
|
| 33 |
+
"Coolant_Pressure": "Coolant pressure reading.",
|
| 34 |
+
"Lub_Oil_Temperature": "Lubricating oil temperature in Celsius.",
|
| 35 |
+
"Coolant_Temperature": "Coolant temperature in Celsius."
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
st.set_page_config(
|
| 39 |
+
page_title="Engine Predictive Maintenance",
|
| 40 |
+
page_icon="🛠️",
|
| 41 |
+
layout="centered"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
@st.cache_resource
|
| 45 |
+
def load_model():
|
| 46 |
+
# Load the registered model from Hugging Face Model Hub.
|
| 47 |
+
token = os.getenv("HF_TOKEN")
|
| 48 |
+
model_path = hf_hub_download(
|
| 49 |
+
repo_id=MODEL_REPO_ID,
|
| 50 |
+
filename=MODEL_FILENAME,
|
| 51 |
+
token=token
|
| 52 |
+
)
|
| 53 |
+
return joblib.load(model_path)
|
| 54 |
+
|
| 55 |
+
st.title("Engine Predictive Maintenance")
|
| 56 |
+
st.write(
|
| 57 |
+
"Enter engine sensor readings to predict whether the engine is operating normally "
|
| 58 |
+
"or may require maintenance."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
model = load_model()
|
| 62 |
+
|
| 63 |
+
# ------------------------------------------------------------
|
| 64 |
+
# Capture input readings and save them into a dataframe
|
| 65 |
+
# ------------------------------------------------------------
|
| 66 |
+
input_values = {}
|
| 67 |
+
for feature in FEATURE_COLUMNS:
|
| 68 |
+
input_values[feature] = st.number_input(
|
| 69 |
+
label=feature,
|
| 70 |
+
value=float(FEATURE_DEFAULTS.get(feature, 0.0)),
|
| 71 |
+
help=FEATURE_HELP.get(feature, "Enter sensor value")
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
input_df = pd.DataFrame([input_values], columns=FEATURE_COLUMNS)
|
| 75 |
+
input_df.to_csv("latest_input.csv", index=False)
|
| 76 |
+
|
| 77 |
+
st.subheader("Input DataFrame")
|
| 78 |
+
st.dataframe(input_df, use_container_width=True)
|
| 79 |
+
|
| 80 |
+
if st.button("Predict Engine Condition"):
|
| 81 |
+
prediction = int(model.predict(input_df)[0])
|
| 82 |
+
|
| 83 |
+
probability_maintenance = None
|
| 84 |
+
if hasattr(model, "predict_proba"):
|
| 85 |
+
probability_maintenance = float(model.predict_proba(input_df)[0, 1])
|
| 86 |
+
|
| 87 |
+
if prediction == 1:
|
| 88 |
+
st.error("Prediction: Maintenance / Faulty condition")
|
| 89 |
+
st.write("Recommended action: inspect the engine before continuing heavy operation.")
|
| 90 |
+
else:
|
| 91 |
+
st.success("Prediction: Normal / Healthy condition")
|
| 92 |
+
st.write("Recommended action: continue normal monitoring.")
|
| 93 |
+
|
| 94 |
+
if probability_maintenance is not None:
|
| 95 |
+
st.metric("Maintenance Probability", f"{probability_maintenance:.2%}")
|
streamlit_hf_space/deployment_config.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"space_repo_id": "premswan/engine-predictive-maintenance-streamlit",
|
| 3 |
+
"model_repo_id": "premswan/engine-predictive-maintenance-model",
|
| 4 |
+
"model_filename": "best_engine_maintenance_model.joblib",
|
| 5 |
+
"sdk": "streamlit",
|
| 6 |
+
"port": 7860,
|
| 7 |
+
"feature_columns": [
|
| 8 |
+
"Engine_RPM",
|
| 9 |
+
"Lub_Oil_Pressure",
|
| 10 |
+
"Fuel_Pressure",
|
| 11 |
+
"Coolant_Pressure",
|
| 12 |
+
"Lub_Oil_Temperature",
|
| 13 |
+
"Coolant_Temperature"
|
| 14 |
+
],
|
| 15 |
+
"runtime_files": [
|
| 16 |
+
"app.py",
|
| 17 |
+
"requirements.txt",
|
| 18 |
+
"Dockerfile",
|
| 19 |
+
".streamlit/config.toml",
|
| 20 |
+
"README.md",
|
| 21 |
+
"push_to_hf_space.py"
|
| 22 |
+
]
|
| 23 |
+
}
|
streamlit_hf_space/push_to_hf_space.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import HfApi, login
|
| 4 |
+
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
SPACE_REPO_ID = os.getenv("SPACE_REPO_ID", "premswan/engine-predictive-maintenance-streamlit")
|
| 7 |
+
SPACE_DIR = Path(__file__).resolve().parent
|
| 8 |
+
|
| 9 |
+
if not HF_TOKEN:
|
| 10 |
+
raise RuntimeError("HF_TOKEN environment variable is required to push the Hugging Face Space.")
|
| 11 |
+
|
| 12 |
+
login(token=HF_TOKEN, add_to_git_credential=True)
|
| 13 |
+
api = HfApi(token=HF_TOKEN)
|
| 14 |
+
|
| 15 |
+
api.create_repo(
|
| 16 |
+
repo_id=SPACE_REPO_ID,
|
| 17 |
+
repo_type="space",
|
| 18 |
+
space_sdk="streamlit",
|
| 19 |
+
exist_ok=True,
|
| 20 |
+
private=False,
|
| 21 |
+
token=HF_TOKEN
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
api.upload_folder(
|
| 25 |
+
folder_path=str(SPACE_DIR),
|
| 26 |
+
repo_id=SPACE_REPO_ID,
|
| 27 |
+
repo_type="space",
|
| 28 |
+
path_in_repo=".",
|
| 29 |
+
commit_message="Deploy Streamlit predictive maintenance app",
|
| 30 |
+
token=HF_TOKEN
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
print(f"Space deployed: https://huggingface.co/spaces/{SPACE_REPO_ID}")
|
streamlit_hf_space/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
xgboost
|
| 6 |
+
joblib
|
| 7 |
+
huggingface_hub
|
streamlit_hf_space/sample_input.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Engine_RPM,Lub_Oil_Pressure,Fuel_Pressure,Coolant_Pressure,Lub_Oil_Temperature,Coolant_Temperature
|
| 2 |
+
800.0,3.2,6.5,2.4,78.0,80.0
|