ZhouwqZJ commited on
Commit
0429dc0
·
1 Parent(s): 2da8cbf

modified: app.py

Browse files
Files changed (2) hide show
  1. app.py +35 -545
  2. app_2.py +547 -0
app.py CHANGED
@@ -1,547 +1,37 @@
1
- import os
2
- import re
3
- import time
4
- from io import BytesIO
5
- import uuid
6
- from dataclasses import dataclass
7
- from glob import iglob
8
- import argparse
9
- from einops import rearrange
10
- #from fire import Fire
11
- from PIL import ExifTags, Image
12
- from safetensors.torch import load_file, save_file
13
- import spaces
14
-
15
- import torch
16
- import torch.nn.functional as F
17
  import gradio as gr
18
- import numpy as np
19
- from transformers import pipeline
20
-
21
- from src.flux.sampling import denoise_fireflow, get_schedule, prepare, prepare_image, unpack, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent, get_noise
22
- from src.flux.util import (configs, embed_watermark, load_ae, load_clip, load_flow_model, load_t5)
23
-
24
- os.environ["CUDA_VISIBLE_DEVICES"] = "2"
25
-
26
- @dataclass
27
- class SamplingOptions:
28
- source_prompt: str
29
- target_prompt: str
30
- # prompt: str
31
- width: int
32
- height: int
33
- num_steps: int
34
- guidance: float
35
- seed: int | None
36
-
37
- @torch.inference_mode()
38
- def encode(init_image, torch_device, ae):
39
- init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
40
- init_image = init_image.unsqueeze(0)
41
- init_image = init_image.to(torch_device)
42
- with torch.no_grad():
43
- init_image = ae.encode(init_image.to()).to(torch.bfloat16)
44
- return init_image
45
-
46
-
47
- class FluxEditor:
48
- def __init__(self, args):
49
- self.args = args
50
- self.device = torch.device(args.device)
51
- self.offload = args.offload
52
- self.name = args.name
53
- self.is_schnell = args.name == "flux-schnell"
54
-
55
- self.feature_path = 'feature'
56
-
57
- self.reset()
58
-
59
- self.add_sampling_metadata = True
60
-
61
- if self.name not in configs:
62
- available = ", ".join(configs.keys())
63
- raise ValueError(f"Got unknown model name: {self.name}, chose from {available}")
64
-
65
- # init all components
66
- self.clip = load_clip(self.device)
67
- self.t5 = load_t5(self.device, max_length=256 if self.name == "flux-schnell" else 512)
68
- self.model = load_flow_model(self.name, device="cpu" if self.offload else self.device)
69
- self.ae = load_ae(self.name, device="cpu" if self.offload else self.device)
70
- self.t5.eval()
71
- self.clip.eval()
72
- self.ae.eval()
73
- self.model.eval()
74
-
75
- # clear history
76
- if os.path.exists("history_gradio/history.safetensors"):
77
- os.remove("history_gradio/history.safetensors")
78
-
79
-
80
- @torch.inference_mode()
81
- def reset(self):
82
- out_root = 'src/gradio_utils/gradio_outputs'
83
- name_dir = f'exp_{len(os.listdir(out_root))}'
84
- self.output_dir = os.path.join(out_root, name_dir)
85
- if not os.path.exists(self.output_dir):
86
- os.makedirs(self.output_dir)
87
- self.instructions = ['source']
88
- self.source_image = None
89
- self.history_tensors = {
90
- "source img": torch.zeros((1, 1, 1)),
91
- "prev img": torch.zeros((1, 1, 1))}
92
-
93
- source_prompt = "(Optional) Describe the content of the uploaded image."
94
- traget_prompt = "(Required) Describe the desired content of the edited image."
95
- gallery = None
96
- output_image = None
97
- return source_prompt, traget_prompt, gallery, output_image
98
-
99
-
100
- @torch.inference_mode()
101
- def process_image(self,
102
- init_image,
103
- source_prompt,
104
- target_prompt,
105
- editing_strategy,
106
- denoise_strategy,
107
- num_steps,
108
- guidance,
109
- attn_guidance_start_block,
110
- inject_step,
111
- init_image_2=None):
112
- if init_image is None:
113
- img, gr_gallery = self.generate_image(prompt=target_prompt)
114
- else:
115
- img, gr_gallery = self.edit(init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2)
116
- return img, gr_gallery
117
-
118
-
119
- @spaces.GPU(duration=120)
120
- @torch.inference_mode()
121
- def generate_image(
122
- self,
123
- width=512,
124
- height=512,
125
- num_steps=28,
126
- guidance=3.5,
127
- seed=None,
128
- prompt='',
129
- init_image=None,
130
- image2image_strength=0.0,
131
- add_sampling_metadata=True,
132
- ):
133
-
134
- if seed is None:
135
- g_seed = torch.Generator(device="cpu").seed()
136
- print(f"Generating '{prompt}' with seed {g_seed}")
137
- t0 = time.perf_counter()
138
-
139
- if init_image is not None:
140
- if isinstance(init_image, np.ndarray):
141
- init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 255.0
142
- init_image = init_image.unsqueeze(0)
143
- init_image = init_image.to(self.device)
144
- init_image = torch.nn.functional.interpolate(init_image, (height, width))
145
- if self.offload:
146
- self.ae.encoder.to(self.device)
147
- init_image = self.ae.encode(init_image.to())
148
- if self.offload:
149
- self.ae = self.ae.cpu()
150
- torch.cuda.empty_cache()
151
-
152
- # prepare input
153
- x = get_noise(
154
- 1,
155
- height,
156
- width,
157
- device=self.device,
158
- dtype=torch.bfloat16,
159
- seed=g_seed,
160
- )
161
- timesteps = get_schedule(
162
- num_steps,
163
- x.shape[-1] * x.shape[-2] // 4,
164
- shift=(not self.is_schnell),
165
- )
166
- if init_image is not None:
167
- t_idx = int((1 - image2image_strength) * num_steps)
168
- t = timesteps[t_idx]
169
- timesteps = timesteps[t_idx:]
170
- x = t * x + (1.0 - t) * init_image.to(x.dtype)
171
-
172
- if self.offload:
173
- self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
174
- inp = prepare(t5=self.t5, clip=self.clip, img=x, prompt=prompt)
175
-
176
- # offload TEs to CPU, load model to gpu
177
- if self.offload:
178
- self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
179
- torch.cuda.empty_cache()
180
- self.model = self.model.to(self.device)
181
-
182
- # denoise initial noise
183
- info = {}
184
- info['feature'] = {}
185
- info['inject_step'] = 0
186
- info['editing_strategy']= ""
187
- info['start_layer_index'] = 0
188
- info['end_layer_index'] = 37
189
- info['reuse_v']= False
190
- qkv_ratio = '1.0,1.0,1.0'
191
- info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
192
- x = denoise_rf(self.model, **inp, timesteps=timesteps, guidance=guidance, inverse=False, info=info)
193
-
194
- # offload model, load autoencoder to gpu
195
- if self.offload:
196
- self.model.cpu()
197
- torch.cuda.empty_cache()
198
- self.ae.decoder.to(x.device)
199
-
200
- # decode latents to pixel space
201
- x = unpack(x[0].float(), height, width)
202
- with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
203
- x = self.ae.decode(x)
204
-
205
- if self.offload:
206
- self.ae.decoder.cpu()
207
- torch.cuda.empty_cache()
208
-
209
- t1 = time.perf_counter()
210
-
211
- print(f"Done in {t1 - t0:.1f}s.")
212
- # bring into PIL format
213
- x = x.clamp(-1, 1)
214
- x = embed_watermark(x.float())
215
- x = rearrange(x[0], "c h w -> h w c")
216
-
217
- img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
218
-
219
- filename = os.path.join(self.output_dir,f"round_0000_[{prompt}].jpg")
220
- os.makedirs(os.path.dirname(filename), exist_ok=True)
221
- exif_data = Image.Exif()
222
- if init_image is None:
223
- exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
224
- else:
225
- exif_data[ExifTags.Base.Software] = "AI generated;img2img;flux"
226
- exif_data[ExifTags.Base.Make] = "Black Forest Labs"
227
- exif_data[ExifTags.Base.Model] = self.name
228
- if add_sampling_metadata:
229
- exif_data[ExifTags.Base.ImageDescription] = prompt
230
- img.save(filename, format="jpeg", exif=exif_data, quality=95, subsampling=0)
231
- self.instructions = [prompt]
232
-
233
- #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
234
- img_and_prompt = []
235
- history_imgs = sorted(os.listdir(self.output_dir))
236
- for img_file, prompt_txt in zip(history_imgs, self.instructions):
237
- img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
238
- history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
239
- return img, history_gallery
240
-
241
-
242
- @spaces.GPU(duration=120)
243
- @torch.inference_mode()
244
- def edit(self, init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2=None):
245
-
246
- torch.cuda.empty_cache()
247
- seed = None
248
-
249
- if self.offload:
250
- self.model.cpu()
251
- torch.cuda.empty_cache()
252
- self.ae.encoder.to(self.device)
253
-
254
- #----------------------------- 0.1 prepare multi-turn editing -------------------------------------#
255
- info = {}
256
- shape = init_image.shape
257
- new_h = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
258
- new_w = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
259
-
260
- if not any("round_0000" in fname for fname in os.listdir(self.output_dir)):
261
- Image.fromarray(init_image).save(os.path.join(self.output_dir,"round_0000_[source].jpg"))
262
-
263
-
264
- init_image = init_image[:new_h, :new_w, :]
265
- width, height = init_image.shape[0], init_image.shape[1]
266
- init_image = encode(init_image, self.device, self.ae)
267
-
268
- print(init_image.shape)
269
-
270
- if init_image_2 is None:
271
- print("init_image_2 is not provided, proceeding with single image processing.")
272
- else:
273
- init_image_2_pil = Image.fromarray(init_image_2) # Convert NumPy array to PIL Image
274
- init_image_2_pil = init_image_2_pil.resize((new_w, new_h), Image.Resampling.LANCZOS)
275
- init_image_2 = np.array(init_image_2_pil) # Convert back to NumPy (if needed)
276
- init_image_2 = encode(init_image_2, self.device, self.ae)
277
-
278
- rng = torch.Generator(device="cpu")
279
- opts = SamplingOptions(
280
- source_prompt=source_prompt,
281
- target_prompt=target_prompt,
282
- width=width,
283
- height=height,
284
- num_steps=num_steps,
285
- guidance=guidance,
286
- seed=seed,
287
- )
288
- if opts.seed is None:
289
- opts.seed = torch.Generator(device="cpu").seed()
290
-
291
- print(f"Editing with prompt:\n{opts.source_prompt}")
292
- t0 = time.perf_counter()
293
-
294
- opts.seed = None
295
- if self.offload:
296
- self.ae = self.ae.cpu()
297
- torch.cuda.empty_cache()
298
- self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
299
-
300
- #----------------------------- 0.2 prepare attention strategy -------------------------------------#
301
- info = {}
302
- info['feature'] = {}
303
- info['inject_step'] = inject_step
304
- info['editing_strategy']= " ".join(editing_strategy)
305
- info['start_layer_index'] = 0
306
- info['end_layer_index'] = 37
307
- info['reuse_v']= False
308
- qkv_ratio = '1.0,1.0,1.0'
309
- info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
310
- info['attn_guidance'] = attn_guidance_start_block
311
- info['lqr_stop'] = 0.25
312
-
313
- if not os.path.exists(self.feature_path):
314
- os.mkdir(self.feature_path)
315
-
316
-
317
- #----------------------------- 0.3 prepare latents -------------------------------------#
318
- with torch.no_grad():
319
- inp = prepare(self.t5, self.clip, init_image, prompt=opts.source_prompt)
320
- inp_target = prepare(self.t5, self.clip, init_image, prompt=opts.target_prompt)
321
- if self.source_image is None:
322
- self.source_image = inp['img']
323
- inp_target_2 = None
324
- if not init_image_2 is None:
325
- inp_target_2 = prepare_image(init_image_2)
326
- info['lqr_stop'] = 0.35
327
-
328
- timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=(self.name != "flux-schnell"))
329
- #timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=False)
330
-
331
- # offload TEs to CPU, load model to gpu
332
- if self.offload:
333
- self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
334
- torch.cuda.empty_cache()
335
- self.model = self.model.to(self.device)
336
-
337
-
338
-
339
- #----------------------------- 1 Inverting current image -------------------------------------#
340
- denoise_strategies = ['fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion', 'multi_turn_consistent']
341
- denoise_funcs = [denoise_fireflow, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent]
342
- denoise_func = denoise_funcs[denoise_strategies.index(denoise_strategy)]
343
- with torch.no_grad():
344
- z, info = denoise_func(self.model, **inp, timesteps=timesteps, guidance=1, inverse=True, info=info)
345
-
346
-
347
-
348
-
349
- #----------------------------- 2 history_tensors used to implement dual-LQR guiding editing -------------------------------------#
350
- inp_target["img"] = z
351
- timesteps = get_schedule(opts.num_steps, inp_target["img"].shape[1], shift=(self.name != "flux-schnell"))
352
-
353
- if torch.all(self.history_tensors['source img'] == 0):
354
- self.history_tensors = {
355
- "source img": inp["img"],
356
- "prev img": inp_target_2}
357
- else:
358
- if inp_target_2 is None:
359
- self.history_tensors["prev img"] = inp["img"]
360
- else:
361
- self.history_tensors["source img"] = inp["img"]
362
- self.history_tensors["prev img"] = inp_target_2
363
-
364
- #----------------------------- 3 sampling -------------------------------------#
365
- if denoise_strategy in ['rf_inversion', 'multi_turn_consistent']:
366
- x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=guidance, inverse=False, info=info, img_LQR=self.history_tensors)
367
- else:
368
- x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=opts.guidance, inverse=False, info=info)
369
-
370
-
371
- #----------------------------- 4 update history_tensors -------------------------------------#
372
- info = {}
373
- self.history_tensors["source img"] = self.source_image
374
- self.history_tensors["prev img"] = x
375
- '''save_file(history_tensors, "history_gradio/history.safetensors")'''
376
-
377
- # offload model, load autoencoder to gpu
378
- if self.offload:
379
- self.model.cpu()
380
- torch.cuda.empty_cache()
381
- self.ae.decoder.to(x.device)
382
-
383
-
384
-
385
- #----------------------------- 5 decode x to image -------------------------------------#
386
- x = unpack(x.float(), opts.width, opts.height)
387
-
388
- with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
389
- x = self.ae.decode(x)
390
-
391
- if torch.cuda.is_available():
392
- torch.cuda.synchronize()
393
- t1 = time.perf_counter()
394
-
395
- # bring into PIL format and save
396
- x = x.clamp(-1, 1)
397
- x = embed_watermark(x.float())
398
- x = rearrange(x[0], "c h w -> h w c")
399
-
400
- img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
401
- exif_data = Image.Exif()
402
- exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
403
- exif_data[ExifTags.Base.Make] = "Black Forest Labs"
404
- exif_data[ExifTags.Base.Model] = self.name
405
- if self.add_sampling_metadata:
406
- exif_data[ExifTags.Base.ImageDescription] = source_prompt
407
-
408
-
409
-
410
- #-------------------------------- 6 save image -------------------------------------#
411
-
412
- #-------------------- 6.1 prepare output folder ----------------------#
413
- if not os.path.exists(self.output_dir):
414
- os.makedirs(self.output_dir)
415
- idx = 1
416
- #-------------------- 6.2 editing round ----------------------#
417
- else:
418
- fns = [fn for fn in os.listdir(self.output_dir)]
419
- if len(fns) > 0:
420
- idx = max(int(fn.split("_")[1]) for fn in fns) + 1
421
- else:
422
- idx = 1
423
- formatted_idx = str(idx).zfill(4) # Format as a 4-digit string
424
-
425
- #-------------------- 6.3 output name ----------------------#
426
- if denoise_strategy == 'multi_turn_consistent':
427
- denoise_strategy = 'MTC'
428
- if target_prompt == '':
429
- target_prompt = 'Reconstruction'
430
- if target_prompt == source_prompt:
431
- target_prompt = 'Reconstruction: ' + target_prompt
432
-
433
- output_name = f"round_{formatted_idx}_[{" ".join(target_prompt.split()[-5:])}]_{denoise_strategy}.jpg"
434
- fn = os.path.join(self.output_dir, output_name)
435
-
436
- print(f"Done in {t1 - t0:.1f}s. Saving {fn}")
437
- img.save(fn)
438
-
439
- if 'Reconstruction' in target_prompt:
440
- target_prompt = source_prompt
441
- self.instructions.append(target_prompt)
442
- print("End Edit")
443
-
444
- #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
445
- img_and_prompt = []
446
- history_imgs = sorted(os.listdir(self.output_dir))
447
- for img_file, prompt_txt in zip(history_imgs, self.instructions):
448
- img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
449
- history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
450
-
451
- return img, history_gallery
452
-
453
-
454
- def on_select(gallery, selected: gr.SelectData):
455
- return gallery[selected.index][0], gallery[selected.index][1]
456
-
457
- def on_upload(path, uploaded: gr.EventData):
458
- return path[0][0]
459
-
460
- def on_change(init_image, changed: gr.EventData):
461
- img_path = list(changed.target.temp_files)
462
- return gr.Gallery(value=[(img_path[0], "")], label="History Image", interactive=True, columns=3)
463
-
464
- def create_demo(model_name: str, device: str = "cuda" if torch.cuda.is_available() else "cpu", offload: bool = False):
465
- editor = FluxEditor(args)
466
- is_schnell = model_name == "flux-schnell"
467
-
468
- # Pre-defined examples
469
- examples = [
470
- ["src/gradio_utils/gradio_examples/000000000011.jpg", "", "a photo of a eagle standing on the branch", ['attn_guidance'], 15, 3.5, 11, 0],
471
- ["src/gradio_utils/gradio_examples/221000000002.jpg", "", "a cat wearing a hat standing on the fence", ['attn_guidance'], 15, 3.5, 11, 0],
472
- ]
473
-
474
- with gr.Blocks() as demo:
475
- gr.Markdown(f"# Multi-turn Consistent Image Editing (FLUX.1-dev)")
476
-
477
- with gr.Row():
478
- with gr.Column():
479
- source_prompt = gr.Textbox(label="Source Prompt", value="(Optional) Describe the content of the uploaded image.")
480
- target_prompt = gr.Textbox(label="Target Prompt", value="(Required) Describe the desired content of the edited image.")
481
- with gr.Row():
482
- init_image = gr.Image(label="Initial Image", visible=False, width=200)
483
- init_image_2 = gr.Image(label="Input Image 2", visible=False, width=200)
484
- gallery = gr.Gallery(label ="History Image", interactive=True, columns=3)
485
- editing_strategy = gr.CheckboxGroup(
486
- label="Editing Technique",
487
- choices=['attn_guidance', 'replace_v', 'add_q', 'add_k', 'add_v', 'replace_q', 'replace_k'],
488
- value=['attn_guidance'], # Default: none selected
489
- interactive=True
490
- )
491
- denoise_strategy = gr.Dropdown(
492
- ['multi_turn_consistent', 'fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion'],
493
- label="Denoising Technique", value='multi_turn_consistent')
494
- generate_btn = gr.Button("Generate")
495
-
496
- with gr.Column():
497
- with gr.Accordion("Advanced Options", open=True):
498
- num_steps = gr.Slider(1, 30, 15, step=1, label="Number of steps")
499
- guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Text Guidance", interactive=not is_schnell)
500
- attn_guidance_start_block = gr.Slider(0, 18, 11, step=1, label="Top activated attn-maps", interactive=not is_schnell)
501
- inject_step = gr.Slider(0, 15, 1, step=1, label="Number of inject steps")
502
- output_image = gr.Image(label="Generated/Edited Image")
503
- reset_btn = gr.Button("Reset")
504
-
505
- gallery.select(on_select, gallery, [init_image, source_prompt])
506
- gallery.upload(on_upload, gallery, init_image)
507
- init_image.change(on_change, init_image, gallery)
508
-
509
- generate_btn.click(
510
- fn=editor.process_image,
511
- inputs=[init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2],
512
- outputs=[output_image, gallery]
513
- )
514
- reset_btn.click(fn = editor.reset, outputs=[source_prompt, target_prompt, gallery, output_image])
515
-
516
- # Add examples
517
- gr.Examples(
518
- examples=examples,
519
- inputs=[
520
- init_image,
521
- source_prompt,
522
- target_prompt,
523
- editing_strategy,
524
- num_steps,
525
- guidance,
526
- attn_guidance_start_block,
527
- inject_step
528
- ]
529
- )
530
-
531
-
532
- return demo
533
-
534
-
535
-
536
- import argparse
537
- parser = argparse.ArgumentParser(description="Flux")
538
- parser.add_argument("--name", type=str, default="flux-dev", choices=list(configs.keys()), help="Model name")
539
- parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu", help="Device to use")
540
- parser.add_argument("--offload", action="store_true", help="Offload model to CPU when not in use")
541
- parser.add_argument("--share", action="store_true", help="Create a public link to your demo")
542
- parser.add_argument("--port", type=int, default=9090)
543
- args = parser.parse_args()
544
 
545
- demo = create_demo(args.name, args.device, args.offload)
546
- #demo.launch(server_name='0.0.0.0', share=args.share, server_port=args.port)
547
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ def calculator(num1, operation, num2):
4
+ if operation == "add":
5
+ return num1 + num2
6
+ elif operation == "subtract":
7
+ return num1 - num2
8
+ elif operation == "multiply":
9
+ return num1 * num2
10
+ elif operation == "divide":
11
+ return num1 / num2
12
+
13
+ with gr.Blocks() as demo:
14
+ with gr.Row():
15
+ with gr.Column():
16
+ num_1 = gr.Number(value=4)
17
+ operation = gr.Radio(["add", "subtract", "multiply", "divide"])
18
+ num_2 = gr.Number(value=0)
19
+ submit_btn = gr.Button(value="Calculate")
20
+ with gr.Column():
21
+ result = gr.Number()
22
+
23
+ submit_btn.click(
24
+ calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False
25
+ )
26
+ examples = gr.Examples(
27
+ examples=[
28
+ [5, "add", 3],
29
+ [4, "divide", 2],
30
+ [-4, "multiply", 2.5],
31
+ [0, "subtract", 1.2],
32
+ ],
33
+ inputs=[num_1, operation, num_2],
34
+ )
35
+
36
+ if __name__ == "__main__":
37
+ demo.launch(show_api=False)
app_2.py ADDED
@@ -0,0 +1,547 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import time
4
+ from io import BytesIO
5
+ import uuid
6
+ from dataclasses import dataclass
7
+ from glob import iglob
8
+ import argparse
9
+ from einops import rearrange
10
+ #from fire import Fire
11
+ from PIL import ExifTags, Image
12
+ from safetensors.torch import load_file, save_file
13
+ import spaces
14
+
15
+ import torch
16
+ import torch.nn.functional as F
17
+ import gradio as gr
18
+ import numpy as np
19
+ from transformers import pipeline
20
+
21
+ from src.flux.sampling import denoise_fireflow, get_schedule, prepare, prepare_image, unpack, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent, get_noise
22
+ from src.flux.util import (configs, embed_watermark, load_ae, load_clip, load_flow_model, load_t5)
23
+
24
+ os.environ["CUDA_VISIBLE_DEVICES"] = "2"
25
+
26
+ @dataclass
27
+ class SamplingOptions:
28
+ source_prompt: str
29
+ target_prompt: str
30
+ # prompt: str
31
+ width: int
32
+ height: int
33
+ num_steps: int
34
+ guidance: float
35
+ seed: int | None
36
+
37
+ @torch.inference_mode()
38
+ def encode(init_image, torch_device, ae):
39
+ init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
40
+ init_image = init_image.unsqueeze(0)
41
+ init_image = init_image.to(torch_device)
42
+ with torch.no_grad():
43
+ init_image = ae.encode(init_image.to()).to(torch.bfloat16)
44
+ return init_image
45
+
46
+
47
+ class FluxEditor:
48
+ def __init__(self, args):
49
+ self.args = args
50
+ self.device = torch.device(args.device)
51
+ self.offload = args.offload
52
+ self.name = args.name
53
+ self.is_schnell = args.name == "flux-schnell"
54
+
55
+ self.feature_path = 'feature'
56
+
57
+ self.reset()
58
+
59
+ self.add_sampling_metadata = True
60
+
61
+ if self.name not in configs:
62
+ available = ", ".join(configs.keys())
63
+ raise ValueError(f"Got unknown model name: {self.name}, chose from {available}")
64
+
65
+ # init all components
66
+ self.clip = load_clip(self.device)
67
+ self.t5 = load_t5(self.device, max_length=256 if self.name == "flux-schnell" else 512)
68
+ self.model = load_flow_model(self.name, device="cpu" if self.offload else self.device)
69
+ self.ae = load_ae(self.name, device="cpu" if self.offload else self.device)
70
+ self.t5.eval()
71
+ self.clip.eval()
72
+ self.ae.eval()
73
+ self.model.eval()
74
+
75
+ # clear history
76
+ if os.path.exists("history_gradio/history.safetensors"):
77
+ os.remove("history_gradio/history.safetensors")
78
+
79
+
80
+ @torch.inference_mode()
81
+ def reset(self):
82
+ out_root = 'src/gradio_utils/gradio_outputs'
83
+ name_dir = f'exp_{len(os.listdir(out_root))}'
84
+ self.output_dir = os.path.join(out_root, name_dir)
85
+ if not os.path.exists(self.output_dir):
86
+ os.makedirs(self.output_dir)
87
+ self.instructions = ['source']
88
+ self.source_image = None
89
+ self.history_tensors = {
90
+ "source img": torch.zeros((1, 1, 1)),
91
+ "prev img": torch.zeros((1, 1, 1))}
92
+
93
+ source_prompt = "(Optional) Describe the content of the uploaded image."
94
+ traget_prompt = "(Required) Describe the desired content of the edited image."
95
+ gallery = None
96
+ output_image = None
97
+ return source_prompt, traget_prompt, gallery, output_image
98
+
99
+
100
+ @torch.inference_mode()
101
+ def process_image(self,
102
+ init_image,
103
+ source_prompt,
104
+ target_prompt,
105
+ editing_strategy,
106
+ denoise_strategy,
107
+ num_steps,
108
+ guidance,
109
+ attn_guidance_start_block,
110
+ inject_step,
111
+ init_image_2=None):
112
+ if init_image is None:
113
+ img, gr_gallery = self.generate_image(prompt=target_prompt)
114
+ else:
115
+ img, gr_gallery = self.edit(init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2)
116
+ return img, gr_gallery
117
+
118
+
119
+ @spaces.GPU(duration=120)
120
+ @torch.inference_mode()
121
+ def generate_image(
122
+ self,
123
+ width=512,
124
+ height=512,
125
+ num_steps=28,
126
+ guidance=3.5,
127
+ seed=None,
128
+ prompt='',
129
+ init_image=None,
130
+ image2image_strength=0.0,
131
+ add_sampling_metadata=True,
132
+ ):
133
+
134
+ if seed is None:
135
+ g_seed = torch.Generator(device="cpu").seed()
136
+ print(f"Generating '{prompt}' with seed {g_seed}")
137
+ t0 = time.perf_counter()
138
+
139
+ if init_image is not None:
140
+ if isinstance(init_image, np.ndarray):
141
+ init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 255.0
142
+ init_image = init_image.unsqueeze(0)
143
+ init_image = init_image.to(self.device)
144
+ init_image = torch.nn.functional.interpolate(init_image, (height, width))
145
+ if self.offload:
146
+ self.ae.encoder.to(self.device)
147
+ init_image = self.ae.encode(init_image.to())
148
+ if self.offload:
149
+ self.ae = self.ae.cpu()
150
+ torch.cuda.empty_cache()
151
+
152
+ # prepare input
153
+ x = get_noise(
154
+ 1,
155
+ height,
156
+ width,
157
+ device=self.device,
158
+ dtype=torch.bfloat16,
159
+ seed=g_seed,
160
+ )
161
+ timesteps = get_schedule(
162
+ num_steps,
163
+ x.shape[-1] * x.shape[-2] // 4,
164
+ shift=(not self.is_schnell),
165
+ )
166
+ if init_image is not None:
167
+ t_idx = int((1 - image2image_strength) * num_steps)
168
+ t = timesteps[t_idx]
169
+ timesteps = timesteps[t_idx:]
170
+ x = t * x + (1.0 - t) * init_image.to(x.dtype)
171
+
172
+ if self.offload:
173
+ self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
174
+ inp = prepare(t5=self.t5, clip=self.clip, img=x, prompt=prompt)
175
+
176
+ # offload TEs to CPU, load model to gpu
177
+ if self.offload:
178
+ self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
179
+ torch.cuda.empty_cache()
180
+ self.model = self.model.to(self.device)
181
+
182
+ # denoise initial noise
183
+ info = {}
184
+ info['feature'] = {}
185
+ info['inject_step'] = 0
186
+ info['editing_strategy']= ""
187
+ info['start_layer_index'] = 0
188
+ info['end_layer_index'] = 37
189
+ info['reuse_v']= False
190
+ qkv_ratio = '1.0,1.0,1.0'
191
+ info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
192
+ x = denoise_rf(self.model, **inp, timesteps=timesteps, guidance=guidance, inverse=False, info=info)
193
+
194
+ # offload model, load autoencoder to gpu
195
+ if self.offload:
196
+ self.model.cpu()
197
+ torch.cuda.empty_cache()
198
+ self.ae.decoder.to(x.device)
199
+
200
+ # decode latents to pixel space
201
+ x = unpack(x[0].float(), height, width)
202
+ with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
203
+ x = self.ae.decode(x)
204
+
205
+ if self.offload:
206
+ self.ae.decoder.cpu()
207
+ torch.cuda.empty_cache()
208
+
209
+ t1 = time.perf_counter()
210
+
211
+ print(f"Done in {t1 - t0:.1f}s.")
212
+ # bring into PIL format
213
+ x = x.clamp(-1, 1)
214
+ x = embed_watermark(x.float())
215
+ x = rearrange(x[0], "c h w -> h w c")
216
+
217
+ img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
218
+
219
+ filename = os.path.join(self.output_dir,f"round_0000_[{prompt}].jpg")
220
+ os.makedirs(os.path.dirname(filename), exist_ok=True)
221
+ exif_data = Image.Exif()
222
+ if init_image is None:
223
+ exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
224
+ else:
225
+ exif_data[ExifTags.Base.Software] = "AI generated;img2img;flux"
226
+ exif_data[ExifTags.Base.Make] = "Black Forest Labs"
227
+ exif_data[ExifTags.Base.Model] = self.name
228
+ if add_sampling_metadata:
229
+ exif_data[ExifTags.Base.ImageDescription] = prompt
230
+ img.save(filename, format="jpeg", exif=exif_data, quality=95, subsampling=0)
231
+ self.instructions = [prompt]
232
+
233
+ #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
234
+ img_and_prompt = []
235
+ history_imgs = sorted(os.listdir(self.output_dir))
236
+ for img_file, prompt_txt in zip(history_imgs, self.instructions):
237
+ img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
238
+ history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
239
+ return img, history_gallery
240
+
241
+
242
+ @spaces.GPU(duration=120)
243
+ @torch.inference_mode()
244
+ def edit(self, init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2=None):
245
+
246
+ torch.cuda.empty_cache()
247
+ seed = None
248
+
249
+ if self.offload:
250
+ self.model.cpu()
251
+ torch.cuda.empty_cache()
252
+ self.ae.encoder.to(self.device)
253
+
254
+ #----------------------------- 0.1 prepare multi-turn editing -------------------------------------#
255
+ info = {}
256
+ shape = init_image.shape
257
+ new_h = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
258
+ new_w = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
259
+
260
+ if not any("round_0000" in fname for fname in os.listdir(self.output_dir)):
261
+ Image.fromarray(init_image).save(os.path.join(self.output_dir,"round_0000_[source].jpg"))
262
+
263
+
264
+ init_image = init_image[:new_h, :new_w, :]
265
+ width, height = init_image.shape[0], init_image.shape[1]
266
+ init_image = encode(init_image, self.device, self.ae)
267
+
268
+ print(init_image.shape)
269
+
270
+ if init_image_2 is None:
271
+ print("init_image_2 is not provided, proceeding with single image processing.")
272
+ else:
273
+ init_image_2_pil = Image.fromarray(init_image_2) # Convert NumPy array to PIL Image
274
+ init_image_2_pil = init_image_2_pil.resize((new_w, new_h), Image.Resampling.LANCZOS)
275
+ init_image_2 = np.array(init_image_2_pil) # Convert back to NumPy (if needed)
276
+ init_image_2 = encode(init_image_2, self.device, self.ae)
277
+
278
+ rng = torch.Generator(device="cpu")
279
+ opts = SamplingOptions(
280
+ source_prompt=source_prompt,
281
+ target_prompt=target_prompt,
282
+ width=width,
283
+ height=height,
284
+ num_steps=num_steps,
285
+ guidance=guidance,
286
+ seed=seed,
287
+ )
288
+ if opts.seed is None:
289
+ opts.seed = torch.Generator(device="cpu").seed()
290
+
291
+ print(f"Editing with prompt:\n{opts.source_prompt}")
292
+ t0 = time.perf_counter()
293
+
294
+ opts.seed = None
295
+ if self.offload:
296
+ self.ae = self.ae.cpu()
297
+ torch.cuda.empty_cache()
298
+ self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
299
+
300
+ #----------------------------- 0.2 prepare attention strategy -------------------------------------#
301
+ info = {}
302
+ info['feature'] = {}
303
+ info['inject_step'] = inject_step
304
+ info['editing_strategy']= " ".join(editing_strategy)
305
+ info['start_layer_index'] = 0
306
+ info['end_layer_index'] = 37
307
+ info['reuse_v']= False
308
+ qkv_ratio = '1.0,1.0,1.0'
309
+ info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
310
+ info['attn_guidance'] = attn_guidance_start_block
311
+ info['lqr_stop'] = 0.25
312
+
313
+ if not os.path.exists(self.feature_path):
314
+ os.mkdir(self.feature_path)
315
+
316
+
317
+ #----------------------------- 0.3 prepare latents -------------------------------------#
318
+ with torch.no_grad():
319
+ inp = prepare(self.t5, self.clip, init_image, prompt=opts.source_prompt)
320
+ inp_target = prepare(self.t5, self.clip, init_image, prompt=opts.target_prompt)
321
+ if self.source_image is None:
322
+ self.source_image = inp['img']
323
+ inp_target_2 = None
324
+ if not init_image_2 is None:
325
+ inp_target_2 = prepare_image(init_image_2)
326
+ info['lqr_stop'] = 0.35
327
+
328
+ timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=(self.name != "flux-schnell"))
329
+ #timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=False)
330
+
331
+ # offload TEs to CPU, load model to gpu
332
+ if self.offload:
333
+ self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
334
+ torch.cuda.empty_cache()
335
+ self.model = self.model.to(self.device)
336
+
337
+
338
+
339
+ #----------------------------- 1 Inverting current image -------------------------------------#
340
+ denoise_strategies = ['fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion', 'multi_turn_consistent']
341
+ denoise_funcs = [denoise_fireflow, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent]
342
+ denoise_func = denoise_funcs[denoise_strategies.index(denoise_strategy)]
343
+ with torch.no_grad():
344
+ z, info = denoise_func(self.model, **inp, timesteps=timesteps, guidance=1, inverse=True, info=info)
345
+
346
+
347
+
348
+
349
+ #----------------------------- 2 history_tensors used to implement dual-LQR guiding editing -------------------------------------#
350
+ inp_target["img"] = z
351
+ timesteps = get_schedule(opts.num_steps, inp_target["img"].shape[1], shift=(self.name != "flux-schnell"))
352
+
353
+ if torch.all(self.history_tensors['source img'] == 0):
354
+ self.history_tensors = {
355
+ "source img": inp["img"],
356
+ "prev img": inp_target_2}
357
+ else:
358
+ if inp_target_2 is None:
359
+ self.history_tensors["prev img"] = inp["img"]
360
+ else:
361
+ self.history_tensors["source img"] = inp["img"]
362
+ self.history_tensors["prev img"] = inp_target_2
363
+
364
+ #----------------------------- 3 sampling -------------------------------------#
365
+ if denoise_strategy in ['rf_inversion', 'multi_turn_consistent']:
366
+ x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=guidance, inverse=False, info=info, img_LQR=self.history_tensors)
367
+ else:
368
+ x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=opts.guidance, inverse=False, info=info)
369
+
370
+
371
+ #----------------------------- 4 update history_tensors -------------------------------------#
372
+ info = {}
373
+ self.history_tensors["source img"] = self.source_image
374
+ self.history_tensors["prev img"] = x
375
+ '''save_file(history_tensors, "history_gradio/history.safetensors")'''
376
+
377
+ # offload model, load autoencoder to gpu
378
+ if self.offload:
379
+ self.model.cpu()
380
+ torch.cuda.empty_cache()
381
+ self.ae.decoder.to(x.device)
382
+
383
+
384
+
385
+ #----------------------------- 5 decode x to image -------------------------------------#
386
+ x = unpack(x.float(), opts.width, opts.height)
387
+
388
+ with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
389
+ x = self.ae.decode(x)
390
+
391
+ if torch.cuda.is_available():
392
+ torch.cuda.synchronize()
393
+ t1 = time.perf_counter()
394
+
395
+ # bring into PIL format and save
396
+ x = x.clamp(-1, 1)
397
+ x = embed_watermark(x.float())
398
+ x = rearrange(x[0], "c h w -> h w c")
399
+
400
+ img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
401
+ exif_data = Image.Exif()
402
+ exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
403
+ exif_data[ExifTags.Base.Make] = "Black Forest Labs"
404
+ exif_data[ExifTags.Base.Model] = self.name
405
+ if self.add_sampling_metadata:
406
+ exif_data[ExifTags.Base.ImageDescription] = source_prompt
407
+
408
+
409
+
410
+ #-------------------------------- 6 save image -------------------------------------#
411
+
412
+ #-------------------- 6.1 prepare output folder ----------------------#
413
+ if not os.path.exists(self.output_dir):
414
+ os.makedirs(self.output_dir)
415
+ idx = 1
416
+ #-------------------- 6.2 editing round ----------------------#
417
+ else:
418
+ fns = [fn for fn in os.listdir(self.output_dir)]
419
+ if len(fns) > 0:
420
+ idx = max(int(fn.split("_")[1]) for fn in fns) + 1
421
+ else:
422
+ idx = 1
423
+ formatted_idx = str(idx).zfill(4) # Format as a 4-digit string
424
+
425
+ #-------------------- 6.3 output name ----------------------#
426
+ if denoise_strategy == 'multi_turn_consistent':
427
+ denoise_strategy = 'MTC'
428
+ if target_prompt == '':
429
+ target_prompt = 'Reconstruction'
430
+ if target_prompt == source_prompt:
431
+ target_prompt = 'Reconstruction: ' + target_prompt
432
+
433
+ output_name = f"round_{formatted_idx}_[{" ".join(target_prompt.split()[-5:])}]_{denoise_strategy}.jpg"
434
+ fn = os.path.join(self.output_dir, output_name)
435
+
436
+ print(f"Done in {t1 - t0:.1f}s. Saving {fn}")
437
+ img.save(fn)
438
+
439
+ if 'Reconstruction' in target_prompt:
440
+ target_prompt = source_prompt
441
+ self.instructions.append(target_prompt)
442
+ print("End Edit")
443
+
444
+ #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
445
+ img_and_prompt = []
446
+ history_imgs = sorted(os.listdir(self.output_dir))
447
+ for img_file, prompt_txt in zip(history_imgs, self.instructions):
448
+ img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
449
+ history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
450
+
451
+ return img, history_gallery
452
+
453
+
454
+ def on_select(gallery, selected: gr.SelectData):
455
+ return gallery[selected.index][0], gallery[selected.index][1]
456
+
457
+ def on_upload(path, uploaded: gr.EventData):
458
+ return path[0][0]
459
+
460
+ def on_change(init_image, changed: gr.EventData):
461
+ img_path = list(changed.target.temp_files)
462
+ return gr.Gallery(value=[(img_path[0], "")], label="History Image", interactive=True, columns=3)
463
+
464
+ def create_demo(model_name: str, device: str = "cuda" if torch.cuda.is_available() else "cpu", offload: bool = False):
465
+ editor = FluxEditor(args)
466
+ is_schnell = model_name == "flux-schnell"
467
+
468
+ # Pre-defined examples
469
+ examples = [
470
+ ["src/gradio_utils/gradio_examples/000000000011.jpg", "", "a photo of a eagle standing on the branch", ['attn_guidance'], 15, 3.5, 11, 0],
471
+ ["src/gradio_utils/gradio_examples/221000000002.jpg", "", "a cat wearing a hat standing on the fence", ['attn_guidance'], 15, 3.5, 11, 0],
472
+ ]
473
+
474
+ with gr.Blocks() as demo:
475
+ gr.Markdown(f"# Multi-turn Consistent Image Editing (FLUX.1-dev)")
476
+
477
+ with gr.Row():
478
+ with gr.Column():
479
+ source_prompt = gr.Textbox(label="Source Prompt", value="(Optional) Describe the content of the uploaded image.")
480
+ target_prompt = gr.Textbox(label="Target Prompt", value="(Required) Describe the desired content of the edited image.")
481
+ with gr.Row():
482
+ init_image = gr.Image(label="Initial Image", visible=False, width=200)
483
+ init_image_2 = gr.Image(label="Input Image 2", visible=False, width=200)
484
+ gallery = gr.Gallery(label ="History Image", interactive=True, columns=3)
485
+ editing_strategy = gr.CheckboxGroup(
486
+ label="Editing Technique",
487
+ choices=['attn_guidance', 'replace_v', 'add_q', 'add_k', 'add_v', 'replace_q', 'replace_k'],
488
+ value=['attn_guidance'], # Default: none selected
489
+ interactive=True
490
+ )
491
+ denoise_strategy = gr.Dropdown(
492
+ ['multi_turn_consistent', 'fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion'],
493
+ label="Denoising Technique", value='multi_turn_consistent')
494
+ generate_btn = gr.Button("Generate")
495
+
496
+ with gr.Column():
497
+ with gr.Accordion("Advanced Options", open=True):
498
+ num_steps = gr.Slider(1, 30, 15, step=1, label="Number of steps")
499
+ guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Text Guidance", interactive=not is_schnell)
500
+ attn_guidance_start_block = gr.Slider(0, 18, 11, step=1, label="Top activated attn-maps", interactive=not is_schnell)
501
+ inject_step = gr.Slider(0, 15, 1, step=1, label="Number of inject steps")
502
+ output_image = gr.Image(label="Generated/Edited Image")
503
+ reset_btn = gr.Button("Reset")
504
+
505
+ gallery.select(on_select, gallery, [init_image, source_prompt])
506
+ gallery.upload(on_upload, gallery, init_image)
507
+ init_image.change(on_change, init_image, gallery)
508
+
509
+ generate_btn.click(
510
+ fn=editor.process_image,
511
+ inputs=[init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2],
512
+ outputs=[output_image, gallery]
513
+ )
514
+ reset_btn.click(fn = editor.reset, outputs=[source_prompt, target_prompt, gallery, output_image])
515
+
516
+ # Add examples
517
+ gr.Examples(
518
+ examples=examples,
519
+ inputs=[
520
+ init_image,
521
+ source_prompt,
522
+ target_prompt,
523
+ editing_strategy,
524
+ num_steps,
525
+ guidance,
526
+ attn_guidance_start_block,
527
+ inject_step
528
+ ]
529
+ )
530
+
531
+
532
+ return demo
533
+
534
+
535
+
536
+ import argparse
537
+ parser = argparse.ArgumentParser(description="Flux")
538
+ parser.add_argument("--name", type=str, default="flux-dev", choices=list(configs.keys()), help="Model name")
539
+ parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu", help="Device to use")
540
+ parser.add_argument("--offload", action="store_true", help="Offload model to CPU when not in use")
541
+ parser.add_argument("--share", action="store_true", help="Create a public link to your demo")
542
+ parser.add_argument("--port", type=int, default=9090)
543
+ args = parser.parse_args()
544
+
545
+ demo = create_demo(args.name, args.device, args.offload)
546
+ #demo.launch(server_name='0.0.0.0', share=args.share, server_port=args.port)
547
+ demo.launch(share=True)