Upload folder using huggingface_hub
Browse files- Dockerfile +27 -0
- README.md +7 -4
- app.py +128 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.12 installed
|
| 2 |
+
FROM python:3.12-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory inside the container to /app
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Install Python dependencies listed in requirements.txt
|
| 11 |
+
RUN pip3 install -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose port (HuggingFace Spaces expects 7860)
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Create a non-root user with a home dir
|
| 17 |
+
RUN useradd -m app && chown -R app:app /app
|
| 18 |
+
|
| 19 |
+
# Switch to that user
|
| 20 |
+
USER app
|
| 21 |
+
|
| 22 |
+
# Point Streamlit config to the new user's home (writable)
|
| 23 |
+
ENV HOME=/home/app
|
| 24 |
+
ENV STREAMLIT_CONFIG_DIR=/home/app/.streamlit
|
| 25 |
+
|
| 26 |
+
# Define the command to run the Streamlit app on port 7860 and make it accessible externally
|
| 27 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
---
|
| 2 |
title: Predictive Maintenance App
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Predictive Maintenance App
|
| 3 |
+
emoji: 🔧
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Predictive Maintenance Prediction App
|
| 12 |
+
|
| 13 |
+
Predict whether an engine requires maintenance based on sensor readings.
|
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import joblib
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# ---------- Page Config ----------
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Predictive Maintenance",
|
| 10 |
+
page_icon="🔧",
|
| 11 |
+
layout="wide",
|
| 12 |
+
initial_sidebar_state="collapsed",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# ---------- Custom CSS ----------
|
| 16 |
+
st.markdown(
|
| 17 |
+
"""
|
| 18 |
+
<style>
|
| 19 |
+
.block-container {
|
| 20 |
+
padding-top: 2rem;
|
| 21 |
+
}
|
| 22 |
+
div.stButton > button {
|
| 23 |
+
background-color: #007bff;
|
| 24 |
+
color: white;
|
| 25 |
+
border: none;
|
| 26 |
+
padding: 0.75rem 3rem;
|
| 27 |
+
border-radius: 0.5rem;
|
| 28 |
+
cursor: pointer;
|
| 29 |
+
}
|
| 30 |
+
div.stButton > button:hover {
|
| 31 |
+
background-color: #0069d9;
|
| 32 |
+
}
|
| 33 |
+
</style>
|
| 34 |
+
""",
|
| 35 |
+
unsafe_allow_html=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# ---------- Helper Functions ----------
|
| 39 |
+
def slugify(name: str) -> str:
|
| 40 |
+
"""Convert name to HF-compatible slug (underscores to hyphens)."""
|
| 41 |
+
return name.replace("_", "-")
|
| 42 |
+
|
| 43 |
+
# ---------- Load Model (full pipeline with preprocessor) ----------
|
| 44 |
+
hf_username = os.getenv("HF_USERNAME")
|
| 45 |
+
hf_model_name = slugify(os.getenv("HF_MODEL_NAME", "predictive-maintenance-model"))
|
| 46 |
+
|
| 47 |
+
model_path = hf_hub_download(
|
| 48 |
+
repo_id=f"{hf_username}/{hf_model_name}",
|
| 49 |
+
filename="best_engine_maintenance_model.joblib"
|
| 50 |
+
)
|
| 51 |
+
model = joblib.load(model_path)
|
| 52 |
+
|
| 53 |
+
# Production threshold for maintenance classification
|
| 54 |
+
THRESHOLD = 0.4
|
| 55 |
+
|
| 56 |
+
# ---------- App Header ----------
|
| 57 |
+
st.title("🔧 Engine Predictive Maintenance")
|
| 58 |
+
st.write("Predict whether an engine requires maintenance based on sensor readings.")
|
| 59 |
+
|
| 60 |
+
st.markdown("---")
|
| 61 |
+
|
| 62 |
+
# ---------- Sensor Readings ----------
|
| 63 |
+
st.subheader("📊 Sensor Readings")
|
| 64 |
+
|
| 65 |
+
col1, col2, col3 = st.columns(3, gap="medium")
|
| 66 |
+
|
| 67 |
+
# Column 1 - Engine Performance
|
| 68 |
+
with col1:
|
| 69 |
+
st.markdown("**Engine Performance**")
|
| 70 |
+
engine_rpm = st.number_input("Engine RPM", min_value=0, max_value=3000, value=1500)
|
| 71 |
+
fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=25.0, value=6.5, step=0.1)
|
| 72 |
+
|
| 73 |
+
# Column 2 - Lubrication System
|
| 74 |
+
with col2:
|
| 75 |
+
st.markdown("**Lubrication System**")
|
| 76 |
+
lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=10.0, value=3.5, step=0.1)
|
| 77 |
+
lub_oil_temp = st.number_input("Lub Oil Temperature (°C)", min_value=0.0, max_value=150.0, value=85.0, step=1.0)
|
| 78 |
+
|
| 79 |
+
# Column 3 - Cooling System
|
| 80 |
+
with col3:
|
| 81 |
+
st.markdown("**Cooling System**")
|
| 82 |
+
coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, max_value=10.0, value=2.0, step=0.1)
|
| 83 |
+
coolant_temp = st.number_input("Coolant Temperature (°C)", min_value=0.0, max_value=200.0, value=90.0, step=1.0)
|
| 84 |
+
|
| 85 |
+
st.markdown("---")
|
| 86 |
+
|
| 87 |
+
# ---------- Derived Features (calculated automatically) ----------
|
| 88 |
+
rpm_x_fuel_pressure = engine_rpm * fuel_pressure
|
| 89 |
+
rpm_bins = 0 if engine_rpm < 300 else (1 if engine_rpm <= 1500 else 2)
|
| 90 |
+
oil_health_index = lub_oil_pressure / lub_oil_temp if lub_oil_temp > 0 else 0
|
| 91 |
+
|
| 92 |
+
# ---------- Create Input DataFrame ----------
|
| 93 |
+
input_data = pd.DataFrame([{
|
| 94 |
+
'engine_rpm': engine_rpm,
|
| 95 |
+
'lub_oil_pressure': lub_oil_pressure,
|
| 96 |
+
'fuel_pressure': fuel_pressure,
|
| 97 |
+
'coolant_pressure': coolant_pressure,
|
| 98 |
+
'lub_oil_temp': lub_oil_temp,
|
| 99 |
+
'coolant_temp': coolant_temp,
|
| 100 |
+
'rpm_x_fuel_pressure': rpm_x_fuel_pressure,
|
| 101 |
+
'rpm_bins': rpm_bins,
|
| 102 |
+
'oil_health_index': oil_health_index
|
| 103 |
+
}])
|
| 104 |
+
|
| 105 |
+
# ---------- Preview & Predict ----------
|
| 106 |
+
st.subheader("📦 Feature Preview")
|
| 107 |
+
with st.expander("Click to expand (includes derived features)", expanded=False):
|
| 108 |
+
cols = st.columns(3)
|
| 109 |
+
for i, (field, value) in enumerate(input_data.iloc[0].items()):
|
| 110 |
+
with cols[i % 3]:
|
| 111 |
+
display_value = f"{value:.4f}" if isinstance(value, float) else value
|
| 112 |
+
st.metric(label=field, value=display_value)
|
| 113 |
+
|
| 114 |
+
if st.button("Predict Maintenance Need"):
|
| 115 |
+
# Pipeline handles preprocessing (StandardScaler)
|
| 116 |
+
probability = model.predict_proba(input_data)[0, 1]
|
| 117 |
+
prediction = 1 if probability >= THRESHOLD else 0
|
| 118 |
+
|
| 119 |
+
st.markdown("---")
|
| 120 |
+
st.subheader("Prediction Result")
|
| 121 |
+
|
| 122 |
+
if prediction == 1:
|
| 123 |
+
st.error(f"⚠️ **Maintenance Required** (Probability: {probability:.2%})")
|
| 124 |
+
st.write("The engine shows signs of degradation. Schedule maintenance soon.")
|
| 125 |
+
else:
|
| 126 |
+
st.success(f"✅ **Normal Operation** (Probability of failure: {probability:.2%})")
|
| 127 |
+
st.write("The engine is operating within normal parameters.")
|
| 128 |
+
st.balloons()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.3
|
| 2 |
+
huggingface_hub==0.27.1
|
| 3 |
+
streamlit==1.41.1
|
| 4 |
+
joblib==1.4.2
|
| 5 |
+
scikit-learn==1.6.0
|