el-el-san commited on
Commit
83901f1
·
verified ·
1 Parent(s): 9f960f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -18
app.py CHANGED
@@ -1,39 +1,136 @@
1
  import gradio as gr
2
  import numpy as np
 
3
  import random
4
  #from diffusers import DiffusionPipeline
5
- from diffusers import StableDiffusionXLPipeline
 
 
 
 
 
6
  import torch
7
  import spaces
8
 
 
 
9
 
10
- MAX_SEED = np.iinfo(np.int32).max
11
- MAX_IMAGE_SIZE = 1216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- #pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
14
- pipe = StableDiffusionXLPipeline.from_pretrained(
 
 
 
 
 
 
 
 
 
 
 
 
15
  #"yodayo-ai/kivotos-xl-2.0",
16
  "yodayo-ai/holodayo-xl-2.1",
17
- torch_dtype=torch.float16,
18
- use_safetensors=True,
19
- custom_pipeline="lpw_stable_diffusion_xl",
20
- add_watermarker=False,
21
- variant="fp16"
22
  )
23
- pipe.to('cuda')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  prompt = "1girl, solo, upper body, v, smile, looking at viewer, outdoors, night, masterpiece, best quality, very aesthetic, absurdres"
26
  negative_prompt = "nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @spaces.GPU
29
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
30
 
31
  if randomize_seed:
32
  seed = random.randint(0, MAX_SEED)
33
-
 
 
 
 
 
 
 
 
 
 
 
 
34
  generator = torch.Generator().manual_seed(seed)
35
 
36
- image = pipe(
37
  prompt = prompt+", masterpiece, best quality, very aesthetic, absurdres",
38
  negative_prompt = negative_prompt,
39
  guidance_scale = guidance_scale,
@@ -43,7 +140,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
43
  generator = generator
44
  ).images[0]
45
 
46
- return image
47
 
48
  css="""
49
  #col-container {
@@ -71,7 +168,8 @@ with gr.Blocks(css=css) as demo:
71
  )
72
 
73
  run_button = gr.Button("Run", scale=0)
74
-
 
75
  result = gr.Image(label="Result", show_label=False)
76
 
77
  with gr.Accordion("Advanced Settings", open=False):
@@ -80,7 +178,8 @@ with gr.Blocks(css=css) as demo:
80
  label="Negative prompt",
81
  max_lines=1,
82
  placeholder="Enter a negative prompt",
83
- visible=False,
 
84
  )
85
 
86
  seed = gr.Slider(
@@ -131,7 +230,7 @@ with gr.Blocks(css=css) as demo:
131
 
132
  run_button.click(
133
  fn = infer,
134
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
135
  outputs = [result]
136
  )
137
 
 
1
  import gradio as gr
2
  import numpy as np
3
+ import PIL.Image
4
  import random
5
  #from diffusers import DiffusionPipeline
6
+ #from diffusers import StableDiffusionXLPipeline
7
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
8
+ from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
9
+ from controlnet_aux import PidiNetDetector, HEDdetector
10
+ from diffusers.utils import load_image
11
+ import cv2
12
  import torch
13
  import spaces
14
 
15
+ def nms(x, t, s):
16
+ x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
17
 
18
+ f1 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
19
+ f2 = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
20
+ f3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8)
21
+ f4 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.uint8)
22
+
23
+ y = np.zeros_like(x)
24
+
25
+ for f in [f1, f2, f3, f4]:
26
+ np.putmask(y, cv2.dilate(x, kernel=f) == x, x)
27
+
28
+ z = np.zeros_like(y, dtype=np.uint8)
29
+ z[y > t] = 255
30
+ return z
31
+
32
+ def HWC3(x):
33
+ assert x.dtype == np.uint8
34
+ if x.ndim == 2:
35
+ x = x[:, :, None]
36
+ assert x.ndim == 3
37
+ H, W, C = x.shape
38
+ assert C == 1 or C == 3 or C == 4
39
+ if C == 3:
40
+ return x
41
+ if C == 1:
42
+ return np.concatenate([x, x, x], axis=2)
43
+ if C == 4:
44
+ color = x[:, :, 0:3].astype(np.float32)
45
+ alpha = x[:, :, 3:4].astype(np.float32) / 255.0
46
+ y = color * alpha + 255.0 * (1.0 - alpha)
47
+ y = y.clip(0, 255).astype(np.uint8)
48
+ return y
49
+
50
+
51
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
52
 
53
+ # eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
54
+
55
+
56
+ controlnet = ControlNetModel.from_pretrained(
57
+ #"xinsir/controlnet-scribble-sdxl-1.0",
58
+ "2vXpSwA7/test_controlnet2/CN-anytest_v4-marged_am_dim256.safetensors"
59
+
60
+ torch_dtype=torch.float16
61
+ )
62
+
63
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
64
+
65
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
66
+ #"sd-community/sdxl-flash",
67
  #"yodayo-ai/kivotos-xl-2.0",
68
  "yodayo-ai/holodayo-xl-2.1",
69
+ controlnet=controlnet,
70
+ vae=vae,
71
+ torch_dtype=torch.float16,
72
+ # scheduler=eulera_scheduler,
 
73
  )
74
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
75
+
76
+ pipe.to(device)
77
+
78
+
79
+ MAX_SEED = np.iinfo(np.int32).max
80
+ MAX_IMAGE_SIZE = 1216
81
+
82
+ #pipe = StableDiffusionXLPipeline.from_pretrained(
83
+ # #"yodayo-ai/kivotos-xl-2.0",
84
+ # "yodayo-ai/holodayo-xl-2.1",
85
+ # torch_dtype=torch.float16,
86
+ # use_safetensors=True,
87
+ # custom_pipeline="lpw_stable_diffusion_xl",
88
+ # add_watermarker=False,
89
+ # variant="fp16"
90
+ #)
91
+ #pipe.to('cuda')
92
 
93
  prompt = "1girl, solo, upper body, v, smile, looking at viewer, outdoors, night, masterpiece, best quality, very aesthetic, absurdres"
94
  negative_prompt = "nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
95
 
96
+ def nms(x, t, s):
97
+ x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
98
+
99
+ f1 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
100
+ f2 = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
101
+ f3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8)
102
+ f4 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.uint8)
103
+
104
+ y = np.zeros_like(x)
105
+
106
+ for f in [f1, f2, f3, f4]:
107
+ np.putmask(y, cv2.dilate(x, kernel=f) == x, x)
108
+
109
+ z = np.zeros_like(y, dtype=np.uint8)
110
+ z[y > t] = 255
111
+ return z
112
+
113
  @spaces.GPU
114
+ def infer(image: PIL.Image.Image,prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps)-> PIL.Image.Image:
115
 
116
  if randomize_seed:
117
  seed = random.randint(0, MAX_SEED)
118
+
119
+ controlnet_img = processor(image, scribble=False)
120
+ # following is some processing to simulate human sketch draw, different threshold can generate different width of lines
121
+ controlnet_img = np.array(controlnet_img)
122
+ controlnet_img = nms(controlnet_img, 127, 3)
123
+ controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
124
+
125
+ # higher threshold, thiner line
126
+ random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
127
+ controlnet_img[controlnet_img > random_val] = 255
128
+ controlnet_img[controlnet_img < 255] = 0
129
+ image = Image.fromarray(controlnet_img)
130
+
131
  generator = torch.Generator().manual_seed(seed)
132
 
133
+ output_image = pipe(
134
  prompt = prompt+", masterpiece, best quality, very aesthetic, absurdres",
135
  negative_prompt = negative_prompt,
136
  guidance_scale = guidance_scale,
 
140
  generator = generator
141
  ).images[0]
142
 
143
+ return output_image
144
 
145
  css="""
146
  #col-container {
 
168
  )
169
 
170
  run_button = gr.Button("Run", scale=0)
171
+
172
+ image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512))
173
  result = gr.Image(label="Result", show_label=False)
174
 
175
  with gr.Accordion("Advanced Settings", open=False):
 
178
  label="Negative prompt",
179
  max_lines=1,
180
  placeholder="Enter a negative prompt",
181
+ #visible=False,
182
+ value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
183
  )
184
 
185
  seed = gr.Slider(
 
230
 
231
  run_button.click(
232
  fn = infer,
233
+ inputs = [image,prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
234
  outputs = [result]
235
  )
236