Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,637 Bytes
55866f4 ade193e 3a5de53 48daf06 55866f4 c02178b 5f5f2bb ade193e 55866f4 3a5de53 be12820 3a5de53 08f9860 3a5de53 55866f4 bde9560 29c7873 bde9560 227c367 bde9560 227c367 bde9560 54f7994 227c367 54f7994 55866f4 d6a75b9 be12820 d6a75b9 be12820 3a5de53 be12820 3a5de53 227c367 3a5de53 be12820 54f7994 be12820 3a5de53 d6a75b9 be12820 d6a75b9 be12820 227c367 be12820 227c367 be12820 227c367 be12820 227c367 d6a75b9 be12820 227c367 d6a75b9 227c367 be12820 d6a75b9 be12820 d6a75b9 be12820 d6a75b9 be12820 d6a75b9 be12820 bde9560 be12820 bde9560 be12820 bde9560 be12820 bde9560 be12820 dc35ac5 be12820 29c7873 be12820 54f7994 be12820 54f7994 be12820 bde9560 be12820 bde9560 be12820 bde9560 227c367 bde9560 29c7873 227c367 bde9560 227c367 bde9560 be12820 bde9560 be12820 bde9560 227c367 bde9560 d6a75b9 bde9560 55866f4 48daf06 d6a75b9 5f8123c |
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
import spaces
import gradio as gr
from PIL import Image
import math
import io
import base64
import subprocess
import os
from concept_attention import ConceptAttentionFluxPipeline
import os
from huggingface_hub import login
# Load token from environment
hf_token = os.getenv("AccessToken")
# Log in with the token (this sets it globally in the session)
login(token=hf_token)
IMG_SIZE = 210
COLUMNS = 5
def update_default_concepts(prompt):
default_concepts = {
"A dog by a tree": ["dog", "grass", "tree", "background"],
"A man on the beach": ["man", "dirt", "ocean", "sky"],
"A hot air balloon": ["balloon", "sky", "water", "tree"]
}
return gr.update(value=default_concepts.get(prompt, []))
pipeline = ConceptAttentionFluxPipeline(model_name="flux-schnell")# , offload_model=True) # , device="cuda:0") # , offload_model=True)
def convert_pil_to_bytes(img):
img = img.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST)
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return img_str
@spaces.GPU(duration=60)
def encode_image(image, prompt, concepts, seed, layer_start_index, noise_timestep, num_samples):
try:
if not prompt:
prompt = ""
prompt = prompt.strip()
if len(concepts) == 0:
raise gr.Error("Please enter at least 1 concept", duration=10)
if len(concepts) > 9:
raise gr.Error("Please enter at most 9 concepts", duration=10)
image = image.convert("RGB")
pipeline_output = pipeline.encode_image(
image=image,
prompt=prompt,
concepts=concepts,
width=1024,
height=1024,
seed=seed,
num_samples=num_samples,
noise_timestep=noise_timestep,
num_steps=4,
layer_indices=list(range(layer_start_index, 19)),
softmax=True if len(concepts) > 1 else False
)
output_image = pipeline_output.image
output_space_heatmaps = pipeline_output.concept_heatmaps
output_space_heatmaps = [heatmap.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST) for heatmap in output_space_heatmaps]
output_space_maps_and_labels = [(output_space_heatmaps[concept_index], concepts[concept_index]) for concept_index in range(len(concepts))]
cross_attention_heatmaps = pipeline_output.cross_attention_maps
cross_attention_heatmaps = [heatmap.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST) for heatmap in cross_attention_heatmaps]
cross_attention_maps_and_labels = []
prompt_tokens = prompt.split()
for concept_index in range(len(concepts)):
concept = concepts[concept_index]
if concept in prompt_tokens:
cross_attention_maps_and_labels.append(
(cross_attention_heatmaps[concept_index], concept)
)
else:
# Exclude this concept because it is only generated due to ConceptAttention's causal attention mechanism
empty_image = Image.new("RGB", (IMG_SIZE, IMG_SIZE), (39, 39, 42))
cross_attention_maps_and_labels.append(
(empty_image, concept)
)
return output_image, \
gr.update(value=output_space_maps_and_labels, columns=len(output_space_maps_and_labels)), \
gr.update(value=cross_attention_maps_and_labels, columns=len(cross_attention_maps_and_labels))
except gr.Error as e:
return None, gr.update(value=[], columns=1) # , gr.update(value=[], columns=1)
@spaces.GPU(duration=60)
def generate_image(prompt, concepts, seed, layer_start_index, timestep_start_index):
try:
if not prompt:
raise gr.Error("Please enter a prompt", duration=10)
if not prompt.strip():
raise gr.Error("Please enter a prompt", duration=10)
prompt = prompt.strip()
if len(concepts) == 0:
raise gr.Error("Please enter at least 1 concept", duration=10)
if len(concepts) > 9:
raise gr.Error("Please enter at most 9 concepts", duration=10)
pipeline_output = pipeline.generate_image(
prompt=prompt,
concepts=concepts,
width=1024,
height=1024,
seed=seed,
timesteps=list(range(timestep_start_index, 4)),
num_inference_steps=4,
layer_indices=list(range(layer_start_index, 19)),
softmax=True if len(concepts) > 1 else False
)
output_image = pipeline_output.image
output_space_heatmaps = pipeline_output.concept_heatmaps
output_space_heatmaps = [heatmap.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST) for heatmap in output_space_heatmaps]
output_space_maps_and_labels = [(output_space_heatmaps[concept_index], concepts[concept_index]) for concept_index in range(len(concepts))]
cross_attention_heatmaps = pipeline_output.cross_attention_maps
cross_attention_heatmaps = [heatmap.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST) for heatmap in cross_attention_heatmaps]
cross_attention_maps_and_labels = []
prompt_tokens = prompt.split()
for concept_index in range(len(concepts)):
concept = concepts[concept_index]
if concept in prompt_tokens:
cross_attention_maps_and_labels.append(
(cross_attention_heatmaps[concept_index], concept)
)
else:
# Exclude this concept because it is only generated due to ConceptAttention's causal attention mechanism
empty_image = Image.new("RGB", (IMG_SIZE, IMG_SIZE), (39, 39, 42))
cross_attention_maps_and_labels.append(
(empty_image, concept)
)
return output_image, \
gr.update(value=output_space_maps_and_labels, columns=len(output_space_maps_and_labels)), \
gr.update(value=cross_attention_maps_and_labels, columns=len(cross_attention_maps_and_labels))
except gr.Error as e:
return None, gr.update(value=[], columns=1), gr.update(value=[], columns=1)
with gr.Blocks(
css="""
.container {
max-width: 1300px;
margin: 0 auto;
padding: 20px;
}
.application {
max-width: 1200px;
}
.generated-image {
display: flex;
align-items: center;
justify-content: center;
height: 100%; /* Ensures full height */
}
.input {
height: 47px;
}
.input-column-label {}
.gallery {
height: 220px;
}
.run-button-column {
width: 100px !important;
}
.gallery-container {
scrollbar-width: thin;
scrollbar-color: grey black;
}
@media (min-width: 1280px) {
.svg-container {
min-width: 250px;
display: flex;
flex-direction: column;
padding-top: 340px;
}
.callout {
width: 250px;
}
.input-row {
height: 100px;
}
.input-column {
flex-direction: column;
gap: 0px;
height: 100%;
}
}
@media (max-width: 1280px) {
.svg-container {
display: none !important;
}
.callout {
display: none;
}
}
/*
@media (max-width: 1024px) {
.svg-container {
display: none !important;
display: flex;
flex-direction: column;
}
.callout {
display: none;
}
}
*/
.header {
display: flex;
flex-direction: column;
}
#title {
font-size: 4.4em;
color: #F3B13E;
text-align: center;
margin: 5px;
}
#subtitle {
font-size: 3.0em;
color: #FAE2BA;
text-align: center;
margin: 5px;
}
#abstract {
text-align: center;
font-size: 2.0em;
color:rgb(219, 219, 219);
margin: 5px;
margin-top: 10px;
}
#links {
text-align: center;
font-size: 2.0em;
margin: 5px;
}
#links a {
color: #93B7E9;
text-decoration: none;
}
.caption-label {
font-size: 1.15em;
}
.gallery label {
font-size: 1.15em;
}
"""
) as demo:
# with gr.Column(elem_classes="container"):
with gr.Row(elem_classes="container", scale=8):
with gr.Column(elem_classes="application-content", scale=10):
with gr.Row(scale=3, elem_classes="header"):
gr.HTML("""
<h1 id='title'> ConceptAttention </h1>
<h1 id='subtitle'> Visualize Any Concepts in Your Generated Images </h1>
<h1 id='abstract'> Interpret diffusion models with precise, high-quality heatmaps. </h1>
<h1 id='links'> <a href='https://arxiv.org/abs/2502.04320'> Paper </a> | <a href='https://github.com/helblazer811/ConceptAttention'> Code </a> </h1>
""")
with gr.Tab(label="Generate Image"):
with gr.Row(elem_classes="input-row", scale=2):
with gr.Column(scale=4, elem_classes="input-column", min_width=250):
gr.HTML(
"Write a Prompt",
elem_classes="input-column-label"
)
prompt = gr.Dropdown(
["A dog by a tree", "A man on the beach", "A hot air balloon"],
container=False,
allow_custom_value=True,
elem_classes="input"
)
with gr.Column(scale=7, elem_classes="input-column"):
gr.HTML(
"Select or Write Concepts",
elem_classes="input-column-label"
)
concepts = gr.Dropdown(
["dog", "grass", "tree", "dragon", "sky", "rock", "cloud", "balloon", "water", "background"],
value=["dog", "grass", "tree", "background"],
multiselect=True,
label="Concepts",
container=False,
allow_custom_value=True,
# scale=4,
elem_classes="input",
max_choices=5
)
with gr.Column(scale=1, min_width=100, elem_classes="input-column run-button-column"):
gr.HTML(
"​",
elem_classes="input-column-label"
)
submit_btn = gr.Button(
"Run",
elem_classes="input"
)
with gr.Row(elem_classes="gallery-container", scale=8):
with gr.Column(scale=1, min_width=250):
generated_image = gr.Image(
elem_classes="generated-image",
show_label=False,
)
with gr.Column(scale=4):
concept_attention_gallery = gr.Gallery(
label="Concept Attention (Ours)",
show_label=True,
# columns=3,
rows=1,
object_fit="contain",
height="200px",
elem_classes="gallery",
elem_id="concept-attention-gallery",
# scale=4
)
cross_attention_gallery = gr.Gallery(
label="Cross Attention",
show_label=True,
# columns=3,
rows=1,
object_fit="contain",
height="200px",
elem_classes="gallery",
# scale=4
)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(minimum=0, maximum=10000, step=1, label="Seed", value=42)
layer_start_index = gr.Slider(minimum=0, maximum=18, step=1, label="Layer Start Index", value=10)
timestep_start_index = gr.Slider(minimum=0, maximum=4, step=1, label="Timestep Start Index", value=2)
submit_btn.click(
fn=generate_image,
inputs=[prompt, concepts, seed, layer_start_index, timestep_start_index],
outputs=[generated_image, concept_attention_gallery, cross_attention_gallery]
)
prompt.change(update_default_concepts, inputs=[prompt], outputs=[concepts])
# Automatically process the first example on launch
demo.load(
generate_image,
inputs=[prompt, concepts, seed, layer_start_index, timestep_start_index],
outputs=[generated_image, concept_attention_gallery, cross_attention_gallery]
)
with gr.Tab(label="Explain a Real Image"):
with gr.Row(elem_classes="input-row", scale=2):
with gr.Column(scale=4, elem_classes="input-column", min_width=250):
gr.HTML(
"Write a Prompt (Optional)",
elem_classes="input-column-label"
)
# prompt = gr.Dropdown(
# ["A dog by a tree", "A man on the beach", "A hot air balloon"],
# container=False,
# allow_custom_value=True,
# elem_classes="input"
# )
prompt = gr.Textbox(
placeholder="Write a prompt (Optional)",
container=False,
elem_classes="input"
)
with gr.Column(scale=7, elem_classes="input-column"):
gr.HTML(
"Select or Write Concepts",
elem_classes="input-column-label"
)
concepts = gr.Dropdown(
["dog", "grass", "tree", "dragon", "sky", "rock", "cloud", "balloon", "water", "background"],
value=["dog", "grass", "tree", "background"],
multiselect=True,
label="Concepts",
container=False,
allow_custom_value=True,
# scale=4,
elem_classes="input",
max_choices=5
)
with gr.Column(scale=1, min_width=100, elem_classes="input-column run-button-column"):
gr.HTML(
"​",
elem_classes="input-column-label"
)
submit_btn = gr.Button(
"Run",
elem_classes="input"
)
with gr.Row(elem_classes="gallery-container", scale=8, equal_height=True):
with gr.Column(scale=1, min_width=250):
input_image = gr.Image(
elem_classes="generated-image",
show_label=False,
interactive=True,
type="pil",
image_mode="RGB",
scale=1
)
with gr.Column(scale=2):
concept_attention_gallery = gr.Gallery(
label="Concept Attention (Ours)",
show_label=True,
# columns=3,
rows=1,
object_fit="contain",
height="200px",
elem_classes="gallery",
elem_id="concept-attention-gallery",
# scale=4
)
cross_attention_gallery = gr.Gallery(
label="Cross Attention",
show_label=True,
# columns=3,
rows=1,
object_fit="contain",
height="200px",
elem_classes="gallery",
# scale=4
)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(minimum=0, maximum=10000, step=1, label="Seed", value=42)
num_samples = gr.Slider(minimum=1, maximum=10, step=1, label="Number of Samples", value=4)
layer_start_index = gr.Slider(minimum=0, maximum=18, step=1, label="Layer Start Index", value=10)
noise_timestep = gr.Slider(minimum=0, maximum=4, step=1, label="Noise Timestep", value=2)
submit_btn.click(
fn=encode_image,
inputs=[input_image, prompt, concepts, seed, layer_start_index, noise_timestep, num_samples],
outputs=[input_image, concept_attention_gallery, cross_attention_gallery]
)
# # Automatically process the first example on launch
# demo.load(
# encode_image,
# inputs=[input_image, prompt, concepts, seed, layer_start_index, noise_timestep, num_samples],
# outputs=[input_image, concept_attention_gallery, cross_attention_gallery]
# )
with gr.Column(scale=2, min_width=200, elem_classes="svg-column"):
with gr.Row(scale=8):
gr.HTML("<div></div>")
with gr.Row(scale=4, elem_classes="svg-container"):
concept_attention_callout_svg = gr.HTML(
"<img src='/gradio_api/file=ConceptAttentionCallout.svg' class='callout'/>",
# container=False,
)
cross_attention_callout_svg = gr.HTML(
"<img src='/gradio_api/file=CrossAttentionCallout.svg' class='callout'/>",
# container=False,
)
with gr.Row(scale=4):
gr.HTML("<div></div>")
if __name__ == "__main__":
if os.path.exists("/data-nvme/zerogpu-offload"):
subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
demo.launch(
allowed_paths=["."]
)
# share=True,
# server_name="0.0.0.0",
# inbrowser=True,
# # share=False,
# server_port=6754,
# quiet=True,
# max_threads=1
# )
|