John6666 commited on
Commit
e0e80e4
1 Parent(s): 591efd6

Upload 3 files

Browse files
Files changed (3) hide show
  1. dc.py +6 -2
  2. env.py +2 -0
  3. modutils.py +66 -0
dc.py CHANGED
@@ -79,7 +79,7 @@ from env import (
79
  DOWNLOAD_VAE_LIST, DOWNLOAD_EMBEDS)
80
 
81
  from modutils import (to_list, list_uniq, list_sub, get_model_id_list, get_tupled_embed_list,
82
- get_tupled_model_list, get_lora_model_list, download_private_repo, download_things)
83
 
84
  # - **Download Models**
85
  download_model = ", ".join(DOWNLOAD_MODEL_LIST)
@@ -173,11 +173,15 @@ class GuiSD:
173
  def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
174
 
175
  # download link model > model_name
 
 
 
 
176
 
177
  self.update_storage_models()
178
 
179
  vae_model = vae_model if vae_model != "None" else None
180
- model_type = get_model_type(model_name)
181
  dtype_model = torch.bfloat16 if model_type == "FLUX" else torch.float16
182
 
183
  if not os.path.exists(model_name):
 
79
  DOWNLOAD_VAE_LIST, DOWNLOAD_EMBEDS)
80
 
81
  from modutils import (to_list, list_uniq, list_sub, get_model_id_list, get_tupled_embed_list,
82
+ get_tupled_model_list, get_lora_model_list, download_private_repo, download_things, download_link_model)
83
 
84
  # - **Download Models**
85
  download_model = ", ".join(DOWNLOAD_MODEL_LIST)
 
173
  def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
174
 
175
  # download link model > model_name
176
+ if "http" in model_name: #
177
+ model_name, model_type = download_link_model(model_name, DIRECTORY_MODELS) #
178
+ is_link_model = True #
179
+ else: is_link_model = False #
180
 
181
  self.update_storage_models()
182
 
183
  vae_model = vae_model if vae_model != "None" else None
184
+ model_type = get_model_type(model_name) if not is_link_model else model_type #
185
  dtype_model = torch.bfloat16 if model_type == "FLUX" else torch.float16
186
 
187
  if not os.path.exists(model_name):
env.py CHANGED
@@ -51,6 +51,8 @@ LOAD_DIFFUSERS_FORMAT_MODEL = [
51
  'Spestly/OdysseyXL-4.0',
52
  'hanzogak/comradeshipXL-v14T14H',
53
  'hanzogak/comradeshipXL-v14VT',
 
 
54
  'OnomaAIResearch/Illustrious-xl-early-release-v0',
55
  'Laxhar/noobai-XL-1.0',
56
  'Laxhar/noobai-XL-Vpred-1.0',
 
51
  'Spestly/OdysseyXL-4.0',
52
  'hanzogak/comradeshipXL-v14T14H',
53
  'hanzogak/comradeshipXL-v14VT',
54
+ 'BlueDancer/Artisanica_XL',
55
+ 'neta-art/neta-noob-1.0',
56
  'OnomaAIResearch/Illustrious-xl-early-release-v0',
57
  'Laxhar/noobai-XL-1.0',
58
  'Laxhar/noobai-XL-Vpred-1.0',
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",