Spaces:
Sleeping
Sleeping
File size: 4,923 Bytes
4120479 ba844d5 c694422 4120479 1475e41 ba844d5 5042a41 ba844d5 4120479 ba844d5 4120479 ad8076c 3eb8dac 5042a41 ba844d5 8428948 ba844d5 ce04d24 ba844d5 de0fd08 ba844d5 f494526 ba844d5 f494526 ba844d5 b4852dc ba844d5 b6fa736 ba844d5 |
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 |
import gradio as gr
from huggingface_hub import login
from diffusers import DiffusionPipeline, StableDiffusionXLPipeline
import torch
import copy
import os
import spaces
import random
hf_token = os.environ.get("HF_TOKEN")
login(token = hf_token)
original_pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
@spaces.GPU
def infer(lora_1_id, lora_1_sfts, lora_2_id, lora_2_sfts, prompt, negative_prompt, lora_1_scale, lora_2_scale, seed):
unet = copy.deepcopy(original_pipe.unet)
text_encoder = copy.deepcopy(original_pipe.text_encoder)
text_encoder_2 = copy.deepcopy(original_pipe.text_encoder_2)
pipe = StableDiffusionXLPipeline(
vae = original_pipe.vae,
text_encoder = text_encoder,
text_encoder_2 = text_encoder_2,
scheduler = original_pipe.scheduler,
tokenizer = original_pipe.tokenizer,
tokenizer_2 = original_pipe.tokenizer_2,
unet = unet
)
pipe.to("cuda")
pipe.load_lora_weights(
lora_1_id,
weight_name = lora_1_sfts,
low_cpu_mem_usage = True,
use_auth_token = True
)
pipe.fuse_lora(lora_1_scale)
pipe.load_lora_weights(
lora_2_id,
weight_name = lora_2_sfts,
low_cpu_mem_usage = True,
use_auth_token = True
)
pipe.fuse_lora(lora_2_scale)
if negative_prompt == "" :
negative_prompt = None
if seed < 0 :
seed = random.randit(0, 423538377342)
generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(
prompt = prompt,
negative_prompt = negative_prompt,
num_inference_steps = 25,
width = 1024,
height = 1024,
generator = generator
).images[0]
return image, seed
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
title = gr.HTML(
'''
<h1 style="text-align: center;">LoRA Fusion</h1>
<p style="text-align: center;">Fuse 2 custom LoRa models</p>
'''
)
# PART 1 • MODELS
with gr.Row():
with gr.Column():
lora_1_id = gr.Textbox(
label = "LoRa 1 ID",
placeholder = "username/model_id"
)
lora_1_sfts = gr.Textbox(
label = "Safetensors file",
placeholder = "specific_chosen.safetensors"
)
with gr.Column():
lora_2_id = gr.Textbox(
label = "LoRa 2 ID",
placeholder = "username/model_id"
)
lora_2_sfts = gr.Textbox(
label = "Safetensors file",
placeholder = "specific_chosen.safetensors"
)
# PART 2 • INFERENCE
with gr.Row():
prompt = gr.Textbox(
label = "Your prompt",
info = "Use your trigger words into a coherent prompt",
placeholde = "e.g: a triggerWordOne portrait in triggerWord2 style"
)
run_btn = gr.Button("Run")
output_image = gr.Image(
label = "Output"
)
# Advanced Settings
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
lora_1_scale = gr.Slider(
label = "LoRa 1 scale",
minimum = 0,
maximum = 1,
step = 0.1,
value = 0.7
)
lora_2_scale = gr.Slider(
label = "LoRa 2 scale",
minimum = 0,
maximum = 1,
step = 0.1,
value = 0.7
)
negative_prompt = gr.Textbox(
label = "Negative prompt"
)
seed = gr.Slider(
label = "Seed",
info = "-1 denotes a random seed",
minimum = -1,
maximum = 423538377342,
value = -1
)
last_used_seed = gr.Number(
label = "Last used seed",
info = "the seed used in the last generation",
)
# ACTIONS
run_btn.click(
fn = infer,
inputs = [
lora_1_id,
lora_1_sfts,
lora_2_id,
lora_2_sfts,
prompt,
negative_prompt,
lora_1_scale,
lora_2_scale,
seed
],
outputs = [
output_image,
last_used_seed
]
)
demo.queue(concurrency_count=2).launch()
|