Spaces:
Sleeping
Sleeping
Deploy Predictive Maintenance application
Browse files- Dockerfile +22 -8
- README.md +18 -10
- app.py +144 -0
- deploy_to_hf_space.py +81 -0
- requirements.txt +15 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,34 @@
|
|
| 1 |
-
FROM python:3.13.5-slim
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
build-essential \
|
| 7 |
curl \
|
|
|
|
| 8 |
git \
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
COPY requirements.txt .
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
EXPOSE
|
| 17 |
|
| 18 |
-
HEALTHCHECK
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 7 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 8 |
+
PIP_NO_CACHE_DIR=1 \
|
| 9 |
+
PIP_DISABLE_PIP_VERSION_CHECK=1
|
| 10 |
|
| 11 |
RUN apt-get update && apt-get install -y \
|
| 12 |
build-essential \
|
| 13 |
curl \
|
| 14 |
+
software-properties-common \
|
| 15 |
git \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
+
COPY requirements.txt .
|
| 19 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 20 |
+
pip install --no-cache-dir -r requirements.txt
|
| 21 |
+
|
| 22 |
+
COPY . .
|
| 23 |
+
|
| 24 |
+
RUN groupadd -r appuser && useradd -r -g appuser appuser && \
|
| 25 |
+
chown -R appuser:appuser /code
|
| 26 |
|
| 27 |
+
USER appuser
|
| 28 |
|
| 29 |
+
EXPOSE 7860
|
| 30 |
|
| 31 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
| 32 |
+
CMD curl -f http://localhost:7860/_stcore/health
|
| 33 |
|
| 34 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
README.md
CHANGED
|
@@ -1,19 +1,27 @@
|
|
| 1 |
---
|
| 2 |
title: Predictive Maintenance
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: red
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
-
app_port:
|
| 8 |
-
|
| 9 |
-
- streamlit
|
| 10 |
pinned: false
|
| 11 |
-
|
| 12 |
---
|
| 13 |
|
| 14 |
-
#
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Predictive Maintenance
|
| 3 |
+
emoji: 🔧
|
| 4 |
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
app_file: app.py
|
|
|
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Predictive Maintenance — Engine Health Monitor 🔧
|
| 14 |
|
| 15 |
+
An intelligent ML-powered system that monitors engine sensor data and predicts failure risk in real time.
|
| 16 |
|
| 17 |
+
## Features
|
| 18 |
+
- **Real-time Predictions**: Instant engine health scoring
|
| 19 |
+
- **Interactive Interface**: User-friendly Streamlit web application
|
| 20 |
+
- **Multiple Algorithms**: Best model selected from 6 different algorithms
|
| 21 |
+
- **MLOps Integration**: Complete pipeline with experiment tracking
|
| 22 |
+
|
| 23 |
+
## Technical Stack
|
| 24 |
+
- **Backend**: Python, Scikit-learn, XGBoost
|
| 25 |
+
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
+
import numpy as np
|
| 5 |
+
import joblib
|
| 6 |
+
import os
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Predictive Maintenance",
|
| 12 |
+
page_icon="🔧",
|
| 13 |
+
layout="wide",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.main-header { font-size: 2.5rem; font-weight: bold; text-align: center; color: #e85d04; margin-bottom: 2rem; }
|
| 20 |
+
.prediction-box { padding: 1rem; margin: 1rem 0; border-radius: 10px; text-align: center; }
|
| 21 |
+
.failure-prediction { background-color: #f8d7da; color: #721c24; border: 2px solid #f5c6cb; }
|
| 22 |
+
.healthy-prediction { background-color: #d4edda; color: #155724; border: 2px solid #c3e6cb; }
|
| 23 |
+
</style>
|
| 24 |
+
""", unsafe_allow_html=True)
|
| 25 |
+
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def load_model():
|
| 28 |
+
try:
|
| 29 |
+
model_path = hf_hub_download(
|
| 30 |
+
repo_id="shashidj/Predictive-Maintenance-Model",
|
| 31 |
+
filename="model.pkl"
|
| 32 |
+
)
|
| 33 |
+
return joblib.load(model_path)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Error loading model: {e}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def predict_engine_condition(model, input_data):
|
| 39 |
+
try:
|
| 40 |
+
prediction = model.predict(input_data)[0]
|
| 41 |
+
prediction_proba = model.predict_proba(input_data)[0] if hasattr(model, 'predict_proba') else [0.5, 0.5]
|
| 42 |
+
return prediction, prediction_proba
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"Prediction error: {e}")
|
| 45 |
+
return None, None
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
st.markdown('<div class="main-header">🔧 Predictive Maintenance — Engine Health Monitor</div>', unsafe_allow_html=True)
|
| 49 |
+
|
| 50 |
+
model = load_model()
|
| 51 |
+
if model is None:
|
| 52 |
+
st.error("Failed to load model. Please check your configuration.")
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
st.sidebar.header("📊 Model Information")
|
| 56 |
+
st.sidebar.info("""
|
| 57 |
+
**Algorithm**: Best performing model from 6 algorithms
|
| 58 |
+
**Features**: Engine sensor readings
|
| 59 |
+
**Purpose**: Predict engine failure risk
|
| 60 |
+
**Accuracy**: Optimized through hyperparameter tuning
|
| 61 |
+
""")
|
| 62 |
+
|
| 63 |
+
st.header("Engine Sensor Readings")
|
| 64 |
+
col1, col2 = st.columns(2)
|
| 65 |
+
|
| 66 |
+
with col1:
|
| 67 |
+
st.subheader("🔩 Mechanical Parameters")
|
| 68 |
+
engine_rpm = st.number_input("Engine RPM", min_value=0.0, max_value=5000.0, value=700.0, step=10.0)
|
| 69 |
+
lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=20.0, value=2.5, step=0.1)
|
| 70 |
+
fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=50.0, value=11.8, step=0.1)
|
| 71 |
+
|
| 72 |
+
with col2:
|
| 73 |
+
st.subheader("🌡️ Temperature & Cooling")
|
| 74 |
+
coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, max_value=10.0, value=3.2, step=0.1)
|
| 75 |
+
lub_oil_temp = st.number_input("Lub Oil Temperature (°C)", min_value=0.0, max_value=200.0, value=84.0, step=0.5)
|
| 76 |
+
coolant_temp = st.number_input("Coolant Temperature (°C)", min_value=0.0, max_value=200.0, value=81.6, step=0.5)
|
| 77 |
+
|
| 78 |
+
if st.button("🔮 Predict Engine Condition", type="primary"):
|
| 79 |
+
input_data = pd.DataFrame({
|
| 80 |
+
'Engine rpm': [engine_rpm],
|
| 81 |
+
'Lub oil pressure': [lub_oil_pressure],
|
| 82 |
+
'Fuel pressure': [fuel_pressure],
|
| 83 |
+
'Coolant pressure': [coolant_pressure],
|
| 84 |
+
'lub oil temp': [lub_oil_temp],
|
| 85 |
+
'Coolant temp': [coolant_temp]
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
prediction, prediction_proba = predict_engine_condition(model, input_data)
|
| 89 |
+
|
| 90 |
+
if prediction is not None:
|
| 91 |
+
st.header("🎯 Prediction Results")
|
| 92 |
+
if prediction == 1:
|
| 93 |
+
st.markdown(f"""
|
| 94 |
+
<div class="prediction-box failure-prediction">
|
| 95 |
+
<h3>⚠️ ENGINE FAILURE RISK DETECTED</h3>
|
| 96 |
+
<p><strong>Failure Probability:</strong> {prediction_proba[1]:.2%}</p>
|
| 97 |
+
<p>Immediate maintenance inspection is recommended!</p>
|
| 98 |
+
</div>
|
| 99 |
+
""", unsafe_allow_html=True)
|
| 100 |
+
st.error("💡 Action Required: Schedule maintenance before next operation cycle.")
|
| 101 |
+
else:
|
| 102 |
+
st.markdown(f"""
|
| 103 |
+
<div class="prediction-box healthy-prediction">
|
| 104 |
+
<h3>✅ ENGINE IS HEALTHY</h3>
|
| 105 |
+
<p><strong>Healthy Probability:</strong> {prediction_proba[0]:.2%}</p>
|
| 106 |
+
<p>Engine is operating within normal parameters.</p>
|
| 107 |
+
</div>
|
| 108 |
+
""", unsafe_allow_html=True)
|
| 109 |
+
st.success("💡 No immediate action required. Continue scheduled monitoring.")
|
| 110 |
+
|
| 111 |
+
fig = go.Figure(data=[go.Bar(
|
| 112 |
+
x=['Healthy', 'Failure Risk'],
|
| 113 |
+
y=[prediction_proba[0], prediction_proba[1]],
|
| 114 |
+
marker_color=['#90ee90', '#ff7f7f']
|
| 115 |
+
)])
|
| 116 |
+
fig.update_layout(
|
| 117 |
+
title="Engine Condition Probability Distribution",
|
| 118 |
+
xaxis_title="Condition",
|
| 119 |
+
yaxis_title="Probability",
|
| 120 |
+
yaxis=dict(range=[0, 1], tickformat='.0%')
|
| 121 |
+
)
|
| 122 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 123 |
+
|
| 124 |
+
st.subheader("📋 Sensor Reading Summary")
|
| 125 |
+
summary_df = pd.DataFrame({
|
| 126 |
+
'Sensor': ['Engine RPM', 'Lub Oil Pressure', 'Fuel Pressure',
|
| 127 |
+
'Coolant Pressure', 'Lub Oil Temp', 'Coolant Temp'],
|
| 128 |
+
'Value': [f"{engine_rpm:.1f} RPM", f"{lub_oil_pressure:.2f} bar",
|
| 129 |
+
f"{fuel_pressure:.2f} bar", f"{coolant_pressure:.2f} bar",
|
| 130 |
+
f"{lub_oil_temp:.1f} °C", f"{coolant_temp:.1f} °C"]
|
| 131 |
+
})
|
| 132 |
+
st.table(summary_df)
|
| 133 |
+
|
| 134 |
+
st.markdown("---")
|
| 135 |
+
st.markdown("""
|
| 136 |
+
**About this Application:**
|
| 137 |
+
This Predictive Maintenance system uses machine learning to monitor engine sensor data and predict failure risk in real time.
|
| 138 |
+
Built with MLOps best practices including experiment tracking, model versioning, and automated deployment.
|
| 139 |
+
|
| 140 |
+
**🔧 Technical Stack:** Python • Scikit-learn • MLflow • HuggingFace • Streamlit • Docker
|
| 141 |
+
""")
|
| 142 |
+
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
main()
|
deploy_to_hf_space.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from huggingface_hub import HfApi, create_repo
|
| 5 |
+
from huggingface_hub.utils import RepositoryNotFoundError
|
| 6 |
+
|
| 7 |
+
def deploy_to_huggingface_space():
|
| 8 |
+
SPACE_NAME = "Predictive-Maintenance"
|
| 9 |
+
REPO_ID = f"shashidj/{SPACE_NAME}"
|
| 10 |
+
|
| 11 |
+
print("🚀 Starting deployment to HuggingFace Spaces...")
|
| 12 |
+
print(f"📦 Target Space: {REPO_ID}")
|
| 13 |
+
|
| 14 |
+
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
api.repo_info(repo_id=REPO_ID, repo_type="space")
|
| 18 |
+
print(f"📍 Space '{REPO_ID}' already exists")
|
| 19 |
+
except RepositoryNotFoundError:
|
| 20 |
+
print(f"🆕 Creating new space '{REPO_ID}'...")
|
| 21 |
+
create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="docker", private=False)
|
| 22 |
+
print("✅ Space created successfully")
|
| 23 |
+
|
| 24 |
+
deployment_dir = Path("predictive_maintenance_mlops/deployment")
|
| 25 |
+
required_files = ["Dockerfile", "app.py", "requirements.txt"]
|
| 26 |
+
|
| 27 |
+
print("\n🔍 Verifying deployment files...")
|
| 28 |
+
for file_name in required_files:
|
| 29 |
+
if (deployment_dir / file_name).exists():
|
| 30 |
+
print(f"✅ {file_name} found")
|
| 31 |
+
else:
|
| 32 |
+
print(f"❌ {file_name} missing")
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
+
readme_content = """---
|
| 36 |
+
title: Predictive Maintenance
|
| 37 |
+
emoji: 🔧
|
| 38 |
+
colorFrom: red
|
| 39 |
+
colorTo: gray
|
| 40 |
+
sdk: docker
|
| 41 |
+
app_port: 7860
|
| 42 |
+
app_file: app.py
|
| 43 |
+
pinned: false
|
| 44 |
+
license: mit
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
# Predictive Maintenance — Engine Health Monitor 🔧
|
| 48 |
+
|
| 49 |
+
An intelligent ML-powered system that monitors engine sensor data and predicts failure risk in real time.
|
| 50 |
+
|
| 51 |
+
## Features
|
| 52 |
+
- **Real-time Predictions**: Instant engine health scoring
|
| 53 |
+
- **Interactive Interface**: User-friendly Streamlit web application
|
| 54 |
+
- **Multiple Algorithms**: Best model selected from 6 different algorithms
|
| 55 |
+
- **MLOps Integration**: Complete pipeline with experiment tracking
|
| 56 |
+
|
| 57 |
+
## Technical Stack
|
| 58 |
+
- **Backend**: Python, Scikit-learn, XGBoost
|
| 59 |
+
- **Frontend**: Streamlit
|
| 60 |
+
- **ML Tracking**: MLflow
|
| 61 |
+
- **Deployment**: Docker, HuggingFace Spaces
|
| 62 |
+
"""
|
| 63 |
+
(deployment_dir / "README.md").write_text(readme_content)
|
| 64 |
+
print("✅ README.md created")
|
| 65 |
+
|
| 66 |
+
print("\n📤 Uploading files to HuggingFace Space...")
|
| 67 |
+
api.upload_folder(
|
| 68 |
+
folder_path=str(deployment_dir),
|
| 69 |
+
repo_id=REPO_ID,
|
| 70 |
+
repo_type="space",
|
| 71 |
+
commit_message="Deploy Predictive Maintenance application"
|
| 72 |
+
)
|
| 73 |
+
print(f"\n🎉 Deployment completed!")
|
| 74 |
+
print(f"🌐 https://huggingface.co/spaces/{REPO_ID}")
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
if not os.getenv("HF_TOKEN"):
|
| 79 |
+
print("❌ HF_TOKEN environment variable not set!")
|
| 80 |
+
else:
|
| 81 |
+
deploy_to_huggingface_space()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit==1.28.1
|
| 3 |
+
pandas==2.0.3
|
| 4 |
+
numpy==1.24.3
|
| 5 |
+
scikit-learn==1.3.0
|
| 6 |
+
joblib==1.3.2
|
| 7 |
+
xgboost==1.7.6
|
| 8 |
+
huggingface-hub==0.17.3
|
| 9 |
+
plotly==5.15.0
|
| 10 |
+
matplotlib==3.7.2
|
| 11 |
+
seaborn==0.12.2
|
| 12 |
+
requests==2.31.0
|
| 13 |
+
python-dotenv==1.0.0
|
| 14 |
+
streamlit-option-menu==0.3.6
|
| 15 |
+
psutil==5.9.5
|