File size: 35,393 Bytes
4e0e2d3 38b82b3 5d39f28 b96c8c5 4e0e2d3 b96c8c5 5d39f28 4f7e95f b96c8c5 3b2835d 4f7e95f 3b2835d 15347cd 4f7e95f 15347cd b96c8c5 5d39f28 a447492 5d39f28 84736d2 b96c8c5 a447492 5d39f28 b96c8c5 5d39f28 b96c8c5 80c0bfe b96c8c5 5d39f28 e18e8e2 5d39f28 b96c8c5 a6a747f b96c8c5 4f7e95f 5d39f28 b96c8c5 58f80cc b96c8c5 4f7e95f 5d39f28 b96c8c5 3c02d45 b96c8c5 3c02d45 4f7e95f 3c02d45 b96c8c5 3c02d45 b96c8c5 3c02d45 4f7e95f 3c02d45 b96c8c5 4f7e95f b96c8c5 0a6e1c9 071e791 4f7e95f 071e791 4f7e95f 071e791 b96c8c5 3c02d45 b96c8c5 3c02d45 4f7e95f b96c8c5 a447492 4f7e95f a447492 b96c8c5 5d39f28 b96c8c5 5d39f28 4f7e95f 5d39f28 b96c8c5 5d39f28 b96c8c5 5d39f28 4f7e95f ddd360b e4a20eb 5d39f28 b96c8c5 5d39f28 b96c8c5 4f7e95f b96c8c5 4f7e95f 5d39f28 ec25ef6 84736d2 ec25ef6 4f7e95f ec25ef6 b96c8c5 5d39f28 84736d2 b96c8c5 5d39f28 b96c8c5 58f80cc 4f7e95f b96c8c5 cec477c 84736d2 cec477c 4f7e95f cec477c cbbd444 586b631 cbbd444 586b631 cbbd444 586b631 cbbd444 586b631 cbbd444 586b631 cbbd444 586b631 cbbd444 586b631 cbbd444 cec477c 4f7e95f cec477c 7b077a3 5d39f28 26a1cd3 0a6e1c9 54b262a 26a1cd3 7b077a3 0a6e1c9 26a1cd3 0a6e1c9 26a1cd3 0a6e1c9 26a1cd3 0a6e1c9 26a1cd3 0a6e1c9 7b077a3 0a6e1c9 7b077a3 0a6e1c9 54b262a 45e93a3 7b077a3 54b262a 7b077a3 cec477c 45e93a3 5d39f28 8e3f95a cec477c f7b4ac7 e4a20eb cec477c 29cefb0 cec477c 8e3f95a cec477c 84736d2 8e3f95a c6a81f6 cec477c 5d39f28 cec477c b96c8c5 cec477c 8e3f95a cec477c 586b631 cbbd444 8e3f95a cbbd444 8e3f95a 0a6e1c9 26a1cd3 0a6e1c9 26a1cd3 0a6e1c9 8e3f95a 5d39f28 7b077a3 26a1cd3 7b077a3 26a1cd3 7b077a3 cbbd444 cec477c 5d39f28 cec477c b96c8c5 00e74d5 26a1cd3 c6a81f6 84736d2 c6a81f6 cec477c c6a81f6 b96c8c5 d680fb0 8e3f95a 586b631 5d39f28 8e3f95a 2115a66 b96c8c5 e18e8e2 ec25ef6 cec477c 2115a66 c643307 e18e8e2 c643307 586b631 cbbd444 586b631 cbbd444 586b631 c643307 5d39f28 c1cbdbc b96c8c5 586b631 194a41e c75a58b 54b262a c75a58b 739e268 5d39f28 739e268 e4a20eb 739e268 e4a20eb 739e268 5d39f28 739e268 ec25ef6 e18e8e2 739e268 aaf1179 b96c8c5 5d39f28 739e268 5d39f28 739e268 b1fdc27 ec25ef6 739e268 ec25ef6 739e268 75cc483 739e268 75cc483 5d39f28 7b077a3 5d39f28 96cb6bb 7b077a3 96cb6bb 5d39f28 2115a66 739e268 c643307 e18e8e2 c643307 cbbd444 c643307 ddd360b e4a20eb c643307 0a6e1c9 c643307 99b5399 cbbd444 c643307 586b631 7b077a3 80c0bfe 00e74d5 c643307 194a41e 80c0bfe 1ff2dfa 80c0bfe c6a81f6 80c0bfe f7b4ac7 e4a20eb 80c0bfe ec25ef6 071e791 80c0bfe a447492 80c0bfe cec477c 586b631 cbbd444 80c0bfe 712647c |
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
import gradio as gr
import torch
from diffusers import DiffusionPipeline
from diffusers import AutoencoderKL
from config import *
from helpers import *
def device_change(device, config):
config = set_config(config, 'device', device)
return config, config, assemble_code(config)
def model_refiner_change(refiner, config):
config = set_config(config, 'refiner', refiner)
return config, config, assemble_code(config)
def cpu_offload_change(cpu_offload, config):
config = set_config(config, 'cpu_offload', cpu_offload)
return config, config, assemble_code(config)
def models_change(model, scheduler, config):
config = set_config(config, 'model', model)
use_safetensors = False
trigger_token = ""
# no model selected (because this is UI init run)
if type(model) != list and str(model) != 'None' and str(model) != 'null':
use_safetensors = str(models[model]['use_safetensors'])
model_description = models[model]['description']
trigger_token = models[model]['trigger_token']
# if no scheduler is selected, choose the default one for this model
if scheduler == None:
scheduler = models[model]['scheduler']
else:
model_description = 'Please select a model.'
config["use_safetensors"] = str(use_safetensors)
config["scheduler"] = str(scheduler)
# safety_checker_change(in_safety_checker.value, config)
# requires_safety_checker_change(in_requires_safety_checker.value, config)
return model_description, trigger_token, use_safetensors, scheduler, config, config, assemble_code(config)
def data_type_change(data_type, config):
config = set_config(config, 'data_type', data_type)
return config, config, assemble_code(config)
def tensorfloat32_change(allow_tensorfloat32, config):
config = set_config(config, 'allow_tensorfloat32', allow_tensorfloat32)
return config, config, assemble_code(config)
def inference_steps_change(inference_steps, config):
config = set_config(config, 'inference_steps', inference_steps)
return config, config, assemble_code(config)
def manual_seed_change(manual_seed, config):
config = set_config(config, 'manual_seed', manual_seed)
return config, config, assemble_code(config)
def guidance_scale_change(guidance_scale, config):
config = set_config(config, 'guidance_scale', guidance_scale)
return config, config, assemble_code(config)
def lora_scale_change(lora_scale, config):
config = set_config(config, 'lora_scale', lora_scale)
return config, config, assemble_code(config)
def enable_vae_slicing_change(enable_vae_slicing, config):
config = set_config(config, 'enable_vae_slicing', enable_vae_slicing)
return config, config, assemble_code(config)
def enable_vae_tiling_change(enable_vae_tiling, config):
config = set_config(config, 'enable_vae_tiling', enable_vae_tiling)
return config, config, assemble_code(config)
def prompt_change(prompt, config):
config = set_config(config, 'prompt', prompt)
return config, config, assemble_code(config)
def trigger_token_change(trigger_token, config):
config = set_config(config, 'trigger_token', trigger_token)
return config, config, assemble_code(config)
def negative_prompt_change(negative_prompt, config):
config = set_config(config, 'negative_prompt', negative_prompt)
return config, config, assemble_code(config)
def variant_change(variant, config):
config = set_config(config, 'variant', variant)
return config, config, assemble_code(config)
def attention_slicing_change(attention_slicing, config):
config = set_config(config, 'attention_slicing', attention_slicing)
return config, config, assemble_code(config)
def pre_compile_unet_change(pre_compile_unet, config):
config = set_config(config, 'pre_compile_unet', pre_compile_unet)
return config, config, assemble_code(config)
def safety_checker_change(safety_checker, config):
config = set_config(config, 'safety_checker', safety_checker)
return config, config, assemble_code(config)
def requires_safety_checker_change(requires_safety_checker, config):
config = set_config(config, 'requires_safety_checker', requires_safety_checker)
return config, config, assemble_code(config)
def auto_encoders_change(auto_encoder, config):
if str(auto_encoder) != 'None' and str(auto_encoder) != 'null' and type(auto_encoder) != list:
auto_encoder_description = auto_encoders[auto_encoder]
else:
auto_encoder_description = ''
config = set_config(config, 'auto_encoder', auto_encoder)
return auto_encoder_description, config, config, assemble_code(config)
def schedulers_change(scheduler, config):
if str(scheduler) != 'None' and str(scheduler) != 'null' and type(scheduler) != list:
scheduler_description = schedulers[scheduler]
else:
scheduler_description = 'Please select a scheduler.'
config = set_config(config, 'scheduler', scheduler)
return scheduler_description, config, config, assemble_code(config)
def adapters_textual_inversion_change(adapter_textual_inversion, config):
if str(adapter_textual_inversion) != 'None' and str(adapter_textual_inversion) != 'null' and type(adapter_textual_inversion) != list:
adapter_textual_inversion_description = adapters['textual_inversion'][adapter_textual_inversion]['description']
in_adapters_textual_inversion_token = adapters['textual_inversion'][adapter_textual_inversion]['token']
else:
adapter_textual_inversion_description = ""
in_adapters_textual_inversion_token = ""
config = set_config(config, 'adapter_textual_inversion', adapter_textual_inversion)
return adapter_textual_inversion_description, in_adapters_textual_inversion_token, config, config, assemble_code(config)
def adapters_lora_change(adapter_loras, config):
if len(adapter_loras) > 0:
adapter_lora_description = '; '.join([adapters['lora'][adapter_lora]['description'] for adapter_lora in adapter_loras])
adapter_lora_token = [adapters['lora'][adapter_lora]['token'] for adapter_lora in adapter_loras]
adapter_lora_weight = [adapters['lora'][adapter_lora]['weight'] for adapter_lora in adapter_loras]
adapter_lora_balancing = {}
for adapter_lora in adapter_loras:
if not adapter_lora in config['adapter_lora_balancing']:
adapter_lora_balancing[adapter_lora] = 1
else:
adapter_lora_balancing[adapter_lora] = config['adapter_lora_balancing'][adapter_lora]
else:
adapter_lora_description = []
adapter_lora_token = []
adapter_lora_weight = []
adapter_lora_balancing = {}
config = set_config(config, 'adapter_lora', adapter_loras)
config = set_config(config, 'adapter_lora_token', adapter_lora_token)
config = set_config(config, 'adapter_lora_weight', adapter_lora_weight)
config = set_config(config, 'adapter_lora_balancing', adapter_lora_balancing)
return adapter_lora_description, adapter_lora_token, adapter_lora_weight, adapter_lora_balancing, config, config, assemble_code(config)
def adapters_lora_balancing_change(adapter_lora_balancing, config):
config = set_config(config, 'adapter_lora_balancing', json.loads(adapter_lora_balancing.replace("'", '"').replace('None', 'null').replace('False', 'False')))
return config, config, assemble_code(config)
def textual_inversion_token_change(adapter_textual_inversion_token, config):
config = set_config(config, 'adapter_textual_inversion_token', adapter_textual_inversion_token)
return config, config, assemble_code(config)
def re_run_inference(config, config_history, pipeline, progress=gr.Progress(track_tqdm=True)):
if str(config["model"]) == 'None' and \
str(config["model"]) == 'null' and \
str(config["model"]) == '' and \
str(config["scheduler"]) == 'None':
return "Please select a model AND a scheduler.", "Please select a model AND a scheduler.", None, pipeline
if pipeline == None:
return "Please run full inference first.", "Please run full inference first.", None, pipeline
# # MANUAL SEED/GENERATOR - we probably don't need that again?
# if config["manual_seed"] is None or config["manual_seed"] == '' or int(config["manual_seed"]) < 0:
# generator = None
# else:
# generator = torch.manual_seed(int(config["manual_seed"]))
if len(config["adapter_lora"]) > 0 and len(config["adapter_lora"]) == len(config["adapter_lora_weight"]):
cross_attention_kwargs = {"scale": config["lora_scale"]}
else:
cross_attention_kwargs = None
# MANUAL SEED/GENERATOR
if config["manual_seed"] is None or config["manual_seed"] == '' or int(config["manual_seed"]) < 0:
generator = torch.Generator()
else:
generator = torch.Generator().manual_seed(int(config["manual_seed"]))
prompt = config["prompt"] + config["trigger_token"] + config["adapter_textual_inversion_token"] + ' '.join(config["adapter_lora_token"])
image = pipeline(
prompt = prompt,
generator = generator,
negative_prompt = config["negative_prompt"],
num_inference_steps = int(config["inference_steps"]),
cross_attention_kwargs = cross_attention_kwargs,
guidance_scale = float(config["guidance_scale"])).images
if config['refiner'].lower() != 'none' and config['refiner'].lower() != 'null':
image = refiner(
prompt = prompt,
num_inference_steps = int(config["inference_steps"]),
image=image,
).images
config_history.append(config.copy())
# expected output: out_image, out_config_history, config_history, pipeline
return image[0], dict_list_to_markdown_table(config_history), config_history, pipeline
def run_inference(config, config_history, pipeline, progress=gr.Progress(track_tqdm=True)):
if str(config["model"]) != 'None' and str(config["model"]) != 'null' and str(config["model"]) != '' and str(config["scheduler"]) != 'None':
progress(1, desc="Initializing pipeline...")
torch.cuda.empty_cache()
torch.backends.cuda.matmul.allow_tf32 = get_bool(config["allow_tensorfloat32"]) # Use TensorFloat-32 as of https://huggingface.co/docs/diffusers/main/en/optimization/fp16 faster, but slightly less accurate computations
# INIT PIPELINE
pipeline = get_pipeline(config)
progress(2, desc="Setting pipeline params...")
if str(config["cpu_offload"]).lower() != 'false':
pipeline.enable_model_cpu_offload()
# ATTENTION SLICING
if str(config["attention_slicing"]).lower() == 'true': pipeline.enable_attention_slicing()
# PRE COMPILE UNET
if str(config["pre_compile_unet"]).lower() == 'true': pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)
# AUTO ENCODER
if str(config["auto_encoder"]).lower() != 'none' and str(config["auto_encoder"]).lower() != 'null' and str(config["auto_encoder"]).lower() != '':
pipeline.vae = AutoencoderKL.from_pretrained(config["auto_encoder"], torch_dtype=get_data_type(config["data_type"])).to(config["device"])
if str(config["enable_vae_slicing"]).lower() != 'false': pipeline.enable_vae_slicing()
if str(config["enable_vae_tiling"]).lower() != 'false': pipeline.enable_vae_tiling()
# SAFETY CHECKER
if str(config["safety_checker"]).lower() == 'false': pipeline.safety_checker = None
pipeline.requires_safety_checker = get_bool(config["requires_safety_checker"])
# SCHEDULER/SOLVER
pipeline.scheduler = get_scheduler(config["scheduler"], pipeline.scheduler.config)
# INIT REFINER
if str(config['refiner']).lower() != 'none' and str(config['refiner']).lower() != 'null':
progress(3, desc="Initializing refiner...")
refiner = DiffusionPipeline.from_pretrained(
config['refiner'],
text_encoder_2=pipeline.text_encoder_2,
vae=pipeline.vae,
torch_dtype=get_data_type(config["data_type"]),
use_safetensors=get_bool(config["use_safetensors"]),
variant = get_variant(config["variant"])).to(config["device"])
if str(config["cpu_offload"]).lower() != 'false':
refiner.enable_model_cpu_offload()
if str(config["enable_vae_slicing"]).lower() != 'false': refiner.enable_vae_slicing()
if str(config["enable_vae_tiling"]).lower() != 'false': refiner.enable_vae_tiling()
# ADAPTERS
# TEXTUAL INVERSION
if str(config["adapter_textual_inversion"]).lower() != 'none' and str(config["adapter_textual_inversion"]).lower() != 'null' and str(config["adapter_textual_inversion"]).lower() != '':
progress(4, desc=f"Loading textual inversion adapter {config['adapter_textual_inversion']}...")
pipeline.load_textual_inversion(config["adapter_textual_inversion"], token=config["adapter_textual_inversion_token"])
# LoRA
if len(config["adapter_lora"]) > 0 and len(config["adapter_lora"]) == len(config["adapter_lora_weight"]):
adapter_lora_balancing = []
for adapter_lora_index, adapter_lora in enumerate(config["adapter_lora"]):
progress(5, desc=f"Loading LoRA adapters {config['adapter_lora']}...")
if str(config["adapter_lora_weight"][adapter_lora_index]).lower() != 'none':
pipeline.load_lora_weights(adapter_lora, weight_name=config["adapter_lora_weight"][adapter_lora_index], adapter_name=config["adapter_lora_token"][adapter_lora_index])
else:
pipeline.load_lora_weights(adapter_lora, adapter_name=config["adapter_lora_token"][adapter_lora_index])
adapter_lora_balancing.append(config["adapter_lora_balancing"][adapter_lora])
adapter_weights = adapter_lora_balancing
pipeline.set_adapters(config["adapter_lora_token"], adapter_weights=adapter_weights)
cross_attention_kwargs = {"scale": float(config["lora_scale"])}
else:
cross_attention_kwargs = None
progress(6, desc="Inferencing...")
# MANUAL SEED/GENERATOR
if config["manual_seed"] is None or config["manual_seed"] == '' or int(config["manual_seed"]) < 0:
generator = torch.Generator()
else:
generator = torch.Generator().manual_seed(int(config["manual_seed"]))
prompt = config["prompt"] + config["trigger_token"] + config["adapter_textual_inversion_token"] + ' '.join(config["adapter_lora_token"])
image = pipeline(
prompt = prompt,
negative_prompt = config["negative_prompt"],
generator = generator,
num_inference_steps = int(config["inference_steps"]),
cross_attention_kwargs = cross_attention_kwargs,
guidance_scale = float(config["guidance_scale"])).images
if config['refiner'].lower() != 'none' and config['refiner'].lower() != 'null':
image = refiner(
prompt = prompt,
num_inference_steps = int(config["inference_steps"]),
image=image,
).images
config_history.append(config.copy())
# expected output: out_image, out_config_history, config_history, pipeline
return image[0], dict_list_to_markdown_table(config_history), config_history, pipeline
else:
return "Please select a model AND a scheduler.", "Please select a model AND a scheduler.", None, pipeline
appConfig = load_app_config()
models = appConfig.get("models", {})
schedulers = appConfig.get("schedulers", {})
devices = appConfig.get("devices", [])
refiners = appConfig.get("refiners", [])
auto_encoders = appConfig.get("auto_encoders", [])
adapters = appConfig.get("adapters", [])
js = '''function js(){
window.set_cookie = function(key, value, config){
document.cookie = key+'='+value+'; Path=/; SameSite=Strict';
return [value, config]
}
window.set_model_cookie = function(model, config){
document.cookie = 'model='+ model+'; Path=/; SameSite=Strict';
// some things I just don't understand, this is one of them
return [model, null, null, null, model, config, null]
}
window.set_adapter_textual_inversion_cookie = function(adapter_textual_inversion, config){
document.cookie = 'adapter_textual_inversion='+ adapter_textual_inversion+'; Path=/; SameSite=Strict';
// some things I just don't understand, this is one of them
return [adapter_textual_inversion, null, adapter_textual_inversion, config, null]
}
window.set_adapter_lora_cookie = function(adapter_lora, config){
document.cookie = 'adapter_lora='+ JSON.stringify(adapter_lora)+'; Path=/; SameSite=Strict';
// some things I just don't understand, this is one of them
return [adapter_lora, null, null, null, adapter_lora, config, null]
}
window.set_cookie_2 = function(key, value, config){
document.cookie = key+'='+value+'; Path=/; SameSite=Strict';
// some things I just don't understand, this is one of them
return [value, null, config, null]
}
}
'''
# interface
with gr.Blocks(analytics_enabled=False) as demo:
config = gr.State(value=get_initial_config())
config_history = gr.State(value=[])
pipeline = gr.State()
gr.Markdown('''## Text-2-Image Playground
<small>by <a target="_blank" href="https://nickyreinert.de/">Nicky Reinert</a> |
home base: https://huggingface.co/spaces/n42/pictero
</small>''')
gr.Markdown("### Device")
gr.Markdown("(you may add a custom device address at any time)")
with gr.Row():
in_devices = gr.Dropdown(label="Device:", value=config.value["device"], choices=devices, filterable=True, multiselect=False, allow_custom_value=True, info="")
gr.Column("")
gr.Column("")
with gr.Accordion("Device specific settings", open=False):
with gr.Row():
in_cpu_offload = gr.Radio(label="CPU Offload:", value=config.value["cpu_offload"], choices=["True", "False"], info="This may increase performance, as it offloads computations from the GPU to the CPU. But this can also lead to slower executions and lower effectiveness. Compare running time and outputs before making sure, that this setting will help you, is not supported on MPS")
in_data_type = gr.Radio(label="Data Type:", value=config.value["data_type"], choices=["bfloat16", "float16", "float32"], info="`bfloat16` is not supported on MPS devices right now; `float16` may also not be supported on all devices, Half-precision weights, will save GPU memory, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16")
in_allow_tensorfloat32 = gr.Radio(label="Allow TensorFloat32:", value=config.value["allow_tensorfloat32"], choices=["True", "False"], info="is not supported on MPS devices right now; use TensorFloat-32 is faster, but results in slightly less accurate computations, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
with gr.Row():
in_variant = gr.Radio(label="Variant:", value=config.value["variant"], choices=["fp16", None], info="Use half-precision weights will save GPU memory, not all models support that, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
in_attention_slicing = gr.Radio(label="Attention slicing:", value=config.value["attention_slicing"], choices=["True", "False"], info="Attention operation will be cutted into multiple steps, see https://huggingface.co/docs/diffusers/optimization/mps")
in_pre_compile_unet = gr.Radio(label="Pre-Compile UNet:", value=config.value["pre_compile_unet"], choices=["True", "False"], info="Can speed up the inference process, compilation takes some time, so you should only apply this option when you finalize your inference, does not work on MPS, see https://huggingface.co/docs/diffusers/optimization/torch2.0 ")
gr.Column("")
gr.Markdown("### Model")
with gr.Row():
with gr.Column(scale=1):
in_models = gr.Dropdown(choices=list(models.keys()), label="Model")
with gr.Column(scale=2):
out_model_description = gr.Textbox(value="", label="Description")
with gr.Accordion("Model specific settings", open=False):
with gr.Row():
in_trigger_token = gr.Textbox(value=config.value["trigger_token"], label="Trigger Token", info="will be added to your prompt to `activate` a fine tuned model")
in_model_refiner = gr.Dropdown(value=config.value["refiner"], choices=['none'] + refiners, label="Refiner", allow_custom_value=True, multiselect=False)
gr.Column("")
with gr.Row():
in_use_safetensors = gr.Radio(label="Use safe tensors:", choices=["True", "False"], interactive=False)
in_safety_checker = gr.Radio(label="Enable safety checker:", value=config.value["safety_checker"], choices=["True", "False"])
in_requires_safety_checker = gr.Radio(label="Requires safety checker:", value=config.value["requires_safety_checker"], choices=["True", "False"])
gr.Markdown("### Scheduler")
gr.Markdown("Schedulers employ various strategies for noise control, the scheduler controls parameter adaption between each inference step, depending on the right scheduler for your model, it may only take 10 or 20 steps to achieve very good results, see https://huggingface.co/docs/diffusers/using-diffusers/loading#schedulers")
with gr.Row():
with gr.Column(scale=1):
in_schedulers = gr.Dropdown(value="", choices=list(schedulers.keys()), allow_custom_value=True, label="Scheduler/Solver", info="")
with gr.Column(scale=2):
out_scheduler_description = gr.Textbox(value="", label="Description")
with gr.Accordion("Auto Encoder", open=False):
with gr.Row():
gr.Markdown("**VAE** stands for Variational Auto Encoders. An 'autoencoder' is an artificial neural network that is able to encode input data and decode to output data to bascially recreate the input. The VAE whereas adds a couple of additional layers of complexity to create new and unique output.")
with gr.Row():
in_auto_encoders = gr.Dropdown(value="None", choices=list(auto_encoders.keys()), label="Auto encoder", info="leave empty to not add an auto encoder")
out_auto_encoder_description = gr.Textbox(value="", label="Description")
gr.Column("")
with gr.Row():
in_enable_vae_slicing = gr.Radio(label="Enable VAE slicing:", value=config.value["enable_vae_slicing"], choices=["True", "False"], info="decoding the batches of latents one image at a time, which may reduce memory usage, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
in_enable_vae_tiling= gr.Radio(label="Enable VAE tiling:", value=config.value["enable_vae_tiling"], choices=["True", "False"], info="splitting the image into overlapping tiles, decoding the tiles, and then blending the outputs together to compose the final image, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
gr.Column("")
with gr.Accordion("Adapters", open=False):
with gr.Row():
gr.Markdown('''Adapters allow you to apply finetuned weights to your base model. They come in many flavors depending on how they were trained. See see https://huggingface.co/docs/diffusers/using-diffusers/loading_adapters''')
with gr.Row():
gr.Markdown('#### Textual Inversion Adapters')
with gr.Row():
gr.Markdown('(a technique that enables a model like Stable Diffusion to learn a new concept from just a few sample images)')
with gr.Row():
in_adapters_textual_inversion = gr.Dropdown(value="", choices=list(adapters['textual_inversion'].keys()), label="Textual Inversion Adapter", info="leave empty to not use an adapter")
in_adapters_textual_inversion_token = gr.Textbox(value="", label="Token", info="required to activate the token, will be added to your prompt")
out_adapters_textual_inversion_description = gr.Textbox(value="", label="Description")
with gr.Row():
gr.Markdown('#### LoRA')
with gr.Row():
gr.Markdown('(Low-Rank-Adaption is a performant fine tuning technique)')
with gr.Row():
in_adapters_lora = gr.Dropdown(value="None", choices=list(adapters['lora'].keys()), multiselect=True, label="LoRA Adapter", info="leave empty to not use an adapter")
out_adapters_lora_description = gr.Textbox(value="", label="Description")
in_lora_scale = gr.Slider(minimum=0, maximum=1, step=0.1, label="LoRA Scale", value=config.value["lora_scale"], info="How should the LoRA model influence the result, from 0 (no influence) to 1 (full influencer)")
with gr.Row():
in_adapters_lora_token = gr.Textbox(value="None", label="Token(s)", info="required to activate the token, will be added to your prompt")
in_adapters_lora_weight = gr.Textbox(value="", label="Weight(s)/Checkpoint(s)")
in_adapters_lora_balancing = gr.Textbox(value={}, label="Balancing", info="provide a list of balancing weights in the order of your LoRA adapter (according to `token`s)")
gr.Markdown("### Inference settings")
with gr.Row():
in_prompt = gr.TextArea(label="Prompt", value=config.value["prompt"])
in_negative_prompt = gr.TextArea(label="Negative prompt", value=config.value["negative_prompt"])
with gr.Row():
in_guidance_scale = gr.Slider(minimum=0, maximum=100, step=0.1, label="Guidance Scale", value=config.value["guidance_scale"], info="A low guidance scale leads to a faster inference time, with the drawback that negative prompts don’t have any effect on the denoising process.")
in_inference_steps = gr.Number(label="Inference steps", value=config.value["inference_steps"], info="Each step improves the final result but also results in higher computation")
in_manual_seed = gr.Number(label="Manual seed", value=config.value["manual_seed"], info="Set this to -1 or leave it empty to randomly generate an image. A fixed value will result in a similar image for every run")
gr.Markdown("### Output")
with gr.Row():
gr.Markdown('Hit "Re-Run" to restart the pipeline with your changes to the inference settings only')
with gr.Row():
btn_start_pipeline = gr.Button(value="Run", variant="primary")
btn_re_start_pipeline = gr.Button(value="Re-Run")
btn_stop_pipeline = gr.Button(value="Stop", variant="stop")
with gr.Row():
out_image = gr.Image()
with gr.Accordion("Code and Configuration", open=False):
with gr.Row():
out_code = gr.Code(assemble_code(config.value), label="Code")
# out_config = gr.Code(value=str(config.value), label="Current config")
out_config = gr.JSON(value=config.value, label="Current config")
with gr.Row():
out_config_history = gr.Markdown(dict_list_to_markdown_table(config_history.value))
# `SPECIAL` CHANGE LISTENERS
in_models.change(models_change, inputs=[in_models, in_schedulers, config], outputs=[out_model_description, in_trigger_token, in_use_safetensors, in_schedulers, config, out_config, out_code], js="(model, config) => set_model_cookie(model, config)")
in_schedulers.change(schedulers_change, inputs=[in_schedulers, config], outputs=[out_scheduler_description, config, out_config, out_code], js="(value, config) => set_cookie_2('scheduler', value, config)")
in_auto_encoders.change(auto_encoders_change, inputs=[in_auto_encoders, config], outputs=[out_auto_encoder_description, config, out_config, out_code], js="(value, config) => set_cookie_2('auto_encoder', value, config)")
in_adapters_textual_inversion.change(adapters_textual_inversion_change, inputs=[in_adapters_textual_inversion, config], outputs=[out_adapters_textual_inversion_description, in_adapters_textual_inversion_token, config, out_config, out_code], js="(adapter_textual_inversion, config) => set_adapter_textual_inversion_cookie(adapter_textual_inversion, config)")
in_adapters_lora.change(adapters_lora_change, inputs=[in_adapters_lora, config], outputs=[out_adapters_lora_description, in_adapters_lora_token, in_adapters_lora_weight, in_adapters_lora_balancing, config, out_config, out_code], js="(adapter_lora, config) => set_adapter_lora_cookie(adapter_lora, config)")
# `GENERIC` CHANGE LISTENERS, SAME INPUT, SAME OUTPUT STRUCTURE
in_devices.change(fn=device_change, inputs=[in_devices, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('device', value, config)")
in_data_type.change(data_type_change, inputs=[in_data_type, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('data_type', value, config)")
in_allow_tensorfloat32.change(tensorfloat32_change, inputs=[in_allow_tensorfloat32, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('allow_tensorfloat32', value, config)")
in_variant.change(variant_change, inputs=[in_variant, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('variant', value, config)")
in_attention_slicing.change(attention_slicing_change, inputs=[in_attention_slicing, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('attention_slicing', value, config)")
in_pre_compile_unet.change(pre_compile_unet_change, inputs=[in_pre_compile_unet, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('pre_compile_unet', value, config)")
in_model_refiner.change(model_refiner_change, inputs=[in_model_refiner, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('model_refiner', value, config)")
in_cpu_offload.change(cpu_offload_change, inputs=[in_cpu_offload, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('cpu_offload', value, config)")
in_safety_checker.change(safety_checker_change, inputs=[in_safety_checker, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('safety_checker', value, config)")
in_requires_safety_checker.change(requires_safety_checker_change, inputs=[in_requires_safety_checker, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('requires_safety_checker', value, config)")
in_inference_steps.change(inference_steps_change, inputs=[in_inference_steps, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('inference_steps', value, config)")
in_manual_seed.change(manual_seed_change, inputs=[in_manual_seed, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('manual_seed', value, config)")
in_guidance_scale.change(guidance_scale_change, inputs=[in_guidance_scale, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('guidance_scale', value, config)")
in_lora_scale.change(lora_scale_change, inputs=[in_lora_scale, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('lora_scale', value, config)")
in_enable_vae_slicing.change(enable_vae_slicing_change, inputs=[in_enable_vae_slicing, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('enable_vae_slicing', value, config)")
in_enable_vae_tiling.change(enable_vae_tiling_change, inputs=[in_enable_vae_tiling, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('enable_vae_tiling', value, config)")
in_adapters_textual_inversion_token.change(textual_inversion_token_change, inputs=[in_adapters_textual_inversion_token, config], outputs=[config, out_config, out_code])
in_adapters_lora_balancing.change(adapters_lora_balancing_change, inputs=[in_adapters_lora_balancing, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('adapter_lora_balancing', value, config)")
in_prompt.change(prompt_change, inputs=[in_prompt, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('prompt', value, config)")
in_trigger_token.change(trigger_token_change, inputs=[in_trigger_token, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('trigger_token', value, config)")
in_negative_prompt.change(negative_prompt_change, inputs=[in_negative_prompt, config], outputs=[config, out_config, out_code], js="(value, config) => set_cookie('negative_prompt', value, config)")
ev_run_inference = btn_start_pipeline.click(run_inference, inputs=[config, config_history, pipeline], outputs=[out_image, out_config_history, config_history, pipeline])
ev_re_run_inference = btn_re_start_pipeline.click(re_run_inference, inputs=[config, config_history, pipeline], outputs=[out_image, out_config_history, config_history, pipeline])
btn_stop_pipeline.click(fn=None, inputs=None, outputs=None, cancels=[ev_run_inference, ev_re_run_inference])
# send current respect initial config to init_config to populate parameters to all relevant input fields
# if GET parameter is set, it will overwrite initial config parameters
demo.load(fn=get_config_from_url, js=js,
inputs=[config],
outputs=[
in_models,
in_devices,
in_cpu_offload,
in_use_safetensors,
in_data_type,
in_model_refiner,
in_variant,
in_attention_slicing,
in_pre_compile_unet,
in_safety_checker,
in_requires_safety_checker,
in_auto_encoders,
in_enable_vae_slicing,
in_enable_vae_tiling,
in_schedulers,
in_prompt,
in_trigger_token,
in_negative_prompt,
in_inference_steps,
in_manual_seed,
in_guidance_scale,
in_adapters_textual_inversion,
in_adapters_textual_inversion_token,
in_adapters_lora,
in_adapters_lora_token,
in_adapters_lora_weight,
in_adapters_lora_balancing,
])
demo.launch(show_error=True) |