syeda-Rija20 commited on
Commit
2078288
·
verified ·
1 Parent(s): 44d18f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -68,28 +68,36 @@ def load_text_model():
68
  def load_image_model():
69
  import os
70
  import tensorflow as tf
 
71
 
72
  model_path = "/tmp/image_detector_v2.h5"
73
 
74
- if not os.path.exists(model_path):
75
- import requests
76
- print("Downloading model...")
77
- url = "https://huggingface.co/Muniba930/image-detector/resolve/main/image_detector_v2.h5?download=true"
78
-
79
- with requests.get(url, stream=True) as r:
80
- r.raise_for_status()
81
- with open(model_path, "wb") as f:
82
- for chunk in r.iter_content(chunk_size=8192):
83
- f.write(chunk)
84
 
85
- # Verify file size
86
- size_mb = os.path.getsize(model_path) / (1024 * 1024)
87
- print(f"Downloaded: {size_mb:.1f} MB")
 
88
 
89
- if size_mb < 10:
90
- os.remove(model_path)
91
- st.error(f"Download failed — only {size_mb:.1f} MB received")
92
- return None
 
 
 
 
 
 
 
 
93
 
94
  model = tf.keras.models.load_model(model_path)
95
  return model
 
68
  def load_image_model():
69
  import os
70
  import tensorflow as tf
71
+ import requests
72
 
73
  model_path = "/tmp/image_detector_v2.h5"
74
 
75
+ # Always re-download (remove old corrupted file)
76
+ if os.path.exists(model_path):
77
+ os.remove(model_path)
78
+
79
+ url = "https://huggingface.co/Muniba930/image-detector/resolve/main/image_detector_v2.h5?download=true"
80
+
81
+ with requests.get(url, stream=True, allow_redirects=True) as r:
82
+ r.raise_for_status()
 
 
83
 
84
+ # Debug: show what we're getting
85
+ content_type = r.headers.get("Content-Type", "unknown")
86
+ content_length = r.headers.get("Content-Length", "unknown")
87
+ st.write(f"Debug → Content-Type: {content_type}, Size: {content_length} bytes")
88
 
89
+ with open(model_path, "wb") as f:
90
+ for chunk in r.iter_content(chunk_size=8192):
91
+ f.write(chunk)
92
+
93
+ size_mb = os.path.getsize(model_path) / (1024 * 1024)
94
+ st.write(f"Debug → Downloaded file size: {size_mb:.2f} MB")
95
+
96
+ # Check first bytes to verify it's a real HDF5 file
97
+ with open(model_path, "rb") as f:
98
+ header = f.read(8)
99
+ st.write(f"Debug → File header bytes: {header.hex()}")
100
+ # Real HDF5 file starts with: 894844460d0a1a0a
101
 
102
  model = tf.keras.models.load_model(model_path)
103
  return model