Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,33 +13,34 @@ app.config['UPLOAD_FOLDER'] = 'uploads'
|
|
| 13 |
# Create uploads folder if it doesn't exist
|
| 14 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 15 |
|
| 16 |
-
# Load the model with
|
| 17 |
print("Loading model...")
|
| 18 |
import warnings
|
| 19 |
warnings.filterwarnings('ignore')
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
import tensorflow.keras.backend as K
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
# Convert old 'batch_shape' to new 'shape'
|
| 28 |
-
if 'batch_shape' in kwargs:
|
| 29 |
-
batch_shape = kwargs.pop('batch_shape')
|
| 30 |
-
if batch_shape is not None and len(batch_shape) > 1:
|
| 31 |
-
kwargs['shape'] = batch_shape[1:]
|
| 32 |
-
return InputLayer(*args, **kwargs)
|
| 33 |
|
| 34 |
-
# Try loading with custom objects
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
-
print(f"
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def resize_with_padding(img, target_size):
|
| 45 |
"""Resize image while maintaining aspect ratio and add padding"""
|
|
|
|
| 13 |
# Create uploads folder if it doesn't exist
|
| 14 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 15 |
|
| 16 |
+
# Load the model with compatibility handling
|
| 17 |
print("Loading model...")
|
| 18 |
import warnings
|
| 19 |
warnings.filterwarnings('ignore')
|
| 20 |
|
| 21 |
+
# Set TensorFlow to use less memory
|
| 22 |
+
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
|
|
|
|
| 23 |
|
| 24 |
+
# Try loading with h5py directly for better compatibility
|
| 25 |
+
import h5py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
try:
|
| 28 |
+
# Use legacy loader which is more compatible
|
| 29 |
+
from tensorflow.keras.saving import legacy
|
| 30 |
+
model = legacy.load_model.load_model('cancer_model.h5', compile=False)
|
| 31 |
+
print("Model loaded successfully with legacy loader!")
|
| 32 |
except Exception as e:
|
| 33 |
+
print(f"Legacy loader failed: {e}")
|
| 34 |
+
try:
|
| 35 |
+
# Fallback: try standard loader
|
| 36 |
+
model = load_model('cancer_model.h5', compile=False)
|
| 37 |
+
print("Model loaded successfully!")
|
| 38 |
+
except Exception as e2:
|
| 39 |
+
print(f"Standard loader also failed: {e2}")
|
| 40 |
+
print("\nPlease re-save your model using:")
|
| 41 |
+
print(" model.save('cancer_model.h5', save_format='h5')")
|
| 42 |
+
print("Or convert to SavedModel format for better compatibility")
|
| 43 |
+
raise
|
| 44 |
|
| 45 |
def resize_with_padding(img, target_size):
|
| 46 |
"""Resize image while maintaining aspect ratio and add padding"""
|