collinschreyer-dev commited on
Commit
29555cd
·
1 Parent(s): 667734c

Fix GPU detection: use runtime check instead of import-time

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -33,7 +33,13 @@ from shapely.geometry import shape
33
  # ---------------------------------------------------------------------------
34
  MODEL = None
35
  PROCESSOR = None
36
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
37
  matplotlib.use("Agg")
38
 
39
  # ---------------------------------------------------------------------------
@@ -85,8 +91,11 @@ def load_model():
85
  global MODEL, PROCESSOR
86
  if MODEL is None:
87
  from transformers import Sam3Model, Sam3Processor
88
- MODEL = Sam3Model.from_pretrained("facebook/sam3").to(DEVICE)
 
 
89
  PROCESSOR = Sam3Processor.from_pretrained("facebook/sam3")
 
90
  return MODEL, PROCESSOR
91
 
92
  # ---------------------------------------------------------------------------
@@ -163,8 +172,9 @@ def compute_tile_windows(img_w: int, img_h: int, tile_size: int, overlap: int) -
163
 
164
  def segment_tile(tile_rgb: np.ndarray, prompt: str, confidence: float):
165
  model, processor = load_model()
 
166
  image = Image.fromarray(tile_rgb)
167
- inputs = processor(images=image, text=prompt, return_tensors="pt").to(DEVICE)
168
  with torch.no_grad():
169
  outputs = model(**inputs)
170
  results = processor.post_process_instance_segmentation(
@@ -627,12 +637,18 @@ Upload a GeoTIFF (any size), select feature types to extract, and watch SAM 3 pr
627
  tile by tile with live confidence tracking.
628
  """
629
 
630
- gpu_status = (
631
- f"Running on **{torch.cuda.get_device_name(0)}** "
632
- f"({torch.cuda.get_device_properties(0).total_memory / (1024**3):.0f} GB)"
633
- if torch.cuda.is_available()
634
- else "No GPU detected — inference will be slow"
635
- )
 
 
 
 
 
 
636
 
637
  with gr.Blocks(css=CUSTOM_CSS, title="Janus — Feature Extraction") as demo:
638
  gr.Markdown(HEADER_MD)
 
33
  # ---------------------------------------------------------------------------
34
  MODEL = None
35
  PROCESSOR = None
36
+
37
+
38
+ def get_device():
39
+ """Detect GPU at runtime, not import time."""
40
+ if torch.cuda.is_available():
41
+ return "cuda"
42
+ return "cpu"
43
  matplotlib.use("Agg")
44
 
45
  # ---------------------------------------------------------------------------
 
91
  global MODEL, PROCESSOR
92
  if MODEL is None:
93
  from transformers import Sam3Model, Sam3Processor
94
+ device = get_device()
95
+ print(f"[MODEL] Loading SAM 3 on {device}...")
96
+ MODEL = Sam3Model.from_pretrained("facebook/sam3").to(device)
97
  PROCESSOR = Sam3Processor.from_pretrained("facebook/sam3")
98
+ print(f"[MODEL] SAM 3 loaded on {device}")
99
  return MODEL, PROCESSOR
100
 
101
  # ---------------------------------------------------------------------------
 
172
 
173
  def segment_tile(tile_rgb: np.ndarray, prompt: str, confidence: float):
174
  model, processor = load_model()
175
+ device = get_device()
176
  image = Image.fromarray(tile_rgb)
177
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
178
  with torch.no_grad():
179
  outputs = model(**inputs)
180
  results = processor.post_process_instance_segmentation(
 
637
  tile by tile with live confidence tracking.
638
  """
639
 
640
+ def _gpu_status():
641
+ try:
642
+ if torch.cuda.is_available():
643
+ return (
644
+ f"Running on **{torch.cuda.get_device_name(0)}** "
645
+ f"({torch.cuda.get_device_properties(0).total_memory / (1024**3):.0f} GB)"
646
+ )
647
+ except Exception:
648
+ pass
649
+ return "No GPU detected — inference will be slow"
650
+
651
+ gpu_status = _gpu_status()
652
 
653
  with gr.Blocks(css=CUSTOM_CSS, title="Janus — Feature Extraction") as demo:
654
  gr.Markdown(HEADER_MD)