Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,922 Bytes
55866f4 ade193e 55866f4 ade193e 55866f4 5f0abcc 55866f4 4f83196 5f0abcc 55866f4 4f83196 4b30dce 4f83196 55866f4 4f83196 55866f4 4f83196 55866f4 4f83196 55866f4 4f83196 55866f4 ade193e 55866f4 ade193e 4f83196 ade193e 55866f4 e6d4da8 55866f4 e6d4da8 55866f4 a8c9996 4f83196 0f8eff7 9a2fb25 a8c9996 4f83196 0f8eff7 9a2fb25 a8c9996 4f83196 ade193e 4f83196 55866f4 4f83196 55866f4 c4a91ca 4f83196 1708f47 4f83196 55866f4 5f0abcc 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 |
import spaces
import gradio as gr
from PIL import Image
import math
from concept_attention import ConceptAttentionFluxPipeline
IMG_SIZE = 250
COLUMNS = 5
EXAMPLES = [
[
"A dog by a tree", # prompt
"tree, dog, grass, background", # words
42, # seed
],
[
"A dragon", # prompt
"dragon, sky, rock, cloud", # words
42, # seed
],
[
"A hot air balloon", # prompt
"balloon, sky, water, tree", # words
42, # seed
]
]
pipeline = ConceptAttentionFluxPipeline(model_name="flux-schnell", device="cuda")
@spaces.GPU(duration=60)
def process_inputs(prompt, word_list, seed, layer_start_index, timestep_start_index):
print("Processing inputs")
assert layer_start_index is not None
assert timestep_start_index is not None
prompt = prompt.strip()
if not word_list.strip():
gr.exceptions.InputError("words", "Please enter comma-separated words")
concepts = [w.strip() for w in word_list.split(",")]
if len(concepts) == 0:
raise gr.exceptions.InputError("words", "Please enter at least 1 concept")
if len(concepts) > 9:
raise gr.exceptions.InputError("words", "Please enter at most 9 concepts")
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
concept_heatmaps = pipeline_output.concept_heatmaps
concept_heatmaps = [heatmap.resize((IMG_SIZE, IMG_SIZE), resample=Image.NEAREST) for heatmap in concept_heatmaps]
heatmaps_and_labels = [(concept_heatmaps[concept_index], concepts[concept_index]) for concept_index in range(len(concepts))]
all_images_and_labels = [(output_image, "Generated Image")] + heatmaps_and_labels
num_rows = math.ceil(len(all_images_and_labels) / COLUMNS)
gallery = gr.Gallery(
label="Generated images",
show_label=True,
columns=[COLUMNS],
rows=[num_rows],
object_fit="contain"
)
return gallery
with gr.Blocks(
css="""
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
.title { text-align: center; margin-bottom: 10px; }
.authors { text-align: center; margin-bottom: 10px; }
.affiliations { text-align: center; color: #666; margin-bottom: 10px; }
.abstract { text-align: center; margin-bottom: 40px; }
"""
) as demo:
with gr.Column(elem_classes="container"):
gr.Markdown("# ConceptAttention: Diffusion Transformers Learn Highly Interpretable Features", elem_classes="title")
gr.Markdown("### Alec Helbling¹, Tuna Meral², Ben Hoover¹³, Pinar Yanardag², Duen Horng (Polo) Chau¹", elem_classes="authors")
gr.Markdown("### ¹Georgia Tech · ²Virginia Tech · ³IBM Research", elem_classes="affiliations")
gr.Markdown(
"""
We introduce ConceptAttention, an approach to interpreting the intermediate representations of diffusion transformers.
The user just gives a list of textual concepts and ConceptAttention will produce a set of saliency maps depicting
the location and intensity of these concepts in generated images. Check out our paper: [here](https://arxiv.org/abs/2502.04320).
""",
elem_classes="abstract"
)
with gr.Row(scale=1):
prompt = gr.Textbox(
label="Enter your prompt",
placeholder="Enter your prompt",
value=EXAMPLES[0][0],
scale=4,
show_label=True,
container=False
# height="80px"
)
words = gr.Textbox(
label="Enter a list of concepts (comma-separated)",
placeholder="Enter a list of concepts (comma-separated)",
value=EXAMPLES[0][1],
scale=4,
show_label=True,
container=False
# height="80px"
)
submit_btn = gr.Button(
"Run",
min_width="100px",
scale=1
)
# generated_image = gr.Image(label="Generated Image", elem_classes="input-image")
gallery = gr.Gallery(
label="Generated images",
show_label=True,
# elem_id="gallery",
columns=[COLUMNS],
rows=[1],
object_fit="contain",
# height="auto"
)
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=process_inputs,
inputs=[prompt, words, seed, layer_start_index, timestep_start_index],
outputs=[gallery]
)
gr.Examples(examples=EXAMPLES, inputs=[prompt, words, seed, layer_start_index, timestep_start_index], outputs=[gallery], fn=process_inputs, cache_examples=False)
# Automatically process the first example on launch
# demo.load(process_inputs, inputs=[prompt, words, seed, layer_start_index, timestep_start_index], outputs=[gallery])
if __name__ == "__main__":
demo.launch(max_threads=1)
# share=True,
# server_name="0.0.0.0",
# inbrowser=True,
# # share=False,
# server_port=6754,
# quiet=True,
# max_threads=1
# )
|