import gradio as gr import torch from diffusers import StableDiffusionXLPipeline, AutoencoderKL from blora_utils import BLOCKS, filter_lora, scale_lora 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) 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) 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(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps): content_B_LoRA_path = '' style_B_LoRA_path = 'fffiloni/b_lora_trained_test_7' 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 = 'An eagle in [v42] style' image = pipeline( prompt, generator=torch.Generator(device="cuda").manual_seed(48), num_images_per_prompt=1 ).images[0].resize((512,512)) 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(): 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, ) gr.Examples( examples = examples, inputs = [prompt] ) run_button.click( fn = main, inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs = [result] ) demo.queue().launch()