devmm03 Claude Sonnet 5 commited on
Commit
56da09b
·
1 Parent(s): a385053

fix: decorate InferenceEngine.run_user_file with @spaces.GPU

Browse files

The REST /api/upload endpoint called inference_engine.run_user_file()
directly, outside any @spaces.GPU context. ZeroGPU keeps model weights
disk-offloaded until code runs inside a @spaces.GPU-decorated function,
so conv2d hit a placeholder tensor: "Cannot apply function ... on
disk-offloaded Tensor". Only the Gradio test tab worked, since its
handler was separately decorated.

Decorate the shared run_user_file method instead of each call site, and
drop the now-redundant decorator on the Gradio handler to avoid nesting
two ZeroGPU schedule calls in the same request.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +6 -8
  2. services/inference.py +20 -0
app.py CHANGED
@@ -64,17 +64,15 @@ def b64_to_pil(b64_str: str) -> Image.Image:
64
  return Image.open(BytesIO(data))
65
 
66
 
67
- @spaces.GPU
68
  def run_gradio_inference(image_file):
69
- """Gradio handler for interactive testing."""
 
 
 
 
 
70
  if image_file is None:
71
  return None, None, "Please provide an image or GeoTIFF file."
72
-
73
- import torch
74
- if torch.cuda.is_available() and getattr(inference_engine, "device", None) and inference_engine.device.type != "cuda":
75
- inference_engine.device = torch.device("cuda")
76
- if getattr(inference_engine, "hat_model", None) is not None:
77
- inference_engine.hat_model = inference_engine.hat_model.to("cuda")
78
 
79
  file_path = Path(image_file)
80
  try:
 
64
  return Image.open(BytesIO(data))
65
 
66
 
 
67
  def run_gradio_inference(image_file):
68
+ """Gradio handler for interactive testing.
69
+
70
+ Note: no @spaces.GPU here — InferenceEngine.run_user_file (called below)
71
+ is already decorated, and nesting two @spaces.GPU calls would trigger a
72
+ second, redundant ZeroGPU schedule/allocation from inside the first.
73
+ """
74
  if image_file is None:
75
  return None, None, "Please provide an image or GeoTIFF file."
 
 
 
 
 
 
76
 
77
  file_path = Path(image_file)
78
  try:
services/inference.py CHANGED
@@ -10,6 +10,25 @@ import time
10
  import uuid
11
  from pathlib import Path
12
  from typing import Dict, Any, Optional, Tuple, List
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  import numpy as np
14
  import torch
15
  import torch.nn.functional as F
@@ -301,6 +320,7 @@ class InferenceEngine:
301
  "images": images,
302
  }
303
 
 
304
  def run_user_file(self, file_path: Path, model_name: str = "hat") -> Dict[str, Any]:
305
  """Handles uploaded file or preset file."""
306
  file_path = Path(file_path)
 
10
  import uuid
11
  from pathlib import Path
12
  from typing import Dict, Any, Optional, Tuple, List
13
+
14
+ # IMPORTANT FOR HUGGING FACE ZEROGPU:
15
+ # `spaces` must be imported before any CUDA-related package (e.g. torch).
16
+ # @spaces.GPU is what materializes disk-offloaded model weights onto the
17
+ # GPU for the duration of the call; without it, tensor ops raise
18
+ # "Cannot apply function ... on disk-offloaded Tensor".
19
+ try:
20
+ import spaces
21
+ except ImportError:
22
+ class _MockSpaces:
23
+ @staticmethod
24
+ def GPU(func=None, **kwargs):
25
+ if func is not None:
26
+ return func
27
+ def decorator(f):
28
+ return f
29
+ return decorator
30
+ spaces = _MockSpaces()
31
+
32
  import numpy as np
33
  import torch
34
  import torch.nn.functional as F
 
320
  "images": images,
321
  }
322
 
323
+ @spaces.GPU
324
  def run_user_file(self, file_path: Path, model_name: str = "hat") -> Dict[str, Any]:
325
  """Handles uploaded file or preset file."""
326
  file_path = Path(file_path)