Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
| 2 |
+
|
| 3 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 4 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 5 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 6 |
+
torch_dtype=torch.float16, variant="fp16", use_safetensors=True,
|
| 7 |
+
vae=vae,
|
| 8 |
+
add_watermarker=False,
|
| 9 |
+
).to("cuda")
|
| 10 |
+
|
| 11 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 12 |
+
|
| 13 |
+
def run(prompt="a photo of an astronaut riding a horse on mars", steps=10, seed=20, negative_prompt="", randomize_seed=False):
|
| 14 |
+
if randomize_seed:
|
| 15 |
+
seed = random.randint(0, MAX_SEED)
|
| 16 |
+
|
| 17 |
+
sampling_schedule = [999, 845, 730, 587, 443, 310, 193, 116, 53, 13, 0]
|
| 18 |
+
torch.manual_seed(seed)
|
| 19 |
+
ays_images = pipe(
|
| 20 |
+
prompt,
|
| 21 |
+
negative_prompt=negative_prompt,
|
| 22 |
+
num_images_per_prompt=1,
|
| 23 |
+
timesteps=sampling_schedule,
|
| 24 |
+
).images
|
| 25 |
+
return ays_images[0], seed
|
| 26 |
+
|
| 27 |
+
examples = [
|
| 28 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 29 |
+
"An astronaut riding a green horse",
|
| 30 |
+
"A delicious ceviche cheesecake slice",
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
css="""
|
| 34 |
+
#col-container {
|
| 35 |
+
margin: 0 auto;
|
| 36 |
+
max-width: 520px;
|
| 37 |
+
}
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
with gr.Blocks(css=css) as demo:
|
| 41 |
+
|
| 42 |
+
with gr.Column(elem_id="col-container"):
|
| 43 |
+
gr.Markdown(f"""
|
| 44 |
+
# Align-your-steps
|
| 45 |
+
Unnoficial demo for the official diffusers implementation of [Align your Steps](https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/) by NVIDIA
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
|
| 50 |
+
prompt = gr.Text(
|
| 51 |
+
label="Prompt",
|
| 52 |
+
show_label=False,
|
| 53 |
+
max_lines=1,
|
| 54 |
+
placeholder="Enter your prompt",
|
| 55 |
+
container=False,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
run_button = gr.Button("Run", scale=0)
|
| 59 |
+
|
| 60 |
+
result = gr.Image(label="Result", show_label=False)
|
| 61 |
+
|
| 62 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 63 |
+
|
| 64 |
+
negative_prompt = gr.Text(
|
| 65 |
+
label="Negative prompt",
|
| 66 |
+
max_lines=1,
|
| 67 |
+
placeholder="Enter a negative prompt",
|
| 68 |
+
visible=False,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
seed = gr.Slider(
|
| 72 |
+
label="Seed",
|
| 73 |
+
minimum=0,
|
| 74 |
+
maximum=MAX_SEED,
|
| 75 |
+
step=1,
|
| 76 |
+
value=0,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 80 |
+
|
| 81 |
+
num_inference_steps = gr.Slider(
|
| 82 |
+
label="Number of inference steps",
|
| 83 |
+
minimum=4,
|
| 84 |
+
maximum=12,
|
| 85 |
+
step=1,
|
| 86 |
+
value=8,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
run_button.click(
|
| 90 |
+
fn = infer,
|
| 91 |
+
inputs = [prompt, num_inference_steps, seed, negative_prompt, randomize_seed],
|
| 92 |
+
outputs = [result]
|
| 93 |
+
)
|