Spaces:
Running
Running
File size: 6,301 Bytes
cecb702 9974205 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 3c9cf58 cecb702 9974205 e3f1212 3c9cf58 e3f1212 cecb702 3c9cf58 cecb702 e3f1212 cecb702 3c9cf58 cecb702 e3f1212 20a5d7e cecb702 e3f1212 cecb702 e3f1212 3c9cf58 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 e3f1212 cecb702 73410a4 3c9cf58 cecb702 73410a4 cecb702 e3f1212 cecb702 e3f1212 |
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#!/usr/bin/env python
import os
import random
import gradio as gr
import numpy as np
import PIL.Image
import spaces
import torch
from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
DESCRIPTION = "# Kandinsky 2.2"
if not torch.cuda.is_available():
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
MAX_SEED = np.iinfo(np.int32).max
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
"kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
)
pipe_prior.to(device)
pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
pipe.to(device)
if USE_TORCH_COMPILE:
pipe.unet.to(memory_format=torch.channels_last)
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
else:
pipe_prior = None
pipe = None
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED) # noqa: S311
return seed
@spaces.GPU
def generate(
prompt: str,
negative_prompt: str = "low quality, bad quality",
seed: int = 0,
width: int = 768,
height: int = 768,
guidance_scale_prior: float = 1.0,
guidance_scale: float = 4.0,
num_inference_steps_prior: int = 50,
num_inference_steps: int = 100,
progress: gr.Progress = gr.Progress(track_tqdm=True), # noqa: ARG001, B008
) -> PIL.Image.Image:
generator = torch.Generator().manual_seed(seed)
image_embeds, negative_image_embeds = pipe_prior(
prompt,
negative_prompt,
generator=generator,
guidance_scale=guidance_scale_prior,
num_inference_steps=num_inference_steps_prior,
).to_tuple()
return pipe(
image_embeds=image_embeds,
negative_image_embeds=negative_image_embeds,
height=height,
width=width,
generator=generator,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
).images[0]
examples = [
"An astronaut riding a horse",
"portrait of a young woman, blue eyes, cinematic",
"A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting",
"bird eye view shot of a full body woman with cyan light orange magenta makeup, digital art, long braided hair her face separated by makeup in the style of yin Yang surrealism, symmetrical face, real image, contrasting tone, pastel gradient background",
"A car exploding into colorful dust",
"editorial photography of an organic, almost liquid smoke style armchair",
"birds eye view of a quilted paper style alien planet landscape, vibrant colours, Cinematic lighting",
"Toy smiling cute octopus in a black hat, sticker",
"Red sport car, sticker",
]
with gr.Blocks(css_paths="style.css") as demo:
gr.Markdown(DESCRIPTION)
gr.DuplicateButton(
value="Duplicate Space for private use",
elem_id="duplicate-button",
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
)
with gr.Group():
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
submit_btn=True,
)
result = gr.Image(label="Result", show_label=False)
with gr.Accordion("Advanced options", open=False):
negative_prompt = gr.Text(
label="Negative prompt",
value="low quality, bad quality",
max_lines=1,
placeholder="Enter a negative prompt",
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=768,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=768,
)
guidance_scale_prior = gr.Slider(
label="Guidance scale for prior",
minimum=1,
maximum=20,
step=0.1,
value=1.0,
)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=1,
maximum=20,
step=0.1,
value=4.0,
)
num_inference_steps_prior = gr.Slider(
label="Number of inference steps for prior",
minimum=10,
maximum=100,
step=1,
value=50,
)
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=10,
maximum=150,
step=1,
value=100,
)
gr.Examples(
examples=examples,
inputs=prompt,
outputs=result,
fn=generate,
cache_examples=CACHE_EXAMPLES,
)
gr.on(
triggers=[prompt.submit, negative_prompt.submit],
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=generate,
inputs=[
prompt,
negative_prompt,
seed,
width,
height,
guidance_scale_prior,
guidance_scale,
num_inference_steps_prior,
num_inference_steps,
],
outputs=result,
api_name="run",
)
if __name__ == "__main__":
demo.queue(max_size=20).launch()
|