Spaces:
Sleeping
Sleeping
File size: 8,417 Bytes
a71cef1 536d153 f87d76f 7af6efa 536d153 3f1e323 67d94fb 40810f3 67d94fb f86f2f4 536d153 0adf250 536d153 0adf250 536d153 ac813bc 777940d ac813bc fd52df0 ac813bc 777940d ac813bc fd52df0 ac813bc a71cef1 3629c9d e96f8bf eb7ac3e e96f8bf 536d153 e96f8bf 536d153 e96f8bf 97a751a 536d153 3629c9d 536d153 eb7ac3e ac813bc f86f2f4 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
import spaces
import gradio as gr
import os
import numpy as np
import random
from huggingface_hub import login, ModelCard
import torch
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
from blora_utils import BLOCKS, filter_lora, scale_lora
hf_token = os.environ.get("YOUR_HF_TOKEN_WITH_READ_PERMISSION")
login(token=hf_token)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
SAMPLE_MODEL_IDS = [
'lora-library/B-LoRA-teddybear',
'lora-library/B-LoRA-bull',
'lora-library/B-LoRA-wolf_plushie',
'lora-library/B-LoRA-pen_sketch',
'lora-library/B-LoRA-cartoon_line',
'lora-library/B-LoRA-child',
'lora-library/B-LoRA-vase',
'lora-library/B-LoRA-scary_mug',
'lora-library/B-LoRA-statue',
'lora-library/B-LoRA-colorful_teapot',
'lora-library/B-LoRA-grey_sloth_plushie',
'lora-library/B-LoRA-teapot',
'lora-library/B-LoRA-backpack_dog',
'lora-library/B-LoRA-buddha',
'lora-library/B-LoRA-dog6',
'lora-library/B-LoRA-poop_emoji',
'lora-library/B-LoRA-pot',
'lora-library/B-LoRA-fat_bird',
'lora-library/B-LoRA-elephant',
'lora-library/B-LoRA-metal_bird',
'lora-library/B-LoRA-cat',
'lora-library/B-LoRA-dog2',
'lora-library/B-LoRA-drawing1',
'lora-library/B-LoRA-village_oil',
'lora-library/B-LoRA-watercolor',
'lora-library/B-LoRA-house_3d',
'lora-library/B-LoRA-ink_sketch',
'lora-library/B-LoRA-drawing3',
'lora-library/B-LoRA-crayon_drawing',
'lora-library/B-LoRA-kiss',
'lora-library/B-LoRA-drawing4',
'lora-library/B-LoRA-working_cartoon',
'lora-library/B-LoRA-painting',
'lora-library/B-LoRA-drawing2'
'lora-library/B-LoRA-multi-dog2',
]
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 != "" and content_b_lora is not None:
# Get instance_prompt a.k.a trigger word
content_model_card = ModelCard.load(content_b_lora)
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 != "" and style_b_lora is not None:
# Get instance_prompt a.k.a trigger word
style_model_card = ModelCard.load(style_b_lora)
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
@spaces.GPU()
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.Dropdown(
label="B-LoRa for content",
allow_custom_value=True,
choices=SAMPLE_MODEL_IDS
)
style_b_lora = gr.Dropdown(
label="B-LoRa for style",
allow_custom_value=True,
choices=SAMPLE_MODEL_IDS
)
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() |