linoyts HF staff commited on
Commit
b75c2df
1 Parent(s): 59ce957

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -10,29 +10,30 @@ flash_pipe.scheduler = EulerDiscreteScheduler.from_config(flash_pipe.scheduler.c
10
  clip_slider = CLIPSliderXL(flash_pipe, device=torch.device("cuda"), iterations=150)
11
 
12
  @spaces.GPU
13
- def generate(slider_x, slider_y, prompt, x_concept_1, x_concept_2, y_concept_1, y_concept_2):
14
 
15
  # check if avg diff for directions need to be re-calculated
16
  if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]):
17
- clip_slider.avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1])
18
  x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
19
  if not sorted(slider_y) == sorted([y_concept_1, y_concept_2]):
20
- clip_slider.avg_diff_2nd = clip_slider.find_latent_direction(slider_y[0], slider_y[1])
21
  y_concept_1, y_concept_2 = slider_y[0], slider_y[1]
22
 
23
  comma_concepts_x = ', '.join(slider_x)
24
  comma_concepts_y = ', '.join(slider_y)
25
 
26
- image = clip_slider.generate(prompt, scale=0, scale_2nd=0, num_inference_steps=8)
27
 
28
- return gr.update(label=comma_concepts_x, interactive=True),gr.update(label=comma_concepts_y, interactive=True), x_concept_1, x_concept_2, y_concept_1, y_concept_2, image
 
29
 
30
- def update_x(x,y,prompt):
31
- image = clip_slider.generate(prompt, scale=x, scale_2nd=y, num_inference_steps=8)
32
  return image
33
 
34
- def update_y(x,y,prompt):
35
- image = clip_slider.generate(prompt, scale=x, scale_2nd=y, num_inference_steps=8)
36
  return image
37
 
38
  css = '''
@@ -65,6 +66,9 @@ with gr.Blocks(css=css) as demo:
65
  x_concept_2 = gr.State("")
66
  y_concept_1 = gr.State("")
67
  y_concept_2 = gr.State("")
 
 
 
68
 
69
  with gr.Row():
70
  with gr.Column():
@@ -78,10 +82,10 @@ with gr.Blocks(css=css) as demo:
78
  output_image = gr.Image(elem_id="image_out")
79
 
80
  submit.click(fn=generate,
81
- inputs=[slider_x, slider_y, prompt, x_concept_1, x_concept_2, y_concept_1, y_concept_2],
82
- outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, output_image])
83
- x.change(fn=update_x, inputs=[x,y, prompt], outputs=[output_image])
84
- y.change(fn=update_y, inputs=[x,y, prompt], outputs=[output_image])
85
 
86
  if __name__ == "__main__":
87
  demo.launch()
 
10
  clip_slider = CLIPSliderXL(flash_pipe, device=torch.device("cuda"), iterations=150)
11
 
12
  @spaces.GPU
13
+ def generate(slider_x, slider_y, prompt, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y):
14
 
15
  # check if avg diff for directions need to be re-calculated
16
  if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]):
17
+ avg_diff_x = clip_slider.find_latent_direction(slider_x[0], slider_x[1])
18
  x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
19
  if not sorted(slider_y) == sorted([y_concept_1, y_concept_2]):
20
+ avg_diff_y = clip_slider.find_latent_direction(slider_y[0], slider_y[1])
21
  y_concept_1, y_concept_2 = slider_y[0], slider_y[1]
22
 
23
  comma_concepts_x = ', '.join(slider_x)
24
  comma_concepts_y = ', '.join(slider_y)
25
 
26
+ image = clip_slider.generate(prompt, scale=0, scale_2nd=0, num_inference_steps=8, avg_diff=avg_diff_x, avg_diff_2nd=avg_diff_y)
27
 
28
+ return gr.update(label=comma_concepts_x, interactive=True),gr.update(label=comma_concepts_y, interactive=True),
29
+ x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, image
30
 
31
+ def update_x(x,y,prompt, avg_diff_x, avg_diff_y):
32
+ image = clip_slider.generate(prompt, scale=x, scale_2nd=y, num_inference_steps=8, avg_diff=avg_diff_x, avg_diff_2nd=avg_diff_y)
33
  return image
34
 
35
+ def update_y(x,y,prompt, avg_diff_x, avg_diff_y):
36
+ image = clip_slider.generate(prompt, scale=x, scale_2nd=y, num_inference_steps=8, avg_diff=avg_diff_x, avg_diff_2nd=avg_diff_y)
37
  return image
38
 
39
  css = '''
 
66
  x_concept_2 = gr.State("")
67
  y_concept_1 = gr.State("")
68
  y_concept_2 = gr.State("")
69
+
70
+ avg_diff_x = gr.State()
71
+ avg_diff_y = gr.State()
72
 
73
  with gr.Row():
74
  with gr.Column():
 
82
  output_image = gr.Image(elem_id="image_out")
83
 
84
  submit.click(fn=generate,
85
+ inputs=[slider_x, slider_y, prompt, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y],
86
+ outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, output_image])
87
+ x.change(fn=update_x, inputs=[x,y, prompt, avg_diff_x, avg_diff_y], outputs=[output_image])
88
+ y.change(fn=update_y, inputs=[x,y, prompt, avg_diff_x, avg_diff_y], outputs=[output_image])
89
 
90
  if __name__ == "__main__":
91
  demo.launch()