Maheentouqeer1 commited on
Commit
c6f1743
·
verified ·
1 Parent(s): 7f48e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -427,19 +427,40 @@ def load_text_model():
427
 
428
  @st.cache_resource(show_spinner=False)
429
  def load_image_model():
430
- # Try .keras first, fall back to .h5
431
  import os
432
- try:
433
- path = hf_hub_download(
434
- repo_id="syeda-Rija20/image-detector",
435
- filename="image_detector_v2.keras"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  )
 
 
 
 
437
  except Exception:
438
- path = hf_hub_download(
439
- repo_id="syeda-Rija20/image-detector",
440
- filename="image_detector_v2.h5"
441
- )
442
- model = tf.keras.models.load_model(path)
443
  return model
444
 
445
 
 
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
 
466