Bobby commited on
Commit
50bfbb3
·
1 Parent(s): ef7f209

optimized!

Browse files
Files changed (2) hide show
  1. app.py +66 -61
  2. preprocess.py +2 -11
app.py CHANGED
@@ -24,77 +24,82 @@ MAX_SEED = np.iinfo(np.int32).max
24
  API_KEY = os.environ.get("API_KEY", None)
25
 
26
  print("CUDA version:", torch.version.cuda)
27
- print("loading pipe")
28
  compiled = False
29
- from preprocess import Preprocessor
30
- preprocessor = Preprocessor()
31
 
32
  import spaces
33
 
34
- if gr.NO_RELOAD:
35
- torch.cuda.max_memory_allocated(device="cuda")
36
- # Controlnet Normal
37
- model_id = "lllyasviel/control_v11p_sd15_normalbae"
38
- print("initializing controlnet")
39
- controlnet = ControlNetModel.from_pretrained(
40
- model_id,
41
- torch_dtype=torch.float16,
42
- attn_implementation="flash_attention_2",
43
- ).to("cuda")
44
 
45
- # Scheduler
46
- scheduler = DPMSolverMultistepScheduler.from_pretrained(
47
- "runwayml/stable-diffusion-v1-5",
48
- solver_order=2,
49
- subfolder="scheduler",
50
- use_karras_sigmas=True,
51
- final_sigmas_type="sigma_min",
52
- algorithm_type="sde-dpmsolver++",
53
- prediction_type="epsilon",
54
- thresholding=False,
55
- denoise_final=True,
56
- device_map="cuda",
57
- torch_dtype=torch.float16,
58
- )
59
 
60
- # Stable Diffusion Pipeline URL
61
- # base_model_url = "https://huggingface.co/broyang/hentaidigitalart_v20/blob/main/realcartoon3d_v15.safetensors"
62
- base_model_url = "https://huggingface.co/Lykon/AbsoluteReality/blob/main/AbsoluteReality_1.8.1_pruned.safetensors"
63
- vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
64
 
65
- vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
66
- vae.to(memory_format=torch.channels_last)
 
67
 
68
- pipe = StableDiffusionControlNetPipeline.from_single_file(
69
- base_model_url,
70
- # safety_checker=None,
71
- # load_safety_checker=True,
72
- controlnet=controlnet,
73
- scheduler=scheduler,
74
- vae=vae,
75
- torch_dtype=torch.float16,
76
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="EasyNegativeV2.safetensors", token="EasyNegativeV2",)
79
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="badhandv4.pt", token="badhandv4")
80
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="fcNeg-neg.pt", token="fcNeg-neg")
81
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Ahegao.pt", token="HDA_Ahegao")
82
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Bondage.pt", token="HDA_Bondage")
83
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_pet_play.pt", token="HDA_pet_play")
84
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_unconventional maid.pt", token="HDA_unconventional_maid")
85
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NakedHoodie.pt", token="HDA_NakedHoodie")
86
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NunDress.pt", token="HDA_NunDress")
87
- pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Shibari.pt", token="HDA_Shibari")
88
- pipe.to("cuda")
89
- print("---------------Loaded controlnet pipeline---------------")
90
 
91
- @spaces.GPU(duration=12)
92
- def init(pipe):
93
- pipe.enable_xformers_memory_efficient_attention()
94
- pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
95
- pipe.unet.set_attn_processor(AttnProcessor2_0())
96
- print("Model Compiled!")
97
- init(pipe)
98
 
99
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
100
  if randomize_seed:
 
24
  API_KEY = os.environ.get("API_KEY", None)
25
 
26
  print("CUDA version:", torch.version.cuda)
27
+ print("loading everything")
28
  compiled = False
 
 
29
 
30
  import spaces
31
 
32
+ torch.cuda.max_memory_allocated(device="cuda")
33
+ # Controlnet Normal
34
+ model_id = "lllyasviel/control_v11p_sd15_normalbae"
35
+ print("initializing controlnet")
36
+ controlnet = ControlNetModel.from_pretrained(
37
+ model_id,
38
+ torch_dtype=torch.float16,
39
+ attn_implementation="flash_attention_2",
40
+ ).to("cuda")
 
41
 
42
+ # Scheduler
43
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(
44
+ "runwayml/stable-diffusion-v1-5",
45
+ solver_order=2,
46
+ subfolder="scheduler",
47
+ use_karras_sigmas=True,
48
+ final_sigmas_type="sigma_min",
49
+ algorithm_type="sde-dpmsolver++",
50
+ prediction_type="epsilon",
51
+ thresholding=False,
52
+ denoise_final=True,
53
+ device_map="cuda",
54
+ torch_dtype=torch.float16,
55
+ )
56
 
57
+ # Stable Diffusion Pipeline URL
58
+ # base_model_url = "https://huggingface.co/broyang/hentaidigitalart_v20/blob/main/realcartoon3d_v15.safetensors"
59
+ base_model_url = "https://huggingface.co/Lykon/AbsoluteReality/blob/main/AbsoluteReality_1.8.1_pruned.safetensors"
60
+ vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
61
 
62
+ print('loading vae')
63
+ vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
64
+ vae.to(memory_format=torch.channels_last)
65
 
66
+ print('loading pipe')
67
+ pipe = StableDiffusionControlNetPipeline.from_single_file(
68
+ base_model_url,
69
+ safety_checker=None,
70
+ # load_safety_checker=True,
71
+ controlnet=controlnet,
72
+ scheduler=scheduler,
73
+ vae=vae,
74
+ torch_dtype=torch.float16,
75
+ )
76
+
77
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="EasyNegativeV2.safetensors", token="EasyNegativeV2",)
78
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="badhandv4.pt", token="badhandv4")
79
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="fcNeg-neg.pt", token="fcNeg-neg")
80
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Ahegao.pt", token="HDA_Ahegao")
81
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Bondage.pt", token="HDA_Bondage")
82
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_pet_play.pt", token="HDA_pet_play")
83
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_unconventional maid.pt", token="HDA_unconventional_maid")
84
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NakedHoodie.pt", token="HDA_NakedHoodie")
85
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_NunDress.pt", token="HDA_NunDress")
86
+ pipe.load_textual_inversion("broyang/hentaidigitalart_v20", weight_name="HDA_Shibari.pt", token="HDA_Shibari")
87
+ pipe.to("cuda")
88
+
89
+ print("loading preprocessor")
90
+ from preprocess import Preprocessor
91
+ preprocessor = Preprocessor()
92
+ preprocessor.load("NormalBae")
93
 
94
+ print("---------------Loaded controlnet pipeline---------------")
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ @spaces.GPU(duration=12)
97
+ def init(pipe):
98
+ pipe.enable_xformers_memory_efficient_attention()
99
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
100
+ pipe.unet.set_attn_processor(AttnProcessor2_0())
101
+ print("Model Compiled!")
102
+ init(pipe)
103
 
104
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
105
  if randomize_seed:
preprocess.py CHANGED
@@ -1,12 +1,7 @@
1
- # import numpy as np
2
  import PIL.Image
3
  import torch, gc
4
  from controlnet_aux_local import NormalBaeDetector#, CannyDetector
5
 
6
- # from controlnet_aux.util import HWC3
7
- # import cv2
8
- # from cv_utils import resize_image
9
-
10
  class Preprocessor:
11
  MODEL_ID = "lllyasviel/Annotators"
12
 
@@ -21,14 +16,10 @@ class Preprocessor:
21
  print("Loading NormalBae")
22
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
23
  torch.cuda.empty_cache()
24
- # gc.collect()
25
- # elif name == "Canny":
26
- # self.model = CannyDetector()
27
  else:
28
  raise ValueError
29
-
30
-
31
- self.name = name
32
 
33
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
34
  return self.model(image, **kwargs)
 
 
1
  import PIL.Image
2
  import torch, gc
3
  from controlnet_aux_local import NormalBaeDetector#, CannyDetector
4
 
 
 
 
 
5
  class Preprocessor:
6
  MODEL_ID = "lllyasviel/Annotators"
7
 
 
16
  print("Loading NormalBae")
17
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
18
  torch.cuda.empty_cache()
19
+ self.name = name
 
 
20
  else:
21
  raise ValueError
22
+ return
 
 
23
 
24
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
25
  return self.model(image, **kwargs)