Maheentouqeer1 syeda-Rija20 commited on
Commit
a8a23a2
Β·
1 Parent(s): a084d26

Update app.py (#2)

Browse files

- Update app.py (d3d0d9f7af6ab465ec836d4861e8a615fd26a9fe)


Co-authored-by: Syeda Reja <syeda-Rija20@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -464,10 +464,20 @@ def load_text_model():
464
  # return model
465
  @st.cache_resource(show_spinner=False)
466
  def load_image_model():
467
- import os
468
- import tempfile
 
 
 
 
 
 
 
 
 
 
 
469
 
470
- # Download the weights file
471
  filenames_to_try = [
472
  "image_detector_v2.keras",
473
  "image_detector_v2.h5",
@@ -486,58 +496,29 @@ def load_image_model():
486
 
487
  if path is None:
488
  raise RuntimeError(
489
- "Could not download image model from HuggingFace Hub."
 
490
  )
491
 
492
- # ── Rebuild EfficientNetB3 architecture manually ──
493
- # This bypasses the Keras version mismatch on RandomFlip data_format
494
  try:
495
  import keras
496
- from keras import layers
497
-
498
- # Build the same architecture the model was trained with,
499
- # WITHOUT the augmentation Sequential (not needed at inference)
500
- base = keras.applications.EfficientNetB3(
501
- include_top=False,
502
- weights=None, # no pretrained weights; we load them below
503
- input_shape=(224, 224, 3),
504
- )
505
- base.trainable = True
506
-
507
- inputs = keras.Input(shape=(224, 224, 3))
508
- x = base(inputs, training=False)
509
- x = layers.GlobalAveragePooling2D()(x)
510
- x = layers.BatchNormalization(momentum=0.99, epsilon=0.001)(x)
511
- x = layers.Dense(256, activation="relu")(x)
512
- x = layers.Dropout(0.4)(x, training=False)
513
- outputs = layers.Dense(1, activation="sigmoid")(x)
514
-
515
- model = keras.Model(inputs, outputs)
516
- model.load_weights(path)
517
  return model
518
-
519
- except Exception as e_rebuild:
520
- # ── Last resort: patch RandomFlip then load ──
521
  try:
522
- import keras
523
- from keras.layers import RandomFlip as _RF
524
-
525
- _orig_init = _RF.__init__
526
-
527
- def _patched_init(self, mode="horizontal", seed=None, **kwargs):
528
- kwargs.pop("data_format", None) # strip unsupported kwarg
529
- _orig_init(self, mode=mode, seed=seed, **kwargs)
530
-
531
- _RF.__init__ = _patched_init
532
-
533
- model = keras.saving.load_model(path, compile=False)
534
  return model
535
- except Exception as e_patch:
536
  raise RuntimeError(
537
  f"Failed to load image model.\n"
538
- f"Rebuild attempt: {e_rebuild}\n"
539
- f"Patch attempt: {e_patch}"
540
- )
 
 
 
 
541
 
542
 
543
  # ─────────────────────────────────────────────
 
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",
 
496
 
497
  if path is None:
498
  raise RuntimeError(
499
+ "Could not download image model from HuggingFace Hub. "
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
  # ─────────────────────────────────────────────