import spaces
import torch
from diffusers import StableDiffusion3Pipeline, StableDiffusionPipeline, StableDiffusionXLPipeline, DPMSolverSinglestepScheduler, StableCascadePriorPipeline, StableCascadeDecoderPipeline
import gradio as gr
import os
import random
import numpy
from PIL import Image
import gc # free up memory
HF_TOKEN = os.getenv("HF_TOKEN") # login with hf read token to access sd gated models
if torch.cuda.is_available():
device = "cuda"
print("Using GPU")
else:
device = "cpu"
print("Using CPU")
MAX_SEED = numpy.iinfo(numpy.int32).max
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
# Global dictionary to store pipelines
PIPELINES = {}
def load_pipeline(model_choice):
"""Loads the specified pipeline and stores it in the PIPELINES dictionary."""
if model_choice not in PIPELINES:
if model_choice == "sd3 medium":
PIPELINES[model_choice] = StableDiffusion3Pipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16
)
elif model_choice == "sd2.1":
PIPELINES[model_choice] = StableDiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
)
elif model_choice == "sdxl":
PIPELINES[model_choice] = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
)
elif model_choice == "sdxl flash":
PIPELINES[model_choice] = StableDiffusionXLPipeline.from_pretrained(
"sd-community/sdxl-flash", torch_dtype=torch.float16
)
# Store the original scheduler for resetting
PIPELINES[model_choice].original_scheduler = PIPELINES[model_choice].scheduler
elif model_choice == "stable cascade":
# Store both prior and decoder pipelines under 'stable cascade'
PIPELINES[model_choice] = {
'prior': StableCascadePriorPipeline.from_pretrained(
"stabilityai/stable-cascade-prior", variant="bf16", torch_dtype=torch.bfloat16
),
'decoder': StableCascadeDecoderPipeline.from_pretrained(
"stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.float16
)
}
elif model_choice == "sd1.5":
PIPELINES[model_choice] = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
)
else:
raise ValueError(f"Invalid model choice: {model_choice}")
return PIPELINES[model_choice]
def unload_pipeline(model_choice):
"""Unloads the specified pipeline from the PIPELINES dictionary and frees GPU memory."""
if model_choice in PIPELINES:
del PIPELINES[model_choice]
torch.cuda.empty_cache()
gc.collect()
@spaces.GPU(duration=80)
def run_inference(
prompt,
pipe,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps=None,
prior_guidance_scale=None,
decoder_num_inference_steps=None,
decoder_guidance_scale=None,
):
"""Runs inference with the specified pipeline and parameters."""
# Enable CPU offloading only if a GPU is available, for saving up RAM
if torch.cuda.is_available():
if isinstance(pipe, dict): # Special handling for stable cascade
pipe['prior'].enable_model_cpu_offload()
pipe['decoder'].enable_model_cpu_offload()
else:
pipe.enable_model_cpu_offload()
# Reset the sampler if the model is NOT SDXL Flash
if model_choice != "sdxl flash" and "sdxl flash" in PIPELINES:
PIPELINES["sdxl flash"].scheduler = PIPELINES["sdxl flash"].original_scheduler
# Apply SDXL Flash sampler ONLY if model_choice is 'sdxl flash'
if model_choice == "sdxl flash":
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(
pipe.scheduler.config, timestep_spacing="trailing"
)
if seed == 0:
seed = random.randint(1, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
if isinstance(pipe, dict): # Stable Cascade
with torch.inference_mode():
prior_output = pipe['prior'](
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,
)
with torch.inference_mode():
output = pipe['decoder'](
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: # Other pipelines
with torch.inference_mode():
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
# Helper function to generate images for a single model
def generate_single_image(
prompt,
model_choice,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps=None,
prior_guidance_scale=None,
decoder_num_inference_steps=None,
decoder_guidance_scale=None,
):
# Load the pipeline
pipe = load_pipeline(model_choice)
# Run inference
output = run_inference(
prompt,
pipe,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
)
# Unload the pipeline
unload_pipeline(model_choice)
return output
# Define the image generation function for the Arena tab
def generate_arena_images(
prompt,
negative_prompt,
num_models_to_compare,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
num_inference_steps_c,
guidance_scale_c,
num_inference_steps_d,
guidance_scale_d,
height_a,
width_a,
height_b,
width_b,
height_c,
width_c,
height_d,
width_d,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
model_choice_c,
model_choice_d,
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,
prior_num_inference_steps_c,
prior_guidance_scale_c,
decoder_num_inference_steps_c,
decoder_guidance_scale_c,
prior_num_inference_steps_d,
prior_guidance_scale_d,
decoder_num_inference_steps_d,
decoder_guidance_scale_d,
progress=gr.Progress(track_tqdm=True),
):
# Generate images for selected models
if num_models_to_compare >= 2:
images_a = generate_single_image(
prompt,
model_choice_a,
negative_prompt,
num_inference_steps_a,
guidance_scale_a,
height_a,
width_a,
seed,
num_images_per_prompt,
prior_num_inference_steps_a,
prior_guidance_scale_a,
decoder_num_inference_steps_a,
decoder_guidance_scale_a,
)
images_b = generate_single_image(
prompt,
model_choice_b,
negative_prompt,
num_inference_steps_b,
guidance_scale_b,
height_b,
width_b,
seed,
num_images_per_prompt,
prior_num_inference_steps_b,
prior_guidance_scale_b,
decoder_num_inference_steps_b,
decoder_guidance_scale_b,
)
output_arena_images = images_a, images_b, None, None
if num_models_to_compare >= 3:
images_c = generate_single_image(
prompt,
model_choice_c,
negative_prompt,
num_inference_steps_c,
guidance_scale_c,
height_c,
width_c,
seed,
num_images_per_prompt,
prior_num_inference_steps_c,
prior_guidance_scale_c,
decoder_num_inference_steps_c,
decoder_guidance_scale_c,
)
output_arena_images = images_a, images_b, images_c, None
if num_models_to_compare >= 4:
images_d = generate_single_image(
prompt,
model_choice_d,
negative_prompt,
num_inference_steps_d,
guidance_scale_d,
height_d,
width_d,
seed,
num_images_per_prompt,
prior_num_inference_steps_d,
prior_guidance_scale_d,
decoder_num_inference_steps_d,
decoder_guidance_scale_d,
)
output_arena_images = images_a, images_b, images_c, images_d
return output_arena_images
# Define the image generation function for the Individual tab
def generate_individual_image(
prompt,
model_choice,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
progress=gr.Progress(track_tqdm=True),
):
output = generate_single_image(
prompt,
model_choice,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
)
return output
# 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",
2, # num_models_to_compare
25,
7.5,
25,
7.5,
25,
7.5,
25,
7.5,
1024,
1024,
1024,
1024,
1024,
1024,
1024,
1024,
42,
2,
"sd3 medium",
"sdxl",
"sdxl flash",
"stable cascade",
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
25, # prior_num_inference_steps_c
4.0, # prior_guidance_scale_c
12, # decoder_num_inference_steps_c
0.0, # decoder_guidance_scale_c
25, # prior_num_inference_steps_d
4.0, # prior_guidance_scale_d
12, # decoder_num_inference_steps_d
0.0, # decoder_guidance_scale_d
],
[
"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",
2, # num_models_to_compare
25,
7.5,
25,
7.5,
25,
7.5,
25,
7.5,
1024,
1024,
1024,
1024,
1024,
1024,
1024,
1024,
42,
2,
"sd3 medium",
"sdxl",
"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
25, # prior_num_inference_steps_c
4.0, # prior_guidance_scale_c
12, # decoder_num_inference_steps_c
0.0, # decoder_guidance_scale_c
25, # prior_num_inference_steps_d
4.0, # prior_guidance_scale_d
12, # decoder_num_inference_steps_d
0.0, # decoder_guidance_scale_d
],
]
examples_individual = [
[
"A woman in a red dress singing on top of a building.",
"sdxl",
"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,
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.",
"sdxl",
"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,
25, # prior_num_inference_steps
4.0, # prior_guidance_scale
12, # decoder_num_inference_steps
0.0, # decoder_guidance_scale
],
]
theme = gr.themes.Soft(
primary_hue="emerald",
secondary_hue="blue",
).set(
border_color_primary='*neutral_300',
block_border_width='1px',
block_border_width_dark='1px',
block_title_border_color='*secondary_100',
block_title_border_color_dark='*secondary_200',
input_background_fill_focus='*secondary_300',
input_border_color='*border_color_primary',
input_border_color_focus='*secondary_500',
input_border_width='1px',
input_border_width_dark='1px',
slider_color='*secondary_500',
slider_color_dark='*secondary_600'
)
css = """
.gradio-container{max-width: 1400px !important}
h1{text-align:center}
.extra-option {
display: none;
}
.extra-option.visible {
display: block;
}
"""
with gr.Blocks(theme=theme, css=css) as demo:
with gr.Row():
with gr.Column():
gr.HTML(
"""
Stable Diffusion Arena
"""
)
gr.HTML(
"""
Made by Nick088
"""
)
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...",
)
num_models_to_compare = gr.Dropdown(
label="How many models to compare",
choices=[2, 3, 4],
value=2,
)
model_choice_a = gr.Dropdown(
label="Stable Diffusion Model A",
choices=[
"sd3 medium",
"sd2.1",
"sdxl",
"sdxl flash",
"stable cascade",
"sd1.5",
],
value="sd3 medium",
)
model_choice_b = gr.Dropdown(
label="Stable Diffusion Model B",
choices=[
"sd3 medium",
"sd2.1",
"sdxl",
"sdxl flash",
"stable cascade",
"sd1.5",
],
value="sdxl",
)
model_choice_c = gr.Dropdown(
label="Stable Diffusion Model C",
choices=[
"sd3 medium",
"sd2.1",
"sdxl",
"sdxl flash",
"stable cascade",
"sd1.5",
],
value=None,
visible=False,
)
model_choice_d = gr.Dropdown(
label="Stable Diffusion Model D",
choices=[
"sd3 medium",
"sd2.1",
"sdxl",
"sdxl flash",
"stable cascade",
"sd1.5",
],
value=None,
visible=False,
)
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"
)
result_3 = gr.Gallery(
label="Generated Images (Model C)",
elem_id="gallery_3",
visible=False,
)
result_4 = gr.Gallery(
label="Generated Images (Model D)",
elem_id="gallery_4",
visible=False,
)
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,
)
width_a = gr.Slider(
label="Width (Model A)",
info="Width of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
height_a = gr.Slider(
label="Height (Model A)",
info="Height of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
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=32,
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,
)
width_b = gr.Slider(
label="Width (Model B)",
info="Width of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
height_b = gr.Slider(
label="Height (Model B)",
info="Height of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
with gr.Column(visible=False) as model_c_options:
num_inference_steps_c = gr.Slider(
label="Inference Steps (Model C)",
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_c = gr.Slider(
label="Guidance Scale (Model C)",
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_c = gr.Slider(
label="Prior Inference Steps (Model C)",
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_c = gr.Slider(
label="Prior Guidance Scale (Model C)",
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_c = gr.Slider(
label="Decoder Inference Steps (Model C)",
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_c = gr.Slider(
label="Decoder Guidance Scale (Model C)",
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,
)
width_c = gr.Slider(
label="Width (Model C)",
info="Width of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
height_c = gr.Slider(
label="Height (Model C)",
info="Height of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
with gr.Column(visible=False) as model_d_options:
num_inference_steps_d = gr.Slider(
label="Inference Steps (Model D)",
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_d = gr.Slider(
label="Guidance Scale (Model D)",
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_d = gr.Slider(
label="Prior Inference Steps (Model D)",
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_d = gr.Slider(
label="Prior Guidance Scale (Model D)",
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_d = gr.Slider(
label="Decoder Inference Steps (Model D)",
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_d = gr.Slider(
label="Decoder Guidance Scale (Model D)",
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,
)
width_d = gr.Slider(
label="Width (Model D)",
info="Width of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
height_d = gr.Slider(
label="Height (Model D)",
info="Height of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
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),
width_a: gr.update(step=512, value=1024, maximum=1536),
height_a: gr.update(step=512, value=1024, maximum=1536),
}
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),
width_a: gr.update(step=32, value=1024, maximum=1536),
height_a: gr.update(step=32, value=1024, maximum=1536),
}
elif model_choice_a == "sd1.5":
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_guidance_scale_a: gr.update(visible=True),
decoder_num_inference_steps_a: gr.update(visible=True),
decoder_guidance_scale_a: gr.update(visible=True),
width_a: gr.update(step=32, value=512, maximum=768),
height_a: gr.update(step=32, value=512, maximum=768),
}
elif model_choice_a == "sd2.1":
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),
width_a: gr.update(step=32, value=768, maximum=1024),
height_a: gr.update(step=32, value=768, maximum=1024),
}
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),
width_a: gr.update(step=32, value=1024, maximum=1536),
height_a: gr.update(step=32, value=1024, maximum=1536),
}
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),
width_b: gr.update(step=256, value=1024, maximum=1536),
height_b: gr.update(step=256, value=1024, maximum=1536),
}
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),
width_a: gr.update(step=32, value=1024, maximum=1536),
height_a: gr.update(step=32, value=1024, maximum=1536),
}
elif model_choice_b == "sd1.5":
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),
width_b: gr.update(step=32, value=512, maximum=768),
height_b: gr.update(step=32, value=512, maximum=768),
}
elif model_choice_b == "sd2.1":
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),
width_b: gr.update(step=32, value=768, maximum=1024),
height_b: gr.update(step=32, value=768, maximum=1024),
}
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),
width_b: gr.update(step=32, value=1024, maximum=1536),
height_b: gr.update(step=32, value=1024, maximum=1536),
}
def toggle_visibility_arena_c(model_choice_c):
if model_choice_c == "stable cascade":
return {
num_inference_steps_c: gr.update(visible=False),
guidance_scale_c: gr.update(visible=False),
prior_num_inference_steps_c: gr.update(visible=True),
prior_guidance_scale_c: gr.update(visible=True),
decoder_num_inference_steps_c: gr.update(visible=True),
decoder_guidance_scale_c: gr.update(visible=True),
width_c: gr.update(step=256, value=1024, maximum=1536),
height_c: gr.update(step=256, value=1024, maximum=1536),
}
elif model_choice_c == "sdxl flash":
return {
num_inference_steps_c: gr.update(visible=True, maximum=15, value=8),
guidance_scale_c: gr.update(visible=True, maximum=6.0, value=3.5),
prior_num_inference_steps_c: gr.update(visible=False),
prior_guidance_scale_c: gr.update(visible=False),
decoder_num_inference_steps_c: gr.update(visible=False),
decoder_guidance_scale_c: gr.update(visible=False),
width_c: gr.update(step=32, value=1024, maximum=1536),
height_c: gr.update(step=32, value=1024, maximum=1536),
}
elif model_choice_c == "sd1.5":
return {
num_inference_steps_c: gr.update(visible=True, maximum=50, value=25),
guidance_scale_c: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_c: gr.update(visible=False),
prior_guidance_scale_c: gr.update(visible=False),
decoder_num_inference_steps_c: gr.update(visible=False),
decoder_guidance_scale_c: gr.update(visible=False),
width_c: gr.update(step=32, value=512, maximum=768),
height_c: gr.update(step=32, value=512, maximum=768),
}
elif model_choice_c == "sd2.1":
return {
num_inference_steps_c: gr.update(visible=True, maximum=50, value=25),
guidance_scale_c: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_c: gr.update(visible=False),
prior_guidance_scale_c: gr.update(visible=False),
decoder_num_inference_steps_c: gr.update(visible=False),
decoder_guidance_scale_c: gr.update(visible=False),
width_c: gr.update(step=32, value=768, maximum=1024),
height_c: gr.update(step=32, value=768, maximum=1024),
}
else:
return {
num_inference_steps_c: gr.update(visible=True, maximum=50, value=25),
guidance_scale_c: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_c: gr.update(visible=False),
prior_guidance_scale_c: gr.update(visible=False),
decoder_num_inference_steps_c: gr.update(visible=False),
decoder_guidance_scale_c: gr.update(visible=False),
width_c: gr.update(step=32, value=1024, maximum=1536),
height_c: gr.update(step=32, value=1024, maximum=1536),
}
def toggle_visibility_arena_d(model_choice_d):
if model_choice_d == "stable cascade":
return {
num_inference_steps_d: gr.update(visible=False),
guidance_scale_d: gr.update(visible=False),
prior_num_inference_steps_d: gr.update(visible=True),
prior_guidance_scale_d: gr.update(visible=True),
decoder_num_inference_steps_d: gr.update(visible=True),
decoder_guidance_scale_d: gr.update(visible=True),
width_d: gr.update(step=256, value=1024, maximum=1536),
height_d: gr.update(step=256, value=1024, maximum=1536),
}
elif model_choice_d == "sdxl flash":
return {
num_inference_steps_d: gr.update(visible=True, maximum=15, value=8),
guidance_scale_d: gr.update(visible=True, maximum=6.0, value=3.5),
prior_num_inference_steps_d: gr.update(visible=False),
prior_guidance_scale_d: gr.update(visible=False),
decoder_num_inference_steps_d: gr.update(visible=False),
decoder_guidance_scale_d: gr.update(visible=False),
width_d: gr.update(step=32, value=1024, maximum=1536),
height_d: gr.update(step=32, value=1024, maximum=1536),
}
elif model_choice_d == "sd1.5":
return {
num_inference_steps_d: gr.update(visible=True, maximum=50, value=25),
guidance_scale_d: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_d: gr.update(visible=False),
prior_guidance_scale_d: gr.update(visible=False),
decoder_num_inference_steps_d: gr.update(visible=False),
decoder_guidance_scale_d: gr.update(visible=False),
width_d: gr.update(step=32, value=512, maximum=768),
height_d: gr.update(step=32, value=512, maximum=768),
}
elif model_choice_d == "sd2.1":
return {
num_inference_steps_d: gr.update(visible=True, maximum=50, value=25),
guidance_scale_d: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_d: gr.update(visible=False),
prior_guidance_scale_d: gr.update(visible=False),
decoder_num_inference_steps_d: gr.update(visible=False),
decoder_guidance_scale_d: gr.update(visible=False),
width_d: gr.update(step=32, value=768, maximum=1024),
height_d: gr.update(step=32, value=768, maximum=1024),
}
else:
return {
num_inference_steps_d: gr.update(visible=True, maximum=50, value=25),
guidance_scale_d: gr.update(visible=True, maximum=10.0, value=7.5),
prior_num_inference_steps_d: gr.update(visible=False),
prior_guidance_scale_d: gr.update(visible=False),
decoder_num_inference_steps_d: gr.update(visible=False),
decoder_guidance_scale_d: gr.update(visible=False),
width_d: gr.update(step=32, value=1024, maximum=1536),
height_d: gr.update(step=32, value=1024, maximum=1536),
}
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,
width_a,
height_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,
width_b,
height_b,
],
)
model_choice_c.change(
toggle_visibility_arena_c,
inputs=[model_choice_c],
outputs=[
num_inference_steps_c,
guidance_scale_c,
prior_num_inference_steps_c,
prior_guidance_scale_c,
decoder_num_inference_steps_c,
decoder_guidance_scale_c,
width_c,
height_c,
],
)
model_choice_d.change(
toggle_visibility_arena_d,
inputs=[model_choice_d],
outputs=[
num_inference_steps_d,
guidance_scale_d,
prior_num_inference_steps_d,
prior_guidance_scale_d,
decoder_num_inference_steps_d,
decoder_guidance_scale_d,
width_d,
height_d,
],
)
def toggle_model_options(num_models):
if num_models == 2:
return {
model_choice_c: gr.update(visible=False),
model_d_options: gr.update(visible=False),
model_choice_d: gr.update(visible=False),
result_3: gr.update(visible=False),
result_4: gr.update(visible=False),
model_c_options: gr.update(visible=False),
}
elif num_models == 3:
return {
model_choice_c: gr.update(visible=True),
model_d_options: gr.update(visible=False),
model_choice_d: gr.update(visible=False),
result_3: gr.update(visible=True),
result_4: gr.update(visible=False),
model_c_options: gr.update(visible=True),
}
elif num_models == 4:
return {
model_choice_c: gr.update(visible=True),
model_d_options: gr.update(visible=True),
model_choice_d: gr.update(visible=True),
result_3: gr.update(visible=True),
result_4: gr.update(visible=True),
model_c_options: gr.update(visible=True),
}
num_models_to_compare.change(
toggle_model_options,
inputs=[num_models_to_compare],
outputs=[
model_choice_c,
model_d_options,
model_choice_d,
result_3,
result_4,
model_c_options,
],
)
gr.Examples(
examples=examples_arena,
fn=generate_arena_images,
inputs=[
prompt,
negative_prompt,
num_models_to_compare,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
num_inference_steps_c,
guidance_scale_c,
num_inference_steps_d,
guidance_scale_d,
height_a,
width_a,
height_b,
width_b,
height_c,
width_c,
height_d,
width_d,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
model_choice_c,
model_choice_d,
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,
prior_num_inference_steps_c,
prior_guidance_scale_c,
decoder_num_inference_steps_c,
decoder_guidance_scale_c,
prior_num_inference_steps_d,
prior_guidance_scale_d,
decoder_num_inference_steps_d,
decoder_guidance_scale_d,
],
outputs=[result_1, result_2, result_3, result_4],
cache_examples=CACHE_EXAMPLES
)
gr.on(
triggers=[
prompt.submit,
run_button.click,
],
fn=generate_arena_images,
inputs=[
prompt,
negative_prompt,
num_models_to_compare,
num_inference_steps_a,
guidance_scale_a,
num_inference_steps_b,
guidance_scale_b,
num_inference_steps_c,
guidance_scale_c,
num_inference_steps_d,
guidance_scale_d,
height_a,
width_a,
height_b,
width_b,
height_c,
width_c,
height_d,
width_d,
seed,
num_images_per_prompt,
model_choice_a,
model_choice_b,
model_choice_c,
model_choice_d,
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,
prior_num_inference_steps_c,
prior_guidance_scale_c,
decoder_num_inference_steps_c,
decoder_guidance_scale_c,
prior_num_inference_steps_d,
prior_guidance_scale_d,
decoder_num_inference_steps_d,
decoder_guidance_scale_d,
],
outputs=[result_1, result_2, result_3, result_4],
)
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",
"sd1.5",
],
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=512,
maximum=1280,
value=1024,
step=32,
)
height = gr.Slider(
label="Height",
info="Height of the Image",
minimum=512,
maximum=1280,
value=1024,
step=32,
)
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),
width: gr.update(step=256, value=1024, maximum=1536),
height: gr.update(step=256, value=1024, maximum=1536),
}
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),
width: gr.update(step=32, value=1024, maximum=1536),
height: gr.update(step=32, value=1024, maximum=1536),
}
elif model_choice == "sd1.5":
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),
width: gr.update(step=32, value=512, maximum=768),
height: gr.update(step=32, value=512, maximum=768),
}
elif model_choice == "sd2.1":
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),
width: gr.update(step=32, value=768, maximum=1024),
height: gr.update(step=32, value=768, maximum=1024),
}
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),
width: gr.update(step=32, value=1024, maximum=1536),
height: gr.update(step=32, value=1024, maximum=1536),
}
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,
width,
height,
],
)
gr.Examples(
examples=examples_individual,
fn=generate_individual_image,
inputs=[
prompt,
model_choice,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
],
outputs=[result],
cache_examples=CACHE_EXAMPLES
)
gr.on(
triggers=[
prompt.submit,
run_button.click,
],
fn=generate_individual_image,
inputs=[
prompt,
model_choice,
negative_prompt,
num_inference_steps,
guidance_scale,
height,
width,
seed,
num_images_per_prompt,
prior_num_inference_steps,
prior_guidance_scale,
decoder_num_inference_steps,
decoder_guidance_scale,
],
outputs=[result],
)
demo.queue().launch(share=False)