import gradio as gr import spaces from clip_slider_pipeline import CLIPSliderFlux from diffusers import FluxPipeline import torch import numpy as np import cv2 from PIL import Image from diffusers.utils import load_image from diffusers.pipelines.flux.pipeline_flux_controlnet import FluxControlNetPipeline from diffusers.models.controlnet_flux import FluxControlNetModel def process_controlnet_img(image): controlnet_img = np.array(image) controlnet_img = cv2.Canny(controlnet_img, 100, 200) controlnet_img = HWC3(controlnet_img) controlnet_img = Image.fromarray(controlnet_img) # load pipelines pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16) #pipe.enable_model_cpu_offload() clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda")) base_model = 'black-forest-labs/FLUX.1-schnell' controlnet_model = 'InstantX/FLUX.1-dev-Controlnet-Canny-alpha' # controlnet = FluxControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16) # pipe_controlnet = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16) # t5_slider_controlnet = T5SliderFlux(sd_pipe=pipe_controlnet,device=torch.device("cuda")) @spaces.GPU(duration=200) def generate(slider_x, prompt, seed, recalc_directions, iterations, steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x, img2img_type = None, img = None, controlnet_scale= None, ip_adapter_scale=None, ): # check if avg diff for directions need to be re-calculated print("slider_x", slider_x) print("x_concept_1", x_concept_1, "x_concept_2", x_concept_2) torch.manual_seed(seed) if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions: avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations).to(torch.float16) x_concept_1, x_concept_2 = slider_x[0], slider_x[1] if img2img_type=="controlnet canny" and img is not None: control_img = process_controlnet_img(img) image = clip_slider.generate(prompt, guidance_scale=guidance_scale, image=control_img, controlnet_conditioning_scale =controlnet_scale, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=avg_diff, avg_diff_2nd=avg_diff_2nd) elif img2img_type=="ip adapter" and img is not None: image = clip_slider.generate(prompt, guidance_scale=guidance_scale, ip_adapter_image=img, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=avg_diff, avg_diff_2nd=avg_diff_2nd) else: # text to image image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=0, scale_2nd=0, seed=seed, num_inference_steps=steps, avg_diff=avg_diff) #comma_concepts_x = ', '.join(slider_x) comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}" avg_diff_x = avg_diff.cpu() return gr.update(label=comma_concepts_x, interactive=True), x_concept_1, x_concept_2, avg_diff_x, image @spaces.GPU def update_scales(x,prompt,seed, steps, guidance_scale, avg_diff_x, img2img_type = None, img = None, controlnet_scale= None, ip_adapter_scale=None,): avg_diff = avg_diff_x.cuda() torch.manual_seed(seed) if img2img_type=="controlnet canny" and img is not None: control_img = process_controlnet_img(img) 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) elif img2img_type=="ip adapter" and img is not None: image = clip_slider.generate(prompt, guidance_scale=guidance_scale, ip_adapter_image=img, scale=x,seed=seed, num_inference_steps=steps, avg_diff=avg_diff) else: image = clip_slider.generate(prompt, guidance_scale=guidance_scale, scale=x, seed=seed, num_inference_steps=steps, avg_diff=avg_diff) return image @spaces.GPU def update_x(x,y,prompt,seed, steps, avg_diff_x, avg_diff_y, img2img_type = None, img = None): avg_diff = avg_diff_x.cuda() avg_diff_2nd = avg_diff_y.cuda() 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) return image @spaces.GPU def update_y(x,y,prompt,seed, steps, avg_diff_x, avg_diff_y, img2img_type = None, img = None): avg_diff = avg_diff_x.cuda() avg_diff_2nd = avg_diff_y.cuda() 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) return image def reset_recalc_directions(): return True css = ''' #group { position: relative; width: 420px; height: 420px; margin-bottom: 20px; background-color: white } #x { position: absolute; bottom: 0; left: 25px; width: 400px; } #y { position: absolute; bottom: 20px; left: 67px; width: 400px; transform: rotate(-90deg); transform-origin: left bottom; } #image_out{position:absolute; width: 80%; right: 10px; top: 40px} ''' with gr.Blocks(css=css) as demo: gr.Markdown("""