Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
| 5 |
+
import functools
|
| 6 |
+
import os
|
| 7 |
+
import torch
|
| 8 |
+
from diffusers import FluxPipeline
|
| 9 |
+
from peft import LoraConfig, get_peft_model, PeftModel
|
| 10 |
+
|
| 11 |
+
huggingface_token = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev",
|
| 14 |
+
torch_dtype=torch.float16,
|
| 15 |
+
token=huggingface_token,
|
| 16 |
+
custom_pipeline='quickjkee/swd_pipeline_flux').to('cuda')
|
| 17 |
+
distill_check = 'yresearch/swd_flux'
|
| 18 |
+
pipe.transformer = PeftModel.from_pretrained(
|
| 19 |
+
pipe.transformer,
|
| 20 |
+
distill_check,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
+
MAX_IMAGE_SIZE = 1024
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@spaces.GPU()
|
| 29 |
+
def infer(prompt, seed, randomize_seed):
|
| 30 |
+
|
| 31 |
+
if randomize_seed:
|
| 32 |
+
seed = random.randint(0, MAX_SEED)
|
| 33 |
+
|
| 34 |
+
generator = torch.Generator().manual_seed(seed)
|
| 35 |
+
sigmas = [1.0000, 0.8956, 0.7363, 0.6007, 0.0000]
|
| 36 |
+
scales = [64, 80, 96, 128]
|
| 37 |
+
|
| 38 |
+
image = pipe(
|
| 39 |
+
prompt=prompt,
|
| 40 |
+
height=int(scales[0] * 8),
|
| 41 |
+
width=int(scales[0] * 8),
|
| 42 |
+
scales=scales,
|
| 43 |
+
sigmas=sigmas,
|
| 44 |
+
timesteps=torch.tensor(sigmas[:-1]).to('cuda') * 1000,
|
| 45 |
+
guidance_scale=4.5,
|
| 46 |
+
max_sequence_length=512,
|
| 47 |
+
generator=torch.Generator("cpu").manual_seed(0)
|
| 48 |
+
).images[0]
|
| 49 |
+
|
| 50 |
+
return image
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
examples = [
|
| 54 |
+
"3d digital art of an adorable ghost, holding a heart shaped pumpkin, Halloween, super cute, spooky haunted house background",
|
| 55 |
+
'Long-exposure night photography of a starry sky over a mountain range, with light trails.',
|
| 56 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 57 |
+
"A gold astronaut meditating in a lush green forest by a lake",
|
| 58 |
+
"A group of friends sitting around a campfire."
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
css = """
|
| 62 |
+
#col-container {
|
| 63 |
+
margin: 0 auto;
|
| 64 |
+
max-width: 520px;
|
| 65 |
+
}
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
if torch.cuda.is_available():
|
| 69 |
+
power_device = "GPU"
|
| 70 |
+
else:
|
| 71 |
+
power_device = "CPU"
|
| 72 |
+
|
| 73 |
+
with gr.Blocks(css=css) as demo:
|
| 74 |
+
with gr.Column(elem_id="col-container"):
|
| 75 |
+
gr.Markdown(
|
| 76 |
+
f"""
|
| 77 |
+
# ⚡ Scale-wise Distillation ⚡
|
| 78 |
+
# ⚡ Image Generation with 6-step SwD ⚡
|
| 79 |
+
This is a demo of [Scale-wise Distillation](https://yandex-research.github.io/swd/),
|
| 80 |
+
a diffusion distillation method proposed in [Scale-wise Distillation of Diffusion Models](https://arxiv.org/abs/2503.16397)
|
| 81 |
+
by [Yandex Research](https://github.com/yandex-research).
|
| 82 |
+
Currently running on {power_device}.
|
| 83 |
+
"""
|
| 84 |
+
)
|
| 85 |
+
gr.Markdown(
|
| 86 |
+
"If you enjoy the space, feel free to give a ⭐ to the <a href='https://github.com/yandex-research/swd' target='_blank'>Github Repo</a>. [](https://github.com/yandex-research/invertible-cd)"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
prompt = gr.Text(
|
| 91 |
+
label="Prompt",
|
| 92 |
+
show_label=False,
|
| 93 |
+
max_lines=1,
|
| 94 |
+
placeholder="Enter your prompt",
|
| 95 |
+
container=False,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
run_button = gr.Button("Run", scale=0)
|
| 99 |
+
|
| 100 |
+
result = gr.Image(label="Result", show_label=False)
|
| 101 |
+
|
| 102 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 103 |
+
seed = gr.Slider(
|
| 104 |
+
label="Seed",
|
| 105 |
+
minimum=0,
|
| 106 |
+
maximum=MAX_SEED,
|
| 107 |
+
step=1,
|
| 108 |
+
value=0,
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
gr.Examples(
|
| 115 |
+
examples=examples,
|
| 116 |
+
inputs=[prompt],
|
| 117 |
+
cache_examples=False
|
| 118 |
+
)
|
| 119 |
+
run_button.click(
|
| 120 |
+
fn=infer,
|
| 121 |
+
inputs=[prompt, seed, randomize_seed],
|
| 122 |
+
outputs=[result]
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
demo.queue().launch(share=False)
|