ritz26 commited on
Commit
abbd19b
·
1 Parent(s): fa395c1

Update the code to solve tensorflow error on delpoyment

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -33,20 +33,23 @@ model, processor = None, None
33
  device = None
34
 
35
  def load_model():
36
- """Load model on demand (no caching to save storage)"""
37
  global model, processor, device
38
 
39
- # Determine device
40
- device = "cuda" if torch.cuda.is_available() else "cpu"
41
  print(f"[INFO] Using device: {device}")
42
 
43
  print("[INFO] Loading SAM model and processor...")
44
- model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50", cache_dir="/tmp/.cache")
 
 
 
 
45
  processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50", cache_dir="/tmp/.cache")
46
 
47
- # Move model to device
48
- model = model.to(device)
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"""