import gradio as gr from huggingface_hub import login import os hf_token = os.environ.get("HF_TOKEN") login(token=hf_token) from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL from diffusers.utils import load_image from PIL import Image import torch import numpy as np import cv2 vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) controlnet = ControlNetModel.from_pretrained( "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16 ) pipe = StableDiffusionXLControlNetPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", controlnet=controlnet, vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) pipe.to("cuda") #pipe.enable_model_cpu_offload() def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, steps, seed, progress=gr.Progress(track_tqdm=True)): if preprocessor == "canny": image = load_image(image_in) image = np.array(image) image = cv2.Canny(image, 100, 200) image = image[:, :, None] image = np.concatenate([image, image, image], axis=2) image = Image.fromarray(image) if use_custom_model: custom_model = model_name # This is where you load your trained weights pipe.load_lora_weights(custom_model, use_auth_token=True) prompt = prompt negative_prompt = negative_prompt generator = torch.Generator(device="cuda").manual_seed(seed) if use_custom_model: lora_scale=custom_lora_weight images = pipe( prompt, negative_prompt=negative_prompt, image=image, controlnet_conditioning_scale=controlnet_conditioning_scale, guidance_scale = guidance_scale, num_inference_steps=steps, generator=generator, cross_attention_kwargs={"scale": lora_scale} ).images else: images = pipe( prompt, negative_prompt=negative_prompt, image=image, controlnet_conditioning_scale=controlnet_conditioning_scale, guidance_scale = guidance_scale, num_inference_steps=steps, generator=generator, ).images images[0].save(f"result.png") return f"result.png" css=""" #col-container{ margin: 0 auto; max-width: 680px; text-align: left; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.HTML("""