Spaces:
Sleeping
Sleeping
File size: 4,046 Bytes
ebf93e0 58a725a fffa5fe 64dee90 dad5c89 ebf93e0 dad5c89 58a725a dad5c89 ee00232 dad5c89 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 127 128 129 130 |
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
from diffusers import DPMSolverMultistepScheduler as DefaultDPMSolver
import random
import torch
import numpy as np
import gradio as gr
import spaces
# 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", 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() |