John6666 commited on
Commit
703d8a1
·
verified ·
1 Parent(s): b3bf5dd

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. modutils.py +69 -3
  3. requirements.txt +2 -2
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏆😻
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.6.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
 
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.20.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
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",
@@ -1640,7 +1706,7 @@ EXAMPLES_GUI = [
1640
  "Euler",
1641
  1152,
1642
  896,
1643
- "votepurchase/animagine-xl-3.1",
1644
  ],
1645
  [
1646
  "solo, princess Zelda OOT, score_9, score_8_up, score_8, medium breasts, cute, eyelashes, cute small face, long hair, crown braid, hairclip, pointy ears, soft curvy body, looking at viewer, smile, blush, white dress, medium body, (((holding the Master Sword))), standing, deep forest in the background",
@@ -1679,7 +1745,7 @@ EXAMPLES_GUI = [
1679
  "Euler",
1680
  1024,
1681
  1024,
1682
- "Raelina/Raemu-XL-V4",
1683
  ],
1684
  [
1685
  "yoshida yuuko, machikado mazoku, 1girl, solo, demon horns,horns, school uniform, long hair, open mouth, skirt, demon girl, ahoge, shiny, shiny hair, anime artwork",
@@ -1692,7 +1758,7 @@ EXAMPLES_GUI = [
1692
  "Euler",
1693
  1024,
1694
  1024,
1695
- "cagliostrolab/animagine-xl-3.1",
1696
  ],
1697
  ]
1698
 
 
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",
 
1706
  "Euler",
1707
  1152,
1708
  896,
1709
+ "cagliostrolab/animagine-xl-4.0",
1710
  ],
1711
  [
1712
  "solo, princess Zelda OOT, score_9, score_8_up, score_8, medium breasts, cute, eyelashes, cute small face, long hair, crown braid, hairclip, pointy ears, soft curvy body, looking at viewer, smile, blush, white dress, medium body, (((holding the Master Sword))), standing, deep forest in the background",
 
1745
  "Euler",
1746
  1024,
1747
  1024,
1748
+ "Raelina/Raemu-XL-V5",
1749
  ],
1750
  [
1751
  "yoshida yuuko, machikado mazoku, 1girl, solo, demon horns,horns, school uniform, long hair, open mouth, skirt, demon girl, ahoge, shiny, shiny hair, anime artwork",
 
1758
  "Euler",
1759
  1024,
1760
  1024,
1761
+ "cagliostrolab/animagine-xl-4.0",
1762
  ],
1763
  ]
1764
 
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- torch
2
  git+https://github.com/huggingface/diffusers.git
3
- git+https://github.com/huggingface/transformers.git
4
  git+https://github.com/huggingface/peft.git
5
  git+https://github.com/huggingface/accelerate.git
6
  sentencepiece
 
1
+ torch==2.4.0
2
  git+https://github.com/huggingface/diffusers.git
3
+ transformers==4.48.3
4
  git+https://github.com/huggingface/peft.git
5
  git+https://github.com/huggingface/accelerate.git
6
  sentencepiece