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ea962a
72cdcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4beb6
72cdcc6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
Streamlit Application for Engine Predictive Maintenance
Production Version
"""

import streamlit as st
import pandas as pd
import os
import sys

# Print to console (will show in HF Space logs)
print("=" * 70, file=sys.stderr)
print("APP STARTING - INITIALIZATION", file=sys.stderr)
print("=" * 70, file=sys.stderr)

# Page Configuration MUST be first
st.set_page_config(
    page_title="Engine Predictive Maintenance",
    page_icon="πŸ”§",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Import after page config
try:
    print("Importing huggingface_hub...", file=sys.stderr)
    from huggingface_hub import hf_hub_download, login
    print("Importing joblib...", file=sys.stderr)
    import joblib
    print("βœ“ All imports successful", file=sys.stderr)
except Exception as e:
    print(f"βœ— Import error: {e}", file=sys.stderr)
    st.error(f"Import failed: {e}")
    st.stop()

# CRITICAL: Feature columns must EXACTLY match model training
FEATURE_COLUMNS = [
    "Engine rpm",
    "Lub oil pressure",
    "Fuel pressure",
    "Coolant pressure",
    "lub oil temp",
    "Coolant temp"
]

# Custom CSS
st.markdown("""
<style>
.main-header {
    font-size: 42px;
    font-weight: bold;
    color: #1f77b4;
    text-align: center;
    margin-bottom: 10px;
}
.sub-header {
    font-size: 18px;
    color: #555;
    text-align: center;
    margin-bottom: 30px;
}
.prediction-box {
    padding: 20px;
    border-radius: 10px;
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    margin-top: 20px;
}
.normal {
    background-color: #d4edda;
    color: #155724;
    border: 2px solid #c3e6cb;
}
.maintenance {
    background-color: #f8d7da;
    color: #721c24;
    border: 2px solid #f5c6cb;
}
.metric-card {
    background-color: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    border-left: 4px solid #1f77b4;
}
</style>
""", unsafe_allow_html=True)


@st.cache_resource
def load_model():
    """Load model from Hugging Face with detailed logging and retries"""
    
    print("\n" + "=" * 70, file=sys.stderr)
    print("LOADING MODEL FROM HUGGING FACE", file=sys.stderr)
    print("=" * 70, file=sys.stderr)
    
    max_retries = 3
    retry_count = 0
    
    while retry_count < max_retries:
        try:
            # Get HF_TOKEN and clean it
            hf_token = os.environ.get("HF_TOKEN")
            if hf_token:
                hf_token = hf_token.strip()  # Remove any newlines or whitespace
            
            print(f"HF_TOKEN found: {hf_token is not None}", file=sys.stderr)
            
            if hf_token:
                print("Authenticating with Hugging Face...", file=sys.stderr)
                login(token=hf_token)
                print("βœ“ Authentication successful", file=sys.stderr)
            else:
                print("⚠ No HF_TOKEN - attempting public access", file=sys.stderr)
            
            # Download model
            print("\nDownloading model...", file=sys.stderr)
            print("  Repo: Quantum9999/xgb-predictive-maintenance", file=sys.stderr)
            print("  File: xgb_tuned_model.joblib", file=sys.stderr)
            
            model_path = hf_hub_download(
                repo_id="Quantum9999/xgb-predictive-maintenance",
                filename="xgb_tuned_model.joblib",
                token=hf_token,
                cache_dir="/tmp/hf_cache"
            )
            print(f"βœ“ Model downloaded: {model_path}", file=sys.stderr)
            
            # Load model
            print("Loading model into memory...", file=sys.stderr)
            model = joblib.load(model_path)
            print("βœ“ Model loaded successfully", file=sys.stderr)
            
            # Verify model features
            if hasattr(model, 'feature_names_in_'):
                print(f"Model expects features: {model.feature_names_in_}", file=sys.stderr)
            
            print("=" * 70 + "\n", file=sys.stderr)
            
            return model, None
            
        except Exception as e:
            retry_count += 1
            error_msg = f"Model loading attempt {retry_count}/{max_retries} failed: {str(e)}"
            print(f"βœ— {error_msg}", file=sys.stderr)
            
            if retry_count < max_retries:
                import time
                wait_time = 2 * retry_count
                print(f"Retrying in {wait_time} seconds...", file=sys.stderr)
                time.sleep(wait_time)
            else:
                import traceback
                print(f"Final traceback:\n{traceback.format_exc()}", file=sys.stderr)
                print("=" * 70 + "\n", file=sys.stderr)
                return None, error_msg


def main():
    """Main application"""
    
    print("Starting main application...", file=sys.stderr)
    
    # Header
    st.markdown(
        '<div class="main-header">πŸ”§ Engine Predictive Maintenance System</div>',
        unsafe_allow_html=True
    )
    st.markdown(
        '<div class="sub-header">AI-powered engine health monitoring & failure prediction</div>',
        unsafe_allow_html=True
    )

    # Load model with progress indicator
    with st.spinner("Loading AI model... This may take a moment."):
        model, error = load_model()
    
    if model is None:
        st.error(f"❌ Failed to load prediction model")
        st.code(error)
        
        with st.expander("πŸ” Troubleshooting"):
            st.write("**Possible Issues:**")
            st.write("1. HF_TOKEN not set in Space secrets")
            st.write("2. Model repository is private")
            st.write("3. Model filename is incorrect")
            st.write("4. Network connectivity issue")
            
            st.write("\n**Current Configuration:**")
            st.write(f"- HF_TOKEN set: {os.environ.get('HF_TOKEN') is not None}")
            st.write("- Expected repo: Quantum9999/xgb-predictive-maintenance")
            st.write("- Expected file: xgb_tuned_model.joblib")
        
        st.stop()
    
    st.success("βœ“ Model loaded successfully!")

    # Sidebar
    with st.sidebar:
        st.header("ℹ️ About")
        st.write(
            "This application predicts engine maintenance needs using "
            "machine learning analysis of 6 critical sensor parameters."
        )
        
        st.header("πŸ“Š Model Information")
        st.markdown("""
        - **Algorithm**: XGBoost Classifier
        - **Features**: 6 sensor readings
        - **Target Classes**: 
          - 0: Normal Operation
          - 1: Maintenance Required
        - **Training Data**: 19,535 records
        """)
        
        st.header("🎯 How to Use")
        st.markdown("""
        1. Enter current sensor readings
        2. Click 'Predict Engine Condition'
        3. Review prediction and confidence
        4. Take action based on results
        """)
        
        st.header("πŸ“ˆ Sensor Ranges")
        st.markdown("""
        **Normal Operating Ranges:**
        - RPM: 161 - 2,239
        - Lub Oil Pressure: 0.003 - 7.3 bar
        - Fuel Pressure: 0.003 - 21.1 bar
        - Coolant Pressure: 0.002 - 7.5 bar
        - Lub Oil Temp: 71 - 90 Β°C
        - Coolant Temp: 62 - 196 Β°C
        """)

    # Main content
    st.header("πŸ“ Enter Engine Sensor Readings")
    st.markdown("---")

    # Input columns
    col1, col2 = st.columns(2)

    with col1:
        st.subheader("βš™οΈ Speed & Pressure Sensors")
        
        engine_rpm = st.number_input(
            "Engine RPM (Revolutions per Minute)",
            min_value=100.0,
            max_value=2500.0,
            value=791.0,
            step=10.0,
            help="Engine speed - Normal range: 161-2,239 RPM"
        )
        
        lub_oil_pressure = st.number_input(
            "Lubrication Oil Pressure (bar)",
            min_value=0.0,
            max_value=10.0,
            value=3.3,
            step=0.1,
            help="Lubricating oil pressure - Normal range: 0.003-7.266 bar"
        )
        
        fuel_pressure = st.number_input(
            "Fuel Pressure (bar)",
            min_value=0.0,
            max_value=25.0,
            value=6.7,
            step=0.1,
            help="Fuel delivery pressure - Normal range: 0.003-21.138 bar"
        )

    with col2:
        st.subheader("🌑️ Temperature & Coolant Sensors")
        
        coolant_pressure = st.number_input(
            "Coolant Pressure (bar)",
            min_value=0.0,
            max_value=10.0,
            value=2.3,
            step=0.1,
            help="Coolant system pressure - Normal range: 0.002-7.479 bar"
        )
        
        lub_oil_temp = st.number_input(
            "Lubrication Oil Temperature (Β°C)",
            min_value=60.0,
            max_value=100.0,
            value=77.6,
            step=0.5,
            help="Lubricating oil temperature - Normal range: 71.3-89.6 Β°C"
        )
        
        coolant_temp = st.number_input(
            "Coolant Temperature (Β°C)",
            min_value=50.0,
            max_value=200.0,
            value=78.4,
            step=0.5,
            help="Engine coolant temperature - Normal range: 61.7-195.5 Β°C"
        )

    # Prediction button
    st.markdown("---")
    
    if st.button("πŸ” Predict Engine Condition", use_container_width=True, type="primary"):
        # Create input DataFrame with exact column names
        input_df = pd.DataFrame([{
            "Engine rpm": engine_rpm,
            "Lub oil pressure": lub_oil_pressure,
            "Fuel pressure": fuel_pressure,
            "Coolant pressure": coolant_pressure,
            "lub oil temp": lub_oil_temp,
            "Coolant temp": coolant_temp
        }])

        try:
            print(f"Making prediction with input: {input_df.to_dict()}", file=sys.stderr)
            
            # Make prediction
            prediction = model.predict(input_df)[0]
            proba = model.predict_proba(input_df)[0]
            
            print(f"Prediction: {prediction}, Probabilities: {proba}", file=sys.stderr)

            # Display results
            st.markdown("---")
            st.header("🎯 Prediction Result")

            if prediction == 0:
                st.markdown(
                    '<div class="prediction-box normal">βœ… Engine Operating Normally</div>',
                    unsafe_allow_html=True
                )
                st.success("βœ“ No maintenance required at this time. Engine is functioning within normal parameters.")
            else:
                st.markdown(
                    '<div class="prediction-box maintenance">⚠️ Maintenance Required</div>',
                    unsafe_allow_html=True
                )
                st.warning("⚠ Engine shows signs of potential failure. Schedule maintenance as soon as possible to prevent breakdown.")

            # Confidence scores
            st.subheader("πŸ“Š Prediction Confidence")
            
            conf_col1, conf_col2 = st.columns(2)
            
            with conf_col1:
                st.markdown('<div class="metric-card">', unsafe_allow_html=True)
                st.metric(
                    label="Normal Operation Probability",
                    value=f"{proba[0]:.2%}",
                    help="Confidence that engine is operating normally"
                )
                st.markdown('</div>', unsafe_allow_html=True)
            
            with conf_col2:
                st.markdown('<div class="metric-card">', unsafe_allow_html=True)
                st.metric(
                    label="Maintenance Required Probability",
                    value=f"{proba[1]:.2%}",
                    help="Confidence that engine requires maintenance"
                )
                st.markdown('</div>', unsafe_allow_html=True)

            # Input summary
            with st.expander("πŸ“‹ View Input Summary"):
                st.dataframe(
                    input_df.T.rename(columns={0: "Value"}),
                    use_container_width=True
                )
                
            # Recommendations
            with st.expander("πŸ’‘ Recommendations"):
                if prediction == 0:
                    st.markdown("""
                    **Current Status: Healthy**
                    - Continue regular monitoring
                    - Maintain current maintenance schedule
                    - Monitor for any sudden changes in sensor readings
                    - Schedule next routine inspection as planned
                    """)
                else:
                    st.markdown("""
                    **Immediate Actions Required:**
                    - Schedule comprehensive engine inspection
                    - Check lubrication system
                    - Inspect cooling system
                    - Review fuel delivery system
                    - Monitor engine closely until serviced
                    - Consider reducing operational load
                    """)
        
        except Exception as e:
            error_msg = f"Prediction error: {e}"
            print(f"βœ— {error_msg}", file=sys.stderr)
            import traceback
            print(f"Traceback:\n{traceback.format_exc()}", file=sys.stderr)
            
            st.error(f"❌ {error_msg}")
            st.info("Please verify all sensor values are within valid ranges and try again.")
    
    # Footer
    st.markdown("---")
    st.markdown(
        "<p style='text-align: center; color: #666; font-size: 14px;'>"
        "πŸ€– Built with XGBoost & Streamlit | πŸ€— Model hosted on Hugging Face<br>"
        "Developed as part of ML Deployment & Automation Project"
        "</p>",
        unsafe_allow_html=True
    )


if __name__ == "__main__":
    print("Entering main()...", file=sys.stderr)
    try:
        main()
        print("βœ“ Main completed successfully", file=sys.stderr)
    except Exception as e:
        print(f"βœ— FATAL ERROR: {e}", file=sys.stderr)
        import traceback
        print(f"Traceback:\n{traceback.format_exc()}", file=sys.stderr)
        st.error(f"Application error: {e}")