Spaces:
Sleeping
Sleeping
File size: 6,769 Bytes
536d153 f87d76f 67d94fb 536d153 67d94fb 40810f3 67d94fb 536d153 0adf250 536d153 0adf250 536d153 ac813bc 3629c9d e96f8bf eb7ac3e e96f8bf 536d153 e96f8bf 536d153 e96f8bf 97a751a 536d153 3629c9d 536d153 eb7ac3e ac813bc eb7ac3e 536d153 ac813bc 536d153 40810f3 536d153 40810f3 536d153 ac813bc 536d153 eb7ac3e 536d153 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
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 = 2048
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 load_b_loras(content_b_lora, style_b_lora):
if content_b_lora is not None:
# Get instance_prompt a.k.a trigger word
content_model_card = ModelCard.load(custom_model)
content_model_repo_data = content_model_card.data.to_dict()
content_model_instance_prompt = content_model_repo_data.get("instance_prompt")
else:
content_model_instance_prompt = ''
if style_b_lora is not None:
# Get instance_prompt a.k.a trigger word
style_model_card = ModelCard.load(custom_model)
style_model_repo_data = style_model_card.data.to_dict()
style_model_instance_prompt = style_model_repo_data.get("instance_prompt")
style_model_instance_prompt = f"in {style_model_instance_prompt} style"
else:
style_model_instance_prompt = ''
prepared_prompt = f"{content_model_instance_prompt} {style_model_instance_prompt}"
return prepared_prompt
def main(content_b_lora, style_b_lora, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
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"""
# B-LoRas Inference
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.Column():
load_b_loras_btn = gr.Button("load models")
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=1024,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
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,
)
load_b_loras_btn.click(
fn = load_b_loras,
inputs = [content_b_lora, style_b_lora],
outputs = [prompt]
)
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() |