Quantum9999 commited on
Commit
03c03a5
·
verified ·
1 Parent(s): b635c87

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +15 -12
  2. app.py +315 -0
  3. requirements.txt +7 -3
Dockerfile CHANGED
@@ -1,20 +1,23 @@
1
- FROM python:3.13.5-slim
2
 
3
  WORKDIR /app
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
- COPY src/ ./src/
13
 
14
- RUN pip3 install -r requirements.txt
 
15
 
16
- EXPOSE 8501
 
 
17
 
18
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
 
19
 
20
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
 
1
+ FROM python:3.10-slim
2
 
3
  WORKDIR /app
4
 
5
+ # Copy requirements and install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
 
 
8
 
9
+ # Copy application file
10
+ COPY app.py .
11
 
12
+ # Expose Streamlit default port
13
+ EXPOSE 7860
14
 
15
+ # Set environment variables for Streamlit
16
+ ENV STREAMLIT_SERVER_PORT=7860
17
+ ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
18
 
19
+ # Health check
20
+ HEALTHCHECK CMD curl --fail http://localhost:7860/_stcore/health
21
 
22
+ # Run the application
23
+ CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
app.py ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Streamlit Application for Engine Predictive Maintenance
3
+ Production-ready deployment with proper error handling
4
+ """
5
+
6
+ import streamlit as st
7
+ import pandas as pd
8
+ from huggingface_hub import hf_hub_download, login
9
+ import joblib
10
+ import os
11
+
12
+ # Page Configuration
13
+ st.set_page_config(
14
+ page_title="Engine Predictive Maintenance",
15
+ page_icon="🔧",
16
+ layout="wide",
17
+ initial_sidebar_state="expanded"
18
+ )
19
+
20
+ # Custom CSS
21
+ st.markdown("""
22
+ <style>
23
+ .main-header {
24
+ font-size: 42px;
25
+ font-weight: bold;
26
+ color: #1f77b4;
27
+ text-align: center;
28
+ margin-bottom: 10px;
29
+ }
30
+ .sub-header {
31
+ font-size: 18px;
32
+ color: #555;
33
+ text-align: center;
34
+ margin-bottom: 30px;
35
+ }
36
+ .prediction-box {
37
+ padding: 20px;
38
+ border-radius: 10px;
39
+ text-align: center;
40
+ font-size: 24px;
41
+ font-weight: bold;
42
+ margin-top: 20px;
43
+ }
44
+ .normal {
45
+ background-color: #d4edda;
46
+ color: #155724;
47
+ border: 2px solid #c3e6cb;
48
+ }
49
+ .maintenance {
50
+ background-color: #f8d7da;
51
+ color: #721c24;
52
+ border: 2px solid #f5c6cb;
53
+ }
54
+ .metric-card {
55
+ background-color: #f8f9fa;
56
+ padding: 15px;
57
+ border-radius: 8px;
58
+ border-left: 4px solid #1f77b4;
59
+ }
60
+ </style>
61
+ """, unsafe_allow_html=True)
62
+
63
+
64
+ @st.cache_resource
65
+ def load_model():
66
+ """Load model from Hugging Face with authentication"""
67
+ try:
68
+ # Authenticate
69
+ hf_token = os.environ.get("HF_TOKEN")
70
+ if hf_token:
71
+ login(token=hf_token)
72
+
73
+ # Download model
74
+ model_path = hf_hub_download(
75
+ repo_id="Quantum9999/xgb-predictive-maintenance",
76
+ filename="xgb_tuned_model.joblib",
77
+ token=hf_token
78
+ )
79
+
80
+ # Load model
81
+ model = joblib.load(model_path)
82
+ return model, None
83
+
84
+ except Exception as e:
85
+ return None, str(e)
86
+
87
+
88
+ def main():
89
+ # Header
90
+ st.markdown(
91
+ '<div class="main-header">🔧 Engine Predictive Maintenance System</div>',
92
+ unsafe_allow_html=True
93
+ )
94
+ st.markdown(
95
+ '<div class="sub-header">AI-powered engine health monitoring & failure prediction</div>',
96
+ unsafe_allow_html=True
97
+ )
98
+
99
+ # Load model
100
+ model, error = load_model()
101
+
102
+ if model is None:
103
+ st.error(f"❌ Failed to load prediction model: {error}")
104
+ st.info("Please check Hugging Face configuration and ensure HF_TOKEN is set correctly.")
105
+ return
106
+
107
+ # Sidebar
108
+ with st.sidebar:
109
+ st.header("ℹ️ About")
110
+ st.write(
111
+ "This application predicts engine maintenance needs using "
112
+ "machine learning analysis of 6 critical sensor parameters."
113
+ )
114
+
115
+ st.header("📊 Model Information")
116
+ st.markdown("""
117
+ - **Algorithm**: XGBoost Classifier
118
+ - **Features**: 6 sensor readings
119
+ - **Target Classes**:
120
+ - 0: Normal Operation
121
+ - 1: Maintenance Required
122
+ - **Training Data**: 19,535 records
123
+ - **Test Accuracy**: ~92%
124
+ """)
125
+
126
+ st.header("🎯 How to Use")
127
+ st.markdown("""
128
+ 1. Enter current sensor readings in the input fields
129
+ 2. Click **'Predict Engine Condition'**
130
+ 3. Review prediction and confidence scores
131
+ 4. Take action based on results
132
+ """)
133
+
134
+ st.header("📈 Sensor Ranges")
135
+ st.markdown("""
136
+ **Normal Operating Ranges:**
137
+ - RPM: 161 - 2,239
138
+ - Lub Oil Pressure: 0.003 - 7.3 bar
139
+ - Fuel Pressure: 0.003 - 21.1 bar
140
+ - Coolant Pressure: 0.002 - 7.5 bar
141
+ - Lub Oil Temp: 71 - 90 °C
142
+ - Coolant Temp: 62 - 196 °C
143
+ """)
144
+
145
+ # Main content
146
+ st.header("📝 Enter Engine Sensor Readings")
147
+ st.markdown("---")
148
+
149
+ # Create two columns for input
150
+ col1, col2 = st.columns(2)
151
+
152
+ with col1:
153
+ st.subheader("⚙️ Speed & Pressure Sensors")
154
+
155
+ engine_rpm = st.number_input(
156
+ "Engine RPM (Revolutions per Minute)",
157
+ min_value=100.0,
158
+ max_value=2500.0,
159
+ value=791.0,
160
+ step=10.0,
161
+ help="Engine speed - Normal range: 161-2,239 RPM"
162
+ )
163
+
164
+ lub_oil_pressure = st.number_input(
165
+ "Lubrication Oil Pressure (bar)",
166
+ min_value=0.0,
167
+ max_value=10.0,
168
+ value=3.3,
169
+ step=0.1,
170
+ help="Lubricating oil pressure - Normal range: 0.003-7.266 bar"
171
+ )
172
+
173
+ fuel_pressure = st.number_input(
174
+ "Fuel Pressure (bar)",
175
+ min_value=0.0,
176
+ max_value=25.0,
177
+ value=6.7,
178
+ step=0.1,
179
+ help="Fuel delivery pressure - Normal range: 0.003-21.138 bar"
180
+ )
181
+
182
+ with col2:
183
+ st.subheader("🌡️ Temperature & Coolant Sensors")
184
+
185
+ coolant_pressure = st.number_input(
186
+ "Coolant Pressure (bar)",
187
+ min_value=0.0,
188
+ max_value=10.0,
189
+ value=2.3,
190
+ step=0.1,
191
+ help="Coolant system pressure - Normal range: 0.002-7.479 bar"
192
+ )
193
+
194
+ lub_oil_temp = st.number_input(
195
+ "Lubrication Oil Temperature (°C)",
196
+ min_value=60.0,
197
+ max_value=100.0,
198
+ value=77.6,
199
+ step=0.5,
200
+ help="Lubricating oil temperature - Normal range: 71.3-89.6 °C"
201
+ )
202
+
203
+ coolant_temp = st.number_input(
204
+ "Coolant Temperature (°C)",
205
+ min_value=50.0,
206
+ max_value=200.0,
207
+ value=78.4,
208
+ step=0.5,
209
+ help="Engine coolant temperature - Normal range: 61.7-195.5 °C"
210
+ )
211
+
212
+ # Prediction button
213
+ st.markdown("---")
214
+
215
+ if st.button("🔍 Predict Engine Condition", use_container_width=True, type="primary"):
216
+ # Prepare input data
217
+ input_df = pd.DataFrame([{
218
+ "Engine RPM": engine_rpm,
219
+ "Lub Oil Pressure": lub_oil_pressure,
220
+ "Fuel Pressure": fuel_pressure,
221
+ "Coolant Pressure": coolant_pressure,
222
+ "Lub Oil Temperature": lub_oil_temp,
223
+ "Coolant Temperature": coolant_temp
224
+ }])
225
+
226
+ try:
227
+ # Make prediction
228
+ prediction = model.predict(input_df)[0]
229
+ proba = model.predict_proba(input_df)[0]
230
+
231
+ # Display results
232
+ st.markdown("---")
233
+ st.header("🎯 Prediction Result")
234
+
235
+ if prediction == 0:
236
+ st.markdown(
237
+ '<div class="prediction-box normal">✅ Engine Operating Normally</div>',
238
+ unsafe_allow_html=True
239
+ )
240
+ st.success("✓ No maintenance required at this time. Engine is functioning within normal parameters.")
241
+ else:
242
+ st.markdown(
243
+ '<div class="prediction-box maintenance">⚠️ Maintenance Required</div>',
244
+ unsafe_allow_html=True
245
+ )
246
+ st.warning("⚠ Engine shows signs of potential failure. Schedule maintenance as soon as possible to prevent breakdown.")
247
+
248
+ # Confidence scores
249
+ st.subheader("📊 Prediction Confidence")
250
+
251
+ conf_col1, conf_col2 = st.columns(2)
252
+
253
+ with conf_col1:
254
+ st.markdown('<div class="metric-card">', unsafe_allow_html=True)
255
+ st.metric(
256
+ label="Normal Operation Probability",
257
+ value=f"{proba[0]:.2%}",
258
+ help="Confidence that engine is operating normally"
259
+ )
260
+ st.markdown('</div>', unsafe_allow_html=True)
261
+
262
+ with conf_col2:
263
+ st.markdown('<div class="metric-card">', unsafe_allow_html=True)
264
+ st.metric(
265
+ label="Maintenance Required Probability",
266
+ value=f"{proba[1]:.2%}",
267
+ help="Confidence that engine requires maintenance"
268
+ )
269
+ st.markdown('</div>', unsafe_allow_html=True)
270
+
271
+ # Input summary
272
+ with st.expander("📋 View Input Summary"):
273
+ st.dataframe(
274
+ input_df.T.rename(columns={0: "Value"}),
275
+ use_container_width=True
276
+ )
277
+
278
+ # Recommendations
279
+ with st.expander("💡 Recommendations"):
280
+ if prediction == 0:
281
+ st.markdown("""
282
+ **Current Status: Healthy**
283
+ - Continue regular monitoring
284
+ - Maintain current maintenance schedule
285
+ - Monitor for any sudden changes in sensor readings
286
+ - Schedule next routine inspection as planned
287
+ """)
288
+ else:
289
+ st.markdown("""
290
+ **Immediate Actions Required:**
291
+ - Schedule comprehensive engine inspection
292
+ - Check lubrication system
293
+ - Inspect cooling system
294
+ - Review fuel delivery system
295
+ - Monitor engine closely until serviced
296
+ - Consider reducing operational load
297
+ """)
298
+
299
+ except Exception as e:
300
+ st.error(f"❌ Prediction error: {e}")
301
+ st.info("Please verify all sensor values are within valid ranges and try again.")
302
+
303
+ # Footer
304
+ st.markdown("---")
305
+ st.markdown(
306
+ "<p style='text-align: center; color: #666; font-size: 14px;'>"
307
+ "🤖 Built with XGBoost & Streamlit | 🤗 Model hosted on Hugging Face<br>"
308
+ "Developed as part of ML Deployment & Automation Project"
309
+ "</p>",
310
+ unsafe_allow_html=True
311
+ )
312
+
313
+
314
+ if __name__ == "__main__":
315
+ main()
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
1
+ streamlit==1.31.0
2
+ pandas==2.1.4
3
+ numpy==1.26.3
4
+ scikit-learn==1.4.0
5
+ xgboost==2.0.3
6
+ joblib==1.3.2
7
+ huggingface-hub==0.20.3