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

Update app.py (#1)

Browse files

- Update app.py (2b097d385d623014870a7ecb8a9e148084e000a5)


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

Files changed (1) hide show
  1. app.py +86 -10
app.py CHANGED
@@ -425,11 +425,49 @@ def load_text_model():
425
  return tokenizer, model
426
 
427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  @st.cache_resource(show_spinner=False)
429
  def load_image_model():
430
  import os
431
- import keras # Keras 3 explicit import
432
 
 
433
  filenames_to_try = [
434
  "image_detector_v2.keras",
435
  "image_detector_v2.h5",
@@ -448,20 +486,58 @@ def load_image_model():
448
 
449
  if path is None:
450
  raise RuntimeError(
451
- "Could not download image model from HuggingFace Hub. "
452
- "Check that 'syeda-Rija20/image-detector' is public and the file exists."
453
  )
454
 
455
- # Try keras 3 native load first, then tf.keras fallback
 
456
  try:
457
- model = keras.saving.load_model(path, compile=False)
458
- except Exception:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459
  try:
460
- model = tf.keras.models.load_model(path, compile=False)
461
- except Exception as e:
462
- raise RuntimeError(f"Failed to load image model: {e}")
 
 
 
 
 
 
 
463
 
464
- return model
 
 
 
 
 
 
 
465
 
466
 
467
  # ─────────────────────────────────────────────
 
425
  return tokenizer, model
426
 
427
 
428
+ # @st.cache_resource(show_spinner=False)
429
+ # def load_image_model():
430
+ # import os
431
+ # import keras # Keras 3 explicit import
432
+
433
+ # filenames_to_try = [
434
+ # "image_detector_v2.keras",
435
+ # "image_detector_v2.h5",
436
+ # ]
437
+
438
+ # path = None
439
+ # for fname in filenames_to_try:
440
+ # try:
441
+ # path = hf_hub_download(
442
+ # repo_id="syeda-Rija20/image-detector",
443
+ # filename=fname
444
+ # )
445
+ # break
446
+ # except Exception:
447
+ # continue
448
+
449
+ # if path is None:
450
+ # raise RuntimeError(
451
+ # "Could not download image model from HuggingFace Hub. "
452
+ # "Check that 'syeda-Rija20/image-detector' is public and the file exists."
453
+ # )
454
+
455
+ # # Try keras 3 native load first, then tf.keras fallback
456
+ # try:
457
+ # model = keras.saving.load_model(path, compile=False)
458
+ # except Exception:
459
+ # try:
460
+ # model = tf.keras.models.load_model(path, compile=False)
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
  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
 
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
  # ─────────────────────────────────────────────