linoyts HF staff commited on
Commit
003a054
1 Parent(s): d6b3ea2

add controlnet [wip]

Browse files
Files changed (1) hide show
  1. app.py +43 -5
app.py CHANGED
@@ -2,10 +2,33 @@ import gradio as gr
2
  import spaces
3
  import torch
4
  from clip_slider_pipeline import CLIPSliderXL
5
- from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler, AutoencoderKL
6
  import time
7
  import numpy as np
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
10
  pipe = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash", vae=vae).to("cuda", torch.float16)
11
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
@@ -13,18 +36,32 @@ clip_slider = CLIPSliderXL(pipe, device=torch.device("cuda"))
13
 
14
  pipe_adapter = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash").to("cuda", torch.float16)
15
  pipe_adapter.scheduler = EulerDiscreteScheduler.from_config(pipe_adapter.scheduler.config)
16
- pipe_adapter.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
17
  # scale = 0.8
18
  # pipe_adapter.set_ip_adapter_scale(scale)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- clip_slider_ip = CLIPSliderXL(sd_pipe=pipe_adapter,
21
- device=torch.device("cuda"))
22
 
23
  @spaces.GPU(duration=120)
24
  def generate(slider_x, slider_y, prompt, seed, iterations, steps,
25
  x_concept_1, x_concept_2, y_concept_1, y_concept_2,
26
  avg_diff_x_1, avg_diff_x_2,
27
- avg_diff_y_1, avg_diff_y_2):
 
 
28
  start_time = time.time()
29
  # check if avg diff for directions need to be re-calculated
30
  print("slider_x", slider_x)
@@ -131,6 +168,7 @@ with gr.Blocks(css=css) as demo:
131
  image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512))
132
  slider_x_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
133
  slider_y_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
 
134
  prompt_a = gr.Textbox(label="Prompt")
135
  submit_a = gr.Button("Submit")
136
  with gr.Group(elem_id="group"):
 
2
  import spaces
3
  import torch
4
  from clip_slider_pipeline import CLIPSliderXL
5
+ from diffusers import StableDiffusionXLPipeline, ControlNetModel, StableDiffusionXLControlNetPipeline, EulerDiscreteScheduler, AutoencoderKL
6
  import time
7
  import numpy as np
8
+ import cv2
9
+ from PIL import Image
10
 
11
+
12
+ def HWC3(x):
13
+ assert x.dtype == np.uint8
14
+ if x.ndim == 2:
15
+ x = x[:, :, None]
16
+ assert x.ndim == 3
17
+ H, W, C = x.shape
18
+ assert C == 1 or C == 3 or C == 4
19
+ if C == 3:
20
+ return x
21
+ if C == 1:
22
+ return np.concatenate([x, x, x], axis=2)
23
+ if C == 4:
24
+ color = x[:, :, 0:3].astype(np.float32)
25
+ alpha = x[:, :, 3:4].astype(np.float32) / 255.0
26
+ y = color * alpha + 255.0 * (1.0 - alpha)
27
+ y = y.clip(0, 255).astype(np.uint8)
28
+ return y
29
+
30
+
31
+ # load pipelines
32
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
33
  pipe = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash", vae=vae).to("cuda", torch.float16)
34
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
 
36
 
37
  pipe_adapter = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash").to("cuda", torch.float16)
38
  pipe_adapter.scheduler = EulerDiscreteScheduler.from_config(pipe_adapter.scheduler.config)
39
+ #pipe_adapter.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
40
  # scale = 0.8
41
  # pipe_adapter.set_ip_adapter_scale(scale)
42
+ clip_slider_ip = CLIPSliderXL(sd_pipe=pipe_adapter, device=torch.device("cuda"))
43
+
44
+ controlnet = ControlNetModel.from_pretrained(
45
+ "xinsir/controlnet-canny-sdxl-1.0", # insert here your choice of controlnet
46
+ torch_dtype=torch.float16
47
+ )
48
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
49
+ pipe_controlnet = StableDiffusionXLControlNetPipeline.from_pretrained(
50
+ "sd-community/sdxl-flash",
51
+ controlnet=controlnet,
52
+ vae=vae,
53
+ torch_dtype=torch.float16,
54
+ )
55
+ clip_slider_controlnet = CLIPSliderXL(sd_pipe=pipe_controlnet,device=torch.device("cuda"))
56
 
 
 
57
 
58
  @spaces.GPU(duration=120)
59
  def generate(slider_x, slider_y, prompt, seed, iterations, steps,
60
  x_concept_1, x_concept_2, y_concept_1, y_concept_2,
61
  avg_diff_x_1, avg_diff_x_2,
62
+ avg_diff_y_1, avg_diff_y_2
63
+ img2img_type = None,
64
+ img = None):
65
  start_time = time.time()
66
  # check if avg diff for directions need to be re-calculated
67
  print("slider_x", slider_x)
 
168
  image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512))
169
  slider_x_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
170
  slider_y_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
171
+ img2img_type = gr.Radio(["controlnet canny", "ip adapter"], label="", info="")
172
  prompt_a = gr.Textbox(label="Prompt")
173
  submit_a = gr.Button("Submit")
174
  with gr.Group(elem_id="group"):