StaticExposure commited on
Commit
32011ef
·
verified ·
1 Parent(s): 810b0c1

Fix custom LoRA loading: remap ai-toolkit diffusion_model.* keys to diffusers transformer.*

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -179,6 +179,32 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
179
  return image
180
 
181
  @spaces.GPU(duration=70)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, aspect_ratio, lora_scale, speed_mode, progress=gr.Progress(track_tqdm=True)):
183
  if selected_index is None:
184
  raise gr.Error("You must select a LoRA before proceeding.")
@@ -215,12 +241,7 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, asp
215
 
216
  # Load the selected style LoRA
217
  weight_name = selected_lora.get("weights", None)
218
- pipe.load_lora_weights(
219
- lora_path,
220
- weight_name=weight_name,
221
- low_cpu_mem_usage=True,
222
- adapter_name="style"
223
- )
224
 
225
  # Set both adapters active with their weights
226
  pipe.set_adapters(["lightning", "style"], adapter_weights=[1.0, lora_scale])
@@ -228,12 +249,7 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, asp
228
  # Quality mode - only load the style LoRA
229
  with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
230
  weight_name = selected_lora.get("weights", None)
231
- pipe.load_lora_weights(
232
- lora_path,
233
- weight_name=weight_name,
234
- low_cpu_mem_usage=True,
235
- adapter_name="style"
236
- )
237
  pipe.set_adapters(["style"], adapter_weights=[lora_scale])
238
 
239
  # Set random seed for reproducibility
 
179
  return image
180
 
181
  @spaces.GPU(duration=70)
182
+
183
+ def _load_style_lora(pipe, lora_path, weight_name=None):
184
+ """Load a style LoRA, converting ai-toolkit / ComfyUI key names to diffusers names."""
185
+ import os
186
+ from safetensors.torch import load_file
187
+ from huggingface_hub import hf_hub_download, list_repo_files
188
+ try:
189
+ if os.path.isfile(str(lora_path)):
190
+ local = str(lora_path)
191
+ else:
192
+ wn = weight_name
193
+ if not wn:
194
+ cands = [f for f in list_repo_files(lora_path) if f.endswith(".safetensors")]
195
+ if not cands:
196
+ raise FileNotFoundError("no .safetensors in " + str(lora_path))
197
+ wn = cands[0]
198
+ local = hf_hub_download(lora_path, wn)
199
+ sd = load_file(local)
200
+ if any(k.startswith("diffusion_model.") for k in sd):
201
+ sd = {("transformer." + k[len("diffusion_model."):]): v for k, v in sd.items()}
202
+ print("[patch] remapped diffusion_model.* -> transformer.* for", lora_path)
203
+ pipe.load_lora_weights(sd, adapter_name="style")
204
+ except Exception as e:
205
+ print("[patch] remap loader failed (" + repr(e) + "), falling back to default loader")
206
+ pipe.load_lora_weights(lora_path, weight_name=weight_name, low_cpu_mem_usage=True, adapter_name="style")
207
+
208
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, aspect_ratio, lora_scale, speed_mode, progress=gr.Progress(track_tqdm=True)):
209
  if selected_index is None:
210
  raise gr.Error("You must select a LoRA before proceeding.")
 
241
 
242
  # Load the selected style LoRA
243
  weight_name = selected_lora.get("weights", None)
244
+ _load_style_lora(pipe, lora_path, weight_name)
 
 
 
 
 
245
 
246
  # Set both adapters active with their weights
247
  pipe.set_adapters(["lightning", "style"], adapter_weights=[1.0, lora_scale])
 
249
  # Quality mode - only load the style LoRA
250
  with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
251
  weight_name = selected_lora.get("weights", None)
252
+ _load_style_lora(pipe, lora_path, weight_name)
 
 
 
 
 
253
  pipe.set_adapters(["style"], adapter_weights=[lora_scale])
254
 
255
  # Set random seed for reproducibility