primerz commited on
Commit
cad7bba
·
verified ·
1 Parent(s): 86e1d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -269,7 +269,7 @@ def update_selection(selected_state: gr.SelectData, sdxl_loras, face_strength, i
269
  lora_repo = sdxl_loras[selected_state.index]["repo"]
270
  new_placeholder = "Type a prompt to use your selected LoRA"
271
  weight_name = sdxl_loras[selected_state.index]["weights"]
272
- updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) {'(non-commercial LoRA, `cc-by-nc`)' if sdxl_loras[selected_state.index]['is_nc'] else '' }"
273
 
274
  for lora_list in lora_defaults:
275
  if lora_list["model"] == sdxl_loras[selected_state.index]["repo"]:
@@ -355,7 +355,8 @@ def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_stre
355
  # Enhanced prompt processing
356
  if custom_lora_path and custom_lora[1]:
357
  prompt = f"{prompt} {custom_lora[1]}"
358
- else:
 
359
  for lora_list in lora_defaults:
360
  if lora_list["model"] == sdxl_loras[selected_state_index]["repo"]:
361
  prompt_full = lora_list.get("prompt", None)
@@ -385,9 +386,11 @@ def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_stre
385
  elif custom_lora_path:
386
  repo_name = custom_lora_path
387
  full_path_lora = custom_lora_path
388
- else:
389
  repo_name = sdxl_loras[selected_state_index]["repo"]
390
  full_path_lora = state_dicts[repo_name]["saved_name"]
 
 
391
 
392
  repo_name = repo_name.rstrip("/").lower()
393
 
@@ -469,29 +472,33 @@ def generate_image(prompt, negative, face_emb, face_image, face_kps, image_stren
469
  # Load LoRA with better error handling
470
  try:
471
  pipe.load_lora_weights(full_path_lora)
472
- pipe.fuse_lora(lora_scale)
473
  last_fused = True
474
 
475
  # Handle pivotal tuning embeddings (if needed for future LoRAs)
476
- is_pivotal = sdxl_loras[selected_state_index]["is_pivotal"]
477
- if is_pivotal:
478
- text_embedding_name = sdxl_loras[selected_state_index]["text_embedding_weights"]
479
- embedding_path = hf_hub_download(repo_id=repo_name, filename=text_embedding_name, repo_type="model")
480
- state_dict_embedding = load_file(embedding_path)
481
- pipe.load_textual_inversion(
482
- state_dict_embedding["clip_l" if "clip_l" in state_dict_embedding else "text_encoders_0"],
483
- token=["<s0>", "<s1>"],
484
- text_encoder=pipe.text_encoder,
485
- tokenizer=pipe.tokenizer
486
- )
487
- pipe.load_textual_inversion(
488
- state_dict_embedding["clip_g" if "clip_g" in state_dict_embedding else "text_encoders_1"],
489
- token=["<s0>", "<s1>"],
490
- text_encoder=pipe.text_encoder_2,
491
- tokenizer=pipe.tokenizer_2
492
- )
 
 
493
  except Exception as e:
494
  print(f"Error loading LoRA: {e}")
 
 
495
  raise gr.Error(f"Failed to load LoRA: {str(e)}")
496
 
497
  print("Processing prompt...")
@@ -651,7 +658,7 @@ with gr.Blocks(css="custom.css") as demo:
651
  display: block;
652
  font-weight: normal;
653
  opacity: 0.75;
654
- ">🔥 Improved: Better face identity preservation, Enhanced landscape mode, Multiple face support<br>AlbedoBase XL v2.1 + InstantID + ControlNet</small></span></h1>""",
655
  elem_id="title",
656
  )
657
  selected_state = gr.State()
 
269
  lora_repo = sdxl_loras[selected_state.index]["repo"]
270
  new_placeholder = "Type a prompt to use your selected LoRA"
271
  weight_name = sdxl_loras[selected_state.index]["weights"]
272
+ updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨ {'(non-commercial LoRA, `cc-by-nc`)' if sdxl_loras[selected_state.index]['is_nc'] else '' }"
273
 
274
  for lora_list in lora_defaults:
275
  if lora_list["model"] == sdxl_loras[selected_state.index]["repo"]:
 
355
  # Enhanced prompt processing
356
  if custom_lora_path and custom_lora[1]:
357
  prompt = f"{prompt} {custom_lora[1]}"
358
+ elif selected_state_index >= 0 and selected_state_index < len(sdxl_loras):
359
+ # Only apply default prompt template if we have a valid selection
360
  for lora_list in lora_defaults:
361
  if lora_list["model"] == sdxl_loras[selected_state_index]["repo"]:
362
  prompt_full = lora_list.get("prompt", None)
 
386
  elif custom_lora_path:
387
  repo_name = custom_lora_path
388
  full_path_lora = custom_lora_path
389
+ elif selected_state_index >= 0 and selected_state_index < len(sdxl_loras):
390
  repo_name = sdxl_loras[selected_state_index]["repo"]
391
  full_path_lora = state_dicts[repo_name]["saved_name"]
392
+ else:
393
+ raise gr.Error("Invalid style selection. Please select a style again.")
394
 
395
  repo_name = repo_name.rstrip("/").lower()
396
 
 
472
  # Load LoRA with better error handling
473
  try:
474
  pipe.load_lora_weights(full_path_lora)
475
+ pipe.fuse_lora(lora_scale=lora_scale)
476
  last_fused = True
477
 
478
  # Handle pivotal tuning embeddings (if needed for future LoRAs)
479
+ # Only check this if we're not using a custom LoRA and have a valid index
480
+ if selected_state_index >= 0 and selected_state_index < len(sdxl_loras):
481
+ is_pivotal = sdxl_loras[selected_state_index]["is_pivotal"]
482
+ if is_pivotal:
483
+ text_embedding_name = sdxl_loras[selected_state_index]["text_embedding_weights"]
484
+ embedding_path = hf_hub_download(repo_id=repo_name, filename=text_embedding_name, repo_type="model")
485
+ state_dict_embedding = load_file(embedding_path)
486
+ pipe.load_textual_inversion(
487
+ state_dict_embedding["clip_l" if "clip_l" in state_dict_embedding else "text_encoders_0"],
488
+ token=["<s0>", "<s1>"],
489
+ text_encoder=pipe.text_encoder,
490
+ tokenizer=pipe.tokenizer
491
+ )
492
+ pipe.load_textual_inversion(
493
+ state_dict_embedding["clip_g" if "clip_g" in state_dict_embedding else "text_encoders_1"],
494
+ token=["<s0>", "<s1>"],
495
+ text_encoder=pipe.text_encoder_2,
496
+ tokenizer=pipe.tokenizer_2
497
+ )
498
  except Exception as e:
499
  print(f"Error loading LoRA: {e}")
500
+ import traceback
501
+ traceback.print_exc()
502
  raise gr.Error(f"Failed to load LoRA: {str(e)}")
503
 
504
  print("Processing prompt...")
 
658
  display: block;
659
  font-weight: normal;
660
  opacity: 0.75;
661
+ ">🔥 Improved: Better face identity preservation, Enhanced landscape mode, Multiple face support<br>AlbedoBase XL v2.1 + InstantID + ControlNet</small></span></h1>""",
662
  elem_id="title",
663
  )
664
  selected_state = gr.State()