Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,10 +25,16 @@ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell",
|
|
| 25 |
|
| 26 |
pipe.transformer.to(memory_format=torch.channels_last)
|
| 27 |
pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
|
| 28 |
-
|
| 29 |
clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda"))
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@spaces.GPU(duration=200)
|
| 33 |
def generate(slider_x, prompt, seed, recalc_directions, iterations, steps, guidance_scale,
|
| 34 |
x_concept_1, x_concept_2,
|
|
@@ -41,6 +47,7 @@ def generate(slider_x, prompt, seed, recalc_directions, iterations, steps, guida
|
|
| 41 |
# check if avg diff for directions need to be re-calculated
|
| 42 |
print("slider_x", slider_x)
|
| 43 |
print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
|
|
|
|
| 44 |
|
| 45 |
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
|
| 46 |
#avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations).to(torch.float16)
|
|
@@ -59,6 +66,7 @@ def generate(slider_x, prompt, seed, recalc_directions, iterations, steps, guida
|
|
| 59 |
seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
|
| 60 |
|
| 61 |
|
|
|
|
| 62 |
comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}"
|
| 63 |
|
| 64 |
avg_diff_x = avg_diff.cpu()
|
|
@@ -71,6 +79,7 @@ def update_scales(x,prompt,seed, steps, guidance_scale,
|
|
| 71 |
img2img_type = None, img = None,
|
| 72 |
controlnet_scale= None, ip_adapter_scale=None,):
|
| 73 |
avg_diff = avg_diff_x.cuda()
|
|
|
|
| 74 |
if img2img_type=="controlnet canny" and img is not None:
|
| 75 |
control_img = process_controlnet_img(img)
|
| 76 |
image = t5_slider_controlnet.generate(prompt, guidance_scale=guidance_scale, image=control_img, controlnet_conditioning_scale =controlnet_scale, scale=x, seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
|
|
@@ -81,6 +90,27 @@ def update_scales(x,prompt,seed, steps, guidance_scale,
|
|
| 81 |
return image
|
| 82 |
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def reset_recalc_directions():
|
| 85 |
return True
|
| 86 |
|
|
@@ -131,7 +161,10 @@ intro = """
|
|
| 131 |
</p>
|
| 132 |
"""
|
| 133 |
with gr.Blocks(css=css) as demo:
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
gr.HTML(intro)
|
| 136 |
|
| 137 |
x_concept_1 = gr.State("")
|
|
@@ -144,6 +177,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 144 |
|
| 145 |
recalc_directions = gr.State(False)
|
| 146 |
|
|
|
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column():
|
| 149 |
slider_x = gr.Dropdown(label="Slider concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
|
@@ -168,9 +202,63 @@ with gr.Blocks(css=css) as demo:
|
|
| 168 |
step=0.1,
|
| 169 |
value=5,
|
| 170 |
)
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
seed = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
submit.click(fn=generate,
|
| 175 |
inputs=[slider_x, prompt, seed, recalc_directions, iterations, steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x],
|
| 176 |
outputs=[x, x_concept_1, x_concept_2, avg_diff_x, output_image])
|
|
@@ -178,6 +266,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 178 |
iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|
| 179 |
seed.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|
| 180 |
x.change(fn=update_scales, inputs=[x, prompt, seed, steps, guidance_scale, avg_diff_x], outputs=[output_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
demo.launch()
|
|
|
|
| 25 |
|
| 26 |
pipe.transformer.to(memory_format=torch.channels_last)
|
| 27 |
pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
|
| 28 |
+
#pipe.enable_model_cpu_offload()
|
| 29 |
clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda"))
|
| 30 |
|
| 31 |
|
| 32 |
+
base_model = 'black-forest-labs/FLUX.1-schnell'
|
| 33 |
+
controlnet_model = 'InstantX/FLUX.1-dev-Controlnet-Canny-alpha'
|
| 34 |
+
# controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
|
| 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 |
@spaces.GPU(duration=200)
|
| 39 |
def generate(slider_x, prompt, seed, recalc_directions, iterations, steps, guidance_scale,
|
| 40 |
x_concept_1, x_concept_2,
|
|
|
|
| 47 |
# check if avg diff for directions need to be re-calculated
|
| 48 |
print("slider_x", slider_x)
|
| 49 |
print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2)
|
| 50 |
+
#torch.manual_seed(seed)
|
| 51 |
|
| 52 |
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
|
| 53 |
#avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations).to(torch.float16)
|
|
|
|
| 66 |
seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
|
| 67 |
|
| 68 |
|
| 69 |
+
#comma_concepts_x = ', '.join(slider_x)
|
| 70 |
comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}"
|
| 71 |
|
| 72 |
avg_diff_x = avg_diff.cpu()
|
|
|
|
| 79 |
img2img_type = None, img = None,
|
| 80 |
controlnet_scale= None, ip_adapter_scale=None,):
|
| 81 |
avg_diff = avg_diff_x.cuda()
|
| 82 |
+
torch.manual_seed(seed)
|
| 83 |
if img2img_type=="controlnet canny" and img is not None:
|
| 84 |
control_img = process_controlnet_img(img)
|
| 85 |
image = t5_slider_controlnet.generate(prompt, guidance_scale=guidance_scale, image=control_img, controlnet_conditioning_scale =controlnet_scale, scale=x, seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
|
|
|
|
| 90 |
return image
|
| 91 |
|
| 92 |
|
| 93 |
+
|
| 94 |
+
@spaces.GPU
|
| 95 |
+
def update_x(x,y,prompt,seed, steps,
|
| 96 |
+
avg_diff_x, avg_diff_y,
|
| 97 |
+
img2img_type = None,
|
| 98 |
+
img = None):
|
| 99 |
+
avg_diff = avg_diff_x.cuda()
|
| 100 |
+
avg_diff_2nd = avg_diff_y.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_x, avg_diff_y,
|
| 107 |
+
img2img_type = None,
|
| 108 |
+
img = None):
|
| 109 |
+
avg_diff = avg_diff_x.cuda()
|
| 110 |
+
avg_diff_2nd = avg_diff_y.cuda()
|
| 111 |
+
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)
|
| 112 |
+
return image
|
| 113 |
+
|
| 114 |
def reset_recalc_directions():
|
| 115 |
return True
|
| 116 |
|
|
|
|
| 161 |
</p>
|
| 162 |
"""
|
| 163 |
with gr.Blocks(css=css) as demo:
|
| 164 |
+
# gr.Markdown(f"""# Latent Navigation
|
| 165 |
+
# ## Exploring CLIP text space with FLUX.1 schnell 🪐
|
| 166 |
+
# [[code](https://github.com/linoytsaban/semantic-sliders)]
|
| 167 |
+
# """)
|
| 168 |
gr.HTML(intro)
|
| 169 |
|
| 170 |
x_concept_1 = gr.State("")
|
|
|
|
| 177 |
|
| 178 |
recalc_directions = gr.State(False)
|
| 179 |
|
| 180 |
+
#with gr.Tab("text2image"):
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column():
|
| 183 |
slider_x = gr.Dropdown(label="Slider concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
|
|
|
| 202 |
step=0.1,
|
| 203 |
value=5,
|
| 204 |
)
|
| 205 |
+
# correlation = gr.Slider(
|
| 206 |
+
# label="correlation",
|
| 207 |
+
# minimum=0.1,
|
| 208 |
+
# maximum=1.0,
|
| 209 |
+
# step=0.05,
|
| 210 |
+
# value=0.6,
|
| 211 |
+
# )
|
| 212 |
seed = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
|
| 213 |
|
| 214 |
+
|
| 215 |
+
# with gr.Tab(label="image2image"):
|
| 216 |
+
# with gr.Row():
|
| 217 |
+
# with gr.Column():
|
| 218 |
+
# image = gr.ImageEditor(type="pil", image_mode="L", crop_size=(512, 512))
|
| 219 |
+
# slider_x_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
| 220 |
+
# slider_y_a = gr.Dropdown(label="Slider X concept range", allow_custom_value=True, multiselect=True, max_choices=2)
|
| 221 |
+
# img2img_type = gr.Radio(["controlnet canny", "ip adapter"], label="", info="", visible=False, value="controlnet canny")
|
| 222 |
+
# prompt_a = gr.Textbox(label="Prompt")
|
| 223 |
+
# submit_a = gr.Button("Submit")
|
| 224 |
+
# with gr.Column():
|
| 225 |
+
# with gr.Group(elem_id="group"):
|
| 226 |
+
# x_a = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="x", interactive=False)
|
| 227 |
+
# y_a = gr.Slider(minimum=-10, value=0, maximum=10, elem_id="y", interactive=False)
|
| 228 |
+
# output_image_a = gr.Image(elem_id="image_out")
|
| 229 |
+
# with gr.Row():
|
| 230 |
+
# generate_butt_a = gr.Button("generate")
|
| 231 |
+
|
| 232 |
+
# with gr.Accordion(label="advanced options", open=False):
|
| 233 |
+
# iterations_a = gr.Slider(label = "num iterations", minimum=0, value=200, maximum=300)
|
| 234 |
+
# steps_a = gr.Slider(label = "num inference steps", minimum=1, value=8, maximum=30)
|
| 235 |
+
# guidance_scale_a = gr.Slider(
|
| 236 |
+
# label="Guidance scale",
|
| 237 |
+
# minimum=0.1,
|
| 238 |
+
# maximum=10.0,
|
| 239 |
+
# step=0.1,
|
| 240 |
+
# value=5,
|
| 241 |
+
# )
|
| 242 |
+
# controlnet_conditioning_scale = gr.Slider(
|
| 243 |
+
# label="controlnet conditioning scale",
|
| 244 |
+
# minimum=0.5,
|
| 245 |
+
# maximum=5.0,
|
| 246 |
+
# step=0.1,
|
| 247 |
+
# value=0.7,
|
| 248 |
+
# )
|
| 249 |
+
# ip_adapter_scale = gr.Slider(
|
| 250 |
+
# label="ip adapter scale",
|
| 251 |
+
# minimum=0.5,
|
| 252 |
+
# maximum=5.0,
|
| 253 |
+
# step=0.1,
|
| 254 |
+
# value=0.8,
|
| 255 |
+
# visible=False
|
| 256 |
+
# )
|
| 257 |
+
# seed_a = gr.Slider(minimum=0, maximum=np.iinfo(np.int32).max, label="Seed", interactive=True, randomize=True)
|
| 258 |
+
|
| 259 |
+
# submit.click(fn=generate,
|
| 260 |
+
# 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],
|
| 261 |
+
# outputs=[x, y, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, output_image])
|
| 262 |
submit.click(fn=generate,
|
| 263 |
inputs=[slider_x, prompt, seed, recalc_directions, iterations, steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x],
|
| 264 |
outputs=[x, x_concept_1, x_concept_2, avg_diff_x, output_image])
|
|
|
|
| 266 |
iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|
| 267 |
seed.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|
| 268 |
x.change(fn=update_scales, inputs=[x, prompt, seed, steps, guidance_scale, avg_diff_x], outputs=[output_image])
|
| 269 |
+
# generate_butt_a.click(fn=update_scales, inputs=[x_a,y_a, prompt_a, seed_a, steps_a, guidance_scale_a, avg_diff_x, avg_diff_y, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale], outputs=[output_image_a])
|
| 270 |
+
# submit_a.click(fn=generate,
|
| 271 |
+
# inputs=[slider_x_a, slider_y_a, prompt_a, seed_a, iterations_a, steps_a, guidance_scale_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, img2img_type, image, controlnet_conditioning_scale, ip_adapter_scale],
|
| 272 |
+
# outputs=[x_a, y_a, x_concept_1, x_concept_2, y_concept_1, y_concept_2, avg_diff_x, avg_diff_y, output_image_a])
|
| 273 |
+
|
| 274 |
|
| 275 |
if __name__ == "__main__":
|
| 276 |
demo.launch()
|