multimodalart HF staff commited on
Commit
59e8fca
1 Parent(s): b82845b
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -35,6 +35,8 @@ controlnet_model = 'InstantX/FLUX.1-dev-Controlnet-Canny-alpha'
35
  # pipe_controlnet = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
36
  # t5_slider_controlnet = T5SliderFlux(sd_pipe=pipe_controlnet,device=torch.device("cuda"))
37
 
 
 
38
  def convert_to_centered_scale(num):
39
  if num <= 0:
40
  raise ValueError("Input must be a positive integer")
@@ -49,7 +51,7 @@ def convert_to_centered_scale(num):
49
  return tuple(range(start, end + 1))
50
 
51
  @spaces.GPU(duration=200)
52
- def generate(concept_1, concept_2, scale, prompt, seed=42, recalc_directions=True, iterations=200, steps=4, interm_steps=9, guidance_scale=3.5,
53
  x_concept_1="", x_concept_2="",
54
  avg_diff_x=None,
55
  img2img_type = None, img = None,
@@ -62,7 +64,9 @@ def generate(concept_1, concept_2, scale, prompt, seed=42, recalc_directions=Tru
62
  print("slider_x", slider_x)
63
  print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
64
  #torch.manual_seed(seed)
65
-
 
 
66
  if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
67
  #avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations).to(torch.float16)
68
  avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
@@ -92,7 +96,7 @@ def generate(concept_1, concept_2, scale, prompt, seed=42, recalc_directions=Tru
92
  post_generation_slider_update = gr.update(label=comma_concepts_x, value=0, minimum=scale_min, maximum=scale_max, interactive=True)
93
  avg_diff_x = avg_diff.cpu()
94
 
95
- return x_concept_1, x_concept_2, avg_diff_x, export_to_gif(images, "clip.gif", fps=5), canvas, images, images[scale_middle], post_generation_slider_update
96
 
97
  @spaces.GPU
98
  def update_scales(x,prompt,seed, steps, interm_steps, guidance_scale,
@@ -257,8 +261,8 @@ with gr.Blocks(css=css) as demo:
257
  step=0.1,
258
  value=3.5,
259
  )
260
-
261
- seed = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
262
 
263
 
264
  # with gr.Tab(label="image2image"):
@@ -309,8 +313,8 @@ with gr.Blocks(css=css) as demo:
309
  # inputs=[slider_x, slider_y, prompt, seed, iterations, steps, guidance_scale, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y],
310
  # outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, output_image])
311
  submit.click(fn=generate,
312
- inputs=[concept_1, concept_2, x, prompt, seed, recalc_directions, iterations, steps, interm_steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x, total_images],
313
- outputs=[x_concept_1, x_concept_2, avg_diff_x, output_image, image_seq, total_images, post_generation_image, post_generation_slider])
314
 
315
  iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
316
  seed.change(fn=reset_recalc_directions, outputs=[recalc_directions])
 
35
  # pipe_controlnet = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
36
  # t5_slider_controlnet = T5SliderFlux(sd_pipe=pipe_controlnet,device=torch.device("cuda"))
37
 
38
+ MAX_SEED = 2**32-1
39
+
40
  def convert_to_centered_scale(num):
41
  if num <= 0:
42
  raise ValueError("Input must be a positive integer")
 
51
  return tuple(range(start, end + 1))
52
 
53
  @spaces.GPU(duration=200)
54
+ def generate(concept_1, concept_2, scale, prompt, randomize_seed=True, seed=42, recalc_directions=True, iterations=200, steps=4, interm_steps=9, guidance_scale=3.5,
55
  x_concept_1="", x_concept_2="",
56
  avg_diff_x=None,
57
  img2img_type = None, img = None,
 
64
  print("slider_x", slider_x)
65
  print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
66
  #torch.manual_seed(seed)
67
+ if randomize_seed:
68
+ seed = random.randint(0, MAX_SEED)
69
+
70
  if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
71
  #avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations).to(torch.float16)
72
  avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
 
96
  post_generation_slider_update = gr.update(label=comma_concepts_x, value=0, minimum=scale_min, maximum=scale_max, interactive=True)
97
  avg_diff_x = avg_diff.cpu()
98
 
99
+ return x_concept_1, x_concept_2, avg_diff_x, export_to_gif(images, "clip.gif", fps=5), canvas, images, images[scale_middle], post_generation_slider_update, seed
100
 
101
  @spaces.GPU
102
  def update_scales(x,prompt,seed, steps, interm_steps, guidance_scale,
 
261
  step=0.1,
262
  value=3.5,
263
  )
264
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
265
+ seed = gr.Slider(minimum=0, maximum=MAX_SEED, step=1, label="Seed", interactive=True, randomize=True)
266
 
267
 
268
  # with gr.Tab(label="image2image"):
 
313
  # inputs=[slider_x, slider_y, prompt, seed, iterations, steps, guidance_scale, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y],
314
  # outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, output_image])
315
  submit.click(fn=generate,
316
+ inputs=[concept_1, concept_2, x, prompt, randomize_seed, seed, recalc_directions, iterations, steps, interm_steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x, total_images],
317
+ outputs=[x_concept_1, x_concept_2, avg_diff_x, output_image, image_seq, total_images, post_generation_image, post_generation_slider, seed])
318
 
319
  iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
320
  seed.change(fn=reset_recalc_directions, outputs=[recalc_directions])