John6666 commited on
Commit
ffc9aa4
·
verified ·
1 Parent(s): 3eed168

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. env.py +2 -0
  3. modutils.py +66 -0
app.py CHANGED
@@ -65,7 +65,7 @@ from modutils import (list_uniq, download_private_repo, get_model_id_list, get_t
65
  get_lora_model_list, get_all_lora_tupled_list, update_loras, apply_lora_prompt, set_prompt_loras,
66
  get_my_lora, upload_file_lora, move_file_lora, search_civitai_lora, select_civitai_lora,
67
  update_civitai_selection, get_civitai_tag, CIVITAI_SORT, CIVITAI_PERIOD, CIVITAI_BASEMODEL,
68
- set_textual_inversion_prompt, get_model_pipeline, change_interface_mode, get_t2i_model_info,
69
  get_tupled_model_list, save_gallery_images, save_gallery_history, set_optimization, set_sampler_settings,
70
  set_quick_presets, process_style_prompt, optimization_list, save_images, download_things, valid_model_name,
71
  preset_styles, preset_quality, preset_sampler_setting, translate_to_en, EXAMPLES_GUI, RESOURCES)
 
65
  get_lora_model_list, get_all_lora_tupled_list, update_loras, apply_lora_prompt, set_prompt_loras,
66
  get_my_lora, upload_file_lora, move_file_lora, search_civitai_lora, select_civitai_lora,
67
  update_civitai_selection, get_civitai_tag, CIVITAI_SORT, CIVITAI_PERIOD, CIVITAI_BASEMODEL,
68
+ set_textual_inversion_prompt, get_model_pipeline, change_interface_mode, get_t2i_model_info, download_link_model,
69
  get_tupled_model_list, save_gallery_images, save_gallery_history, set_optimization, set_sampler_settings,
70
  set_quick_presets, process_style_prompt, optimization_list, save_images, download_things, valid_model_name,
71
  preset_styles, preset_quality, preset_sampler_setting, translate_to_en, EXAMPLES_GUI, RESOURCES)
env.py CHANGED
@@ -21,6 +21,8 @@ LOAD_DIFFUSERS_FORMAT_MODEL = [
21
  'Spestly/OdysseyXL-4.0',
22
  'hanzogak/comradeshipXL-v14T14H',
23
  'hanzogak/comradeshipXL-v14VT',
 
 
24
  'OnomaAIResearch/Illustrious-xl-early-release-v0',
25
  'Raelina/Rae-Diffusion-XL-V2',
26
  'Raelina/Raemu-XL-V4',
 
21
  'Spestly/OdysseyXL-4.0',
22
  'hanzogak/comradeshipXL-v14T14H',
23
  'hanzogak/comradeshipXL-v14VT',
24
+ 'BlueDancer/Artisanica_XL',
25
+ 'neta-art/neta-noob-1.0',
26
  'OnomaAIResearch/Illustrious-xl-early-release-v0',
27
  'Raelina/Rae-Diffusion-XL-V2',
28
  'Raelina/Raemu-XL-V4',
modutils.py CHANGED
@@ -19,6 +19,9 @@ from unidecode import unidecode
19
  import copy
20
  from datetime import datetime, timezone, timedelta
21
  FILENAME_TIMEZONE = timezone(timedelta(hours=9)) # JST
 
 
 
22
 
23
 
24
  from env import (HF_LORA_PRIVATE_REPOS1, HF_LORA_PRIVATE_REPOS2,
@@ -1628,6 +1631,69 @@ def get_model_pipeline(repo_id: str):
1628
  return default
1629
 
1630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631
  EXAMPLES_GUI = [
1632
  [
1633
  "1girl, souryuu asuka langley, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors, masterpiece, best quality, very aesthetic, absurdres",
 
19
  import copy
20
  from datetime import datetime, timezone, timedelta
21
  FILENAME_TIMEZONE = timezone(timedelta(hours=9)) # JST
22
+ import torch
23
+ from safetensors.torch import load_file
24
+ import gc
25
 
26
 
27
  from env import (HF_LORA_PRIVATE_REPOS1, HF_LORA_PRIVATE_REPOS2,
 
1631
  return default
1632
 
1633
 
1634
+ MODEL_TYPE_KEY = {
1635
+ "model.diffusion_model.output_blocks.1.1.norm.bias": "SDXL",
1636
+ "model.diffusion_model.input_blocks.11.0.out_layers.3.weight": "SD 1.5",
1637
+ "double_blocks.0.img_attn.norm.key_norm.scale": "FLUX",
1638
+ "model.diffusion_model.double_blocks.0.img_attn.norm.key_norm.scale": "FLUX",
1639
+ "model.diffusion_model.joint_blocks.9.x_block.attn.ln_k.weight": "SD 3.5",
1640
+ }
1641
+
1642
+
1643
+ def safe_clean(path: str):
1644
+ try:
1645
+ if Path(path).exists():
1646
+ if Path(path).is_dir(): shutil.rmtree(str(Path(path)))
1647
+ else: Path(path).unlink()
1648
+ print(f"Deleted: {path}")
1649
+ else: print(f"File not found: {path}")
1650
+ except Exception as e:
1651
+ print(f"Failed to delete: {path} {e}")
1652
+
1653
+
1654
+ def read_safetensors_key(path: str):
1655
+ try:
1656
+ keys = []
1657
+ state_dict = load_file(str(Path(path)))
1658
+ for k in list(state_dict.keys()):
1659
+ keys.append(k)
1660
+ state_dict.pop(k)
1661
+ except Exception as e:
1662
+ print(e)
1663
+ finally:
1664
+ del state_dict
1665
+ torch.cuda.empty_cache()
1666
+ gc.collect()
1667
+ return keys
1668
+
1669
+
1670
+ def get_model_type_from_key(path: str):
1671
+ default = "SDXL"
1672
+ try:
1673
+ keys = read_safetensors_key(path)
1674
+ for k, v in MODEL_TYPE_KEY.items():
1675
+ if k in set(keys):
1676
+ print(f"Model type is {v}.")
1677
+ return v
1678
+ print("Model type could not be identified.")
1679
+ except Exception:
1680
+ return default
1681
+ return default
1682
+
1683
+
1684
+ def download_link_model(url: str, localdir: str):
1685
+ try:
1686
+ new_file = None
1687
+ new_file = get_download_file(localdir, url, CIVITAI_API_KEY)
1688
+ if not new_file or Path(new_file).suffix.lower() not in set([".safetensors", ".ckpt", ".bin", ".sft"]):
1689
+ if Path(new_file).exists(): Path(new_file).unlink()
1690
+ raise gr.Error(f"Safetensors file not found: {url}")
1691
+ model_type = get_model_type_from_key(new_file)
1692
+ return new_file, model_type
1693
+ except Exception as e:
1694
+ raise gr.Error(f"Failed to load single model file: {url} {e}")
1695
+
1696
+
1697
  EXAMPLES_GUI = [
1698
  [
1699
  "1girl, souryuu asuka langley, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors, masterpiece, best quality, very aesthetic, absurdres",