Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,412 Bytes
44fec7d 09b91a9 e518b27 09b91a9 e518b27 44fec7d e518b27 44fec7d e518b27 09b91a9 e518b27 09b91a9 e518b27 09b91a9 e518b27 09b91a9 e518b27 9ae73d2 09b91a9 44fec7d 09b91a9 e518b27 3954b30 e518b27 2f0a1c2 e518b27 9ea88c8 e518b27 9ae73d2 e518b27 8e05eec e518b27 9ea88c8 9ae73d2 9ea88c8 e518b27 09b91a9 c490b57 09b91a9 e518b27 44fec7d e518b27 9ae73d2 e518b27 2f0a1c2 e518b27 9ae73d2 2f0a1c2 e518b27 09b91a9 2f0a1c2 09b91a9 e518b27 e63593c 09b91a9 e518b27 44fec7d 09b91a9 2f0a1c2 9ae73d2 2f0a1c2 7cfb22a 2f0a1c2 09b91a9 9ae73d2 2f0a1c2 9ae73d2 2f0a1c2 09b91a9 9ae73d2 e63593c 9ae73d2 2f0a1c2 9ae73d2 2f0a1c2 7cfb22a 9ae73d2 2f0a1c2 09b91a9 e518b27 09b91a9 939afee 09b91a9 2f0a1c2 9ae73d2 09b91a9 e518b27 09b91a9 |
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
import spaces # import first
import random
import numpy as np
import torch
from diffusers import StableDiffusionXLPipeline
import gradio as gr
from tkg import apply_tkg_noise, ColorSet, COLOR_SET_MAP
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
device = "cuda"
model_repo_id = "cagliostrolab/animagine-xl-4.0" # Replace to the model you would like to use
pipe = StableDiffusionXLPipeline.from_pretrained(
"cagliostrolab/animagine-xl-4.0",
torch_dtype=torch.bfloat16,
custom_pipeline="lpw_stable_diffusion_xl",
add_watermarker=False,
)
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
@spaces.GPU
def infer(
prompt: str,
negative_prompt: str,
seed: int,
randomize_seed: bool,
width: int,
height: int,
guidance_scale: float,
num_inference_steps: int,
tkg_channels: list[int] = [0, 1, 1, 0],
chroma_key_shift: float = 0.11,
progress=gr.Progress(track_tqdm=True),
):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
latents = torch.randn(
(
1,
4, # 4 channels
height // 8,
width // 8,
),
generator=generator,
device=device,
dtype=torch.bfloat16,
)
tkg_latents = apply_tkg_noise(
latents,
shift=chroma_key_shift,
delta_shift=0.1,
std_dev=0.5,
factor=8,
channels=tkg_channels,
).to(torch.bfloat16)
latents = torch.cat(
[
tkg_latents,
latents,
],
dim=0,
)
images = pipe(
latents=latents,
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
num_images_per_prompt=2,
generator=generator,
).images
w_tkg, wo_tkg = images
return w_tkg, wo_tkg, seed
def color_name_to_channels(color_name: str) -> list[int]:
if color_name in COLOR_SET_MAP:
return COLOR_SET_MAP[color_name].channels
else:
raise ValueError(f"Unknown color name: {color_name}")
def on_generate(
prompt: str,
negative_prompt: str,
seed: int,
randomize_seed: bool,
width: int,
height: int,
guidance_scale: float,
num_inference_steps: int,
color_name: str,
chroma_key_shift: float,
*args,
**kwargs
):
tkg_channels = color_name_to_channels(color_name)
# TODO: custom channels
w_tkg, wo_tkg, seed = infer(
prompt,
negative_prompt,
seed,
randomize_seed,
width,
height,
guidance_scale,
num_inference_steps,
tkg_channels=tkg_channels,
chroma_key_shift=chroma_key_shift,
*args,
**kwargs,
)
return w_tkg, wo_tkg, seed
examples = [
# "1girl, arima kana, oshi no ko, hoshimachi suisei, hoshimachi suisei \(1st costume\), cosplay, looking at viewer, smile, outdoors, night, v, masterpiece, high score, great score, absurdres",
"1girl, solo, school uniform, cat ears, full body, looking at viewer, straight-on, chibi, simple background, best quality",
"1girl, solo, hand up, waving, long hair, sideways glance, upper body, cropped torso, simple background, best quality",
]
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown(
"""
# TKG Chroma-Key with AnimagineXL 4.0
TKG-DMπ₯π: Training-free Chroma Key Content Generation Diffusion Model
- arXiv: https://arxiv.org/abs/2411.15580
- GitHub: https://github.com/ryugo417/TKG-DM
""")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
max_lines=4,
placeholder="Enter your prompt",
)
color_set = gr.Dropdown(
label="Background color",
choices=list(COLOR_SET_MAP.keys()),
value="green",
)
with gr.Accordion("TKG Settings", open=False):
chroma_key_shift = gr.Slider(
label="Latent mean shift for chroma key",
minimum=0.0,
maximum=0.2,
step=0.005,
value=0.11,
)
with gr.Accordion("Advanced Settings", open=False):
negative_prompt = gr.Textbox(
label="Negative prompt",
max_lines=4,
placeholder="Enter a negative prompt",
value="lowres, bad anatomy, bad hands, text, error, missing finger, extra digits, fewer digits, cropped, worst quality, low quality, low score, bad score, average score, signature, watermark, username, blurry",
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=832,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1152,
)
with gr.Row():
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.0,
maximum=10.0,
step=0.1,
value=5.0,
)
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=50,
step=1,
value=25,
)
with gr.Column():
run_button = gr.Button("Generate", variant="primary")
with gr.Row():
result_w_tkg = gr.Image(label="with TKG")
result_wo_tkg = gr.Image(label="without TKG")
gr.Examples(examples=examples, inputs=[prompt])
gr.on(
triggers=[run_button.click, prompt.submit],
fn=on_generate,
inputs=[
prompt,
negative_prompt,
seed,
randomize_seed,
width,
height,
guidance_scale,
num_inference_steps,
color_set,
chroma_key_shift,
],
outputs=[result_w_tkg, result_wo_tkg, seed],
)
if __name__ == "__main__":
demo.launch()
|