Tyler Ng commited on
Commit
f53953f
·
verified ·
1 Parent(s): 9b7b885

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -105,6 +105,7 @@ class ModelManager:
105
  def __init__(self):
106
  self._inspy: Optional[Remover] = None
107
  self._withoutbg: Optional[object] = None
 
108
  self._torch_models: Dict[str, torch.nn.Module] = {}
109
  self._torch_model_on_gpu: Optional[str] = None
110
  self._rembg_sessions: Dict[str, object] = {}
@@ -130,9 +131,24 @@ class ModelManager:
130
  self._inspy = Remover(jit=False)
131
  return self._inspy
132
 
133
- def _load_withoutbg(self):
134
- if self._withoutbg is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  self._withoutbg = WithoutBG.opensource()
 
 
136
  return self._withoutbg
137
 
138
  def _offload_torch_models_from_gpu(self, keep_name: str):
@@ -274,6 +290,7 @@ class ModelManager:
274
  out = ensure_rgba(out)
275
 
276
  elif model_name == "withoutBG":
 
277
  model = self._load_withoutbg()
278
  out = model.remove_background(img_rgb)
279
  out = ensure_rgba(out)
 
105
  def __init__(self):
106
  self._inspy: Optional[Remover] = None
107
  self._withoutbg: Optional[object] = None
108
+ self._withoutbg_had_gpu: bool = False # Track if withoutBG was loaded with GPU
109
  self._torch_models: Dict[str, torch.nn.Module] = {}
110
  self._torch_model_on_gpu: Optional[str] = None
111
  self._rembg_sessions: Dict[str, object] = {}
 
131
  self._inspy = Remover(jit=False)
132
  return self._inspy
133
 
134
+ def _load_withoutbg(self, force_reload: bool = False):
135
+ """
136
+ Load withoutBG model.
137
+ Automatically reloads if GPU became available after initial load.
138
+ """
139
+ gpu_available_now = torch.cuda.is_available()
140
+
141
+ # Reload if: forced, not loaded yet, or GPU is now available but wasn't before
142
+ need_reload = (
143
+ force_reload or
144
+ self._withoutbg is None or
145
+ (gpu_available_now and not self._withoutbg_had_gpu)
146
+ )
147
+
148
+ if need_reload:
149
  self._withoutbg = WithoutBG.opensource()
150
+ self._withoutbg_had_gpu = gpu_available_now
151
+
152
  return self._withoutbg
153
 
154
  def _offload_torch_models_from_gpu(self, keep_name: str):
 
290
  out = ensure_rgba(out)
291
 
292
  elif model_name == "withoutBG":
293
+ # Will auto-reload if GPU became available (ZeroGPU)
294
  model = self._load_withoutbg()
295
  out = model.remove_background(img_rgb)
296
  out = ensure_rgba(out)