bann commited on
Commit
42cbb83
·
1 Parent(s): feee32b

fix(lora): patch non-diffusers Wan LoRA converter for ComfyUI and Civitai key compatibility

Browse files
Files changed (1) hide show
  1. app.py +53 -3
app.py CHANGED
@@ -24,6 +24,53 @@ from PIL import Image, ImageOps
24
  from diffusers.utils import export_to_video
25
  from safetensors import safe_open
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  DEFAULT_CIVITAI_KEY = "50a9e1bd474c03b856070a7272d8015c"
28
  DEFAULT_MODEL_REPO = os.environ.get("WAN_MODEL_REPO", "Wan-AI/Wan2.2-I2V-A14B-Diffusers")
29
  GPU_SIZE = os.environ.get("WAN_GPU_SIZE", "xlarge")
@@ -534,11 +581,14 @@ def _generate_video_gpu(
534
  for lora_path, lora_scale in lora_configs:
535
  if lora_path and lora_scale > 0:
536
  adapter_name = f"lora_{len(active_lora_names)}"
537
- pipe.load_lora_weights(lora_path, adapter_name=adapter_name)
538
- active_lora_names.append(adapter_name)
 
 
 
539
 
540
  if active_lora_names:
541
- scales = [scale for _, scale in lora_configs if scale > 0]
542
  pipe.set_adapters(active_lora_names, adapter_weights=scales)
543
 
544
  generator = torch.Generator("cuda").manual_seed(int(seed))
 
24
  from diffusers.utils import export_to_video
25
  from safetensors import safe_open
26
 
27
+ # =========================================================================
28
+ # Monkey-patch Diffusers Wan LoRA converter for broad community compatibility
29
+ # =========================================================================
30
+ try:
31
+ import diffusers.loaders.lora_conversion_utils as conv_utils
32
+ import diffusers.loaders.lora_pipeline as lora_pipe
33
+
34
+ _orig_wan_converter = getattr(conv_utils, "_convert_non_diffusers_wan_lora_to_diffusers", None)
35
+
36
+ def patched_convert_non_diffusers_wan_lora_to_diffusers(original_state_dict):
37
+ # 1. Normalize prefixes: community LoRAs often start directly with "blocks."
38
+ # while diffusers internal converter expects "diffusion_model.blocks."
39
+ prefixed_dict = {}
40
+ for k, v in original_state_dict.items():
41
+ if k.startswith("blocks."):
42
+ prefixed_dict[f"diffusion_model.{k}"] = v
43
+ elif k.startswith("transformer.blocks."):
44
+ prefixed_dict[f"diffusion_model.{k[12:]}"] = v
45
+ else:
46
+ prefixed_dict[k] = v
47
+
48
+ if _orig_wan_converter is not None:
49
+ try:
50
+ # Attempt standard conversion with normalized keys
51
+ return _orig_wan_converter(prefixed_dict.copy())
52
+ except Exception as err:
53
+ print(f"[wan] standard converter note: {err}; applying robust fallback mapping", flush=True)
54
+
55
+ # 2. Robust fallback mapping for any non-standard Wan LoRA format
56
+ converted_dict = {}
57
+ for k, v in prefixed_dict.items():
58
+ new_k = k
59
+ if new_k.startswith("diffusion_model."):
60
+ new_k = new_k[len("diffusion_model."):]
61
+ if not new_k.startswith("transformer."):
62
+ new_k = f"transformer.{new_k}"
63
+ converted_dict[new_k] = v
64
+ return converted_dict
65
+
66
+ if _orig_wan_converter is not None:
67
+ conv_utils._convert_non_diffusers_wan_lora_to_diffusers = patched_convert_non_diffusers_wan_lora_to_diffusers
68
+ if hasattr(lora_pipe, "_convert_non_diffusers_wan_lora_to_diffusers"):
69
+ lora_pipe._convert_non_diffusers_wan_lora_to_diffusers = patched_convert_non_diffusers_wan_lora_to_diffusers
70
+ print("[wan] LoRA converter patch successfully applied.", flush=True)
71
+ except Exception as patch_err:
72
+ print(f"[wan] could not apply LoRA converter patch: {patch_err}", flush=True)
73
+
74
  DEFAULT_CIVITAI_KEY = "50a9e1bd474c03b856070a7272d8015c"
75
  DEFAULT_MODEL_REPO = os.environ.get("WAN_MODEL_REPO", "Wan-AI/Wan2.2-I2V-A14B-Diffusers")
76
  GPU_SIZE = os.environ.get("WAN_GPU_SIZE", "xlarge")
 
581
  for lora_path, lora_scale in lora_configs:
582
  if lora_path and lora_scale > 0:
583
  adapter_name = f"lora_{len(active_lora_names)}"
584
+ try:
585
+ pipe.load_lora_weights(lora_path, adapter_name=adapter_name)
586
+ active_lora_names.append(adapter_name)
587
+ except Exception as lora_err:
588
+ print(f"[wan] warning: skipping incompatible LoRA `{lora_path}`: {lora_err}", flush=True)
589
 
590
  if active_lora_names:
591
+ scales = [scale for _, scale in lora_configs if scale > 0][:len(active_lora_names)]
592
  pipe.set_adapters(active_lora_names, adapter_weights=scales)
593
 
594
  generator = torch.Generator("cuda").manual_seed(int(seed))