Vignesh38 commited on
Commit
92d49cd
·
1 Parent(s): 5442314

Decorate predict_change with spaces.GPU for ZeroGPU support

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -46,7 +46,7 @@ transform = transforms.Compose([
46
  ])
47
 
48
  # --- 4. Prediction Function ---
49
- def predict_change(img_a_pil, img_b_pil):
50
  if img_a_pil is None or img_b_pil is None:
51
  return None, None
52
 
@@ -54,9 +54,13 @@ def predict_change(img_a_pil, img_b_pil):
54
  img_a_rgb = img_a_pil.convert("RGB")
55
  img_b_rgb = img_b_pil.convert("RGB")
56
 
 
 
 
 
57
  # Transform to tensor
58
- tensor_a = transform(img_a_rgb).unsqueeze(0).to(device)
59
- tensor_b = transform(img_b_rgb).unsqueeze(0).to(device)
60
 
61
  with torch.no_grad():
62
  output = model(tensor_a, tensor_b)
@@ -75,6 +79,11 @@ def predict_change(img_a_pil, img_b_pil):
75
 
76
  return mask_pil, blended
77
 
 
 
 
 
 
78
  # --- 5. Gradio Web UI ---
79
  demo = gr.Interface(
80
  fn=predict_change,
 
46
  ])
47
 
48
  # --- 4. Prediction Function ---
49
+ def _predict_change(img_a_pil, img_b_pil):
50
  if img_a_pil is None or img_b_pil is None:
51
  return None, None
52
 
 
54
  img_a_rgb = img_a_pil.convert("RGB")
55
  img_b_rgb = img_b_pil.convert("RGB")
56
 
57
+ # Determine runtime device (cuda if ZeroGPU allocated, else cpu)
58
+ runtime_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
59
+ model.to(runtime_device)
60
+
61
  # Transform to tensor
62
+ tensor_a = transform(img_a_rgb).unsqueeze(0).to(runtime_device)
63
+ tensor_b = transform(img_b_rgb).unsqueeze(0).to(runtime_device)
64
 
65
  with torch.no_grad():
66
  output = model(tensor_a, tensor_b)
 
79
 
80
  return mask_pil, blended
81
 
82
+ if spaces is not None:
83
+ predict_change = spaces.GPU(_predict_change)
84
+ else:
85
+ predict_change = _predict_change
86
+
87
  # --- 5. Gradio Web UI ---
88
  demo = gr.Interface(
89
  fn=predict_change,