WonwoongCho commited on
Commit
f42d7ce
·
1 Parent(s): cc512e6

debugging app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -31
app.py CHANGED
@@ -21,14 +21,25 @@ pipe = FluxPipeline.from_pretrained(
21
  )
22
  pipe = pipe.to("cuda")
23
 
 
24
  @spaces.GPU
25
- def process_image_and_text(image, text, seed):
26
  set_seed(seed)
27
- # image = Image.open(img_path).convert('RGB')
28
  image = resize_and_add_margin(image, target_size=512)
29
-
30
  image_list = [image]
31
-
 
 
 
 
 
 
 
 
 
 
 
 
32
  out = pipe(
33
  prompt=text,
34
  height=512,
@@ -139,32 +150,6 @@ def create_app():
139
  output_image = gr.Image(type="pil", elem_id="output")
140
 
141
 
142
- blended_attn_procs = {}
143
- for name, _ in pipe.transformer.attn_processors.items():
144
- if "single" in name:
145
- blended_attn_procs[name] = FluxBlendedAttnProcessor2_0(3072, ba_scale=scale, num_ref=1).to(device="cuda", dtype=dtype)
146
- else:
147
- blended_attn_procs[name] = pipe.transformer.attn_processors[name]
148
-
149
- pipe.transformer.set_attn_processor(blended_attn_procs)
150
-
151
- model_path = hf_hub_download(
152
- repo_id="WonwoongCho/IT-Blender",
153
- filename="FLUX/it-blender.bin",
154
- token=token
155
- )
156
-
157
- pretrained_blended_attn_weights = torch.load(model_path, map_location=pipe._execution_device)
158
-
159
- key_changed_blended_attn_weights = {}
160
- for key, value in pretrained_blended_attn_weights.items():
161
- block_idx = int(key.split(".")[0]) - 21
162
- k_or_v = key.split("_")[2]
163
- changed_key = f'single_transformer_blocks.{block_idx}.attn.processor.blended_attention_{k_or_v}_proj.weight'
164
- key_changed_blended_attn_weights[changed_key] = value.to(dtype=dtype, device="cuda")
165
-
166
- missing_keys, unexpected_keys = pipe.transformer.load_state_dict(key_changed_blended_attn_weights, strict=False)
167
-
168
  with gr.Row():
169
  examples = gr.Examples(
170
  examples=get_samples(),
@@ -174,7 +159,7 @@ def create_app():
174
 
175
  submit_btn.click(
176
  fn=process_image_and_text,
177
- inputs=[original_image, text, seed],
178
  outputs=output_image,
179
  )
180
 
 
21
  )
22
  pipe = pipe.to("cuda")
23
 
24
+
25
  @spaces.GPU
26
+ def process_image_and_text(image, text, seed, scale):
27
  set_seed(seed)
 
28
  image = resize_and_add_margin(image, target_size=512)
 
29
  image_list = [image]
30
+
31
+ # Dynamically set attention processors using user-specified scale
32
+ blended_attn_procs = {}
33
+ for name, _ in pipe.transformer.attn_processors.items():
34
+ if "single" in name:
35
+ processor = FluxBlendedAttnProcessor2_0(3072, ba_scale=float(scale), num_ref=1)
36
+ processor = processor.to(device="cuda", dtype=dtype)
37
+ blended_attn_procs[name] = processor
38
+ else:
39
+ blended_attn_procs[name] = pipe.transformer.attn_processors[name]
40
+
41
+ pipe.transformer.set_attn_processor(blended_attn_procs)
42
+
43
  out = pipe(
44
  prompt=text,
45
  height=512,
 
150
  output_image = gr.Image(type="pil", elem_id="output")
151
 
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  with gr.Row():
154
  examples = gr.Examples(
155
  examples=get_samples(),
 
159
 
160
  submit_btn.click(
161
  fn=process_image_and_text,
162
+ inputs=[original_image, text, seed, scale],
163
  outputs=output_image,
164
  )
165