prithivMLmods commited on
Commit
92668fa
·
verified ·
1 Parent(s): 99377c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -506
app.py CHANGED
@@ -1,531 +1,166 @@
1
- import spaces
2
  import gradio as gr
 
 
3
  import torch
4
- from PIL import Image
5
- from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
6
  import random
7
- import uuid
8
- from typing import Tuple, Union, List, Optional, Any, Dict
9
- import numpy as np
10
- import time
11
- import zipfile
12
- from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
13
-
14
- # Description for the app
15
- DESCRIPTION = """## flux comparator hpc/."""
16
 
17
- # Helper functions
18
- def save_image(img):
19
- unique_name = str(uuid.uuid4()) + ".png"
20
- img.save(unique_name)
21
- return unique_name
22
-
23
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
24
- if randomize_seed:
25
- seed = random.randint(0, MAX_SEED)
26
- return seed
27
 
28
  MAX_SEED = np.iinfo(np.int32).max
29
- MAX_IMAGE_SIZE = 2048
30
-
31
- # Load pipelines for both models
32
- # Flux.1-dev-realism
33
- base_model_dev = "prithivMLmods/Flux.1-Merged" # Merge of (black-forest-labs/FLUX.1-dev + black-forest-labs/FLUX.1-schnell)
34
- pipe_dev = DiffusionPipeline.from_pretrained(base_model_dev, torch_dtype=torch.bfloat16)
35
- lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
36
- trigger_word = "Super Realism"
37
- pipe_dev.load_lora_weights(lora_repo)
38
- pipe_dev.to("cuda")
39
-
40
- # Flux.1-krea
41
- dtype = torch.bfloat16
42
- device = "cuda" if torch.cuda.is_available() else "cpu"
43
- taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
44
- # Merge of (black-forest-labs/FLUX.1-dev + https://huggingface.co/black-forest-labs/FLUX.1-Krea-dev)
45
- good_vae = AutoencoderKL.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", subfolder="vae", torch_dtype=dtype).to(device)
46
- pipe_krea = DiffusionPipeline.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", torch_dtype=dtype, vae=taef1).to(device)
47
-
48
- # Define the flux_pipe_call_that_returns_an_iterable_of_images for flux.1-krea
49
- @torch.inference_mode()
50
- def flux_pipe_call_that_returns_an_iterable_of_images(
51
- self,
52
- prompt: Union[str, List[str]] = None,
53
- prompt_2: Optional[Union[str, List[str]]] = None,
54
- height: Optional[int] = None,
55
- width: Optional[int] = None,
56
- num_inference_steps: int = 28,
57
- timesteps: List[int] = None,
58
- guidance_scale: float = 3.5,
59
- num_images_per_prompt: Optional[int] = 1,
60
- generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
61
- latents: Optional[torch.FloatTensor] = None,
62
- prompt_embeds: Optional[torch.FloatTensor] = None,
63
- pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
64
- output_type: Optional[str] = "pil",
65
- return_dict: bool = True,
66
- joint_attention_kwargs: Optional[Dict[str, Any]] = None,
67
- max_sequence_length: int = 512,
68
- good_vae: Optional[Any] = None,
69
- ):
70
- height = height or self.default_sample_size * self.vae_scale_factor
71
- width = width or self.default_sample_size * self.vae_scale_factor
72
-
73
- self.check_inputs(
74
- prompt,
75
- prompt_2,
76
- height,
77
- width,
78
- prompt_embeds=prompt_embeds,
79
- pooled_prompt_embeds=pooled_prompt_embeds,
80
- max_sequence_length=max_sequence_length,
81
- )
82
-
83
- self._guidance_scale = guidance_scale
84
- self._joint_attention_kwargs = joint_attention_kwargs
85
- self._interrupt = False
86
-
87
- batch_size = 1 if isinstance(prompt, str) else len(prompt)
88
- device = self._execution_device
89
-
90
- lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None
91
- prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt(
92
- prompt=prompt,
93
- prompt_2=prompt_2,
94
- prompt_embeds=prompt_embeds,
95
- pooled_prompt_embeds=pooled_prompt_embeds,
96
- device=device,
97
- num_images_per_prompt=num_images_per_prompt,
98
- max_sequence_length=max_sequence_length,
99
- lora_scale=lora_scale,
100
- )
101
-
102
- num_channels_latents = self.transformer.config.in_channels // 4
103
- latents, latent_image_ids = self.prepare_latents(
104
- batch_size * num_images_per_prompt,
105
- num_channels_latents,
106
- height,
107
- width,
108
- prompt_embeds.dtype,
109
- device,
110
- generator,
111
- latents,
112
- )
113
 
114
- sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
115
- image_seq_len = latents.shape[1]
116
- mu = calculate_shift(
117
- image_seq_len,
118
- self.scheduler.config.base_image_seq_len,
119
- self.scheduler.config.max_image_seq_len,
120
- self.scheduler.config.base_shift,
121
- self.scheduler.config.max_shift,
122
- )
123
- timesteps, num_inference_steps = retrieve_timesteps(
124
- self.scheduler,
125
- num_inference_steps,
126
- device,
127
- timesteps,
128
- sigmas,
129
- mu=mu,
130
- )
131
- self._num_timesteps = len(timesteps)
132
-
133
- guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None
134
-
135
- for i, t in enumerate(timesteps):
136
- if self.interrupt:
137
- continue
138
-
139
- timestep = t.expand(latents.shape[0]).to(latents.dtype)
140
-
141
- noise_pred = self.transformer(
142
- hidden_states=latents,
143
- timestep=timestep / 1000,
144
- guidance=guidance,
145
- pooled_projections=pooled_prompt_embeds,
146
- encoder_hidden_states=prompt_embeds,
147
- txt_ids=text_ids,
148
- img_ids=latent_image_ids,
149
- joint_attention_kwargs=self.joint_attention_kwargs,
150
- return_dict=False,
151
- )[0]
152
-
153
- latents_for_image = self._unpack_latents(latents, height, width, self.vae_scale_factor)
154
- latents_for_image = (latents_for_image / self.vae.config.scaling_factor) + self.vae.config.shift_factor
155
- image = self.vae.decode(latents_for_image, return_dict=False)[0]
156
- yield self.image_processor.postprocess(image, output_type=output_type)[0]
157
-
158
- latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
159
- torch.cuda.empty_cache()
160
-
161
- latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
162
- latents = (latents / good_vae.config.scaling_factor) + good_vae.config.shift_factor
163
- image = good_vae.decode(latents, return_dict=False)[0]
164
- self.maybe_free_model_hooks()
165
- torch.cuda.empty_cache()
166
- yield self.image_processor.postprocess(image, output_type=output_type)[0]
167
-
168
- pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe_krea)
169
-
170
- # Helper functions for flux.1-krea
171
- def calculate_shift(
172
- image_seq_len,
173
- base_seq_len: int = 256,
174
- max_seq_len: int = 4096,
175
- base_shift: float = 0.5,
176
- max_shift: float = 1.16,
177
- ):
178
- m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
179
- b = base_shift - m * base_seq_len
180
- mu = image_seq_len * m + b
181
- return mu
182
 
183
- def retrieve_timesteps(
184
- scheduler,
185
- num_inference_steps: Optional[int] = None,
186
- device: Optional[Union[str, torch.device]] = None,
187
- timesteps: Optional[List[int]] = None,
188
- sigmas: Optional[List[float]] = None,
189
- **kwargs,
190
- ):
191
- if timesteps is not None and sigmas is not None:
192
- raise ValueError("Only one of `timesteps` or `sigmas` can be passed.")
193
- if timesteps is not None:
194
- scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
195
- timesteps = scheduler.timesteps
196
- num_inference_steps = len(timesteps)
197
- elif sigmas is not None:
198
- scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
199
- timesteps = scheduler.timesteps
200
- num_inference_steps = len(timesteps)
201
- else:
202
- scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
203
- timesteps = scheduler.timesteps
204
- return timesteps, num_inference_steps
205
-
206
- # Styles for flux.1-dev-realism
207
- style_list = [
208
- {"name": "3840 x 2160", "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
209
- {"name": "2560 x 1440", "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
210
- {"name": "HD+", "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
211
- {"name": "Style Zero", "prompt": "{prompt}", "negative_prompt": ""},
212
- ]
213
-
214
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
215
- DEFAULT_STYLE_NAME = "Style Zero"
216
- STYLE_NAMES = list(styles.keys())
217
-
218
- def apply_style(style_name: str, positive: str) -> Tuple[str, str]:
219
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
220
- return p.replace("{prompt}", positive), n
221
-
222
- # Generation function for flux.1-dev-realism
223
  @spaces.GPU
224
- def generate_dev(
225
- prompt: str,
226
- negative_prompt: str = "",
227
- use_negative_prompt: bool = False,
228
- seed: int = 0,
229
- width: int = 1024,
230
- height: int = 1024,
231
- guidance_scale: float = 3,
232
- randomize_seed: bool = False,
233
- style_name: str = DEFAULT_STYLE_NAME,
234
- num_inference_steps: int = 30,
235
- num_images: int = 1,
236
- zip_images: bool = False,
237
- progress=gr.Progress(track_tqdm=True),
238
- ):
239
- positive_prompt, style_negative_prompt = apply_style(style_name, prompt)
240
-
241
- if use_negative_prompt:
242
- final_negative_prompt = style_negative_prompt + " " + negative_prompt
243
- else:
244
- final_negative_prompt = style_negative_prompt
245
-
246
- final_negative_prompt = final_negative_prompt.strip()
247
-
248
- if trigger_word:
249
- positive_prompt = f"{trigger_word} {positive_prompt}"
250
-
251
- seed = int(randomize_seed_fn(seed, randomize_seed))
252
- generator = torch.Generator(device="cuda").manual_seed(seed)
253
-
254
- start_time = time.time()
255
-
256
- images = pipe_dev(
257
- prompt=positive_prompt,
258
- negative_prompt=final_negative_prompt if final_negative_prompt else None,
259
- width=width,
260
- height=height,
261
- guidance_scale=guidance_scale,
262
- num_inference_steps=num_inference_steps,
263
- num_images_per_prompt=num_images,
264
- generator=generator,
265
- output_type="pil",
266
- ).images
267
-
268
- end_time = time.time()
269
- duration = end_time - start_time
270
-
271
- image_paths = [save_image(img) for img in images]
272
-
273
- zip_path = None
274
- if zip_images:
275
- zip_name = str(uuid.uuid4()) + ".zip"
276
- with zipfile.ZipFile(zip_name, 'w') as zipf:
277
- for i, img_path in enumerate(image_paths):
278
- zipf.write(img_path, arcname=f"Img_{i}.png")
279
- zip_path = zip_name
280
-
281
- return image_paths, seed, f"{duration:.2f}", zip_path
282
-
283
- # Generation function for flux.1-krea
284
- @spaces.GPU
285
- def generate_krea(
286
- prompt: str,
287
- seed: int = 0,
288
- width: int = 1024,
289
- height: int = 1024,
290
- guidance_scale: float = 4.5,
291
- randomize_seed: bool = False,
292
- num_inference_steps: int = 28,
293
- num_images: int = 1,
294
- zip_images: bool = False,
295
- progress=gr.Progress(track_tqdm=True),
296
- ):
297
  if randomize_seed:
298
  seed = random.randint(0, MAX_SEED)
299
- generator = torch.Generator().manual_seed(seed)
300
-
301
- start_time = time.time()
302
 
303
- images = []
304
- for _ in range(num_images):
305
- final_img = list(pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images(
 
306
  prompt=prompt,
307
  guidance_scale=guidance_scale,
308
- num_inference_steps=num_inference_steps,
309
- width=width,
310
- height=height,
311
- generator=generator,
312
- output_type="pil",
313
- good_vae=good_vae,
314
- ))[-1] # Take the final image only
315
- images.append(final_img)
316
-
317
- end_time = time.time()
318
- duration = end_time - start_time
319
-
320
- image_paths = [save_image(img) for img in images]
321
-
322
- zip_path = None
323
- if zip_images:
324
- zip_name = str(uuid.uuid4()) + ".zip"
325
- with zipfile.ZipFile(zip_name, 'w') as zipf:
326
- for i, img_path in enumerate(image_paths):
327
- zipf.write(img_path, arcname=f"Img_{i}.png")
328
- zip_path = zip_name
329
-
330
- return image_paths, seed, f"{duration:.2f}", zip_path
331
-
332
- # Main generation function to handle model choice
333
- @spaces.GPU
334
- def generate(
335
- model_choice: str,
336
- prompt: str,
337
- negative_prompt: str = "",
338
- use_negative_prompt: bool = False,
339
- seed: int = 0,
340
- width: int = 1024,
341
- height: int = 1024,
342
- guidance_scale: float = 3,
343
- randomize_seed: bool = False,
344
- style_name: str = DEFAULT_STYLE_NAME,
345
- num_inference_steps: int = 30,
346
- num_images: int = 1,
347
- zip_images: bool = False,
348
- progress=gr.Progress(track_tqdm=True),
349
- ):
350
- if model_choice == "flux.1-dev-merged":
351
- return generate_dev(
352
- prompt=prompt,
353
- negative_prompt=negative_prompt,
354
- use_negative_prompt=use_negative_prompt,
355
- seed=seed,
356
- width=width,
357
- height=height,
358
- guidance_scale=guidance_scale,
359
- randomize_seed=randomize_seed,
360
- style_name=style_name,
361
- num_inference_steps=num_inference_steps,
362
- num_images=num_images,
363
- zip_images=zip_images,
364
- progress=progress,
365
- )
366
- elif model_choice == "flux.1-krea-merged-dev":
367
- return generate_krea(
368
  prompt=prompt,
369
- seed=seed,
370
- width=width,
371
- height=height,
372
  guidance_scale=guidance_scale,
373
- randomize_seed=randomize_seed,
374
- num_inference_steps=num_inference_steps,
375
- num_images=num_images,
376
- zip_images=zip_images,
377
- progress=progress,
378
- )
379
- else:
380
- raise ValueError("Invalid model choice")
381
 
382
- # Examples
383
- examples = [
384
- "An attractive young woman with blue eyes lying face down on the bed, in the style of animated gifs, light white and light amber, jagged edges, the snapshot aesthetic, timeless beauty, goosepunk, sunrays shine upon it --no freckles --chaos 65 --ar 1:2 --profile yruxpc2 --stylize 750 --v 6.1",
385
- "Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
386
- "Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights.",
387
- "High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250"
388
- ]
389
-
390
- css = '''
391
- .gradio-container {
392
- max-width: 590px !important;
393
- margin: 0 auto !important;
394
- }
395
- h1 {
396
- text-align: center;
397
- }
398
- footer {
399
- visibility: hidden;
400
  }
401
- '''
402
 
403
- # Gradio interface
404
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
405
- gr.Markdown(DESCRIPTION)
406
- with gr.Row():
407
- prompt = gr.Text(
408
- label="Prompt",
409
- show_label=False,
410
- max_lines=1,
411
- placeholder="Enter your prompt",
412
- container=False,
413
- )
414
- run_button = gr.Button("Run", scale=0, variant="primary")
415
- result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
416
 
417
- with gr.Row():
418
- # Model choice radio button above additional options
419
- model_choice = gr.Radio(
420
- choices=["flux.1-krea-merged-dev", "flux.1-dev-merged"],
421
- label="Select Model",
422
- value="flux.1-krea-merged-dev"
423
- )
424
-
425
- with gr.Accordion("Additional Options", open=False):
426
- style_selection = gr.Dropdown(
427
- label="Quality Style (for flux.1-dev-realism only)",
428
- choices=STYLE_NAMES,
429
- value=DEFAULT_STYLE_NAME,
430
- interactive=True,
431
- )
432
- use_negative_prompt = gr.Checkbox(label="Use negative prompt (for flux.1-dev-realism only)", value=False)
433
- negative_prompt = gr.Text(
434
- label="Negative prompt",
435
- max_lines=1,
436
- placeholder="Enter a negative prompt",
437
- visible=False,
438
- )
439
- seed = gr.Slider(
440
- label="Seed",
441
- minimum=0,
442
- maximum=MAX_SEED,
443
- step=1,
444
- value=0,
445
- )
446
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
447
  with gr.Row():
448
- width = gr.Slider(
449
- label="Width",
450
- minimum=512,
451
- maximum=2048,
452
- step=64,
453
- value=1024,
454
- )
455
- height = gr.Slider(
456
- label="Height",
457
- minimum=512,
458
- maximum=2048,
459
- step=64,
460
- value=1024,
461
- )
462
- guidance_scale = gr.Slider(
463
- label="Guidance Scale",
464
- minimum=0.1,
465
- maximum=20.0,
466
- step=0.1,
467
- value=3.5,
468
- )
469
- num_inference_steps = gr.Slider(
470
- label="Number of inference steps",
471
- minimum=1,
472
- maximum=40,
473
- step=1,
474
- value=28,
475
- )
476
- num_images = gr.Slider(
477
- label="Number of images",
478
- minimum=1,
479
- maximum=5,
480
- step=1,
481
- value=1,
482
- )
483
- zip_images = gr.Checkbox(label="Zip generated images", value=False)
 
 
 
 
 
 
484
 
485
- gr.Markdown("### Output Information")
486
- seed_display = gr.Textbox(label="Seed used", interactive=False)
487
- generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False)
488
- zip_file = gr.File(label="Download ZIP")
489
-
490
- gr.Examples(
491
- examples=examples,
492
- inputs=prompt,
493
- outputs=[result, seed_display, generation_time, zip_file],
494
- fn=generate,
495
- cache_examples=False,
496
- )
497
-
498
- use_negative_prompt.change(
499
- fn=lambda x: gr.update(visible=x),
500
- inputs=use_negative_prompt,
501
- outputs=negative_prompt,
502
- api_name=False,
503
- )
504
-
505
  gr.on(
506
- triggers=[
507
- prompt.submit,
508
- run_button.click,
509
- ],
510
- fn=generate,
511
- inputs=[
512
- model_choice,
513
- prompt,
514
- negative_prompt,
515
- use_negative_prompt,
516
- seed,
517
- width,
518
- height,
519
- guidance_scale,
520
- randomize_seed,
521
- style_selection,
522
- num_inference_steps,
523
- num_images,
524
- zip_images,
525
- ],
526
- outputs=[result, seed_display, generation_time, zip_file],
527
- api_name="run",
528
  )
529
 
530
- if __name__ == "__main__":
531
- demo.queue(max_size=30).launch(mcp_server=True, ssr_mode=False, show_error=True)
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import spaces
4
  import torch
 
 
5
  import random
6
+ from PIL import Image
 
 
 
 
 
 
 
 
7
 
8
+ from diffusers import FluxKontextPipeline
9
+ from diffusers.utils import load_image
 
 
 
 
 
 
 
 
10
 
11
  MAX_SEED = np.iinfo(np.int32).max
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @spaces.GPU
16
+ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
17
+ """
18
+ Perform image editing using the FLUX.1 Kontext pipeline.
19
+
20
+ This function takes an input image and a text prompt to generate a modified version
21
+ of the image based on the provided instructions. It uses the FLUX.1 Kontext model
22
+ for contextual image editing tasks.
23
+
24
+ Args:
25
+ input_image (PIL.Image.Image): The input image to be edited. Will be converted
26
+ to RGB format if not already in that format.
27
+ prompt (str): Text description of the desired edit to apply to the image.
28
+ Examples: "Remove glasses", "Add a hat", "Change background to beach".
29
+ seed (int, optional): Random seed for reproducible generation. Defaults to 42.
30
+ Must be between 0 and MAX_SEED (2^31 - 1).
31
+ randomize_seed (bool, optional): If True, generates a random seed instead of
32
+ using the provided seed value. Defaults to False.
33
+ guidance_scale (float, optional): Controls how closely the model follows the
34
+ prompt. Higher values mean stronger adherence to the prompt but may reduce
35
+ image quality. Range: 1.0-10.0. Defaults to 2.5.
36
+ steps (int, optional): Controls how many steps to run the diffusion model for.
37
+ Range: 1-30. Defaults to 28.
38
+ progress (gr.Progress, optional): Gradio progress tracker for monitoring
39
+ generation progress. Defaults to gr.Progress(track_tqdm=True).
40
+
41
+ Returns:
42
+ tuple: A 3-tuple containing:
43
+ - PIL.Image.Image: The generated/edited image
44
+ - int: The seed value used for generation (useful when randomize_seed=True)
45
+ - gr.update: Gradio update object to make the reuse button visible
46
+
47
+ Example:
48
+ >>> edited_image, used_seed, button_update = infer(
49
+ ... input_image=my_image,
50
+ ... prompt="Add sunglasses",
51
+ ... seed=123,
52
+ ... randomize_seed=False,
53
+ ... guidance_scale=2.5
54
+ ... )
55
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  if randomize_seed:
57
  seed = random.randint(0, MAX_SEED)
 
 
 
58
 
59
+ if input_image:
60
+ input_image = input_image.convert("RGB")
61
+ image = pipe(
62
+ image=input_image,
63
  prompt=prompt,
64
  guidance_scale=guidance_scale,
65
+ width = input_image.size[0],
66
+ height = input_image.size[1],
67
+ num_inference_steps=steps,
68
+ generator=torch.Generator().manual_seed(seed),
69
+ ).images[0]
70
+ else:
71
+ image = pipe(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  prompt=prompt,
 
 
 
73
  guidance_scale=guidance_scale,
74
+ num_inference_steps=steps,
75
+ generator=torch.Generator().manual_seed(seed),
76
+ ).images[0]
77
+ return image, seed, gr.Button(visible=True)
 
 
 
 
78
 
79
+ @spaces.GPU
80
+ def infer_example(input_image, prompt):
81
+ image, seed, _ = infer(input_image, prompt)
82
+ return image, seed
83
+
84
+ css="""
85
+ #col-container {
86
+ margin: 0 auto;
87
+ max-width: 960px;
 
 
 
 
 
 
 
 
 
88
  }
89
+ """
90
 
91
+ with gr.Blocks(css=css) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
+ with gr.Column(elem_id="col-container"):
94
+ gr.Markdown(f"""# FLUX.1 Kontext [dev]
95
+ Image editing and manipulation model guidance-distilled from FLUX.1 Kontext [pro], [[blog]](https://bfl.ai/announcements/flux-1-kontext-dev) [[model]](https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev)
96
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  with gr.Row():
98
+ with gr.Column():
99
+ input_image = gr.Image(label="Upload the image for editing", type="pil")
100
+ with gr.Row():
101
+ prompt = gr.Text(
102
+ label="Prompt",
103
+ show_label=False,
104
+ max_lines=1,
105
+ placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
106
+ container=False,
107
+ )
108
+ run_button = gr.Button("Run", scale=0)
109
+ with gr.Accordion("Advanced Settings", open=False):
110
+
111
+ seed = gr.Slider(
112
+ label="Seed",
113
+ minimum=0,
114
+ maximum=MAX_SEED,
115
+ step=1,
116
+ value=0,
117
+ )
118
+
119
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
120
+
121
+ guidance_scale = gr.Slider(
122
+ label="Guidance Scale",
123
+ minimum=1,
124
+ maximum=10,
125
+ step=0.1,
126
+ value=2.5,
127
+ )
128
+
129
+ steps = gr.Slider(
130
+ label="Steps",
131
+ minimum=1,
132
+ maximum=30,
133
+ value=28,
134
+ step=1
135
+ )
136
+
137
+ with gr.Column():
138
+ result = gr.Image(label="Result", show_label=False, interactive=False)
139
+ reuse_button = gr.Button("Reuse this image", visible=False)
140
 
141
+
142
+ examples = gr.Examples(
143
+ examples=[
144
+ ["images/14.png", "Change the cat’s eyes to blue."],
145
+ ["images/15.png", "Change the weather to rainy."],
146
+ ["images/16.png", "Change the hair color to gray."]
147
+ ],
148
+ inputs=[input_image, prompt],
149
+ outputs=[result, seed],
150
+ fn=infer_example,
151
+ cache_examples="lazy"
152
+ )
153
+
 
 
 
 
 
 
 
154
  gr.on(
155
+ triggers=[run_button.click, prompt.submit],
156
+ fn = infer,
157
+ inputs = [input_image, prompt, seed, randomize_seed, guidance_scale, steps],
158
+ outputs = [result, seed, reuse_button]
159
+ )
160
+ reuse_button.click(
161
+ fn = lambda image: image,
162
+ inputs = [result],
163
+ outputs = [input_image]
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  )
165
 
166
+ demo.launch(mcp_server=True)