Spaces:
Sleeping
Sleeping
from diffusers import StableDiffusionXLPipeline, AutoencoderKL, DPMSolverMultistepScheduler | |
import random | |
import torch | |
import numpy as np | |
import gradio as gr | |
import spaces | |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) | |
pipe = StableDiffusionXLPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-xl-base-1.0", | |
torch_dtype=torch.float16, variant="fp16", use_safetensors=True, | |
vae=vae, | |
).to("cuda") | |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) | |
MAX_SEED = np.iinfo(np.int32).max | |
def run(prompt="a photo of an astronaut riding a horse on mars", steps=10, seed=20, negative_prompt="", randomize_seed=False): | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
sampling_schedule = [999, 845, 730, 587, 443, 310, 193, 116, 53, 13, 0] | |
torch.manual_seed(seed) | |
ays_images = pipe( | |
prompt, | |
negative_prompt=negative_prompt, | |
num_images_per_prompt=1, | |
timesteps=sampling_schedule, | |
).images | |
return ays_images[0], seed | |
examples = [ | |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", | |
"An astronaut riding a green horse", | |
"A delicious ceviche cheesecake slice", | |
] | |
css=""" | |
#col-container { | |
margin: 0 auto; | |
max-width: 520px; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Column(elem_id="col-container"): | |
gr.Markdown(f""" | |
# Align-your-steps | |
Unnoficial demo for the official diffusers implementation of [Align your Steps](https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/) by NVIDIA | |
""") | |
with gr.Row(): | |
prompt = gr.Text( | |
label="Prompt", | |
show_label=False, | |
max_lines=1, | |
placeholder="Enter your prompt", | |
container=False, | |
) | |
run_button = gr.Button("Run", scale=0) | |
result = gr.Image(label="Result", show_label=False) | |
with gr.Accordion("Advanced Settings", open=False): | |
negative_prompt = gr.Text( | |
label="Negative prompt", | |
max_lines=1, | |
placeholder="Enter a negative prompt", | |
visible=False, | |
) | |
seed = gr.Slider( | |
label="Seed", | |
minimum=0, | |
maximum=MAX_SEED, | |
step=1, | |
value=0, | |
) | |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
num_inference_steps = gr.Slider( | |
label="Number of inference steps", | |
minimum=4, | |
maximum=12, | |
step=1, | |
value=8, | |
) | |
run_button.click( | |
fn = run, | |
inputs = [prompt, num_inference_steps, seed, negative_prompt, randomize_seed], | |
outputs = [result] | |
) | |
demo.launch() |