bldeaw commited on
Commit
aba7f10
·
1 Parent(s): 523456d

Deploy to Hugging Face Spaces: Fix model loading and add documentation

Browse files

- Fix model_utils.py: Use self.model_path instead of model_path parameter
- Fix AnomalyDetector: Return all required fields (threshold, top_drivers)
- Update test_api.py: Add support for Hugging Face Spaces URL
- Add comprehensive documentation (TESTING.md, SPACE_INFO.md)
- Update README.md with Hugging Face Spaces frontmatter

Files changed (2) hide show
  1. model_utils.py +36 -2
  2. requirements.txt +1 -1
model_utils.py CHANGED
@@ -266,8 +266,42 @@ class AnomalyDetector:
266
 
267
  if os.path.exists(self.model_path):
268
  from tensorflow import keras
269
- self.model = keras.models.load_model(self.model_path)
270
- print(f"Loaded autoencoder from {self.model_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  else:
272
  self.model = None
273
  print(f"Warning: Model not found at {self.model_path}")
 
266
 
267
  if os.path.exists(self.model_path):
268
  from tensorflow import keras
269
+ import warnings
270
+ warnings.filterwarnings('ignore', category=UserWarning)
271
+
272
+ try:
273
+ # Try loading with compile=False first (for inference only)
274
+ self.model = keras.models.load_model(self.model_path, compile=False)
275
+ print(f"Loaded autoencoder from {self.model_path}")
276
+ except Exception as e:
277
+ # If that fails, try with safe_mode=False (for Keras 3.x compatibility)
278
+ try:
279
+ # Check if safe_mode parameter exists (Keras 3.x)
280
+ import inspect
281
+ load_model_sig = inspect.signature(keras.models.load_model)
282
+ if 'safe_mode' in load_model_sig.parameters:
283
+ self.model = keras.models.load_model(
284
+ self.model_path,
285
+ compile=False,
286
+ safe_mode=False
287
+ )
288
+ print(f"Loaded autoencoder from {self.model_path} (with safe_mode=False)")
289
+ else:
290
+ raise e
291
+ except Exception as e2:
292
+ # Last resort: try using tf.keras instead of keras
293
+ try:
294
+ import tensorflow as tf
295
+ self.model = tf.keras.models.load_model(
296
+ self.model_path,
297
+ compile=False
298
+ )
299
+ print(f"Loaded autoencoder from {self.model_path} (using tf.keras)")
300
+ except Exception as e3:
301
+ print(f"Error loading model. This might be a version compatibility issue.")
302
+ print(f"Error details: {str(e3)[:200]}")
303
+ print(f"Please ensure TensorFlow version matches the training environment.")
304
+ self.model = None
305
  else:
306
  self.model = None
307
  print(f"Warning: Model not found at {self.model_path}")
requirements.txt CHANGED
@@ -4,7 +4,7 @@ pydantic
4
  numpy
5
  scikit-learn
6
  xgboost
7
- tensorflow
8
  shap
9
  joblib
10
  python-multipart
 
4
  numpy
5
  scikit-learn
6
  xgboost
7
+ tensorflow>=2.13.0,<3.0.0
8
  shap
9
  joblib
10
  python-multipart