ZhouwqZJ commited on
Commit
efe5b18
·
1 Parent(s): 6fb12e9

modified: app.py

Browse files
Files changed (4) hide show
  1. app.py +541 -33
  2. app_2.py +0 -547
  3. app_test.py +37 -0
  4. src/flux/util.py +4 -4
app.py CHANGED
@@ -1,37 +1,545 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
 
 
 
 
 
 
 
 
 
 
 
 
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
+ output_name = f"round_{formatted_idx}_[{" ".join(target_prompt.split()[-5:])}]_{denoise_strategy}.jpg"
432
+ fn = os.path.join(self.output_dir, output_name)
433
+
434
+ print(f"Done in {t1 - t0:.1f}s. Saving {fn}")
435
+ img.save(fn)
436
+
437
+ if 'Reconstruction' in target_prompt:
438
+ target_prompt = source_prompt
439
+ self.instructions.append(target_prompt)
440
+ print("End Edit")
441
+
442
+ #-------------------- 6.4 save editing prompt, update gradio component: gallery ----------------------#
443
+ img_and_prompt = []
444
+ history_imgs = sorted(os.listdir(self.output_dir))
445
+ for img_file, prompt_txt in zip(history_imgs, self.instructions):
446
+ img_and_prompt.append((os.path.join(self.output_dir, img_file), prompt_txt))
447
+ history_gallery = gr.Gallery(value=img_and_prompt, label="History Image", interactive=True, columns=3)
448
+
449
+ return img, history_gallery
450
+
451
+
452
+ def on_select(gallery, selected: gr.SelectData):
453
+ return gallery[selected.index][0], gallery[selected.index][1]
454
+
455
+ def on_upload(path, uploaded: gr.EventData):
456
+ return path[0][0]
457
+
458
+ def on_change(init_image, changed: gr.EventData):
459
+ img_path = list(changed.target.temp_files)
460
+ return gr.Gallery(value=[(img_path[0], "")], label="History Image", interactive=True, columns=3)
461
+
462
+ def create_demo(model_name: str, device: str = "cuda" if torch.cuda.is_available() else "cpu", offload: bool = False):
463
+ editor = FluxEditor(args)
464
+ is_schnell = model_name == "flux-schnell"
465
+
466
+ # Pre-defined examples
467
+ examples = [
468
+ ["src/gradio_utils/gradio_examples/000000000011.jpg", "", "a photo of a eagle standing on the branch", ['attn_guidance'], 15, 3.5, 11, 0],
469
+ ["src/gradio_utils/gradio_examples/221000000002.jpg", "", "a cat wearing a hat standing on the fence", ['attn_guidance'], 15, 3.5, 11, 0],
470
+ ]
471
+
472
+ with gr.Blocks() as demo:
473
+ gr.Markdown(f"# Multi-turn Consistent Image Editing (FLUX.1-dev)")
474
+
475
+ with gr.Row():
476
+ with gr.Column():
477
+ source_prompt = gr.Textbox(label="Source Prompt", value="(Optional) Describe the content of the uploaded image.")
478
+ target_prompt = gr.Textbox(label="Target Prompt", value="(Required) Describe the desired content of the edited image.")
479
+ with gr.Row():
480
+ init_image = gr.Image(label="Initial Image", visible=False, width=200)
481
+ init_image_2 = gr.Image(label="Input Image 2", visible=False, width=200)
482
+ gallery = gr.Gallery(label ="History Image", interactive=True, columns=3)
483
+ editing_strategy = gr.CheckboxGroup(
484
+ label="Editing Technique",
485
+ choices=['attn_guidance', 'replace_v', 'add_q', 'add_k', 'add_v', 'replace_q', 'replace_k'],
486
+ value=['attn_guidance'], # Default: none selected
487
+ interactive=True
488
+ )
489
+ denoise_strategy = gr.Dropdown(
490
+ ['multi_turn_consistent', 'fireflow', 'rf', 'rf_solver', 'midpoint', 'rf_inversion'],
491
+ label="Denoising Technique", value='multi_turn_consistent')
492
+ generate_btn = gr.Button("Generate")
493
+
494
+ with gr.Column():
495
+ with gr.Accordion("Advanced Options", open=True):
496
+ num_steps = gr.Slider(1, 30, 15, step=1, label="Number of steps")
497
+ guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Text Guidance", interactive=not is_schnell)
498
+ attn_guidance_start_block = gr.Slider(0, 18, 11, step=1, label="Top activated attn-maps", interactive=not is_schnell)
499
+ inject_step = gr.Slider(0, 15, 1, step=1, label="Number of inject steps")
500
+ output_image = gr.Image(label="Generated/Edited Image")
501
+ reset_btn = gr.Button("Reset")
502
+
503
+ gallery.select(on_select, gallery, [init_image, source_prompt])
504
+ gallery.upload(on_upload, gallery, init_image)
505
+ init_image.change(on_change, init_image, gallery)
506
+
507
+ generate_btn.click(
508
+ fn=editor.process_image,
509
+ inputs=[init_image, source_prompt, target_prompt, editing_strategy, denoise_strategy, num_steps, guidance, attn_guidance_start_block, inject_step, init_image_2],
510
+ outputs=[output_image, gallery]
511
+ )
512
+ reset_btn.click(fn = editor.reset, outputs=[source_prompt, target_prompt, gallery, output_image])
513
+
514
+ # Add examples
515
+ gr.Examples(
516
+ examples=examples,
517
+ inputs=[
518
+ init_image,
519
+ source_prompt,
520
+ target_prompt,
521
+ editing_strategy,
522
+ num_steps,
523
+ guidance,
524
+ attn_guidance_start_block,
525
+ inject_step
526
+ ]
527
+ )
528
+
529
+
530
+ return demo
531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532
 
533
  if __name__ == "__main__":
534
+ import argparse
535
+ parser = argparse.ArgumentParser(description="Flux")
536
+ parser.add_argument("--name", type=str, default="flux-dev", choices=list(configs.keys()), help="Model name")
537
+ parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu", help="Device to use")
538
+ parser.add_argument("--offload", action="store_true", help="Offload model to CPU when not in use")
539
+ parser.add_argument("--share", action="store_true", help="Create a public link to your demo")
540
+ parser.add_argument("--port", type=int, default=9090)
541
+ args = parser.parse_args()
542
+
543
+ demo = create_demo(args.name, args.device, args.offload)
544
+ #demo.launch(server_name='0.0.0.0', share=args.share, server_port=args.port)
545
+ demo.launch(share=True)
app_2.py DELETED
@@ -1,547 +0,0 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_test.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
src/flux/util.py CHANGED
@@ -114,8 +114,8 @@ def load_flow_model(name: str, device: str | torch.device = "cuda", hf_download:
114
  and configs[name].repo_flow is not None
115
  and hf_download
116
  ):
117
- #ckpt_path = hf_hub_download(configs[name].repo_id, configs[name].repo_flow)
118
- ckpt_path = "/homedata/HuggingFace/black-forest-labs/FLUX.1-dev/flux1-dev.safetensors"
119
 
120
  with torch.device("meta" if ckpt_path is not None else device):
121
  model = Flux(configs[name].params).to(torch.bfloat16)
@@ -147,8 +147,8 @@ def load_ae(name: str, device: str | torch.device = "cuda", hf_download: bool =
147
  and configs[name].repo_ae is not None
148
  and hf_download
149
  ):
150
- #ckpt_path = hf_hub_download(configs[name].repo_id, configs[name].repo_ae)
151
- ckpt_path = "/homedata/HuggingFace/black-forest-labs/FLUX.1-dev/ae.safetensors"
152
 
153
  # Loading the autoencoder
154
  print("Init AE")
 
114
  and configs[name].repo_flow is not None
115
  and hf_download
116
  ):
117
+ ckpt_path = hf_hub_download(configs[name].repo_id, configs[name].repo_flow)
118
+ #ckpt_path = "/homedata/HuggingFace/black-forest-labs/FLUX.1-dev/flux1-dev.safetensors"
119
 
120
  with torch.device("meta" if ckpt_path is not None else device):
121
  model = Flux(configs[name].params).to(torch.bfloat16)
 
147
  and configs[name].repo_ae is not None
148
  and hf_download
149
  ):
150
+ ckpt_path = hf_hub_download(configs[name].repo_id, configs[name].repo_ae)
151
+ #ckpt_path = "/homedata/HuggingFace/black-forest-labs/FLUX.1-dev/ae.safetensors"
152
 
153
  # Loading the autoencoder
154
  print("Init AE")