Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -27,6 +27,11 @@ def HWC3(x):
|
|
27 |
y = y.clip(0, 255).astype(np.uint8)
|
28 |
return y
|
29 |
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# load pipelines
|
32 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
@@ -62,10 +67,12 @@ def generate(slider_x, slider_y, prompt, seed, iterations, steps,
|
|
62 |
avg_diff_y_1, avg_diff_y_2,
|
63 |
img2img_type = None,
|
64 |
img = None):
|
|
|
65 |
start_time = time.time()
|
66 |
# check if avg diff for directions need to be re-calculated
|
67 |
print("slider_x", slider_x)
|
68 |
print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
|
|
|
69 |
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]):
|
70 |
avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
|
71 |
avg_diff_0 = avg_diff[0].to(torch.float16)
|
@@ -73,6 +80,7 @@ def generate(slider_x, slider_y, prompt, seed, iterations, steps,
|
|
73 |
x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
|
74 |
|
75 |
print("avg_diff_0", avg_diff_0.dtype)
|
|
|
76 |
if not sorted(slider_y) == sorted([y_concept_1, y_concept_2]):
|
77 |
avg_diff_2nd = clip_slider.find_latent_direction(slider_y[0], slider_y[1], num_iterations=iterations)
|
78 |
avg_diff_2nd_0 = avg_diff_2nd[0].to(torch.float16)
|
@@ -80,10 +88,20 @@ def generate(slider_x, slider_y, prompt, seed, iterations, steps,
|
|
80 |
y_concept_1, y_concept_2 = slider_y[0], slider_y[1]
|
81 |
end_time = time.time()
|
82 |
print(f"direction time: {end_time - start_time:.2f} ms")
|
|
|
83 |
start_time = time.time()
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
end_time = time.time()
|
86 |
print(f"generation time: {end_time - start_time:.2f} ms")
|
|
|
87 |
comma_concepts_x = ', '.join(slider_x)
|
88 |
comma_concepts_y = ', '.join(slider_y)
|
89 |
|
@@ -95,14 +113,36 @@ def generate(slider_x, slider_y, prompt, seed, iterations, steps,
|
|
95 |
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, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, image
|
96 |
|
97 |
@spaces.GPU
|
98 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
|
100 |
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
|
101 |
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
102 |
return image
|
103 |
|
104 |
@spaces.GPU
|
105 |
-
def update_y(x,y,prompt, seed, steps,
|
|
|
|
|
|
|
106 |
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
|
107 |
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
|
108 |
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
@@ -144,7 +184,7 @@ with gr.Blocks(css=css) as demo:
|
|
144 |
avg_diff_y_1 = gr.State()
|
145 |
avg_diff_y_2 = gr.State()
|
146 |
|
147 |
-
with gr.Tab(""):
|
148 |
with gr.Row():
|
149 |
with gr.Column():
|
150 |
slider_x = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
@@ -184,13 +224,13 @@ with gr.Blocks(css=css) as demo:
|
|
184 |
submit.click(fn=generate,
|
185 |
inputs=[slider_x, slider_y, prompt, seed, iterations, steps, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2],
|
186 |
outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image])
|
187 |
-
x.change(fn=
|
188 |
-
y.change(fn=
|
189 |
submit_a.click(fn=generate,
|
190 |
inputs=[slider_x_a, slider_y_a, prompt_a, seed_a, iterations_a, steps_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2],
|
191 |
outputs=[x_a, y_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image_a])
|
192 |
-
x_a.change(fn=
|
193 |
-
y_a.change(fn=
|
194 |
|
195 |
|
196 |
if __name__ == "__main__":
|
|
|
27 |
y = y.clip(0, 255).astype(np.uint8)
|
28 |
return y
|
29 |
|
30 |
+
def process_controlnet_img(image):
|
31 |
+
controlnet_img = np.array(image)
|
32 |
+
controlnet_img = cv2.Canny(controlnet_img, 100, 200)
|
33 |
+
controlnet_img = HWC3(controlnet_img)
|
34 |
+
controlnet_img = Image.fromarray(controlnet_img)
|
35 |
|
36 |
# load pipelines
|
37 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
|
|
67 |
avg_diff_y_1, avg_diff_y_2,
|
68 |
img2img_type = None,
|
69 |
img = None):
|
70 |
+
|
71 |
start_time = time.time()
|
72 |
# check if avg diff for directions need to be re-calculated
|
73 |
print("slider_x", slider_x)
|
74 |
print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
|
75 |
+
|
76 |
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]):
|
77 |
avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
|
78 |
avg_diff_0 = avg_diff[0].to(torch.float16)
|
|
|
80 |
x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
|
81 |
|
82 |
print("avg_diff_0", avg_diff_0.dtype)
|
83 |
+
|
84 |
if not sorted(slider_y) == sorted([y_concept_1, y_concept_2]):
|
85 |
avg_diff_2nd = clip_slider.find_latent_direction(slider_y[0], slider_y[1], num_iterations=iterations)
|
86 |
avg_diff_2nd_0 = avg_diff_2nd[0].to(torch.float16)
|
|
|
88 |
y_concept_1, y_concept_2 = slider_y[0], slider_y[1]
|
89 |
end_time = time.time()
|
90 |
print(f"direction time: {end_time - start_time:.2f} ms")
|
91 |
+
|
92 |
start_time = time.time()
|
93 |
+
|
94 |
+
if img2img_type=="controlnet canny" and img is not None:
|
95 |
+
control_img = process_controlnet_img(img)
|
96 |
+
image = clip_slider.generate(prompt, image=control_img, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
|
97 |
+
elif img2img_type=="ip adapter" and img is not None:
|
98 |
+
image = clip_slider.generate(prompt, ip_adapter_image=img, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
|
99 |
+
else: # text to image
|
100 |
+
image = clip_slider.generate(prompt, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=(avg_diff_0,avg_diff_1), avg_diff_2nd=(avg_diff_2nd_0,avg_diff_2nd_1))
|
101 |
+
|
102 |
end_time = time.time()
|
103 |
print(f"generation time: {end_time - start_time:.2f} ms")
|
104 |
+
|
105 |
comma_concepts_x = ', '.join(slider_x)
|
106 |
comma_concepts_y = ', '.join(slider_y)
|
107 |
|
|
|
113 |
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, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, image
|
114 |
|
115 |
@spaces.GPU
|
116 |
+
def update_scales(x,y,prompt,seed, steps,
|
117 |
+
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
|
118 |
+
img2img_type = None,
|
119 |
+
img = None)
|
120 |
+
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
|
121 |
+
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
|
122 |
+
if img2img_type=="controlnet canny" and img is not None:
|
123 |
+
control_img = process_controlnet_img(img)
|
124 |
+
image = clip_slider.generate(prompt, image=control_img, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
125 |
+
elif img2img_type=="ip adapter" and img is not None:
|
126 |
+
image = clip_slider.generate(prompt, ip_adapter_image=img, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
127 |
+
else:
|
128 |
+
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
129 |
+
return image
|
130 |
+
|
131 |
+
@spaces.GPU
|
132 |
+
def update_x(x,y,prompt,seed, steps,
|
133 |
+
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
|
134 |
+
img2img_type = None,
|
135 |
+
img = None):
|
136 |
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
|
137 |
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
|
138 |
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
139 |
return image
|
140 |
|
141 |
@spaces.GPU
|
142 |
+
def update_y(x,y,prompt, seed, steps,
|
143 |
+
avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2,
|
144 |
+
img2img_type = None,
|
145 |
+
img = None):
|
146 |
avg_diff = (avg_diff_x_1.cuda(), avg_diff_x_2.cuda())
|
147 |
avg_diff_2nd = (avg_diff_y_1.cuda(), avg_diff_y_2.cuda())
|
148 |
image = clip_slider.generate(prompt, scale=x, scale_2nd=y, seed=seed, num_inference_steps=steps, avg_diff=avg_diff,avg_diff_2nd=avg_diff_2nd)
|
|
|
184 |
avg_diff_y_1 = gr.State()
|
185 |
avg_diff_y_2 = gr.State()
|
186 |
|
187 |
+
with gr.Tab("text2image"):
|
188 |
with gr.Row():
|
189 |
with gr.Column():
|
190 |
slider_x = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
|
|
224 |
submit.click(fn=generate,
|
225 |
inputs=[slider_x, slider_y, prompt, seed, iterations, steps, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2],
|
226 |
outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image])
|
227 |
+
x.change(fn=update_scales, inputs=[x,y, prompt, seed, steps, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image])
|
228 |
+
y.change(fn=update_scales, inputs=[x,y, prompt, seed, steps, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image])
|
229 |
submit_a.click(fn=generate,
|
230 |
inputs=[slider_x_a, slider_y_a, prompt_a, seed_a, iterations_a, steps_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2],
|
231 |
outputs=[x_a, y_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2, output_image_a])
|
232 |
+
x_a.change(fn=update_scales, inputs=[x_a,y_a, prompt_a, seed_a, steps_a, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image_a])
|
233 |
+
y_a.change(fn=update_scales, inputs=[x_a,y_a, prompt, seed_a, steps_a, avg_diff_x_1, avg_diff_x_2, avg_diff_y_1, avg_diff_y_2], outputs=[output_image_a])
|
234 |
|
235 |
|
236 |
if __name__ == "__main__":
|