Spaces:
Sleeping
Sleeping
File size: 19,026 Bytes
cd2465c 3051f7b f217e4d 003a054 2d30f4b 9cf8208 003a054 266a724 003a054 978580d 003a054 bf71114 164edec e4e5057 003a054 e4e5057 003a054 e4e5057 266a724 cd2465c ff24fe8 9303de6 b1c5569 17a8f06 9303de6 efbe74e 978580d 2d30f4b f217e4d 9a397ea 978580d f217e4d 6c3f8be 47fa492 f217e4d e4f255d ecc78e5 978580d f217e4d 6c3f8be 47fa492 f217e4d 2d30f4b 978580d 2d30f4b 978580d 9303de6 978580d 9303de6 efbe74e b0b3cd4 978580d 9303de6 978580d 2d30f4b 978580d cd2465c 47fa492 f217e4d b1c5569 f217e4d 64b9ad0 9303de6 d5ce88c 9724323 5c4b76b 978580d 9303de6 978580d 9303de6 5c4b76b b0b3cd4 978580d 9303de6 978580d 7c81d9d b486cec 4b0fbd1 cd2465c 64b9ad0 978580d 7c81d9d b486cec 4b0fbd1 b486cec efbe74e cd2465c bbab3de cd2465c f217e4d d5a8945 b1c5569 efbe74e f217e4d 978580d 164edec 6ea5f8e 3f860d6 8a38e02 164edec 9724323 164edec 9724323 164edec e4f255d e4e5057 164edec e4e5057 164edec 003a054 164edec 3f860d6 8a38e02 164edec d6b3ea2 164edec 9724323 164edec efbe74e 57b3673 efbe74e 385c5f2 efbe74e 8a38e02 efbe74e 164edec e4f255d 9724323 e4f255d efbe74e 5c1eec8 efbe74e 5c4b76b efbe74e 6ea5f8e c2a2454 1a08984 6ea5f8e e4f255d 9724323 164edec 6ea5f8e 50d6862 164edec cd2465c 50d6862 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
import gradio as gr
import spaces
import torch
from clip_slider_pipeline import CLIPSliderXL
from diffusers import StableDiffusionXLPipeline, ControlNetModel, StableDiffusionXLControlNetPipeline, EulerDiscreteScheduler, AutoencoderKL
import time
import numpy as np
import cv2
from PIL import Image
from ledits.pipeline_leditspp_stable_diffusion_xl import LEditsPPPipelineStableDiffusionXL
def HWC3(x):
assert x.dtype == np.uint8
if x.ndim == 2:
x = x[:, :, None]
assert x.ndim == 3
H, W, C = x.shape
assert C == 1 or C == 3 or C == 4
if C == 3:
return x
if C == 1:
return np.concatenate([x, x, x], axis=2)
if C == 4:
color = x[:, :, 0:3].astype(np.float32)
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
y = color * alpha + 255.0 * (1.0 - alpha)
y = y.clip(0, 255).astype(np.uint8)
return y
def process_controlnet_img(image):
controlnet_img = np.array(image)
controlnet_img = cv2.Canny(controlnet_img, 100, 200)
controlnet_img = HWC3(controlnet_img)
controlnet_img = Image.fromarray(controlnet_img)
# load pipelines
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash", vae=vae).to("cuda", torch.float16)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
clip_slider = CLIPSliderXL(pipe, device=torch.device("cuda"))
pipe_adapter = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash").to("cuda", torch.float16)
pipe_adapter.scheduler = EulerDiscreteScheduler.from_config(pipe_adapter.scheduler.config)
#pipe_adapter.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
# scale = 0.8
# pipe_adapter.set_ip_adapter_scale(scale)
clip_slider_ip = CLIPSliderXL(sd_pipe=pipe_adapter, device=torch.device("cuda"))
controlnet = ControlNetModel.from_pretrained(
"xinsir/controlnet-canny-sdxl-1.0", # insert here your choice of controlnet
torch_dtype=torch.float16
)
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe_controlnet = StableDiffusionXLControlNetPipeline.from_pretrained(
"sd-community/sdxl-flash",
controlnet=controlnet,
vae=vae,
torch_dtype=torch.float16,
)
clip_slider_controlnet = CLIPSliderXL(sd_pipe=pipe_controlnet,device=torch.device("cuda"))
pipe_inv = LEditsPPPipelineStableDiffusionXL.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", vae=vae,
torch_dtype=torch.float16
)
clip_slider_inv = CLIPSliderXL(sd_pipe=pipe_inv,device=torch.device("cuda"))
@spaces.GPU(duration=120)
def generate(slider_x, slider_y, prompt, seed, iterations, steps, guidance_scale,
x_concept_1, x_concept_2, y_concept_1, y_concept_2,
avg_diff_x_1, avg_diff_x_2,
avg_diff_y_1, avg_diff_y_2,
img2img_type = None, img = None,
controlnet_scale= None, ip_adapter_scale=None,
edit_threshold=None, edit_guidance_scale = None,
init_latents=None, zs=None):
start_time = time.time()
# check if avg diff for directions need to be re-calculated
print("slider_x", slider_x)
print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]):
avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
avg_diff_0 = avg_diff[0].to(torch.float16)
avg_diff_1 = avg_diff[1].to(torch.float16)
x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
print("avg_diff_0", avg_diff_0.dtype)
if not sorted(slider_y) == sorted([y_concept_1, y_concept_2]):
avg_diff_2nd = clip_slider.find_latent_direction(slider_y[0], slider_y[1], num_iterations=iterations)
avg_diff_2nd_0 = avg_diff_2nd[0].to(torch.float16)
avg_diff_2nd_1 = avg_diff_2nd[1].to(torch.float16)
y_concept_1, y_concept_2 = slider_y[0], slider_y[1]
end_time = time.time()
print(f"direction time: {end_time - start_time:.2f} ms")
start_time = time.time()
if img2img_type=="controlnet canny" and img is not None:
control_img = process_controlnet_img(img)
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, image=control_img, controlnet_conditioning_scale =controlnet_scale, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
elif img2img_type=="ip adapter" and img is not None:
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, ip_adapter_image=img, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
elif img2img_type=="inversion":
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1), init_latents = init_latents, zs=zs, edit_threshold=edit_threshold, edit_guidance_scale = edit_guidance_scale)
else: # text to image
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
end_time = time.time()
print(f"generation time: {end_time - start_time:.2f} ms")
comma_concepts_x = ', '.join(slider_x)
comma_concepts_y = ', '.join(slider_y)
avg_diff_x_1 = avg_diff_0.cpu()
avg_diff_x_2 = avg_diff_1.cpu()
avg_diff_y_1 = avg_diff_2nd_0.cpu()
avg_diff_y_2 = avg_diff_2nd_1.cpu()
return gr.update(label=comma_concepts_x, interactive=True),gr.update(label=comma_concepts_y, interactive=True), x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, image
@spaces.GPU
def update_scales(x,y,prompt,seed, steps, guidance_scale,
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
img2img_type = None, img = None,
controlnet_scale= None, ip_adapter_scale=None,
edit_threshold=None, edit_guidance_scale = None,
init_latents=None, zs=None):
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
if img2img_type=="controlnet canny" and img is not None:
control_img = process_controlnet_img(img)
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, image=control_img, controlnet_conditioning_scale =controlnet_scale, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
elif img2img_type=="ip adapter" and img is not None:
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, ip_adapter_image=img, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
elif img2img_type=="inversion":
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1), edit_threshold=edit_threshold, edit_guidance_scale = edit_guidance_scale, init_latents = init_latents, zs=zs)
else:
image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
return image
@spaces.GPU
def update_x(x,y,prompt,seed, steps,
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
img2img_type = None,
img = None):
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
return image
@spaces.GPU
def update_y(x,y,prompt, seed, steps,
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
img2img_type = None,
img = None):
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
return image
@spaces.GPU
def invert(image, num_inversion_steps=50, skip=0.3):
_ = clip_slider_inv.pipe.invert(
source_prompt = "",
image = image,
num_inversion_steps = num_inversion_steps,
skip = skip
)
return clip_slider_inv.pipe.init_latents, lip_slider_inv.pipe.zs
def reset_do_inversion():
return True
css = '''
#group {
position: relative;
width: 420px;
height: 420px;
margin-bottom: 20px;
background-color: white
}
#x {
position: absolute;
bottom: 0;
left: 25px;
width: 400px;
}
#y {
position: absolute;
bottom: 20px;
left: 67px;
width: 400px;
transform: rotate(-90deg);
transform-origin: left bottom;
}
#image_out{position:absolute; width: 80%; right: 10px; top: 40px}
'''
with gr.Blocks(css=css) as demo:
x_concept_1 = gr.State("")
x_concept_2 = gr.State("")
y_concept_1 = gr.State("")
y_concept_2 = gr.State("")
avg_diff_x_1 = gr.State()
avg_diff_x_2 = gr.State()
avg_diff_y_1 = gr.State()
avg_diff_y_2 = gr.State()
do_inversion = gr.State()
init_latents = gr.State()
zs = gr.State()
with gr.Tab("text2image"):
with gr.Row():
with gr.Column():
slider_x = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
slider_y = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
prompt = gr.Textbox(label="Prompt")
submit = gr.Button("find directions")
with gr.Column():
with gr.Group(elem_id="group"):
x = gr.Slider(minimum=-7, value=0, maximum=7, elem_id="x", interactive=False)
y = gr.Slider(minimum=-7, value=0, maximum=7, elem_id="y", interactive=False)
output_image = gr.Image(elem_id="image_out")
with gr.Row():
generate_butt = gr.Button("generate")
with gr.Accordion(label="advanced options", open=False):
iterations = gr.Slider(label = "num iterations", minimum=0, value=200, maximum=400)
steps = gr.Slider(label = "num inference steps", minimum=1, value=8, maximum=30)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=5,
)
seed = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
with gr.Tab(label="image2image"):
with gr.Row():
with gr.Column():
image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512))
slider_x_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
slider_y_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
img2img_type = gr.Radio(["controlnet canny", "ip adapter"], label="", info="")
prompt_a = gr.Textbox(label="Prompt")
submit_a = gr.Button("Submit")
with gr.Column():
with gr.Group(elem_id="group"):
x_a = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="x", interactive=False)
y_a = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="y", interactive=False)
output_image_a = gr.Image(elem_id="image_out")
with gr.Row():
generate_butt_a = gr.Button("generate")
with gr.Accordion(label="advanced options", open=False):
iterations_a = gr.Slider(label = "num iterations", minimum=0, value=200, maximum=300)
steps_a = gr.Slider(label = "num inference steps", minimum=1, value=8, maximum=30)
guidance_scale_a = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=5,
)
controlnet_conditioning_scale = gr.Slider(
label="controlnet conditioning scale",
minimum=0.5,
maximum=5.0,
step=0.1,
value=0.7,
)
ip_adapter_scale = gr.Slider(
label="ip adapter scale",
minimum=0.5,
maximum=5.0,
step=0.1,
value=0.8,
)
seed_a = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
with gr.Tab(label="inversion"):
with gr.Row():
with gr.Column():
image_inv = gr.Image(height=512, width=512)
slider_x_inv = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
slider_y_inv = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
prompt_inv = gr.Textbox(label="Prompt")
img2img_type_inv = gr.Radio(["inversion"], label="",value="inversion", info="", visible=False)
submit_inv = gr.Button("Submit")
with gr.Column():
with gr.Group(elem_id="group"):
x_inv = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="x", interactive=False)
y_inv = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="y", interactive=False)
output_image_inv = gr.Image(elem_id="image_out")
with gr.Row():
generate_butt_inv = gr.Button("generate")
with gr.Accordion(label="advanced options", open=False):
iterations_inv = gr.Slider(label = "num iterations", minimum=0, value=200, maximum=300)
steps_inv = gr.Slider(label = "num inference steps", minimum=1, value=8, maximum=30)
guidance_scale_inv = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=5,
)
# edit_threshold=None, edit_guidance_scale = None,
# init_latents=None, zs=None
edit_threshold = gr.Slider(
label="edit threshold",
minimum=0.01,
maximum=0.99,
step=0.1,
value=0.3,
)
edit_guidance_scale = gr.Slider(
label="edit guidance scale",
minimum=0,
maximum=20,
step=0.25,
value=5,
)
seed_inv = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
submit.click(fn=generate,
inputs=[slider_x, slider_y, prompt, seed, iterations, steps, guidance_scale, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2],
outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image])
image_inv.change(fn=reset_do_inversion, outputs=[do_inversion]).then(fn=invert, inputs=[image_inv], outputs=[init_latents,zs])
submit_inv.click(fn=generate,
inputs=[slider_x_inv, slider_y_inv, prompt_inv, seed_inv, iterations_inv, steps_inv, guidance_scale_inv, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type_inv, image, controlnet_conditioning_scale, ip_adapter_scale ,edit_threshold, edit_guidance_scale, init_latents, zs],
outputs=[x_inv, y_inv, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image_inv])
generate_butt.click(fn=update_scales, inputs=[x,y, prompt, seed, steps, guidance_scale, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image])
generate_butt_a.click(fn=update_scales, inputs=[x_a,y_a, prompt_a, seed_a, steps_a, guidance_scale_a, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale], outputs=[output_image_a])
generate_butt_inv.click(fn=update_scales, inputs=[x_inv,y_inv, prompt_inv, seed_inv, steps_inv, guidance_scale_inv, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type_inv, image, controlnet_conditioning_scale, ip_adapter_scale ,edit_threshold, edit_guidance_scale, init_latents, zs], outputs=[output_image_inv])
#x.change(fn=update_scales, inputs=[x,y, prompt, seed, steps, guidance_scale, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image])
#y.change(fn=update_scales, inputs=[x,y, prompt, seed, steps, guidance_scale, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image])
submit_a.click(fn=generate,
inputs=[slider_x_a, slider_y_a, prompt_a, seed_a, iterations_a, steps_a, guidance_scale_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale],
outputs=[x_a, y_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image_a])
#x_a.change(fn=update_scales, inputs=[x_a,y_a, prompt_a, seed_a, steps_a, guidance_scale_a, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale], outputs=[output_image_a])
#y_a.change(fn=update_scales, inputs=[x_a,y_a, prompt, seed_a, steps_a, guidance_scale_a, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale], outputs=[output_image_a])
if __name__ == "__main__":
demo.launch() |