Spaces:
Runtime error
Runtime error
Update the code to solve tensorflow error on delpoyment
Browse files
app.py
CHANGED
|
@@ -33,20 +33,23 @@ model, processor = None, None
|
|
| 33 |
device = None
|
| 34 |
|
| 35 |
def load_model():
|
| 36 |
-
"""Load model on demand (
|
| 37 |
global model, processor, device
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
device = "
|
| 41 |
print(f"[INFO] Using device: {device}")
|
| 42 |
|
| 43 |
print("[INFO] Loading SAM model and processor...")
|
| 44 |
-
model = SamModel.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50", cache_dir="/tmp/.cache")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
print(f"[INFO] Model and processor loaded successfully on {device}!")
|
| 50 |
|
| 51 |
def cleanup_temp_files():
|
| 52 |
"""Clean up temporary files to save storage"""
|
|
|
|
| 33 |
device = None
|
| 34 |
|
| 35 |
def load_model():
|
| 36 |
+
"""Load model on demand (CPU-only to avoid meta tensor/device issues on Spaces)."""
|
| 37 |
global model, processor, device
|
| 38 |
|
| 39 |
+
# Force CPU on Spaces to avoid meta tensor errors when moving devices
|
| 40 |
+
device = "cpu"
|
| 41 |
print(f"[INFO] Using device: {device}")
|
| 42 |
|
| 43 |
print("[INFO] Loading SAM model and processor...")
|
| 44 |
+
model = SamModel.from_pretrained(
|
| 45 |
+
"Zigeng/SlimSAM-uniform-50",
|
| 46 |
+
cache_dir="/tmp/.cache",
|
| 47 |
+
torch_dtype=torch.float32,
|
| 48 |
+
)
|
| 49 |
processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50", cache_dir="/tmp/.cache")
|
| 50 |
|
| 51 |
+
# Do NOT move model with .to(); keep it on CPU to prevent meta tensor errors
|
| 52 |
+
print("[INFO] Model and processor loaded successfully on CPU!")
|
|
|
|
| 53 |
|
| 54 |
def cleanup_temp_files():
|
| 55 |
"""Clean up temporary files to save storage"""
|