Spaces:
Sleeping
Sleeping
Deploy Engine Condition Monitoring App with Docker
Browse files- .streamlit/config.toml +8 -0
- Dockerfile +21 -0
- app.py +225 -0
- requirements.txt +7 -0
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
[server]
|
| 3 |
+
headless = true
|
| 4 |
+
enableCORS = false
|
| 5 |
+
enableXsrfProtection = false
|
| 6 |
+
|
| 7 |
+
[browser]
|
| 8 |
+
gatherUsageStats = false
|
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy requirements and install dependencies
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
# Copy application files
|
| 11 |
+
COPY . .
|
| 12 |
+
|
| 13 |
+
# Expose Streamlit port
|
| 14 |
+
EXPOSE 8501
|
| 15 |
+
|
| 16 |
+
# Health check
|
| 17 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
| 18 |
+
CMD python -c "import requests; requests.get('http://localhost:8501/healthz')" || exit 1
|
| 19 |
+
|
| 20 |
+
# Run Streamlit app
|
| 21 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from huggingface_hub import hf_hub_download, login
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # optional if model is public
|
| 11 |
+
HF_MODEL_REPO = os.getenv("HF_MODEL_REPO", "dhani10/engine-condition-model")
|
| 12 |
+
MODEL_FILE = os.getenv("MODEL_FILE", "best_engine_model.joblib")
|
| 13 |
+
|
| 14 |
+
# Use a writable cache (esp. on Spaces)
|
| 15 |
+
os.environ.setdefault("HF_HOME", "/tmp/huggingface")
|
| 16 |
+
os.environ.setdefault("HF_HUB_CACHE", "/tmp/huggingface/hub")
|
| 17 |
+
os.makedirs(os.environ["HF_HUB_CACHE"], exist_ok=True)
|
| 18 |
+
|
| 19 |
+
if HF_TOKEN:
|
| 20 |
+
try:
|
| 21 |
+
login(HF_TOKEN)
|
| 22 |
+
except Exception:
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_model():
|
| 27 |
+
p = hf_hub_download(
|
| 28 |
+
repo_id=HF_MODEL_REPO,
|
| 29 |
+
filename=MODEL_FILE,
|
| 30 |
+
repo_type="model",
|
| 31 |
+
token=HF_TOKEN,
|
| 32 |
+
cache_dir=os.environ["HF_HUB_CACHE"],
|
| 33 |
+
)
|
| 34 |
+
return joblib.load(p)
|
| 35 |
+
|
| 36 |
+
model = load_model()
|
| 37 |
+
|
| 38 |
+
# Engine sensor features
|
| 39 |
+
ENGINE_FEATURES = [
|
| 40 |
+
"Engine rpm",
|
| 41 |
+
"Lub oil pressure",
|
| 42 |
+
"Fuel pressure",
|
| 43 |
+
"Coolant pressure",
|
| 44 |
+
"lub oil temp",
|
| 45 |
+
"Coolant temp"
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
st.set_page_config(page_title="Engine Condition Monitor", layout="centered")
|
| 49 |
+
st.title("🏭 Engine Condition Monitoring System")
|
| 50 |
+
st.caption("Enter sensor readings to predict engine condition (Normal/Faulty)")
|
| 51 |
+
|
| 52 |
+
# Add information about the model
|
| 53 |
+
with st.expander("ℹ️ About this Model"):
|
| 54 |
+
st.write('''
|
| 55 |
+
This model predicts engine condition based on real-time sensor readings:
|
| 56 |
+
- **Normal (0)**: Engine operating within normal parameters
|
| 57 |
+
- **Faulty (1)**: Engine showing signs of potential failure
|
| 58 |
+
|
| 59 |
+
**Typical Operating Ranges:**
|
| 60 |
+
- Engine RPM: 600-2500
|
| 61 |
+
- Lub Oil Pressure: 2.0-4.0 bar
|
| 62 |
+
- Fuel Pressure: 8.0-15.0 bar
|
| 63 |
+
- Coolant Pressure: 1.5-4.0 bar
|
| 64 |
+
- Lub Oil Temp: 75-110°C
|
| 65 |
+
- Coolant Temp: 70-100°C
|
| 66 |
+
''')
|
| 67 |
+
|
| 68 |
+
# Sensor input form
|
| 69 |
+
with st.form("engine_predict_form"):
|
| 70 |
+
st.subheader("🔧 Sensor Readings")
|
| 71 |
+
|
| 72 |
+
col1, col2 = st.columns(2)
|
| 73 |
+
|
| 74 |
+
with col1:
|
| 75 |
+
engine_rpm = st.slider(
|
| 76 |
+
"Engine RPM",
|
| 77 |
+
min_value=0,
|
| 78 |
+
max_value=3000,
|
| 79 |
+
value=1800,
|
| 80 |
+
help="Engine rotations per minute"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
lub_oil_pressure = st.slider(
|
| 84 |
+
"Lub Oil Pressure (bar)",
|
| 85 |
+
min_value=0.0,
|
| 86 |
+
max_value=6.0,
|
| 87 |
+
value=3.1,
|
| 88 |
+
step=0.1,
|
| 89 |
+
help="Lubricating oil pressure in bar"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
fuel_pressure = st.slider(
|
| 93 |
+
"Fuel Pressure (bar)",
|
| 94 |
+
min_value=0.0,
|
| 95 |
+
max_value=20.0,
|
| 96 |
+
value=12.0,
|
| 97 |
+
step=0.1,
|
| 98 |
+
help="Fuel system pressure in bar"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with col2:
|
| 102 |
+
coolant_pressure = st.slider(
|
| 103 |
+
"Coolant Pressure (bar)",
|
| 104 |
+
min_value=0.0,
|
| 105 |
+
max_value=5.0,
|
| 106 |
+
value=2.9,
|
| 107 |
+
step=0.1,
|
| 108 |
+
help="Cooling system pressure in bar"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
lub_oil_temp = st.slider(
|
| 112 |
+
"Lub Oil Temp (°C)",
|
| 113 |
+
min_value=0,
|
| 114 |
+
max_value=150,
|
| 115 |
+
value=92,
|
| 116 |
+
help="Lubricating oil temperature in °C"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
coolant_temp = st.slider(
|
| 120 |
+
"Coolant Temp (°C)",
|
| 121 |
+
min_value=0,
|
| 122 |
+
max_value=150,
|
| 123 |
+
value=89,
|
| 124 |
+
help="Coolant temperature in °C"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
submitted = st.form_submit_button("🔍 Predict Engine Condition")
|
| 128 |
+
|
| 129 |
+
if submitted:
|
| 130 |
+
# Build input data
|
| 131 |
+
ui_row = {
|
| 132 |
+
"Engine rpm": float(engine_rpm),
|
| 133 |
+
"Lub oil pressure": float(lub_oil_pressure),
|
| 134 |
+
"Fuel pressure": float(fuel_pressure),
|
| 135 |
+
"Coolant pressure": float(coolant_pressure),
|
| 136 |
+
"lub oil temp": float(lub_oil_temp),
|
| 137 |
+
"Coolant temp": float(coolant_temp)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
# Create DataFrame with expected columns
|
| 141 |
+
row = pd.DataFrame([ui_row])
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
# Make prediction
|
| 145 |
+
prediction = model.predict(row)[0]
|
| 146 |
+
probability = None
|
| 147 |
+
if hasattr(model, "predict_proba"):
|
| 148 |
+
probability = model.predict_proba(row)[0]
|
| 149 |
+
|
| 150 |
+
# Display results
|
| 151 |
+
st.subheader("🎯 Prediction Result")
|
| 152 |
+
|
| 153 |
+
if prediction == 1:
|
| 154 |
+
st.error("🚨 **FAULTY CONDITION DETECTED**")
|
| 155 |
+
st.warning("⚠️ Engine shows signs of potential failure. Immediate maintenance recommended.")
|
| 156 |
+
|
| 157 |
+
if probability is not None:
|
| 158 |
+
st.metric(
|
| 159 |
+
"Confidence Score",
|
| 160 |
+
f"{probability[1]:.1%}",
|
| 161 |
+
delta=f"Faulty probability",
|
| 162 |
+
delta_color="inverse"
|
| 163 |
+
)
|
| 164 |
+
else:
|
| 165 |
+
st.success("✅ **NORMAL OPERATION**")
|
| 166 |
+
st.info("🌡️ Engine operating within normal parameters.")
|
| 167 |
+
|
| 168 |
+
if probability is not None:
|
| 169 |
+
st.metric(
|
| 170 |
+
"Confidence Score",
|
| 171 |
+
f"{probability[0]:.1%}",
|
| 172 |
+
delta=f"Normal probability",
|
| 173 |
+
delta_color="normal"
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Display detailed probabilities
|
| 177 |
+
if probability is not None:
|
| 178 |
+
col1, col2 = st.columns(2)
|
| 179 |
+
with col1:
|
| 180 |
+
st.progress(probability[0], text=f"Normal: {probability[0]:.1%}")
|
| 181 |
+
with col2:
|
| 182 |
+
st.progress(probability[1], text=f"Faulty: {probability[1]:.1%}")
|
| 183 |
+
|
| 184 |
+
# Show input values
|
| 185 |
+
with st.expander("📊 Sensor Readings Used"):
|
| 186 |
+
st.dataframe(row.T.rename(columns={0: "Value"}))
|
| 187 |
+
|
| 188 |
+
# Add maintenance recommendations for faulty conditions
|
| 189 |
+
if prediction == 1:
|
| 190 |
+
st.subheader("🔧 Recommended Actions")
|
| 191 |
+
issues = []
|
| 192 |
+
|
| 193 |
+
if lub_oil_pressure < 2.5:
|
| 194 |
+
issues.append("Low lubricating oil pressure")
|
| 195 |
+
if fuel_pressure > 13.0:
|
| 196 |
+
issues.append("High fuel pressure")
|
| 197 |
+
if lub_oil_temp > 105:
|
| 198 |
+
issues.append("High lubricating oil temperature")
|
| 199 |
+
if coolant_temp > 95:
|
| 200 |
+
issues.append("High coolant temperature")
|
| 201 |
+
|
| 202 |
+
if issues:
|
| 203 |
+
st.write("Potential issues detected:")
|
| 204 |
+
for issue in issues:
|
| 205 |
+
st.write(f"• {issue}")
|
| 206 |
+
|
| 207 |
+
st.write('''
|
| 208 |
+
**Immediate Steps:**
|
| 209 |
+
1. Check oil levels and quality
|
| 210 |
+
2. Inspect cooling system
|
| 211 |
+
3. Verify fuel system components
|
| 212 |
+
4. Consult maintenance manual
|
| 213 |
+
''')
|
| 214 |
+
|
| 215 |
+
except Exception as e:
|
| 216 |
+
st.error(f"❌ Prediction failed: {e}")
|
| 217 |
+
st.write("Expected features:", ENGINE_FEATURES)
|
| 218 |
+
|
| 219 |
+
# Add footer
|
| 220 |
+
st.markdown("---")
|
| 221 |
+
st.caption('''
|
| 222 |
+
**Engine Condition Prediction System** |
|
| 223 |
+
Predictive Maintenance Model |
|
| 224 |
+
[View Model on Hugging Face](https://huggingface.co/dhani10/engine-condition-model)
|
| 225 |
+
''')
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.28.0
|
| 2 |
+
pandas==2.0.3
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
scikit-learn==1.3.0
|
| 5 |
+
joblib==1.3.2
|
| 6 |
+
huggingface_hub==0.19.0
|
| 7 |
+
plotly==5.15.0
|