ZhouwqZJ commited on
Commit
096cc67
·
1 Parent(s): 73f93ea

modified: app.py

Browse files
Files changed (3) hide show
  1. app.py +33 -543
  2. app_2.py +547 -0
  3. app_test.py +0 -37
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
- @dataclass
25
- class SamplingOptions:
26
- source_prompt: str
27
- target_prompt: str
28
- # prompt: str
29
- width: int
30
- height: int
31
- num_steps: int
32
- guidance: float
33
- seed: int | None
34
-
35
- @torch.inference_mode()
36
- def encode(init_image, torch_device, ae):
37
- init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
38
- init_image = init_image.unsqueeze(0)
39
- init_image = init_image.to(torch_device)
40
- with torch.no_grad():
41
- init_image = ae.encode(init_image.to()).to(torch.bfloat16)
42
- return init_image
43
-
44
-
45
- class FluxEditor:
46
- def __init__(self, args):
47
- self.args = args
48
- self.device = torch.device(args.device)
49
- self.offload = args.offload
50
- self.name = args.name
51
- self.is_schnell = args.name == "flux-schnell"
52
-
53
- self.feature_path = 'feature'
54
-
55
- self.reset()
56
-
57
- self.add_sampling_metadata = True
58
-
59
- if self.name not in configs:
60
- available = ", ".join(configs.keys())
61
- raise ValueError(f"Got unknown model name: {self.name}, chose from {available}")
62
-
63
- # init all components
64
- self.clip = load_clip(self.device)
65
- self.t5 = load_t5(self.device, max_length=256 if self.name == "flux-schnell" else 512)
66
- self.model = load_flow_model(self.name, device="cpu" if self.offload else self.device)
67
- self.ae = load_ae(self.name, device="cpu" if self.offload else self.device)
68
- self.t5.eval()
69
- self.clip.eval()
70
- self.ae.eval()
71
- self.model.eval()
72
-
73
- # clear history
74
- if os.path.exists("history_gradio/history.safetensors"):
75
- os.remove("history_gradio/history.safetensors")
76
-
77
-
78
- @torch.inference_mode()
79
- def reset(self):
80
- out_root = 'src/gradio_utils/gradio_outputs'
81
- name_dir = f'exp_{len(os.listdir(out_root))}'
82
- self.output_dir = os.path.join(out_root, name_dir)
83
- if not os.path.exists(self.output_dir):
84
- os.makedirs(self.output_dir)
85
- self.instructions = ['source']
86
- self.source_image = None
87
- self.history_tensors = {
88
- "source img": torch.zeros((1, 1, 1)),
89
- "prev img": torch.zeros((1, 1, 1))}
90
-
91
- source_prompt = "(Optional) Describe the content of the uploaded image."
92
- traget_prompt = "(Required) Describe the desired content of the edited image."
93
- gallery = None
94
- output_image = None
95
- return source_prompt, traget_prompt, gallery, output_image
96
-
97
-
98
- @torch.inference_mode()
99
- def process_image(self,
100
- init_image,
101
- source_prompt,
102
- target_prompt,
103
- editing_strategy,
104
- denoise_strategy,
105
- num_steps,
106
- guidance,
107
- attn_guidance_start_block,
108
- inject_step,
109
- init_image_2=None):
110
- if init_image is None:
111
- img, gr_gallery = self.generate_image(prompt=target_prompt)
112
- else:
113
- 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)
114
- return img, gr_gallery
115
-
116
-
117
- @spaces.GPU(duration=120)
118
- @torch.inference_mode()
119
- def generate_image(
120
- self,
121
- width=512,
122
- height=512,
123
- num_steps=28,
124
- guidance=3.5,
125
- seed=None,
126
- prompt='',
127
- init_image=None,
128
- image2image_strength=0.0,
129
- add_sampling_metadata=True,
130
- ):
131
-
132
- if seed is None:
133
- g_seed = torch.Generator(device="cpu").seed()
134
- print(f"Generating '{prompt}' with seed {g_seed}")
135
- t0 = time.perf_counter()
136
-
137
- if init_image is not None:
138
- if isinstance(init_image, np.ndarray):
139
- init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 255.0
140
- init_image = init_image.unsqueeze(0)
141
- init_image = init_image.to(self.device)
142
- init_image = torch.nn.functional.interpolate(init_image, (height, width))
143
- if self.offload:
144
- self.ae.encoder.to(self.device)
145
- init_image = self.ae.encode(init_image.to())
146
- if self.offload:
147
- self.ae = self.ae.cpu()
148
- torch.cuda.empty_cache()
149
-
150
- # prepare input
151
- x = get_noise(
152
- 1,
153
- height,
154
- width,
155
- device=self.device,
156
- dtype=torch.bfloat16,
157
- seed=g_seed,
158
- )
159
- timesteps = get_schedule(
160
- num_steps,
161
- x.shape[-1] * x.shape[-2] // 4,
162
- shift=(not self.is_schnell),
163
- )
164
- if init_image is not None:
165
- t_idx = int((1 - image2image_strength) * num_steps)
166
- t = timesteps[t_idx]
167
- timesteps = timesteps[t_idx:]
168
- x = t * x + (1.0 - t) * init_image.to(x.dtype)
169
-
170
- if self.offload:
171
- self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
172
- inp = prepare(t5=self.t5, clip=self.clip, img=x, prompt=prompt)
173
-
174
- # offload TEs to CPU, load model to gpu
175
- if self.offload:
176
- self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
177
- torch.cuda.empty_cache()
178
- self.model = self.model.to(self.device)
179
-
180
- # denoise initial noise
181
- info = {}
182
- info['feature'] = {}
183
- info['inject_step'] = 0
184
- info['editing_strategy']= ""
185
- info['start_layer_index'] = 0
186
- info['end_layer_index'] = 37
187
- info['reuse_v']= False
188
- qkv_ratio = '1.0,1.0,1.0'
189
- info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
190
- x = denoise_rf(self.model, **inp, timesteps=timesteps, guidance=guidance, inverse=False, info=info)
191
-
192
- # offload model, load autoencoder to gpu
193
- if self.offload:
194
- self.model.cpu()
195
- torch.cuda.empty_cache()
196
- self.ae.decoder.to(x.device)
197
-
198
- # decode latents to pixel space
199
- x = unpack(x[0].float(), height, width)
200
- with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
201
- x = self.ae.decode(x)
202
-
203
- if self.offload:
204
- self.ae.decoder.cpu()
205
- torch.cuda.empty_cache()
206
-
207
- t1 = time.perf_counter()
208
-
209
- print(f"Done in {t1 - t0:.1f}s.")
210
- # bring into PIL format
211
- x = x.clamp(-1, 1)
212
- x = embed_watermark(x.float())
213
- x = rearrange(x[0], "c h w -> h w c")
214
-
215
- img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
216
-
217
- filename = os.path.join(self.output_dir,f"round_0000_[{prompt}].jpg")
218
- os.makedirs(os.path.dirname(filename), exist_ok=True)
219
- exif_data = Image.Exif()
220
- if init_image is None:
221
- exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
222
- else:
223
- exif_data[ExifTags.Base.Software] = "AI generated;img2img;flux"
224
- exif_data[ExifTags.Base.Make] = "Black Forest Labs"
225
- exif_data[ExifTags.Base.Model] = self.name
226
- if add_sampling_metadata:
227
- exif_data[ExifTags.Base.ImageDescription] = prompt
228
- img.save(filename, format="jpeg", exif=exif_data, quality=95, subsampling=0)
229
- self.instructions = [prompt]
230
-
231
- #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
232
- img_and_prompt = []
233
- history_imgs = sorted(os.listdir(self.output_dir))
234
- for img_file, prompt_txt in zip(history_imgs, self.instructions):
235
- img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
236
- history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
237
- return img, history_gallery
238
-
239
-
240
- @spaces.GPU(duration=120)
241
- @torch.inference_mode()
242
- 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):
243
-
244
- torch.cuda.empty_cache()
245
- seed = None
246
-
247
- if self.offload:
248
- self.model.cpu()
249
- torch.cuda.empty_cache()
250
- self.ae.encoder.to(self.device)
251
-
252
- #----------------------------- 0.1 prepare multi-turn editing -------------------------------------#
253
- info = {}
254
- shape = init_image.shape
255
- new_h = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
256
- new_w = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
257
-
258
- if not any("round_0000" in fname for fname in os.listdir(self.output_dir)):
259
- Image.fromarray(init_image).save(os.path.join(self.output_dir,"round_0000_[source].jpg"))
260
-
261
-
262
- init_image = init_image[:new_h, :new_w, :]
263
- width, height = init_image.shape[0], init_image.shape[1]
264
- init_image = encode(init_image, self.device, self.ae)
265
-
266
- print(init_image.shape)
267
-
268
- if init_image_2 is None:
269
- print("init_image_2 is not provided, proceeding with single image processing.")
270
- else:
271
- init_image_2_pil = Image.fromarray(init_image_2) # Convert NumPy array to PIL Image
272
- init_image_2_pil = init_image_2_pil.resize((new_w, new_h), Image.Resampling.LANCZOS)
273
- init_image_2 = np.array(init_image_2_pil) # Convert back to NumPy (if needed)
274
- init_image_2 = encode(init_image_2, self.device, self.ae)
275
-
276
- rng = torch.Generator(device="cpu")
277
- opts = SamplingOptions(
278
- source_prompt=source_prompt,
279
- target_prompt=target_prompt,
280
- width=width,
281
- height=height,
282
- num_steps=num_steps,
283
- guidance=guidance,
284
- seed=seed,
285
- )
286
- if opts.seed is None:
287
- opts.seed = torch.Generator(device="cpu").seed()
288
-
289
- print(f"Editing with prompt:\n{opts.source_prompt}")
290
- t0 = time.perf_counter()
291
-
292
- opts.seed = None
293
- if self.offload:
294
- self.ae = self.ae.cpu()
295
- torch.cuda.empty_cache()
296
- self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
297
-
298
- #----------------------------- 0.2 prepare attention strategy -------------------------------------#
299
- info = {}
300
- info['feature'] = {}
301
- info['inject_step'] = inject_step
302
- info['editing_strategy']= " ".join(editing_strategy)
303
- info['start_layer_index'] = 0
304
- info['end_layer_index'] = 37
305
- info['reuse_v']= False
306
- qkv_ratio = '1.0,1.0,1.0'
307
- info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
308
- info['attn_guidance'] = attn_guidance_start_block
309
- info['lqr_stop'] = 0.25
310
-
311
- if not os.path.exists(self.feature_path):
312
- os.mkdir(self.feature_path)
313
-
314
-
315
- #----------------------------- 0.3 prepare latents -------------------------------------#
316
- with torch.no_grad():
317
- inp = prepare(self.t5, self.clip, init_image, prompt=opts.source_prompt)
318
- inp_target = prepare(self.t5, self.clip, init_image, prompt=opts.target_prompt)
319
- if self.source_image is None:
320
- self.source_image = inp['img']
321
- inp_target_2 = None
322
- if not init_image_2 is None:
323
- inp_target_2 = prepare_image(init_image_2)
324
- info['lqr_stop'] = 0.35
325
-
326
- timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=(self.name != "flux-schnell"))
327
- #timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=False)
328
-
329
- # offload TEs to CPU, load model to gpu
330
- if self.offload:
331
- self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
332
- torch.cuda.empty_cache()
333
- self.model = self.model.to(self.device)
334
-
335
-
336
-
337
- #----------------------------- 1 Inverting current image -------------------------------------#
338
- denoise_strategies = ['fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion', 'multi_turn_consistent']
339
- denoise_funcs = [denoise_fireflow, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent]
340
- denoise_func = denoise_funcs[denoise_strategies.index(denoise_strategy)]
341
- with torch.no_grad():
342
- z, info = denoise_func(self.model, **inp, timesteps=timesteps, guidance=1, inverse=True, info=info)
343
-
344
-
345
-
346
-
347
- #----------------------------- 2 history_tensors used to implement dual-LQR guiding editing -------------------------------------#
348
- inp_target["img"] = z
349
- timesteps = get_schedule(opts.num_steps, inp_target["img"].shape[1], shift=(self.name != "flux-schnell"))
350
-
351
- if torch.all(self.history_tensors['source img'] == 0):
352
- self.history_tensors = {
353
- "source img": inp["img"],
354
- "prev img": inp_target_2}
355
- else:
356
- if inp_target_2 is None:
357
- self.history_tensors["prev img"] = inp["img"]
358
- else:
359
- self.history_tensors["source img"] = inp["img"]
360
- self.history_tensors["prev img"] = inp_target_2
361
-
362
- #----------------------------- 3 sampling -------------------------------------#
363
- if denoise_strategy in ['rf_inversion', 'multi_turn_consistent']:
364
- x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=guidance, inverse=False, info=info, img_LQR=self.history_tensors)
365
- else:
366
- x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=opts.guidance, inverse=False, info=info)
367
-
368
-
369
- #----------------------------- 4 update history_tensors -------------------------------------#
370
- info = {}
371
- self.history_tensors["source img"] = self.source_image
372
- self.history_tensors["prev img"] = x
373
- '''save_file(history_tensors, "history_gradio/history.safetensors")'''
374
-
375
- # offload model, load autoencoder to gpu
376
- if self.offload:
377
- self.model.cpu()
378
- torch.cuda.empty_cache()
379
- self.ae.decoder.to(x.device)
380
-
381
-
382
-
383
- #----------------------------- 5 decode x to image -------------------------------------#
384
- x = unpack(x.float(), opts.width, opts.height)
385
-
386
- with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
387
- x = self.ae.decode(x)
388
-
389
- if torch.cuda.is_available():
390
- torch.cuda.synchronize()
391
- t1 = time.perf_counter()
392
-
393
- # bring into PIL format and save
394
- x = x.clamp(-1, 1)
395
- x = embed_watermark(x.float())
396
- x = rearrange(x[0], "c h w -> h w c")
397
-
398
- img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
399
- exif_data = Image.Exif()
400
- exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
401
- exif_data[ExifTags.Base.Make] = "Black Forest Labs"
402
- exif_data[ExifTags.Base.Model] = self.name
403
- if self.add_sampling_metadata:
404
- exif_data[ExifTags.Base.ImageDescription] = source_prompt
405
-
406
-
407
-
408
- #-------------------------------- 6 save image -------------------------------------#
409
-
410
- #-------------------- 6.1 prepare output folder ----------------------#
411
- if not os.path.exists(self.output_dir):
412
- os.makedirs(self.output_dir)
413
- idx = 1
414
- #-------------------- 6.2 editing round ----------------------#
415
- else:
416
- fns = [fn for fn in os.listdir(self.output_dir)]
417
- if len(fns) > 0:
418
- idx = max(int(fn.split("_")[1]) for fn in fns) + 1
419
- else:
420
- idx = 1
421
- formatted_idx = str(idx).zfill(4) # Format as a 4-digit string
422
-
423
- #-------------------- 6.3 output name ----------------------#
424
- if denoise_strategy == 'multi_turn_consistent':
425
- denoise_strategy = 'MTC'
426
- if target_prompt == '':
427
- target_prompt = 'Reconstruction'
428
- if target_prompt == source_prompt:
429
- target_prompt = 'Reconstruction: ' + target_prompt
430
-
431
- target_suffix = " ".join(target_prompt.split()[-5:])
432
- output_name = f"round_{formatted_idx}_{target_suffix}_{denoise_strategy}.jpg"
433
-
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
  if __name__ == "__main__":
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
+ @dataclass
25
+ class SamplingOptions:
26
+ source_prompt: str
27
+ target_prompt: str
28
+ # prompt: str
29
+ width: int
30
+ height: int
31
+ num_steps: int
32
+ guidance: float
33
+ seed: int | None
34
+
35
+ @torch.inference_mode()
36
+ def encode(init_image, torch_device, ae):
37
+ init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 127.5 - 1
38
+ init_image = init_image.unsqueeze(0)
39
+ init_image = init_image.to(torch_device)
40
+ with torch.no_grad():
41
+ init_image = ae.encode(init_image.to()).to(torch.bfloat16)
42
+ return init_image
43
+
44
+
45
+ class FluxEditor:
46
+ def __init__(self, args):
47
+ self.args = args
48
+ self.device = torch.device(args.device)
49
+ self.offload = args.offload
50
+ self.name = args.name
51
+ self.is_schnell = args.name == "flux-schnell"
52
+
53
+ self.feature_path = 'feature'
54
+
55
+ self.reset()
56
+
57
+ self.add_sampling_metadata = True
58
+
59
+ if self.name not in configs:
60
+ available = ", ".join(configs.keys())
61
+ raise ValueError(f"Got unknown model name: {self.name}, chose from {available}")
62
+
63
+ # init all components
64
+ self.clip = load_clip(self.device)
65
+ self.t5 = load_t5(self.device, max_length=256 if self.name == "flux-schnell" else 512)
66
+ self.model = load_flow_model(self.name, device="cpu" if self.offload else self.device)
67
+ self.ae = load_ae(self.name, device="cpu" if self.offload else self.device)
68
+ self.t5.eval()
69
+ self.clip.eval()
70
+ self.ae.eval()
71
+ self.model.eval()
72
+
73
+ # clear history
74
+ if os.path.exists("history_gradio/history.safetensors"):
75
+ os.remove("history_gradio/history.safetensors")
76
+
77
+
78
+ @torch.inference_mode()
79
+ def reset(self):
80
+ out_root = 'src/gradio_utils/gradio_outputs'
81
+ name_dir = f'exp_{len(os.listdir(out_root))}'
82
+ self.output_dir = os.path.join(out_root, name_dir)
83
+ if not os.path.exists(self.output_dir):
84
+ os.makedirs(self.output_dir)
85
+ self.instructions = ['source']
86
+ self.source_image = None
87
+ self.history_tensors = {
88
+ "source img": torch.zeros((1, 1, 1)),
89
+ "prev img": torch.zeros((1, 1, 1))}
90
+
91
+ source_prompt = "(Optional) Describe the content of the uploaded image."
92
+ traget_prompt = "(Required) Describe the desired content of the edited image."
93
+ gallery = None
94
+ output_image = None
95
+ return source_prompt, traget_prompt, gallery, output_image
96
+
97
+
98
+ @torch.inference_mode()
99
+ def process_image(self,
100
+ init_image,
101
+ source_prompt,
102
+ target_prompt,
103
+ editing_strategy,
104
+ denoise_strategy,
105
+ num_steps,
106
+ guidance,
107
+ attn_guidance_start_block,
108
+ inject_step,
109
+ init_image_2=None):
110
+ if init_image is None:
111
+ img, gr_gallery = self.generate_image(prompt=target_prompt)
112
+ else:
113
+ 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)
114
+ return img, gr_gallery
115
+
116
+
117
+ @spaces.GPU(duration=120)
118
+ @torch.inference_mode()
119
+ def generate_image(
120
+ self,
121
+ width=512,
122
+ height=512,
123
+ num_steps=28,
124
+ guidance=3.5,
125
+ seed=None,
126
+ prompt='',
127
+ init_image=None,
128
+ image2image_strength=0.0,
129
+ add_sampling_metadata=True,
130
+ ):
131
+
132
+ if seed is None:
133
+ g_seed = torch.Generator(device="cpu").seed()
134
+ print(f"Generating '{prompt}' with seed {g_seed}")
135
+ t0 = time.perf_counter()
136
+
137
+ if init_image is not None:
138
+ if isinstance(init_image, np.ndarray):
139
+ init_image = torch.from_numpy(init_image).permute(2, 0, 1).float() / 255.0
140
+ init_image = init_image.unsqueeze(0)
141
+ init_image = init_image.to(self.device)
142
+ init_image = torch.nn.functional.interpolate(init_image, (height, width))
143
+ if self.offload:
144
+ self.ae.encoder.to(self.device)
145
+ init_image = self.ae.encode(init_image.to())
146
+ if self.offload:
147
+ self.ae = self.ae.cpu()
148
+ torch.cuda.empty_cache()
149
+
150
+ # prepare input
151
+ x = get_noise(
152
+ 1,
153
+ height,
154
+ width,
155
+ device=self.device,
156
+ dtype=torch.bfloat16,
157
+ seed=g_seed,
158
+ )
159
+ timesteps = get_schedule(
160
+ num_steps,
161
+ x.shape[-1] * x.shape[-2] // 4,
162
+ shift=(not self.is_schnell),
163
+ )
164
+ if init_image is not None:
165
+ t_idx = int((1 - image2image_strength) * num_steps)
166
+ t = timesteps[t_idx]
167
+ timesteps = timesteps[t_idx:]
168
+ x = t * x + (1.0 - t) * init_image.to(x.dtype)
169
+
170
+ if self.offload:
171
+ self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
172
+ inp = prepare(t5=self.t5, clip=self.clip, img=x, prompt=prompt)
173
+
174
+ # offload TEs to CPU, load model to gpu
175
+ if self.offload:
176
+ self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
177
+ torch.cuda.empty_cache()
178
+ self.model = self.model.to(self.device)
179
+
180
+ # denoise initial noise
181
+ info = {}
182
+ info['feature'] = {}
183
+ info['inject_step'] = 0
184
+ info['editing_strategy']= ""
185
+ info['start_layer_index'] = 0
186
+ info['end_layer_index'] = 37
187
+ info['reuse_v']= False
188
+ qkv_ratio = '1.0,1.0,1.0'
189
+ info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
190
+ x = denoise_rf(self.model, **inp, timesteps=timesteps, guidance=guidance, inverse=False, info=info)
191
+
192
+ # offload model, load autoencoder to gpu
193
+ if self.offload:
194
+ self.model.cpu()
195
+ torch.cuda.empty_cache()
196
+ self.ae.decoder.to(x.device)
197
+
198
+ # decode latents to pixel space
199
+ x = unpack(x[0].float(), height, width)
200
+ with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
201
+ x = self.ae.decode(x)
202
+
203
+ if self.offload:
204
+ self.ae.decoder.cpu()
205
+ torch.cuda.empty_cache()
206
+
207
+ t1 = time.perf_counter()
208
+
209
+ print(f"Done in {t1 - t0:.1f}s.")
210
+ # bring into PIL format
211
+ x = x.clamp(-1, 1)
212
+ x = embed_watermark(x.float())
213
+ x = rearrange(x[0], "c h w -> h w c")
214
+
215
+ img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
216
+
217
+ filename = os.path.join(self.output_dir,f"round_0000_[{prompt}].jpg")
218
+ os.makedirs(os.path.dirname(filename), exist_ok=True)
219
+ exif_data = Image.Exif()
220
+ if init_image is None:
221
+ exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
222
+ else:
223
+ exif_data[ExifTags.Base.Software] = "AI generated;img2img;flux"
224
+ exif_data[ExifTags.Base.Make] = "Black Forest Labs"
225
+ exif_data[ExifTags.Base.Model] = self.name
226
+ if add_sampling_metadata:
227
+ exif_data[ExifTags.Base.ImageDescription] = prompt
228
+ img.save(filename, format="jpeg", exif=exif_data, quality=95, subsampling=0)
229
+ self.instructions = [prompt]
230
+
231
+ #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
232
+ img_and_prompt = []
233
+ history_imgs = sorted(os.listdir(self.output_dir))
234
+ for img_file, prompt_txt in zip(history_imgs, self.instructions):
235
+ img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
236
+ history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
237
+ return img, history_gallery
238
+
239
+
240
+ @spaces.GPU(duration=120)
241
+ @torch.inference_mode()
242
+ 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):
243
+
244
+ torch.cuda.empty_cache()
245
+ seed = None
246
+
247
+ if self.offload:
248
+ self.model.cpu()
249
+ torch.cuda.empty_cache()
250
+ self.ae.encoder.to(self.device)
251
+
252
+ #----------------------------- 0.1 prepare multi-turn editing -------------------------------------#
253
+ info = {}
254
+ shape = init_image.shape
255
+ new_h = shape[0] if shape[0] % 16 == 0 else shape[0] - shape[0] % 16
256
+ new_w = shape[1] if shape[1] % 16 == 0 else shape[1] - shape[1] % 16
257
+
258
+ if not any("round_0000" in fname for fname in os.listdir(self.output_dir)):
259
+ Image.fromarray(init_image).save(os.path.join(self.output_dir,"round_0000_[source].jpg"))
260
+
261
+
262
+ init_image = init_image[:new_h, :new_w, :]
263
+ width, height = init_image.shape[0], init_image.shape[1]
264
+ init_image = encode(init_image, self.device, self.ae)
265
+
266
+ print(init_image.shape)
267
+
268
+ if init_image_2 is None:
269
+ print("init_image_2 is not provided, proceeding with single image processing.")
270
+ else:
271
+ init_image_2_pil = Image.fromarray(init_image_2) # Convert NumPy array to PIL Image
272
+ init_image_2_pil = init_image_2_pil.resize((new_w, new_h), Image.Resampling.LANCZOS)
273
+ init_image_2 = np.array(init_image_2_pil) # Convert back to NumPy (if needed)
274
+ init_image_2 = encode(init_image_2, self.device, self.ae)
275
+
276
+ rng = torch.Generator(device="cpu")
277
+ opts = SamplingOptions(
278
+ source_prompt=source_prompt,
279
+ target_prompt=target_prompt,
280
+ width=width,
281
+ height=height,
282
+ num_steps=num_steps,
283
+ guidance=guidance,
284
+ seed=seed,
285
+ )
286
+ if opts.seed is None:
287
+ opts.seed = torch.Generator(device="cpu").seed()
288
+
289
+ print(f"Editing with prompt:\n{opts.source_prompt}")
290
+ t0 = time.perf_counter()
291
+
292
+ opts.seed = None
293
+ if self.offload:
294
+ self.ae = self.ae.cpu()
295
+ torch.cuda.empty_cache()
296
+ self.t5, self.clip = self.t5.to(self.device), self.clip.to(self.device)
297
+
298
+ #----------------------------- 0.2 prepare attention strategy -------------------------------------#
299
+ info = {}
300
+ info['feature'] = {}
301
+ info['inject_step'] = inject_step
302
+ info['editing_strategy']= " ".join(editing_strategy)
303
+ info['start_layer_index'] = 0
304
+ info['end_layer_index'] = 37
305
+ info['reuse_v']= False
306
+ qkv_ratio = '1.0,1.0,1.0'
307
+ info['qkv_ratio'] = list(map(float, qkv_ratio.split(',')))
308
+ info['attn_guidance'] = attn_guidance_start_block
309
+ info['lqr_stop'] = 0.25
310
+
311
+ if not os.path.exists(self.feature_path):
312
+ os.mkdir(self.feature_path)
313
+
314
+
315
+ #----------------------------- 0.3 prepare latents -------------------------------------#
316
+ with torch.no_grad():
317
+ inp = prepare(self.t5, self.clip, init_image, prompt=opts.source_prompt)
318
+ inp_target = prepare(self.t5, self.clip, init_image, prompt=opts.target_prompt)
319
+ if self.source_image is None:
320
+ self.source_image = inp['img']
321
+ inp_target_2 = None
322
+ if not init_image_2 is None:
323
+ inp_target_2 = prepare_image(init_image_2)
324
+ info['lqr_stop'] = 0.35
325
+
326
+ timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=(self.name != "flux-schnell"))
327
+ #timesteps = get_schedule(opts.num_steps, inp["img"].shape[1], shift=False)
328
+
329
+ # offload TEs to CPU, load model to gpu
330
+ if self.offload:
331
+ self.t5, self.clip = self.t5.cpu(), self.clip.cpu()
332
+ torch.cuda.empty_cache()
333
+ self.model = self.model.to(self.device)
334
+
335
+
336
+
337
+ #----------------------------- 1 Inverting current image -------------------------------------#
338
+ denoise_strategies = ['fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion', 'multi_turn_consistent']
339
+ denoise_funcs = [denoise_fireflow, denoise_rf, denoise_rf_solver, denoise_midpoint, denoise_rf_inversion, denoise_multi_turn_consistent]
340
+ denoise_func = denoise_funcs[denoise_strategies.index(denoise_strategy)]
341
+ with torch.no_grad():
342
+ z, info = denoise_func(self.model, **inp, timesteps=timesteps, guidance=1, inverse=True, info=info)
343
+
344
+
345
+
346
+
347
+ #----------------------------- 2 history_tensors used to implement dual-LQR guiding editing -------------------------------------#
348
+ inp_target["img"] = z
349
+ timesteps = get_schedule(opts.num_steps, inp_target["img"].shape[1], shift=(self.name != "flux-schnell"))
350
+
351
+ if torch.all(self.history_tensors['source img'] == 0):
352
+ self.history_tensors = {
353
+ "source img": inp["img"],
354
+ "prev img": inp_target_2}
355
+ else:
356
+ if inp_target_2 is None:
357
+ self.history_tensors["prev img"] = inp["img"]
358
+ else:
359
+ self.history_tensors["source img"] = inp["img"]
360
+ self.history_tensors["prev img"] = inp_target_2
361
+
362
+ #----------------------------- 3 sampling -------------------------------------#
363
+ if denoise_strategy in ['rf_inversion', 'multi_turn_consistent']:
364
+ x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=guidance, inverse=False, info=info, img_LQR=self.history_tensors)
365
+ else:
366
+ x, _ = denoise_func(self.model, **inp_target, timesteps=timesteps, guidance=opts.guidance, inverse=False, info=info)
367
+
368
+
369
+ #----------------------------- 4 update history_tensors -------------------------------------#
370
+ info = {}
371
+ self.history_tensors["source img"] = self.source_image
372
+ self.history_tensors["prev img"] = x
373
+ '''save_file(history_tensors, "history_gradio/history.safetensors")'''
374
+
375
+ # offload model, load autoencoder to gpu
376
+ if self.offload:
377
+ self.model.cpu()
378
+ torch.cuda.empty_cache()
379
+ self.ae.decoder.to(x.device)
380
+
381
+
382
+
383
+ #----------------------------- 5 decode x to image -------------------------------------#
384
+ x = unpack(x.float(), opts.width, opts.height)
385
+
386
+ with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
387
+ x = self.ae.decode(x)
388
+
389
+ if torch.cuda.is_available():
390
+ torch.cuda.synchronize()
391
+ t1 = time.perf_counter()
392
+
393
+ # bring into PIL format and save
394
+ x = x.clamp(-1, 1)
395
+ x = embed_watermark(x.float())
396
+ x = rearrange(x[0], "c h w -> h w c")
397
+
398
+ img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
399
+ exif_data = Image.Exif()
400
+ exif_data[ExifTags.Base.Software] = "AI generated;txt2img;flux"
401
+ exif_data[ExifTags.Base.Make] = "Black Forest Labs"
402
+ exif_data[ExifTags.Base.Model] = self.name
403
+ if self.add_sampling_metadata:
404
+ exif_data[ExifTags.Base.ImageDescription] = source_prompt
405
+
406
+
407
+
408
+ #-------------------------------- 6 save image -------------------------------------#
409
+
410
+ #-------------------- 6.1 prepare output folder ----------------------#
411
+ if not os.path.exists(self.output_dir):
412
+ os.makedirs(self.output_dir)
413
+ idx = 1
414
+ #-------------------- 6.2 editing round ----------------------#
415
+ else:
416
+ fns = [fn for fn in os.listdir(self.output_dir)]
417
+ if len(fns) > 0:
418
+ idx = max(int(fn.split("_")[1]) for fn in fns) + 1
419
+ else:
420
+ idx = 1
421
+ formatted_idx = str(idx).zfill(4) # Format as a 4-digit string
422
+
423
+ #-------------------- 6.3 output name ----------------------#
424
+ if denoise_strategy == 'multi_turn_consistent':
425
+ denoise_strategy = 'MTC'
426
+ if target_prompt == '':
427
+ target_prompt = 'Reconstruction'
428
+ if target_prompt == source_prompt:
429
+ target_prompt = 'Reconstruction: ' + target_prompt
430
+
431
+ target_suffix = " ".join(target_prompt.split()[-5:])
432
+ output_name = f"round_{formatted_idx}_{target_suffix}_{denoise_strategy}.jpg"
433
+
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
+ if __name__ == "__main__":
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)
app_test.py DELETED
@@ -1,37 +0,0 @@
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)