multimodalart's picture
Update app.py
e240cf7 verified
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()