Quantum9999 commited on
Commit
e6b4fcd
Β·
verified Β·
1 Parent(s): 4cc4eed

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -1
  2. app.py +78 -58
Dockerfile CHANGED
@@ -33,9 +33,10 @@ USER user
33
  EXPOSE 7860
34
 
35
  # Health check for container monitoring
36
- HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
37
  CMD curl --fail http://localhost:7860/_stcore/health || exit 1
38
 
 
39
  # Run Streamlit on port 7860 with production settings
40
  CMD ["streamlit", "run", "app.py", \
41
  "--server.port=7860", \
 
33
  EXPOSE 7860
34
 
35
  # Health check for container monitoring
36
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
37
  CMD curl --fail http://localhost:7860/_stcore/health || exit 1
38
 
39
+
40
  # Run Streamlit on port 7860 with production settings
41
  CMD ["streamlit", "run", "app.py", \
42
  "--server.port=7860", \
app.py CHANGED
@@ -1,6 +1,5 @@
1
  """
2
  Streamlit Application for Engine Predictive Maintenance
3
- With detailed logging for debugging
4
  """
5
 
6
  import streamlit as st
@@ -34,7 +33,6 @@ except Exception as e:
34
  st.stop()
35
 
36
  # CRITICAL: Feature columns must EXACTLY match model training
37
- # These are from model_prep.py FEATURE_COLUMNS
38
  FEATURE_COLUMNS = [
39
  "Engine rpm",
40
  "Lub oil pressure",
@@ -90,56 +88,69 @@ st.markdown("""
90
 
91
  @st.cache_resource
92
  def load_model():
93
- """Load model from Hugging Face with detailed logging"""
94
 
95
  print("\n" + "=" * 70, file=sys.stderr)
96
  print("LOADING MODEL FROM HUGGING FACE", file=sys.stderr)
97
  print("=" * 70, file=sys.stderr)
98
 
99
- try:
100
- # Check for token
101
- hf_token = os.environ.get("HF_TOKEN")
102
- print(f"HF_TOKEN found: {hf_token is not None}", file=sys.stderr)
103
-
104
- if hf_token:
105
- print("Authenticating with Hugging Face...", file=sys.stderr)
106
- login(token=hf_token)
107
- print("βœ“ Authentication successful", file=sys.stderr)
108
- else:
109
- print("⚠ No HF_TOKEN - attempting public access", file=sys.stderr)
110
-
111
- # Download model
112
- print("\nDownloading model...", file=sys.stderr)
113
- print(" Repo: Quantum9999/xgb-predictive-maintenance", file=sys.stderr)
114
- print(" File: xgb_tuned_model.joblib", file=sys.stderr)
115
-
116
- model_path = hf_hub_download(
117
- repo_id="Quantum9999/xgb-predictive-maintenance",
118
- filename="xgb_tuned_model.joblib",
119
- token=hf_token
120
- )
121
- print(f"βœ“ Model downloaded: {model_path}", file=sys.stderr)
122
-
123
- # Load model
124
- print("Loading model into memory...", file=sys.stderr)
125
- model = joblib.load(model_path)
126
- print("βœ“ Model loaded successfully", file=sys.stderr)
127
-
128
- # Verify model features
129
- if hasattr(model, 'feature_names_in_'):
130
- print(f"Model expects features: {model.feature_names_in_}", file=sys.stderr)
131
-
132
- print("=" * 70 + "\n", file=sys.stderr)
133
-
134
- return model, None
135
-
136
- except Exception as e:
137
- error_msg = f"Model loading failed: {str(e)}"
138
- print(f"βœ— {error_msg}", file=sys.stderr)
139
- import traceback
140
- print(f"Traceback:\n{traceback.format_exc()}", file=sys.stderr)
141
- print("=" * 70 + "\n", file=sys.stderr)
142
- return None, error_msg
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
 
145
  def main():
@@ -157,8 +168,9 @@ def main():
157
  unsafe_allow_html=True
158
  )
159
 
160
- # Load model
161
- model, error = load_model()
 
162
 
163
  if model is None:
164
  st.error(f"❌ Failed to load prediction model")
@@ -175,6 +187,16 @@ def main():
175
  st.write(f"- HF_TOKEN set: {os.environ.get('HF_TOKEN') is not None}")
176
  st.write("- Expected repo: Quantum9999/xgb-predictive-maintenance")
177
  st.write("- Expected file: xgb_tuned_model.joblib")
 
 
 
 
 
 
 
 
 
 
178
 
179
  st.stop()
180
 
@@ -288,20 +310,18 @@ def main():
288
  st.markdown("---")
289
 
290
  if st.button("πŸ” Predict Engine Condition", use_container_width=True, type="primary"):
291
- # CRITICAL FIX: Use exact column names that match model training
292
  input_df = pd.DataFrame([{
293
- "Engine rpm": engine_rpm, # Lowercase 'rpm'
294
- "Lub oil pressure": lub_oil_pressure, # Lowercase 'oil'
295
- "Fuel pressure": fuel_pressure, # Lowercase 'pressure'
296
- "Coolant pressure": coolant_pressure, # Lowercase 'pressure'
297
- "lub oil temp": lub_oil_temp, # All lowercase
298
- "Coolant temp": coolant_temp # Lowercase 'temp'
299
  }])
300
 
301
  try:
302
  print(f"Making prediction with input: {input_df.to_dict()}", file=sys.stderr)
303
- print(f"Input columns: {input_df.columns.tolist()}", file=sys.stderr)
304
- print(f"Expected columns: {FEATURE_COLUMNS}", file=sys.stderr)
305
 
306
  # Make prediction
307
  prediction = model.predict(input_df)[0]
 
1
  """
2
  Streamlit Application for Engine Predictive Maintenance
 
3
  """
4
 
5
  import streamlit as st
 
33
  st.stop()
34
 
35
  # CRITICAL: Feature columns must EXACTLY match model training
 
36
  FEATURE_COLUMNS = [
37
  "Engine rpm",
38
  "Lub oil pressure",
 
88
 
89
  @st.cache_resource
90
  def load_model():
91
+ """Load model from Hugging Face with detailed logging and retries"""
92
 
93
  print("\n" + "=" * 70, file=sys.stderr)
94
  print("LOADING MODEL FROM HUGGING FACE", file=sys.stderr)
95
  print("=" * 70, file=sys.stderr)
96
 
97
+ max_retries = 3
98
+ retry_count = 0
99
+
100
+ while retry_count < max_retries:
101
+ try:
102
+ # CORRECT: Use HF_TOKEN (as configured in your HF Space secrets)
103
+ hf_token = os.environ.get("HF_TOKEN")
104
+ print(f"HF_TOKEN found: {hf_token is not None}", file=sys.stderr)
105
+
106
+ if hf_token:
107
+ print("Authenticating with Hugging Face...", file=sys.stderr)
108
+ login(token=hf_token)
109
+ print("βœ“ Authentication successful", file=sys.stderr)
110
+ else:
111
+ print("⚠ No HF_TOKEN - attempting public access", file=sys.stderr)
112
+
113
+ # Download model
114
+ print("\nDownloading model...", file=sys.stderr)
115
+ print(" Repo: Quantum9999/xgb-predictive-maintenance", file=sys.stderr)
116
+ print(" File: xgb_tuned_model.joblib", file=sys.stderr)
117
+
118
+ model_path = hf_hub_download(
119
+ repo_id="Quantum9999/xgb-predictive-maintenance",
120
+ filename="xgb_tuned_model.joblib",
121
+ token=hf_token,
122
+ cache_dir="/tmp/hf_cache" # Use tmp for faster access
123
+ )
124
+ print(f"βœ“ Model downloaded: {model_path}", file=sys.stderr)
125
+
126
+ # Load model
127
+ print("Loading model into memory...", file=sys.stderr)
128
+ model = joblib.load(model_path)
129
+ print("βœ“ Model loaded successfully", file=sys.stderr)
130
+
131
+ # Verify model features
132
+ if hasattr(model, 'feature_names_in_'):
133
+ print(f"Model expects features: {model.feature_names_in_}", file=sys.stderr)
134
+
135
+ print("=" * 70 + "\n", file=sys.stderr)
136
+
137
+ return model, None
138
+
139
+ except Exception as e:
140
+ retry_count += 1
141
+ error_msg = f"Model loading attempt {retry_count}/{max_retries} failed: {str(e)}"
142
+ print(f"βœ— {error_msg}", file=sys.stderr)
143
+
144
+ if retry_count < max_retries:
145
+ import time
146
+ wait_time = 2 * retry_count
147
+ print(f"Retrying in {wait_time} seconds...", file=sys.stderr)
148
+ time.sleep(wait_time)
149
+ else:
150
+ import traceback
151
+ print(f"Final traceback:\n{traceback.format_exc()}", file=sys.stderr)
152
+ print("=" * 70 + "\n", file=sys.stderr)
153
+ return None, error_msg
154
 
155
 
156
  def main():
 
168
  unsafe_allow_html=True
169
  )
170
 
171
+ # Load model with progress indicator
172
+ with st.spinner("Loading AI model... This may take a moment."):
173
+ model, error = load_model()
174
 
175
  if model is None:
176
  st.error(f"❌ Failed to load prediction model")
 
187
  st.write(f"- HF_TOKEN set: {os.environ.get('HF_TOKEN') is not None}")
188
  st.write("- Expected repo: Quantum9999/xgb-predictive-maintenance")
189
  st.write("- Expected file: xgb_tuned_model.joblib")
190
+
191
+ st.write("\n**Your Setup (from screenshots):**")
192
+ st.write("βœ… HF Space has HF_TOKEN secret (Image 1)")
193
+ st.write("βœ… GitHub has HF_EN_TOKEN secret (Image 2)")
194
+ st.write("βœ… GitHub token for pushing code (Image 3)")
195
+
196
+ st.write("\n**Next Steps:**")
197
+ st.write("1. Verify HF_TOKEN secret exists in Space settings")
198
+ st.write("2. Check Space logs for detailed error messages")
199
+ st.write("3. Ensure model repo is accessible")
200
 
201
  st.stop()
202
 
 
310
  st.markdown("---")
311
 
312
  if st.button("πŸ” Predict Engine Condition", use_container_width=True, type="primary"):
313
+ # Create input DataFrame with exact column names
314
  input_df = pd.DataFrame([{
315
+ "Engine rpm": engine_rpm,
316
+ "Lub oil pressure": lub_oil_pressure,
317
+ "Fuel pressure": fuel_pressure,
318
+ "Coolant pressure": coolant_pressure,
319
+ "lub oil temp": lub_oil_temp,
320
+ "Coolant temp": coolant_temp
321
  }])
322
 
323
  try:
324
  print(f"Making prediction with input: {input_df.to_dict()}", file=sys.stderr)
 
 
325
 
326
  # Make prediction
327
  prediction = model.predict(input_df)[0]