linoyts HF Staff commited on
Commit
73b3534
·
verified ·
1 Parent(s): 3aa678b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -27
app.py CHANGED
@@ -9,6 +9,7 @@ from huggingface_hub import hf_hub_download
9
  import torch
10
  from diffusers import DiffusionPipeline
11
  from huggingface_hub import hf_hub_download
 
12
 
13
  # Constants
14
  MAX_SEED = np.iinfo(np.int32).max
@@ -26,7 +27,7 @@ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev",
26
  # pipe.fuse_lora(lora_scale=0.125)
27
 
28
  #pipe.enable_lora()
29
- pipe.to("cuda")
30
 
31
  def get_examples():
32
  case = [
@@ -38,9 +39,15 @@ def get_examples():
38
  [Image.open("metal.png"), "dragon.png","a dragon, in 3d melting gold metal",0.9, 0.5, 0, 4, 8, 8, 789385745, False,True, 2, True , "text/image guided stylzation"],
39
  ]
40
  return case
41
-
42
- def reset_do_inversion():
 
 
 
 
43
  return True
 
 
44
 
45
  def resize_img(image, max_size=1024):
46
  width, height = image.size
@@ -100,26 +107,43 @@ def invert_and_edit(image,
100
  height = 1024,
101
  inverted_latent_list = None,
102
  do_inversion = True,
 
103
 
104
  ):
105
  if randomize_seed:
106
  seed = random.randint(0, MAX_SEED)
107
- if do_inversion:
108
- inverted_latent_list = pipe(
109
- source_prompt,
110
- height=1024,
111
- width=1024,
112
- guidance_scale=1,
113
- output_type="pil",
114
- num_inference_steps=num_inversion_steps,
115
- max_sequence_length=512,
116
- latents=image2latent(image),
117
- invert_image=True
118
- )
119
- do_inversion = False
 
 
 
 
 
 
 
 
 
120
  else:
121
- # move to gpu because of zero and gr.states
122
- inverted_latent_list = [tensor.to(DEVICE) for tensor in inverted_latent_list]
 
 
 
 
 
 
 
123
 
124
  try:
125
  multimodal_layers = convert_string_to_list(multimodal_layers)
@@ -131,19 +155,20 @@ def invert_and_edit(image,
131
  [source_prompt, edit_prompt],
132
  height=1024,
133
  width=1024,
134
- guidance_scale=[1,3],
135
  output_type="pil",
136
  num_inference_steps=num_inference_steps,
137
  max_sequence_length=512,
138
- latents=inverted_latent_list[-1].tile(2, 1, 1),
139
  inverted_latent_list=inverted_latent_list,
140
  mm_copy_blocks=multimodal_layers,
141
  single_copy_blocks=single_layers,
142
- ).images[1]
143
 
144
  # move back to cpu because of zero and gr.states
145
- inverted_latent_list = [tensor.cpu() for tensor in inverted_latent_list]
146
- return output, inverted_latent_list, do_inversion, seed
 
147
 
148
  # UI CSS
149
  css = """
@@ -157,7 +182,8 @@ css = """
157
  with gr.Blocks(css=css) as demo:
158
 
159
  inverted_latents = gr.State()
160
- do_inversion = gr.State(True)
 
161
 
162
  with gr.Column(elem_id="col-container"):
163
  gr.Markdown(f"""# Stable Flow 🌊🖌️
@@ -205,6 +231,9 @@ following the algorithm proposed in [*Stable Flow: Vital Layers for Training-Fre
205
 
206
  with gr.Column():
207
  result = gr.Image(label="Result")
 
 
 
208
 
209
  with gr.Accordion("Advanced Settings", open=False):
210
 
@@ -271,10 +300,11 @@ following the algorithm proposed in [*Stable Flow: Vital Layers for Training-Fre
271
  width,
272
  height,
273
  inverted_latents,
274
- do_inversion
 
275
 
276
  ],
277
- outputs=[result, inverted_latents, do_inversion, seed],
278
  )
279
 
280
  # gr.Examples(
@@ -284,18 +314,28 @@ following the algorithm proposed in [*Stable Flow: Vital Layers for Training-Fre
284
 
285
  # )
286
 
287
- input_image.change(
 
 
 
 
 
 
 
288
  fn=reset_do_inversion,
 
289
  outputs=[do_inversion]
290
  )
291
 
292
  num_inversion_steps.change(
293
  fn=reset_do_inversion,
 
294
  outputs=[do_inversion]
295
  )
296
 
297
  seed.change(
298
  fn=reset_do_inversion,
 
299
  outputs=[do_inversion]
300
  )
301
 
 
9
  import torch
10
  from diffusers import DiffusionPipeline
11
  from huggingface_hub import hf_hub_download
12
+ # from gradio_imageslider import ImageSlider
13
 
14
  # Constants
15
  MAX_SEED = np.iinfo(np.int32).max
 
27
  # pipe.fuse_lora(lora_scale=0.125)
28
 
29
  #pipe.enable_lora()
30
+ pipe.to(DEVICE)
31
 
32
  def get_examples():
33
  case = [
 
39
  [Image.open("metal.png"), "dragon.png","a dragon, in 3d melting gold metal",0.9, 0.5, 0, 4, 8, 8, 789385745, False,True, 2, True , "text/image guided stylzation"],
40
  ]
41
  return case
42
+
43
+ def reset_image_input():
44
+ return True
45
+
46
+ def reset_do_inversion(image_input):
47
+ if image_input:
48
  return True
49
+ else:
50
+ return False
51
 
52
  def resize_img(image, max_size=1024):
53
  width, height = image.size
 
107
  height = 1024,
108
  inverted_latent_list = None,
109
  do_inversion = True,
110
+ image_input = False,
111
 
112
  ):
113
  if randomize_seed:
114
  seed = random.randint(0, MAX_SEED)
115
+ if image_input:
116
+ if do_inversion:
117
+ inverted_latent_list = pipe(
118
+ source_prompt,
119
+ height=1024,
120
+ width=1024,
121
+ guidance_scale=1,
122
+ output_type="pil",
123
+ num_inference_steps=num_inversion_steps,
124
+ max_sequence_length=512,
125
+ latents=image2latent(image),
126
+ invert_image=True
127
+ )
128
+ do_inversion = False
129
+
130
+ else:
131
+ # move to gpu because of zero and gr.states
132
+ inverted_latent_list = [tensor.to(DEVICE) for tensor in inverted_latent_list]
133
+
134
+ latents = inverted_latent_list[-1].tile(2, 1, 1)
135
+ guidance_scale = [1,3]
136
+ image_input = True
137
  else:
138
+ latents = torch.randn(
139
+ (4096, 64),
140
+ generator=torch.Generator(0).manual_seed(0),
141
+ dtype=torch.float16,
142
+ device=DEVICE,
143
+ ).tile(2, 1, 1)
144
+ guidance_scale = 3.5
145
+ image_input = False
146
+
147
 
148
  try:
149
  multimodal_layers = convert_string_to_list(multimodal_layers)
 
155
  [source_prompt, edit_prompt],
156
  height=1024,
157
  width=1024,
158
+ guidance_scale=guidance_scale,
159
  output_type="pil",
160
  num_inference_steps=num_inference_steps,
161
  max_sequence_length=512,
162
+ latents=latents,
163
  inverted_latent_list=inverted_latent_list,
164
  mm_copy_blocks=multimodal_layers,
165
  single_copy_blocks=single_layers,
166
+ ).images
167
 
168
  # move back to cpu because of zero and gr.states
169
+ if inverted_latent_list is not None:
170
+ inverted_latent_list = [tensor.cpu() for tensor in inverted_latent_list]
171
+ return output[0], output[1], inverted_latent_list, do_inversion, image_input, seed
172
 
173
  # UI CSS
174
  css = """
 
182
  with gr.Blocks(css=css) as demo:
183
 
184
  inverted_latents = gr.State()
185
+ do_inversion = gr.State(False)
186
+ image_input = gr.State(False)
187
 
188
  with gr.Column(elem_id="col-container"):
189
  gr.Markdown(f"""# Stable Flow 🌊🖌️
 
231
 
232
  with gr.Column():
233
  result = gr.Image(label="Result")
234
+ # with gr.Column():
235
+ # with gr.Group():
236
+ # result = ImageSlider(position=0.5)
237
 
238
  with gr.Accordion("Advanced Settings", open=False):
239
 
 
300
  width,
301
  height,
302
  inverted_latents,
303
+ do_inversion,
304
+ image_input
305
 
306
  ],
307
+ outputs=[input_image, result, inverted_latents, do_inversion, image_input, seed],
308
  )
309
 
310
  # gr.Examples(
 
314
 
315
  # )
316
 
317
+ input_image.input(fn=reset_image_input,
318
+ outputs=[image_input]).then(
319
+ fn=reset_do_inversion,
320
+ inputs = [image_input],
321
+ outputs=[do_inversion]
322
+ )
323
+
324
+ source_prompt.change(
325
  fn=reset_do_inversion,
326
+ inputs = [image_input],
327
  outputs=[do_inversion]
328
  )
329
 
330
  num_inversion_steps.change(
331
  fn=reset_do_inversion,
332
+ inputs = [image_input],
333
  outputs=[do_inversion]
334
  )
335
 
336
  seed.change(
337
  fn=reset_do_inversion,
338
+ inputs = [image_input],
339
  outputs=[do_inversion]
340
  )
341