signsur4739379373 commited on
Commit
8670264
·
verified ·
1 Parent(s): 7690aa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -6
app.py CHANGED
@@ -238,14 +238,97 @@ from safetensors.torch import load_file
238
  from huggingface_hub import hf_hub_download
239
  import torch.nn.functional as F
240
 
241
- # --- 1. setup pipeline with lightning (this works fine) ---
242
- pipe = QwenImageEditPlusPipeline.from_single_file(
243
- "path/to/Qwen-Rapid-AIO-NSFW-v21.safetensors",
244
- original_config="Qwen/Qwen-Image-Edit-2511", # pulls the config from the base repo
245
- scheduler=scheduler,
246
- torch_dtype=torch.bfloat16 # use bf16 for speed on zerogpu
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  ).to("cuda")
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  # print("loading lightning lora...")
250
  # pipe.load_lora_weights(
251
  # "lightx2v/Qwen-Image-Edit-2511-Lightning",
 
238
  from huggingface_hub import hf_hub_download
239
  import torch.nn.functional as F
240
 
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+ #################################
254
+
255
+
256
+
257
+ v21_path = hf_hub_download(
258
+ repo_id="Phr00t/Qwen-Image-Edit-Rapid-AIO",
259
+ filename="v21/Qwen-Rapid-AIO-NSFW-v21.safetensors",
260
+ repo_type="model"
261
+ )
262
+ print(f"file ready at: {v21_path}")
263
+
264
+ # 2. load the base architecture from the official qwen repo
265
+ # we need this to create the skeleton of the model
266
+ print("loading base pipeline architecture...")
267
+ pipe = QwenImageEditPlusPipeline.from_pretrained(
268
+ "Qwen/Qwen-Image-Edit-2511",
269
+ scheduler=EulerAncestralDiscreteScheduler.from_pretrained(
270
+ "Qwen/Qwen-Image-Edit-2511",
271
+ subfolder="scheduler"
272
+ ),
273
+ torch_dtype=torch.bfloat16
274
  ).to("cuda")
275
 
276
+ # 3. load the v21 weights
277
+ print("loading v21 weights into memory...")
278
+ state_dict = load_file(v21_path)
279
+
280
+ # 4. filter and inject weights
281
+ # the AIO file is a "frankenstein" merge of unet, vae, and text encoder.
282
+ # we need to map the keys correctly. comfyui keys usually differ from diffusers keys.
283
+
284
+ # we attempt to load the diffusion model (transformer) first as it's the most critical
285
+ print("grafting weights onto the pipeline...")
286
+ try:
287
+ # try loading into the transformer/unet component
288
+ # most comfyui merges for this model flatten the keys.
289
+ # we use strict=False to ignore VAE/CLIP keys that might be in the file but belong elsewhere
290
+ if hasattr(pipe, "transformer"):
291
+ # standard 2511 naming
292
+ incompatible = pipe.transformer.load_state_dict(state_dict, strict=False)
293
+ elif hasattr(pipe, "unet"):
294
+ # older 2509 naming
295
+ incompatible = pipe.unet.load_state_dict(state_dict, strict=False)
296
+ else:
297
+ # absolute fallback: try to load to the root modules
298
+ # this iterates through the pipe and tries to match keys to submodules
299
+ for name, module in pipe.named_children():
300
+ if "model" in name or "transformer" in name or "unet" in name:
301
+ print(f"attempting load into: {name}")
302
+ module.load_state_dict(state_dict, strict=False)
303
+
304
+ print("success. v21 weights are active.")
305
+
306
+ except Exception as e:
307
+ print(f"major error during weight loading: {e}")
308
+ print("attempting root load (desperation mode)...")
309
+ pipe.load_state_dict(state_dict, strict=False)
310
+
311
+ # 5. cleanup and optimize
312
+ del state_dict
313
+ gc.collect()
314
+ torch.cuda.empty_cache()
315
+
316
+
317
+
318
+
319
+
320
+ #################################
321
+
322
+
323
+
324
+ # # --- 1. setup pipeline with lightning (this works fine) ---
325
+ # pipe = QwenImageEditPlusPipeline.from_single_file(
326
+ # "path/to/Qwen-Rapid-AIO-NSFW-v21.safetensors",
327
+ # original_config="Qwen/Qwen-Image-Edit-2511", # pulls the config from the base repo
328
+ # scheduler=scheduler,
329
+ # torch_dtype=torch.bfloat16 # use bf16 for speed on zerogpu
330
+ # ).to("cuda")
331
+
332
  # print("loading lightning lora...")
333
  # pipe.load_lora_weights(
334
  # "lightx2v/Qwen-Image-Edit-2511-Lightning",