Onise commited on
Commit
3404bfb
·
verified ·
1 Parent(s): bc7d867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -208,8 +208,10 @@ LORA_TRIGGER_WORDS = {
208
  "ultimate_realistic_breast":"urb, realistic breast",
209
  }
210
 
211
- # Tracks which adapter names have been loaded into the pipeline this session.
212
- # ZeroGPU resets VRAM on every @spaces.GPU call, so we reload as needed.
 
 
213
  LOADED_ADAPTERS: set[str] = set()
214
 
215
  # ── Helpers ────────────────────────────────────────────────────────────────────
@@ -230,10 +232,8 @@ def append_triggers(current_prompt: str, lora_name: str) -> str:
230
 
231
 
232
  def load_and_apply_stack(extra_adapters: list[str], extra_weights: list[float]):
233
- """Lazy-load any unseen adapters, then activate the full stack.
234
-
235
- Returns the list of adapters that were successfully loaded (some may be
236
- skipped if the remote file is missing or corrupted).
237
  """
238
  if not extra_adapters:
239
  pipe.disable_lora()
@@ -253,8 +253,14 @@ def load_and_apply_stack(extra_adapters: list[str], extra_weights: list[float]):
253
  )
254
  LOADED_ADAPTERS.add(name)
255
  except Exception as e:
256
- print(f"WARNING: Failed to load LoRA '{name}': {e}")
257
- continue
 
 
 
 
 
 
258
  loaded.append(name)
259
  weights_out.append(weight)
260
 
@@ -290,9 +296,9 @@ def infer(
290
  torch.cuda.empty_cache()
291
  # ─────────────────────────────────────────────────────────────────────────
292
 
293
- # ── ZeroGPU FIX: GPU state resets between @spaces.GPU calls, so we must
294
- # clear the adapter tracking set to force reloading on each invocation.
295
- LOADED_ADAPTERS.clear()
296
 
297
  if input_image is None:
298
  raise gr.Error("Please upload an image.")
@@ -352,9 +358,8 @@ def infer(
352
  raise gr.Error(f"GPU error: {e}")
353
  raise gr.Error(f"Inference failed: {e}")
354
  finally:
355
- # ── OOM FIX: Unload LoRAs and clean up after each inference ──────────
356
- if loaded_adapters:
357
- pipe.disable_lora()
358
  gc.collect()
359
  torch.cuda.empty_cache()
360
  # ─────────────────────────────────────────────────────────────────────
 
208
  "ultimate_realistic_breast":"urb, realistic breast",
209
  }
210
 
211
+ # Tracks which adapter names have been successfully loaded into the pipeline.
212
+ # We keep this across @spaces.GPU calls because the LoRA weights stay in RAM
213
+ # (only active GPU state is reset by ZeroGPU). This avoids re-downloading
214
+ # and re-registering the same adapters on every generation.
215
  LOADED_ADAPTERS: set[str] = set()
216
 
217
  # ── Helpers ────────────────────────────────────────────────────────────────────
 
232
 
233
 
234
  def load_and_apply_stack(extra_adapters: list[str], extra_weights: list[float]):
235
+ """Lazy-load any unseen adapters (only once per app lifetime), then
236
+ activate exactly the requested stack for this inference.
 
 
237
  """
238
  if not extra_adapters:
239
  pipe.disable_lora()
 
253
  )
254
  LOADED_ADAPTERS.add(name)
255
  except Exception as e:
256
+ # If it already exists from a previous call in this Space session,
257
+ # we treat it as success (no need to reload weights).
258
+ if "already exists" in str(e).lower() or "Adapter" in str(e):
259
+ print(f"Adapter '{name}' already registered — reusing.")
260
+ LOADED_ADAPTERS.add(name) # ensure it's tracked
261
+ else:
262
+ print(f"WARNING: Failed to load LoRA '{name}': {e}")
263
+ continue
264
  loaded.append(name)
265
  weights_out.append(weight)
266
 
 
296
  torch.cuda.empty_cache()
297
  # ─────────────────────────────────────────────────────────────────────────
298
 
299
+ # Always start with a clean LoRA state (previous active adapters are
300
+ # disabled by the finally block, but this is extra safety).
301
+ pipe.disable_lora()
302
 
303
  if input_image is None:
304
  raise gr.Error("Please upload an image.")
 
358
  raise gr.Error(f"GPU error: {e}")
359
  raise gr.Error(f"Inference failed: {e}")
360
  finally:
361
+ # ── OOM FIX: Unload active LoRAs and clean up after each inference ───
362
+ pipe.disable_lora()
 
363
  gc.collect()
364
  torch.cuda.empty_cache()
365
  # ─────────────────────────────────────────────────────────────────────