radames commited on
Commit
f358c2b
1 Parent(s): 350d95c

anyline preprocessor

Browse files
Files changed (2) hide show
  1. app.py +10 -38
  2. requirements.txt +2 -1
app.py CHANGED
@@ -8,7 +8,7 @@ from diffusers import (
8
  StableDiffusionXLControlNetImg2ImgPipeline,
9
  DDIMScheduler,
10
  )
11
- from canny_gpu import SobelOperator
12
  from compel import Compel, ReturnedEmbeddingsType
13
  from PIL import Image
14
  import os
@@ -63,7 +63,10 @@ if not IS_SPACES_ZERO:
63
  # pipe.enable_xformers_memory_efficient_attention()
64
  pipe.enable_model_cpu_offload()
65
  pipe.enable_vae_tiling()
66
- canny_torch = SobelOperator()
 
 
 
67
 
68
 
69
  def pad_image(image):
@@ -96,23 +99,20 @@ def predict(
96
  strength=1.0,
97
  controlnet_start=0.0,
98
  controlnet_end=1.0,
99
- canny_low=0.01,
100
- canny_high=0.2,
101
  progress=gr.Progress(track_tqdm=True),
102
  ):
103
  if IS_SPACES_ZERO:
104
  apply_hidiffusion(pipe)
105
- canny_torch.to(device)
106
  if input_image is None:
107
  raise gr.Error("Please upload an image.")
108
  padded_image = pad_image(input_image).resize((1024, 1024)).convert("RGB")
109
  conditioning, pooled = compel([prompt, negative_prompt])
110
  generator = torch.manual_seed(seed)
111
  last_time = time.time()
112
- canny_image = canny_torch(padded_image, canny_low, canny_high)
113
  images = pipe(
114
  image=padded_image,
115
- control_image=canny_image,
116
  strength=strength,
117
  prompt_embeds=conditioning[0:1],
118
  pooled_prompt_embeds=pooled[0:1],
@@ -129,7 +129,7 @@ def predict(
129
  eta=1.0,
130
  )
131
  print(f"Time taken: {time.time() - last_time}")
132
- return (padded_image, images.images[0]), padded_image, canny_image
133
 
134
 
135
  css = """
@@ -221,20 +221,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
221
  value=1.0,
222
  label="ControlNet End",
223
  )
224
- canny_low = gr.Slider(
225
- minimum=0,
226
- maximum=1,
227
- step=0.001,
228
- value=0.0,
229
- label="Canny Low Threshold",
230
- )
231
- canny_high = gr.Slider(
232
- minimum=0.0,
233
- maximum=1.0,
234
- step=0.001,
235
- value=0.2,
236
- label="Canny High Threshold",
237
- )
238
 
239
  btn = gr.Button()
240
  with gr.Column(scale=2):
@@ -242,7 +228,7 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
242
  image_slider = ImageSlider(position=0.5)
243
  with gr.Row():
244
  padded_image = gr.Image(type="pil", label="Padded Image")
245
- canny_image = gr.Image(type="pil", label="Canny Image")
246
  inputs = [
247
  image_input,
248
  prompt,
@@ -254,10 +240,8 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
254
  strength,
255
  controlnet_start,
256
  controlnet_end,
257
- canny_low,
258
- canny_high,
259
  ]
260
- outputs = [image_slider, padded_image, canny_image]
261
  btn.click(lambda x: None, inputs=None, outputs=image_slider).then(
262
  fn=predict, inputs=inputs, outputs=outputs
263
  )
@@ -277,8 +261,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
277
  1.0,
278
  0.0,
279
  1.0,
280
- 0.01,
281
- 0.2,
282
  ],
283
  [
284
  "./examples/cybetruck.jpeg",
@@ -291,8 +273,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
291
  0.8,
292
  0.0,
293
  1.0,
294
- 0.01,
295
- 0.2,
296
  ],
297
  [
298
  "./examples/jesus.png",
@@ -305,8 +285,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
305
  0.8,
306
  0.0,
307
  1.0,
308
- 0.01,
309
- 0.2,
310
  ],
311
  [
312
  "./examples/anna-sullivan-DioLM8ViiO8-unsplash.jpg",
@@ -319,8 +297,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
319
  0.8,
320
  0.0,
321
  1.0,
322
- 0.01,
323
- 0.2,
324
  ],
325
  [
326
  "./examples/img_aef651cb-2919-499d-aa49-6d4e2e21a56e_1024.jpg",
@@ -333,8 +309,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
333
  0.8,
334
  0.0,
335
  1.0,
336
- 0.01,
337
- 0.2,
338
  ],
339
  [
340
  "./examples/huggingface.jpg",
@@ -347,8 +321,6 @@ SDXL Controlnet [TheMistoAI/MistoLine](https://huggingface.co/TheMistoAI/MistoLi
347
  0.8,
348
  0.0,
349
  1.0,
350
- 0.0,
351
- 0.95,
352
  ],
353
  ],
354
  cache_examples="lazy",
 
8
  StableDiffusionXLControlNetImg2ImgPipeline,
9
  DDIMScheduler,
10
  )
11
+ from controlnet_aux import AnylineDetector
12
  from compel import Compel, ReturnedEmbeddingsType
13
  from PIL import Image
14
  import os
 
63
  # pipe.enable_xformers_memory_efficient_attention()
64
  pipe.enable_model_cpu_offload()
65
  pipe.enable_vae_tiling()
66
+
67
+ anyline = AnylineDetector.from_pretrained(
68
+ "TheMistoAI/MistoLine", filename="MTEED.pth", subfolder="Anyline"
69
+ ).to(device)
70
 
71
 
72
  def pad_image(image):
 
99
  strength=1.0,
100
  controlnet_start=0.0,
101
  controlnet_end=1.0,
 
 
102
  progress=gr.Progress(track_tqdm=True),
103
  ):
104
  if IS_SPACES_ZERO:
105
  apply_hidiffusion(pipe)
 
106
  if input_image is None:
107
  raise gr.Error("Please upload an image.")
108
  padded_image = pad_image(input_image).resize((1024, 1024)).convert("RGB")
109
  conditioning, pooled = compel([prompt, negative_prompt])
110
  generator = torch.manual_seed(seed)
111
  last_time = time.time()
112
+ anyline_image = anyline(padded_image, detect_resolution=1024)
113
  images = pipe(
114
  image=padded_image,
115
+ control_image=anyline_image,
116
  strength=strength,
117
  prompt_embeds=conditioning[0:1],
118
  pooled_prompt_embeds=pooled[0:1],
 
129
  eta=1.0,
130
  )
131
  print(f"Time taken: {time.time() - last_time}")
132
+ return (padded_image, images.images[0]), padded_image, anyline_image
133
 
134
 
135
  css = """
 
221
  value=1.0,
222
  label="ControlNet End",
223
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
  btn = gr.Button()
226
  with gr.Column(scale=2):
 
228
  image_slider = ImageSlider(position=0.5)
229
  with gr.Row():
230
  padded_image = gr.Image(type="pil", label="Padded Image")
231
+ anyline_image = gr.Image(type="pil", label="Anyline Image")
232
  inputs = [
233
  image_input,
234
  prompt,
 
240
  strength,
241
  controlnet_start,
242
  controlnet_end,
 
 
243
  ]
244
+ outputs = [image_slider, padded_image, anyline_image]
245
  btn.click(lambda x: None, inputs=None, outputs=image_slider).then(
246
  fn=predict, inputs=inputs, outputs=outputs
247
  )
 
261
  1.0,
262
  0.0,
263
  1.0,
 
 
264
  ],
265
  [
266
  "./examples/cybetruck.jpeg",
 
273
  0.8,
274
  0.0,
275
  1.0,
 
 
276
  ],
277
  [
278
  "./examples/jesus.png",
 
285
  0.8,
286
  0.0,
287
  1.0,
 
 
288
  ],
289
  [
290
  "./examples/anna-sullivan-DioLM8ViiO8-unsplash.jpg",
 
297
  0.8,
298
  0.0,
299
  1.0,
 
 
300
  ],
301
  [
302
  "./examples/img_aef651cb-2919-499d-aa49-6d4e2e21a56e_1024.jpg",
 
309
  0.8,
310
  0.0,
311
  1.0,
 
 
312
  ],
313
  [
314
  "./examples/huggingface.jpg",
 
321
  0.8,
322
  0.0,
323
  1.0,
 
 
324
  ],
325
  ],
326
  cache_examples="lazy",
requirements.txt CHANGED
@@ -17,4 +17,5 @@ accelerate
17
  safetensors
18
  hidiffusion==0.1.8
19
  spaces
20
- torch
 
 
17
  safetensors
18
  hidiffusion==0.1.8
19
  spaces
20
+ torch
21
+ controlnet-aux @ git+https://github.com/huggingface/controlnet_aux