Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,87 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if torch.cuda.is_available():
|
| 13 |
-
torch_dtype = torch.float16
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
-
|
| 18 |
-
pipe =
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
| 23 |
|
| 24 |
-
|
| 25 |
-
def infer(
|
| 26 |
-
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
generator=generator,
|
| 49 |
-
).images
|
| 50 |
|
| 51 |
-
return
|
| 52 |
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
"An astronaut riding a green horse",
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
]
|
| 59 |
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Blocks(css=css) as demo:
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
|
@@ -77,18 +92,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
|
| 80 |
-
run_button = gr.Button("Run", scale=0
|
| 81 |
|
| 82 |
result = gr.Image(label="Result", show_label=False)
|
| 83 |
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
seed = gr.Slider(
|
| 93 |
label="Seed",
|
| 94 |
minimum=0,
|
|
@@ -97,58 +105,18 @@ with gr.Blocks(css=css) as demo:
|
|
| 97 |
value=0,
|
| 98 |
)
|
| 99 |
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
+
import functools
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
+
from diffusers import StableDiffusion3Pipeline
|
| 8 |
+
from inference import run
|
| 9 |
+
from peft import LoraConfig, get_peft_model, PeftModel
|
| 10 |
|
| 11 |
+
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium",
|
| 12 |
+
torch_dtype=torch.float16)
|
| 13 |
+
pipe = pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
distill_check = 'yresearch/swd-medium-6-steps'
|
| 16 |
+
pipe.transformer = PeftModel.from_pretrained(
|
| 17 |
+
pipe.transformer,
|
| 18 |
+
distill_check,
|
| 19 |
+
)
|
| 20 |
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
MAX_IMAGE_SIZE = 1024
|
| 23 |
|
| 24 |
|
| 25 |
+
@spaces.GPU()
|
| 26 |
+
def infer(prompt, seed, randomize_seed):
|
| 27 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if randomize_seed:
|
| 29 |
seed = random.randint(0, MAX_SEED)
|
| 30 |
|
| 31 |
generator = torch.Generator().manual_seed(seed)
|
| 32 |
+
sigmas = [1.0000, 0.9454, 0.8959, 0.7904, 0.7371, 0.6022]
|
| 33 |
+
scales = [32, 48, 64, 80, 96, 128]
|
| 34 |
+
|
| 35 |
+
images = run(
|
| 36 |
+
pipe,
|
| 37 |
+
prompt,
|
| 38 |
+
sigmas=sigmas,
|
| 39 |
+
scales=scales,
|
| 40 |
+
num_inference_steps=6,
|
| 41 |
+
|
| 42 |
+
guidance_scale=0.0,
|
| 43 |
+
height=int(scales[0] * 8),
|
| 44 |
+
width=int(scales[0] * 8),
|
| 45 |
generator=generator,
|
| 46 |
+
).images
|
| 47 |
|
| 48 |
+
return images
|
| 49 |
|
| 50 |
|
| 51 |
examples = [
|
|
|
|
| 52 |
"An astronaut riding a green horse",
|
| 53 |
+
'Long-exposure night photography of a starry sky over a mountain range, with light trails.',
|
| 54 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 55 |
+
"A portrait of a girl with blonde, tousled hair, blue eyes",
|
| 56 |
]
|
| 57 |
|
| 58 |
css = """
|
| 59 |
#col-container {
|
| 60 |
margin: 0 auto;
|
| 61 |
+
max-width: 520px;
|
| 62 |
}
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
if torch.cuda.is_available():
|
| 66 |
+
power_device = "GPU"
|
| 67 |
+
else:
|
| 68 |
+
power_device = "CPU"
|
| 69 |
+
|
| 70 |
with gr.Blocks(css=css) as demo:
|
| 71 |
with gr.Column(elem_id="col-container"):
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
f"""
|
| 74 |
+
# ⚡ Scale-wise Distillation ⚡
|
| 75 |
+
# ⚡ Image Generation with 6-step SwD ⚡
|
| 76 |
+
This is a demo of [Scale-wise Distillation](https://yandex-research.github.io/invertible-cd/),
|
| 77 |
+
a diffusion distillation method proposed in [Scale-wise Distillation of Diffusion Models](https://arxiv.org/abs/2406.14539)
|
| 78 |
+
by [Yandex Research](https://github.com/yandex-research).
|
| 79 |
+
Currently running on {power_device}.
|
| 80 |
+
"""
|
| 81 |
+
)
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"If you enjoy the space, feel free to give a ⭐ to the <a href='https://github.com/yandex-research/invertible-cd' target='_blank'>Github Repo</a>. [](https://github.com/yandex-research/invertible-cd)"
|
| 84 |
+
)
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
prompt = gr.Text(
|
|
|
|
| 92 |
container=False,
|
| 93 |
)
|
| 94 |
|
| 95 |
+
run_button = gr.Button("Run", scale=0)
|
| 96 |
|
| 97 |
result = gr.Image(label="Result", show_label=False)
|
| 98 |
|
| 99 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
seed = gr.Slider(
|
| 101 |
label="Seed",
|
| 102 |
minimum=0,
|
|
|
|
| 105 |
value=0,
|
| 106 |
)
|
| 107 |
|
| 108 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
gr.Examples(
|
| 112 |
+
examples=examples,
|
| 113 |
+
inputs=[prompt],
|
| 114 |
+
cache_examples=False
|
| 115 |
+
)
|
| 116 |
+
run_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
fn=infer,
|
| 118 |
+
inputs=[prompt, seed, randomize_seed],
|
| 119 |
+
outputs=[result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
+
demo.queue().launch(share=False)
|
|
|