multimodalart HF Staff commited on
Commit
d4cae39
·
verified ·
1 Parent(s): 278eeb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -48
app.py CHANGED
@@ -6,14 +6,14 @@ import torch
6
  import random
7
  from PIL import Image
8
 
9
- from kontext_pipeline import FluxKontextPipeline
10
  from diffusers import FluxTransformer2DModel
11
  from diffusers.utils import load_image
12
 
13
  from huggingface_hub import hf_hub_download
14
 
15
 
16
- kontext_path = hf_hub_download(repo_id="diffusers/kontext", filename="kontext.safetensors")
17
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
 
@@ -27,32 +27,32 @@ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5
27
  seed = random.randint(0, MAX_SEED)
28
 
29
  input_image = input_image.convert("RGB")
30
- original_width, original_height = input_image.size
31
 
32
- if original_width >= original_height:
33
- new_width = 1024
34
- new_height = int(original_height * (new_width / original_width))
35
- new_height = round(new_height / 64) * 64
36
- else:
37
- new_height = 1024
38
- new_width = int(original_width * (new_height / original_height))
39
- new_width = round(new_width / 64) * 64
40
 
41
- input_image_resized = input_image.resize((new_width, new_height), Image.LANCZOS)
42
  image = pipe(
43
- image=input_image_resized,
44
  prompt=prompt,
45
  guidance_scale=guidance_scale,
46
- width=new_width,
47
- height=new_height,
48
  generator=torch.Generator().manual_seed(seed),
49
  ).images[0]
50
- return image, seed
51
 
52
  css="""
53
  #col-container {
54
  margin: 0 auto;
55
- max-width: 520px;
56
  }
57
  """
58
 
@@ -62,46 +62,54 @@ with gr.Blocks(css=css) as demo:
62
  gr.Markdown(f"""# FLUX.1 Kontext [dev]
63
  """)
64
 
65
- input_image = gr.Image(label="Upload the image for editing", type="pil")
66
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- prompt = gr.Text(
69
- label="Prompt",
70
- show_label=False,
71
- max_lines=1,
72
- placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
73
- container=False,
74
- )
75
-
76
- run_button = gr.Button("Run", scale=0)
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- result = gr.Image(label="Result", show_label=False)
79
 
80
- with gr.Accordion("Advanced Settings", open=False):
81
-
82
- seed = gr.Slider(
83
- label="Seed",
84
- minimum=0,
85
- maximum=MAX_SEED,
86
- step=1,
87
- value=0,
88
- )
89
-
90
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
91
-
92
- guidance_scale = gr.Slider(
93
- label="Guidance Scale",
94
- minimum=1,
95
- maximum=10,
96
- step=0.1,
97
- value=2.5,
98
- )
99
 
100
  gr.on(
101
  triggers=[run_button.click, prompt.submit],
102
  fn = infer,
103
  inputs = [input_image, prompt, seed, randomize_seed, guidance_scale],
104
- outputs = [result, seed]
 
 
 
 
 
105
  )
106
 
107
  demo.launch()
 
6
  import random
7
  from PIL import Image
8
 
9
+ from pipeline_flux_kontext import FluxKontextPipeline
10
  from diffusers import FluxTransformer2DModel
11
  from diffusers.utils import load_image
12
 
13
  from huggingface_hub import hf_hub_download
14
 
15
 
16
+ kontext_path = hf_hub_download(repo_id="diffusers/kontext-v2", filename="dev-opt-2-a-3.safetensors")
17
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
 
 
27
  seed = random.randint(0, MAX_SEED)
28
 
29
  input_image = input_image.convert("RGB")
30
+ # original_width, original_height = input_image.size
31
 
32
+ # if original_width >= original_height:
33
+ # new_width = 1024
34
+ # new_height = int(original_height * (new_width / original_width))
35
+ # new_height = round(new_height / 64) * 64
36
+ # else:
37
+ # new_height = 1024
38
+ # new_width = int(original_width * (new_height / original_height))
39
+ # new_width = round(new_width / 64) * 64
40
 
41
+ #input_image_resized = input_image.resize((new_width, new_height), Image.LANCZOS)
42
  image = pipe(
43
+ image=input_image,
44
  prompt=prompt,
45
  guidance_scale=guidance_scale,
46
+ # width=new_width,
47
+ # height=new_height,
48
  generator=torch.Generator().manual_seed(seed),
49
  ).images[0]
50
+ return image, seed, gr.update(visible=True)
51
 
52
  css="""
53
  #col-container {
54
  margin: 0 auto;
55
+ max-width: 960px;
56
  }
57
  """
58
 
 
62
  gr.Markdown(f"""# FLUX.1 Kontext [dev]
63
  """)
64
 
 
65
  with gr.Row():
66
+ with gr.Column():
67
+ input_image = gr.Image(label="Upload the image for editing", type="pil")
68
+ with gr.Row():
69
+ prompt = gr.Text(
70
+ label="Prompt",
71
+ show_label=False,
72
+ max_lines=1,
73
+ placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
74
+ container=False,
75
+ )
76
+ run_button = gr.Button("Run", scale=0)
77
+ with gr.Accordion("Advanced Settings", open=False):
78
 
79
+ seed = gr.Slider(
80
+ label="Seed",
81
+ minimum=0,
82
+ maximum=MAX_SEED,
83
+ step=1,
84
+ value=0,
85
+ )
86
+
87
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
88
+
89
+ guidance_scale = gr.Slider(
90
+ label="Guidance Scale",
91
+ minimum=1,
92
+ maximum=10,
93
+ step=0.1,
94
+ value=2.5,
95
+ )
96
+
97
+ with gr.Column():
98
+ result = gr.Image(label="Result", show_label=False, interactive=False)
99
+ reuse_button = gr.Button("Reuse this image", visible=False)
100
 
 
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  gr.on(
104
  triggers=[run_button.click, prompt.submit],
105
  fn = infer,
106
  inputs = [input_image, prompt, seed, randomize_seed, guidance_scale],
107
+ outputs = [result, seed, reuse_button]
108
+ )
109
+ reuse_button.click(
110
+ fn = lambda image: image,
111
+ inputs = [result],
112
+ outputs = [input_image]
113
  )
114
 
115
  demo.launch()