syeda-Rija20 commited on
Commit
5457494
Β·
verified Β·
1 Parent(s): a8a23a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -38
app.py CHANGED
@@ -461,28 +461,13 @@ def load_text_model():
461
  # except Exception as e:
462
  # raise RuntimeError(f"Failed to load image model: {e}")
463
 
464
- # return model
465
  @st.cache_resource(show_spinner=False)
466
  def load_image_model():
467
- # ── Patch incompatible Keras layers BEFORE any loading ──
468
- try:
469
- import keras
470
- from keras.layers import RandomFlip, RandomRotation, RandomZoom, RandomBrightness, RandomContrast
471
-
472
- for cls in [RandomFlip, RandomRotation, RandomZoom, RandomBrightness, RandomContrast]:
473
- _orig = cls.__init__
474
- def _patched(self, *args, _orig=_orig, **kwargs):
475
- kwargs.pop("data_format", None)
476
- _orig(self, *args, **kwargs)
477
- cls.__init__ = _patched
478
- except Exception:
479
- pass # If patching fails, try loading anyway
480
-
481
  filenames_to_try = [
482
- "image_detector_v2.keras",
 
483
  "image_detector_v2.h5",
484
  ]
485
-
486
  path = None
487
  for fname in filenames_to_try:
488
  try:
@@ -500,27 +485,10 @@ def load_image_model():
500
  "Check that 'syeda-Rija20/image-detector' is public and the file exists."
501
  )
502
 
503
- try:
504
- import keras
505
- model = keras.saving.load_model(path, compile=False)
506
- return model
507
- except Exception as e1:
508
- try:
509
- import tensorflow as tf
510
- model = tf.keras.models.load_model(path, compile=False)
511
- return model
512
- except Exception as e2:
513
- raise RuntimeError(
514
- f"Failed to load image model.\n"
515
- f"keras attempt: {e1}\n"
516
- f"tf.keras attempt: {e2}"
517
- )
518
-
519
-
520
-
521
-
522
-
523
-
524
  # ─────────────────────────────────────────────
525
  # PREDICTION FUNCTIONS
526
  # ─────────────────────────────────────────────
 
461
  # except Exception as e:
462
  # raise RuntimeError(f"Failed to load image model: {e}")
463
 
 
464
  @st.cache_resource(show_spinner=False)
465
  def load_image_model():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
  filenames_to_try = [
467
+ "image_detector_clean.keras", # ← clean model first
468
+ "image_detector_v2.keras", # ← fallbacks
469
  "image_detector_v2.h5",
470
  ]
 
471
  path = None
472
  for fname in filenames_to_try:
473
  try:
 
485
  "Check that 'syeda-Rija20/image-detector' is public and the file exists."
486
  )
487
 
488
+ import keras
489
+ model = keras.saving.load_model(path, compile=False)
490
+ return model
491
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
  # ─────────────────────────────────────────────
493
  # PREDICTION FUNCTIONS
494
  # ─────────────────────────────────────────────