Spaces:
Sleeping
Sleeping
File size: 3,955 Bytes
58a725a fffa5fe 64dee90 dad5c89 e240cf7 ebf93e0 dad5c89 58a725a ee73d88 e4092f4 ee73d88 dad5c89 a29df6c dad5c89 4f17003 ee00232 2646440 6c716c3 29a07fc |
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 |
import random
import torch
import numpy as np
import gradio as gr
import spaces
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
from diffusers import DPMSolverMultistepScheduler as DefaultDPMSolver
# Add support for setting custom timesteps
class DPMSolverMultistepScheduler(DefaultDPMSolver):
def set_timesteps(
self, num_inference_steps=None, device=None,
timesteps=None
):
if timesteps is None:
super().set_timesteps(num_inference_steps, device)
return
all_sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
self.sigmas = torch.from_numpy(all_sigmas[timesteps])
self.timesteps = torch.tensor(timesteps[:-1]).to(device=device, dtype=torch.int64) # Ignore the last 0
self.num_inference_steps = len(timesteps)
self.model_outputs = [
None,
] * self.config.solver_order
self.lower_order_nums = 0
# add an index counter for schedulers that allow duplicated timesteps
self._step_index = None
self._begin_index = None
self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
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
@spaces.GPU
def run(prompt="a photo of an astronaut riding a horse on mars",
negative_prompt="",
randomize_seed=False,
seed=20,
progress=gr.Progress(track_tqdm=True)
):
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,
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 (AYS) - Stable Diffusion XL
Unnoficial demo for the official diffusers implementation of the [Align your Steps](https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/) scheduler by NVIDIA for SDXL
""")
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)
gr.on(
[run_button.click, prompt.submit, negative_prompt.submit],
fn = run,
inputs = [prompt, negative_prompt, randomize_seed, seed],
outputs = [result, seed]
)
demo.launch() |