Spaces:
Running
on
Zero
Running
on
Zero
File size: 35,055 Bytes
0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0e7c9e6 0900c59 9c05349 0e7c9e6 0900c59 9c05349 0e7c9e6 0900c59 9c05349 0e7c9e6 294f972 30418ed 9c05349 eb56303 9c05349 eb56303 9c05349 eb56303 9c05349 eb56303 9c05349 eb56303 0900c59 60fba13 9c05349 60fba13 9c05349 0900c59 60fba13 05bc72e 60fba13 0900c59 9c05349 0900c59 60fba13 9c05349 60fba13 9c05349 60fba13 9c05349 0900c59 9c05349 0900c59 60fba13 9c05349 60fba13 9c05349 60fba13 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 ec79957 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 30418ed 0900c59 9c05349 0900c59 9c05349 60fba13 9c05349 60fba13 9c246a3 60fba13 9c246a3 9c05349 0900c59 9c05349 60fba13 0900c59 60fba13 9c05349 60fba13 9c05349 60fba13 9c05349 0900c59 60fba13 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 0900c59 9c05349 ec79957 0900c59 9c05349 0900c59 9c05349 0900c59 ec79957 0900c59 |
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 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
import torch
from diffusers import StableDiffusion3Pipeline, StableDiffusionPipeline, StableDiffusionXLPipeline, DPMSolverSinglestepScheduler, StableCascadePriorPipeline, StableCascadeDecoderPipeline
import gradio as gr
import os
import random
import numpy as np
from PIL import Image
import spaces
HF_TOKEN = os.getenv("HF_TOKEN") # login with hf token to access sd gated models
if torch.cuda.is_available():
device = "cuda"
print("Using GPU")
else:
device = "cpu"
print("Using CPU")
MAX_SEED = np.iinfo(np.int32).max
# Initialize the pipelines for each sd model
sd3_medium_pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
sd3_medium_pipe.enable_model_cpu_offload()
sd2_1_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
sd2_1_pipe.enable_model_cpu_offload()
sdxl_pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
sdxl_pipe.enable_model_cpu_offload()
sdxl_flash_pipe = StableDiffusionXLPipeline.from_pretrained("sd-community/sdxl-flash", torch_dtype=torch.float16)
sdxl_flash_pipe.enable_model_cpu_offload()
# Ensure sampler uses "trailing" timesteps for sdxl flash.
sdxl_flash_pipe.scheduler = DPMSolverSinglestepScheduler.from_config(sdxl_flash_pipe.scheduler.config, timestep_spacing="trailing")
stable_cascade_prior_pipe = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", variant="bf16", torch_dtype=torch.bfloat16)
stable_cascade_decoder_pipe = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.float16)
stable_cascade_prior_pipe.enable_model_cpu_offload()
stable_cascade_decoder_pipe.enable_model_cpu_offload()
# Helper function to generate images for a single model
@spaces.GPU(duration=80)
def generate_single_image(
prompt,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
model_choice,
generator,
prior_num_inference_steps=None,
prior_guidance_scale=None,
decoder_num_inference_steps=None,
decoder_guidance_scale=None,
):
# Select the correct pipeline based on the model choice
if model_choice == "sd3 medium":
pipe = sd3_medium_pipe
elif model_choice == "sd2.1":
pipe = sd2_1_pipe
elif model_choice == "sdxl":
pipe = sdxl_pipe
elif model_choice == "sdxl flash":
pipe = sdxl_flash_pipe
elif model_choice == "stable cascade":
pipe = stable_cascade_prior_pipe
else:
raise ValueError(f"Invalid model choice: {model_choice}")
if model_choice == "stable cascade":
prior_output = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=prior_num_inference_steps,
guidance_scale=prior_guidance_scale,
height=height,
width=width,
generator=generator,
num_images_per_prompt=num_images_per_prompt,
)
output = stable_cascade_decoder_pipe(
image_embeddings=prior_output.image_embeddings.to(torch.float16),
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=decoder_num_inference_steps,
guidance_scale=decoder_guidance_scale,
).images
else:
output = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
height=height,
width=width,
generator=generator,
num_images_per_prompt=num_images_per_prompt,
).images
return output
# Define the image generation function for the Arena tab
@spaces.GPU(duration=80)
def generate_arena_images(
prompt,
negative_prompt,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
height,
width,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b,
progress=gr.Progress(track_tqdm=True),
):
if seed == 0:
seed = random.randint(1, 2**32 - 1)
generator = torch.Generator().manual_seed(seed)
# Generate images for both models
images_a = generate_single_image(
prompt,
negative_prompt,
num_inference_steps_a,
guidance_scale_a,
height,
width,
seed,
num_images_per_prompt,
model_choice_a,
generator,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a,
)
images_b = generate_single_image(
prompt,
negative_prompt,
num_inference_steps_b,
guidance_scale_b,
height,
width,
seed,
num_images_per_prompt,
model_choice_b,
generator,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b,
)
return images_a, images_b
# Define the image generation function for the Individual tab
@spaces.GPU(duration=80)
def generate_individual_image(
prompt,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
model_choice,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
progress=gr.Progress(track_tqdm=True),
):
if seed == 0:
seed = random.randint(1, 2**32 - 1)
generator = torch.Generator().manual_seed(seed)
output = generate_single_image(
prompt,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
model_choice,
generator,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
)
return output
# Create the Gradio interface
examples_arena = [
[
"A woman in a red dress singing on top of a building.",
"deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
25,
7.5,
25,
7.5,
1024,
1024,
42,
2,
"sd3 medium",
"sdxl",
25, #prior_num_inference_steps_a
4.0, #prior_guidance_scale_a
12, #decoder_num_inference_steps_a
0.0, #decoder_guidance_scale_a
25, #prior_num_inference_steps_b
4.0, #prior_guidance_scale_b
12, #decoder_num_inference_steps_b
0.0 #decoder_guidance_scale_b
],
[
"An astronaut on mars in a futuristic cyborg suit.",
"deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
25,
7.5,
25,
7.5,
1024,
1024,
42,
2,
"sd3 medium",
"sdxl",
25, #prior_num_inference_steps_a
4.0, #prior_guidance_scale_a
12, #decoder_num_inference_steps_a
0.0, #decoder_guidance_scale_a
25, #prior_num_inference_steps_b
4.0, #prior_guidance_scale_b
12, #decoder_num_inference_steps_b
0.0 #decoder_guidance_scale_b
],
]
examples_individual = [
[
"A woman in a red dress singing on top of a building.",
"deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
25,
7.5,
1024,
1024,
42,
2,
"sdxl",
25, #prior_num_inference_steps
4.0, #prior_guidance_scale
12, #decoder_num_inference_steps
0.0 #decoder_guidance_scale
],
[
"An astronaut on mars in a futuristic cyborg suit.",
"deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
25,
7.5,
1024,
1024,
42,
2,
"sdxl",
25, #prior_num_inference_steps
4.0, #prior_guidance_scale
12, #decoder_num_inference_steps
0.0 #decoder_guidance_scale
],
]
css = """
.gradio-container{max-width: 1000px !important}
h1{text-align:center}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
gr.HTML(
"""
<h1 style='text-align: center'>
Stable Diffusion Arena
</h1>
"""
)
gr.HTML(
"""
Made by <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a>
<br> <a href="https://discord.gg/osai"> <img src="https://img.shields.io/discord/1198701940511617164?color=%23738ADB&label=Discord&style=for-the-badge" alt="Discord"> </a>
"""
)
with gr.Tabs():
with gr.TabItem("Arena"):
with gr.Group():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
info="Describe the image you want",
placeholder="A cat...",
)
model_choice_a = gr.Dropdown(
label="Stable Diffusion Model A",
choices=["sd3 medium", "sd2.1", "sdxl", "sdxl flash", "stable cascade"],
value="sd3 medium",
)
model_choice_b = gr.Dropdown(
label="Stable Diffusion Model B",
choices=["sd3 medium", "sd2.1", "sdxl", "sdxl flash", "stable cascade"],
value="sdxl",
)
run_button = gr.Button("Run")
result_1 = gr.Gallery(label="Generated Images (Model A)", elem_id="gallery_1")
result_2 = gr.Gallery(label="Generated Images (Model B)", elem_id="gallery_2")
with gr.Accordion("Advanced options", open=False):
negative_prompt = gr.Textbox(
label="Negative Prompt",
info="Describe what you don't want in the image",
value="deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
placeholder="Ugly, bad anatomy...",
)
with gr.Row():
with gr.Column():
num_inference_steps_a = gr.Slider(
label="Inference Steps (Model A)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=True
)
guidance_scale_a = gr.Slider(
label="Guidance Scale (Model A)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=7.5,
step=0.1,
visible=True
)
prior_num_inference_steps_a = gr.Slider(
label="Prior Inference Steps (Model A)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=False
)
prior_guidance_scale_a = gr.Slider(
label="Prior Guidance Scale (Model A)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=4.0,
step=0.1,
visible=False
)
decoder_num_inference_steps_a = gr.Slider(
label="Decoder Inference Steps (Model A)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=15,
value=15,
step=1,
visible=False
)
decoder_guidance_scale_a = gr.Slider(
label="Decoder Guidance Scale (Model A)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=0.0,
step=0.1,
visible=False
)
with gr.Column():
num_inference_steps_b = gr.Slider(
label="Inference Steps (Model B)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=True
)
guidance_scale_b = gr.Slider(
label="Guidance Scale (Model B)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=7.5,
step=0.1,
visible=True
)
prior_num_inference_steps_b = gr.Slider(
label="Prior Inference Steps (Model B)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=False
)
prior_guidance_scale_b = gr.Slider(
label="Prior Guidance Scale (Model B)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=4.0,
step=0.1,
visible=False
)
decoder_num_inference_steps_b = gr.Slider(
label="Decoder Inference Steps (Model B)",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=15,
value=12,
step=1,
visible=False
)
decoder_guidance_scale_b = gr.Slider(
label="Decoder Guidance Scale (Model B)",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=0.0,
step=0.1,
visible=False
)
with gr.Row():
width = gr.Slider(
label="Width",
info="Width of the Image",
minimum=256,
maximum=1344,
step=32,
value=1024,
)
height = gr.Slider(
label="Height",
info="Height of the Image",
minimum=256,
maximum=1344,
step=32,
value=1024,
)
with gr.Row():
seed = gr.Slider(
value=42,
minimum=0,
maximum=MAX_SEED,
step=1,
label="Seed",
info="A starting point to initiate the generation process, put 0 for a random one",
)
num_images_per_prompt = gr.Slider(
label="Images Per Prompt",
info="Number of Images to generate with the settings",
minimum=1,
maximum=4,
step=1,
value=2,
)
def toggle_visibility_arena_a(model_choice_a):
if model_choice_a == "stable cascade":
return {
num_inference_steps_a: gr.update(visible=False),
guidance_scale_a: gr.update(visible=False),
prior_num_inference_steps_a: gr.update(visible=True),
prior_guidance_scale_a: gr.update(visible=True),
decoder_num_inference_steps_a: gr.update(visible=True),
decoder_guidance_scale_a: gr.update(visible=True),
}
elif model_choice_a == "sdxl flash":
return {
num_inference_steps_a: gr.update(visible=True, maximum=15, value=8),
guidance_scale_a: gr.update(visible=True, maximum=6.0, value=3.5),
prior_num_inference_steps_a: gr.update(visible=False),
prior_guidance_scale_a: gr.update(visible=False),
decoder_num_inference_steps_a: gr.update(visible=False),
decoder_guidance_scale_a: gr.update(visible=False),
}
else:
return {
num_inference_steps_a: gr.update(visible=True, maximum=50, value=25),
guidance_scale_a: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_a: gr.update(visible=False),
prior_guidance_scale_a: gr.update(visible=False),
decoder_num_inference_steps_a: gr.update(visible=False),
decoder_guidance_scale_a: gr.update(visible=False),
}
def toggle_visibility_arena_b(model_choice_b):
if model_choice_b == "stable cascade":
return {
num_inference_steps_b: gr.update(visible=False),
guidance_scale_b: gr.update(visible=False),
prior_num_inference_steps_b: gr.update(visible=True),
prior_guidance_scale_b: gr.update(visible=True),
decoder_num_inference_steps_b: gr.update(visible=True),
decoder_guidance_scale_b: gr.update(visible=True),
}
elif model_choice_b == "sdxl flash":
return {
num_inference_steps_b: gr.update(visible=True, maximum=15, value=8),
guidance_scale_b: gr.update(visible=True, maximum=6.0, value=3.5),
prior_num_inference_steps_b: gr.update(visible=False),
prior_guidance_scale_b: gr.update(visible=False),
decoder_num_inference_steps_b: gr.update(visible=False),
decoder_guidance_scale_b: gr.update(visible=False),
}
else:
return {
num_inference_steps_b: gr.update(visible=True, maximum=50, value=25),
guidance_scale_b: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_b: gr.update(visible=False),
prior_guidance_scale_b: gr.update(visible=False),
decoder_num_inference_steps_b: gr.update(visible=False),
decoder_guidance_scale_b: gr.update(visible=False),
}
model_choice_a.change(
toggle_visibility_arena_a,
inputs=[model_choice_a],
outputs=[
num_inference_steps_a,
guidance_scale_a,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a
]
)
model_choice_b.change(
toggle_visibility_arena_b,
inputs=[model_choice_b],
outputs=[
num_inference_steps_b,
guidance_scale_b,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b
]
)
gr.Examples(
examples=examples_arena,
inputs=[
prompt,
negative_prompt,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
height,
width,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b,
],
outputs=[result_1, result_2],
fn=generate_arena_images,
)
gr.on(
triggers=[
prompt.submit,
run_button.click,
],
fn=generate_arena_images,
inputs=[
prompt,
negative_prompt,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
height,
width,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b,
],
outputs=[result_1, result_2],
)
with gr.TabItem("Individual"):
with gr.Group():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
info="Describe the image you want",
placeholder="A cat...",
)
model_choice = gr.Dropdown(
label="Stable Diffusion Model",
choices=["sd3 medium", "sd2.1", "sdxl", "sdxl flash", "stable cascade"],
value="sd3 medium",
)
run_button = gr.Button("Run")
result = gr.Gallery(label="Generated AI Images", elem_id="gallery")
with gr.Accordion("Advanced options", open=False):
with gr.Row():
negative_prompt = gr.Textbox(
label="Negative Prompt",
info="Describe what you don't want in the image",
value="deformed, distorted, disfigured, poorly drawn, bad anatomy, incorrect anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
placeholder="Ugly, bad anatomy...",
)
with gr.Row():
num_inference_steps = gr.Slider(
label="Number of Inference Steps",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=True
)
guidance_scale = gr.Slider(
label="Guidance Scale",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=7.5,
step=0.1,
visible=True
)
prior_num_inference_steps = gr.Slider(
label="Prior Inference Steps",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=50,
value=25,
step=1,
visible=False
)
prior_guidance_scale = gr.Slider(
label="Prior Guidance Scale",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=4.0,
step=0.1,
visible=False
)
decoder_num_inference_steps = gr.Slider(
label="Decoder Inference Steps",
info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference",
minimum=1,
maximum=15,
value=12,
step=1,
visible=False
)
decoder_guidance_scale = gr.Slider(
label="Decoder Guidance Scale",
info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.",
minimum=0.0,
maximum=10.0,
value=0.0,
step=0.1,
visible=False
)
with gr.Row():
width = gr.Slider(
label="Width",
info="Width of the Image",
minimum=256,
maximum=1344,
step=32,
value=1024,
)
height = gr.Slider(
label="Height",
info="Height of the Image",
minimum=256,
maximum=1344,
step=32,
value=1024,
)
with gr.Row():
seed = gr.Slider(
value=42,
minimum=0,
maximum=MAX_SEED,
step=1,
label="Seed",
info="A starting point to initiate the generation process, put 0 for a random one",
)
num_images_per_prompt = gr.Slider(
label="Images Per Prompt",
info="Number of Images to generate with the settings",
minimum=1,
maximum=4,
step=1,
value=2,
)
def toggle_visibility_individual(model_choice):
if model_choice == "stable cascade":
return {
num_inference_steps: gr.update(visible=False),
guidance_scale: gr.update(visible=False),
prior_num_inference_steps: gr.update(visible=True),
prior_guidance_scale: gr.update(visible=True),
decoder_num_inference_steps: gr.update(visible=True),
decoder_guidance_scale: gr.update(visible=True),
}
elif model_choice == "sdxl flash":
return {
num_inference_steps: gr.update(visible=True, maximum=15, value=8),
guidance_scale: gr.update(visible=True, maximum=6.0, value=3.5),
prior_num_inference_steps: gr.update(visible=False),
prior_guidance_scale: gr.update(visible=False),
decoder_num_inference_steps: gr.update(visible=False),
decoder_guidance_scale: gr.update(visible=False),
}
else:
return {
num_inference_steps: gr.update(visible=True, maximum=50, value=25),
guidance_scale: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps: gr.update(visible=False),
prior_guidance_scale: gr.update(visible=False),
decoder_num_inference_steps: gr.update(visible=False),
decoder_guidance_scale: gr.update(visible=False),
}
model_choice.change(
toggle_visibility_individual,
inputs=[model_choice],
outputs=[
num_inference_steps,
guidance_scale,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale
]
)
gr.Examples(
examples=examples_individual,
inputs=[
prompt,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
model_choice,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
],
outputs=[result],
fn=generate_individual_image,
)
gr.on(
triggers=[
prompt.submit,
run_button.click,
],
fn=generate_individual_image,
inputs=[
prompt,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
model_choice,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
],
outputs=[result],
)
demo.queue().launch(share=False) |