import gradio as gr import os import numpy as np import random from huggingface_hub import login import torch from diffusers import StableDiffusionXLPipeline, AutoencoderKL from blora_utils import BLOCKS, filter_lora, scale_lora hf_token = os.environ.get("HF_TOKEN") login(token=hf_token) MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 1024 vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) pipeline = StableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, ).to("cuda") def load_b_lora_to_unet(pipe, content_lora_model_id: str = '', style_lora_model_id: str = '', content_alpha: float = 1., style_alpha: float = 1.) -> None: try: # Get Content B-LoRA SD if content_lora_model_id: content_B_LoRA_sd, _ = pipe.lora_state_dict(content_lora_model_id, use_auth_token=True) content_B_LoRA = filter_lora(content_B_LoRA_sd, BLOCKS['content']) content_B_LoRA = scale_lora(content_B_LoRA, content_alpha) else: content_B_LoRA = {} # Get Style B-LoRA SD if style_lora_model_id: style_B_LoRA_sd, _ = pipe.lora_state_dict(style_lora_model_id, use_auth_token=True) style_B_LoRA = filter_lora(style_B_LoRA_sd, BLOCKS['style']) style_B_LoRA = scale_lora(style_B_LoRA, style_alpha) else: style_B_LoRA = {} # Merge B-LoRAs SD res_lora = {**content_B_LoRA, **style_B_LoRA} # Load pipe.load_lora_into_unet(res_lora, None, pipe.unet) except Exception as e: raise type(e)(f'failed to load_b_lora_to_unet, due to: {e}') def main(content_b_lora, style_b_lora, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps): if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator().manual_seed(seed) if content_b_lora is None: content_B_LoRA_path = '' else: content_B_LoRA_path = content_b_lora if style_b_lora is None: style_B_LoRA_path = '' else: style_B_LoRA_path = style_b_lora content_alpha,style_alpha = 1,1.1 load_b_lora_to_unet(pipeline, content_B_LoRA_path, style_B_LoRA_path, content_alpha, style_alpha) prompt = prompt image = pipeline( prompt, generator=generator, num_images_per_prompt=1, width = width, height = height, ).images[0] pipeline.unload_lora_weights() return image css=""" #col-container { margin: 0 auto; max-width: 520px; } """ if torch.cuda.is_available(): power_device = "GPU" else: power_device = "CPU" with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown(f""" # Text-to-Image Gradio Template Currently running on {power_device}. """) with gr.Row(): content_b_lora = gr.Textbox(label="B-LoRa for content") style_b_lora = gr.Textbox(label="B-LoRa for style") with gr.Row(): prompt = gr.Text( label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False, ) run_button = gr.Button("Run", scale=0) result = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): negative_prompt = gr.Text( label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=False, ) seed = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, ) randomize_seed = gr.Checkbox(label="Randomize seed", value=True) with gr.Row(): width = gr.Slider( label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512, ) height = gr.Slider( label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512, ) with gr.Row(): guidance_scale = gr.Slider( label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0, ) num_inference_steps = gr.Slider( label="Number of inference steps", minimum=1, maximum=50, step=1, value=50, ) run_button.click( fn = main, inputs = [content_b_lora, style_b_lora, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs = [result] ) demo.queue().launch()