Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,31 +82,18 @@ def download_file(url, directory=None):
|
|
| 82 |
|
| 83 |
return filepath
|
| 84 |
|
| 85 |
-
def update_selection(evt: gr.SelectData, selected_indices, width, height):
|
| 86 |
selected_index = evt.index
|
| 87 |
selected_indices = selected_indices or []
|
| 88 |
if selected_index in selected_indices:
|
| 89 |
-
# LoRA is already selected, remove it
|
| 90 |
selected_indices.remove(selected_index)
|
| 91 |
else:
|
| 92 |
if len(selected_indices) < 2:
|
| 93 |
selected_indices.append(selected_index)
|
| 94 |
else:
|
| 95 |
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
| 96 |
-
return (
|
| 97 |
-
gr.update(),
|
| 98 |
-
gr.update(),
|
| 99 |
-
gr.update(),
|
| 100 |
-
gr.update(),
|
| 101 |
-
gr.update(),
|
| 102 |
-
gr.update(),
|
| 103 |
-
gr.update(),
|
| 104 |
-
gr.update(),
|
| 105 |
-
gr.update(),
|
| 106 |
-
gr.update(),
|
| 107 |
-
)
|
| 108 |
|
| 109 |
-
# Initialize outputs
|
| 110 |
selected_info_1 = "Select a LoRA 1"
|
| 111 |
selected_info_2 = "Select a LoRA 2"
|
| 112 |
lora_scale_1 = 1.15
|
|
@@ -114,39 +101,25 @@ def update_selection(evt: gr.SelectData, selected_indices, width, height):
|
|
| 114 |
lora_image_1 = None
|
| 115 |
lora_image_2 = None
|
| 116 |
if len(selected_indices) >= 1:
|
| 117 |
-
lora1 =
|
| 118 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 119 |
lora_image_1 = lora1['image']
|
| 120 |
if len(selected_indices) >= 2:
|
| 121 |
-
lora2 =
|
| 122 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 123 |
lora_image_2 = lora2['image']
|
| 124 |
|
| 125 |
-
# Update prompt placeholder based on last selected LoRA
|
| 126 |
if selected_indices:
|
| 127 |
-
last_selected_lora =
|
| 128 |
new_placeholder = f"Type a prompt for {last_selected_lora['title']}"
|
| 129 |
else:
|
| 130 |
new_placeholder = "Type a prompt after selecting a LoRA"
|
| 131 |
|
| 132 |
-
return (
|
| 133 |
-
gr.update(placeholder=new_placeholder),
|
| 134 |
-
selected_info_1,
|
| 135 |
-
selected_info_2,
|
| 136 |
-
selected_indices,
|
| 137 |
-
lora_scale_1,
|
| 138 |
-
lora_scale_2,
|
| 139 |
-
width,
|
| 140 |
-
height,
|
| 141 |
-
lora_image_1,
|
| 142 |
-
lora_image_2,
|
| 143 |
-
)
|
| 144 |
|
| 145 |
-
def remove_lora_1(selected_indices):
|
| 146 |
-
selected_indices = selected_indices or []
|
| 147 |
if len(selected_indices) >= 1:
|
| 148 |
selected_indices.pop(0)
|
| 149 |
-
# Update selected_info_1 and selected_info_2
|
| 150 |
selected_info_1 = "Select a LoRA 1"
|
| 151 |
selected_info_2 = "Select a LoRA 2"
|
| 152 |
lora_scale_1 = 1.15
|
|
@@ -154,20 +127,18 @@ def remove_lora_1(selected_indices):
|
|
| 154 |
lora_image_1 = None
|
| 155 |
lora_image_2 = None
|
| 156 |
if len(selected_indices) >= 1:
|
| 157 |
-
lora1 =
|
| 158 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 159 |
lora_image_1 = lora1['image']
|
| 160 |
if len(selected_indices) >= 2:
|
| 161 |
-
lora2 =
|
| 162 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 163 |
lora_image_2 = lora2['image']
|
| 164 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
| 165 |
|
| 166 |
-
def remove_lora_2(selected_indices):
|
| 167 |
-
selected_indices = selected_indices or []
|
| 168 |
if len(selected_indices) >= 2:
|
| 169 |
selected_indices.pop(1)
|
| 170 |
-
# Update selected_info_1 and selected_info_2
|
| 171 |
selected_info_1 = "Select a LoRA 1"
|
| 172 |
selected_info_2 = "Select a LoRA 2"
|
| 173 |
lora_scale_1 = 1.15
|
|
@@ -175,21 +146,21 @@ def remove_lora_2(selected_indices):
|
|
| 175 |
lora_image_1 = None
|
| 176 |
lora_image_2 = None
|
| 177 |
if len(selected_indices) >= 1:
|
| 178 |
-
lora1 =
|
| 179 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 180 |
lora_image_1 = lora1['image']
|
| 181 |
if len(selected_indices) >= 2:
|
| 182 |
-
lora2 =
|
| 183 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 184 |
lora_image_2 = lora2['image']
|
| 185 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
| 186 |
|
| 187 |
-
def randomize_loras(selected_indices):
|
| 188 |
-
if len(
|
| 189 |
raise gr.Error("Not enough LoRAs to randomize.")
|
| 190 |
-
selected_indices = random.sample(range(len(
|
| 191 |
-
lora1 =
|
| 192 |
-
lora2 =
|
| 193 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 194 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 195 |
lora_scale_1 = 1.15
|
|
@@ -199,6 +170,106 @@ def randomize_loras(selected_indices):
|
|
| 199 |
random_prompt = random.choice(prompt_values)
|
| 200 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
@spaces.GPU(duration=70)
|
| 203 |
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
|
| 204 |
print("Generating image...")
|
|
@@ -206,8 +277,7 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
|
|
| 206 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 207 |
with calculateDuration("Generating image"):
|
| 208 |
# Generate image
|
| 209 |
-
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 210 |
-
prompt=prompt_mash,
|
| 211 |
num_inference_steps=steps,
|
| 212 |
guidance_scale=cfg_scale,
|
| 213 |
width=width,
|
|
@@ -238,11 +308,12 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
|
|
| 238 |
).images[0]
|
| 239 |
return final_image
|
| 240 |
|
| 241 |
-
|
|
|
|
| 242 |
if not selected_indices:
|
| 243 |
raise gr.Error("You must select at least one LoRA before proceeding.")
|
| 244 |
|
| 245 |
-
selected_loras = [
|
| 246 |
|
| 247 |
# Build the prompt with trigger words
|
| 248 |
prepends = []
|
|
@@ -361,108 +432,6 @@ def check_custom_model(link):
|
|
| 361 |
# Assume it's a Hugging Face model path
|
| 362 |
return get_huggingface_safetensors(link)
|
| 363 |
|
| 364 |
-
def add_custom_lora(custom_lora, selected_indices):
|
| 365 |
-
global loras
|
| 366 |
-
if custom_lora:
|
| 367 |
-
try:
|
| 368 |
-
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
| 369 |
-
print(f"Loaded custom LoRA: {repo}")
|
| 370 |
-
existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
|
| 371 |
-
if existing_item_index is None:
|
| 372 |
-
if(repo.endswith(".safetensors") and repo.startswith("http")):
|
| 373 |
-
repo = download_file(repo)
|
| 374 |
-
new_item = {
|
| 375 |
-
"image": image if image else "/home/user/app/custom.png",
|
| 376 |
-
"title": title,
|
| 377 |
-
"repo": repo,
|
| 378 |
-
"weights": path,
|
| 379 |
-
"trigger_word": trigger_word
|
| 380 |
-
}
|
| 381 |
-
print(f"New LoRA: {new_item}")
|
| 382 |
-
existing_item_index = len(loras)
|
| 383 |
-
loras.append(new_item)
|
| 384 |
-
|
| 385 |
-
# Update gallery
|
| 386 |
-
gallery_items = [(item["image"], item["title"]) for item in loras]
|
| 387 |
-
# Update selected_indices if there's room
|
| 388 |
-
if len(selected_indices) < 2:
|
| 389 |
-
selected_indices.append(existing_item_index)
|
| 390 |
-
else:
|
| 391 |
-
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
| 392 |
-
|
| 393 |
-
# Update selected_info and images
|
| 394 |
-
selected_info_1 = "Select a LoRA 1"
|
| 395 |
-
selected_info_2 = "Select a LoRA 2"
|
| 396 |
-
lora_scale_1 = 1.15
|
| 397 |
-
lora_scale_2 = 1.15
|
| 398 |
-
lora_image_1 = None
|
| 399 |
-
lora_image_2 = None
|
| 400 |
-
if len(selected_indices) >= 1:
|
| 401 |
-
lora1 = loras[selected_indices[0]]
|
| 402 |
-
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
| 403 |
-
lora_image_1 = lora1['image'] if lora1['image'] else None
|
| 404 |
-
if len(selected_indices) >= 2:
|
| 405 |
-
lora2 = loras[selected_indices[1]]
|
| 406 |
-
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
| 407 |
-
lora_image_2 = lora2['image'] if lora2['image'] else None
|
| 408 |
-
print("Chegou no final")
|
| 409 |
-
return (
|
| 410 |
-
gr.update(value=gallery_items),
|
| 411 |
-
selected_info_1,
|
| 412 |
-
selected_info_2,
|
| 413 |
-
selected_indices,
|
| 414 |
-
lora_scale_1,
|
| 415 |
-
lora_scale_2,
|
| 416 |
-
lora_image_1,
|
| 417 |
-
lora_image_2
|
| 418 |
-
)
|
| 419 |
-
except Exception as e:
|
| 420 |
-
print(e)
|
| 421 |
-
gr.Warning(str(e))
|
| 422 |
-
return gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 423 |
-
else:
|
| 424 |
-
return gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 425 |
-
|
| 426 |
-
def remove_custom_lora(selected_indices):
|
| 427 |
-
global loras
|
| 428 |
-
if loras:
|
| 429 |
-
custom_lora_repo = loras[-1]['repo']
|
| 430 |
-
# Remove from loras list
|
| 431 |
-
loras = loras[:-1]
|
| 432 |
-
# Remove from selected_indices if selected
|
| 433 |
-
custom_lora_index = len(loras)
|
| 434 |
-
if custom_lora_index in selected_indices:
|
| 435 |
-
selected_indices.remove(custom_lora_index)
|
| 436 |
-
# Update gallery
|
| 437 |
-
gallery_items = [(item["image"], item["title"]) for item in loras]
|
| 438 |
-
# Update selected_info and images
|
| 439 |
-
selected_info_1 = "Select a LoRA 1"
|
| 440 |
-
selected_info_2 = "Select a LoRA 2"
|
| 441 |
-
lora_scale_1 = 1.15
|
| 442 |
-
lora_scale_2 = 1.15
|
| 443 |
-
lora_image_1 = None
|
| 444 |
-
lora_image_2 = None
|
| 445 |
-
if len(selected_indices) >= 1:
|
| 446 |
-
lora1 = loras[selected_indices[0]]
|
| 447 |
-
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 448 |
-
lora_image_1 = lora1['image']
|
| 449 |
-
if len(selected_indices) >= 2:
|
| 450 |
-
lora2 = loras[selected_indices[1]]
|
| 451 |
-
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 452 |
-
lora_image_2 = lora2['image']
|
| 453 |
-
return (
|
| 454 |
-
gr.update(value=gallery_items),
|
| 455 |
-
selected_info_1,
|
| 456 |
-
selected_info_2,
|
| 457 |
-
selected_indices,
|
| 458 |
-
lora_scale_1,
|
| 459 |
-
lora_scale_2,
|
| 460 |
-
lora_image_1,
|
| 461 |
-
lora_image_2
|
| 462 |
-
)
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
css = '''
|
| 467 |
#gen_btn{height: 100%}
|
| 468 |
#title{text-align: center}
|
|
@@ -484,6 +453,7 @@ css = '''
|
|
| 484 |
#custom_lora_btn{margin-top: auto;margin-bottom: 11px}
|
| 485 |
#random_btn{font-size: 300%}
|
| 486 |
'''
|
|
|
|
| 487 |
with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
| 488 |
title = gr.HTML(
|
| 489 |
"""<h1><img src="https://i.imgur.com/wMh2Oek.png" alt="LoRA"> LoRA Lab [beta]</h1><br><span style="
|
|
@@ -493,6 +463,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
|
| 493 |
">Mix and match any FLUX[dev] LoRAs</span>""",
|
| 494 |
elem_id="title",
|
| 495 |
)
|
|
|
|
| 496 |
selected_indices = gr.State([])
|
| 497 |
with gr.Row():
|
| 498 |
with gr.Column(scale=3):
|
|
@@ -560,40 +531,39 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
|
| 560 |
|
| 561 |
gallery.select(
|
| 562 |
update_selection,
|
| 563 |
-
inputs=[selected_indices, width, height],
|
| 564 |
-
outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2]
|
| 565 |
-
)
|
| 566 |
remove_button_1.click(
|
| 567 |
remove_lora_1,
|
| 568 |
-
inputs=[selected_indices],
|
| 569 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 570 |
)
|
| 571 |
remove_button_2.click(
|
| 572 |
remove_lora_2,
|
| 573 |
-
inputs=[selected_indices],
|
| 574 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 575 |
)
|
| 576 |
randomize_button.click(
|
| 577 |
randomize_loras,
|
| 578 |
-
inputs=[selected_indices],
|
| 579 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, prompt]
|
| 580 |
)
|
| 581 |
add_custom_lora_button.click(
|
| 582 |
add_custom_lora,
|
| 583 |
-
inputs=[custom_lora, selected_indices],
|
| 584 |
-
outputs=[gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 585 |
)
|
| 586 |
remove_custom_lora_button.click(
|
| 587 |
remove_custom_lora,
|
| 588 |
-
inputs=[selected_indices],
|
| 589 |
-
outputs=[gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 590 |
)
|
| 591 |
gr.on(
|
| 592 |
triggers=[generate_button.click, prompt.submit],
|
| 593 |
fn=run_lora,
|
| 594 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height],
|
| 595 |
outputs=[result, seed, progress_bar]
|
| 596 |
)
|
| 597 |
|
| 598 |
app.queue()
|
| 599 |
-
app.launch()
|
|
|
|
| 82 |
|
| 83 |
return filepath
|
| 84 |
|
| 85 |
+
def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, height):
|
| 86 |
selected_index = evt.index
|
| 87 |
selected_indices = selected_indices or []
|
| 88 |
if selected_index in selected_indices:
|
|
|
|
| 89 |
selected_indices.remove(selected_index)
|
| 90 |
else:
|
| 91 |
if len(selected_indices) < 2:
|
| 92 |
selected_indices.append(selected_index)
|
| 93 |
else:
|
| 94 |
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
| 95 |
+
return gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), width, height, gr.update(), gr.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
| 97 |
selected_info_1 = "Select a LoRA 1"
|
| 98 |
selected_info_2 = "Select a LoRA 2"
|
| 99 |
lora_scale_1 = 1.15
|
|
|
|
| 101 |
lora_image_1 = None
|
| 102 |
lora_image_2 = None
|
| 103 |
if len(selected_indices) >= 1:
|
| 104 |
+
lora1 = loras_state[selected_indices[0]]
|
| 105 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 106 |
lora_image_1 = lora1['image']
|
| 107 |
if len(selected_indices) >= 2:
|
| 108 |
+
lora2 = loras_state[selected_indices[1]]
|
| 109 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 110 |
lora_image_2 = lora2['image']
|
| 111 |
|
|
|
|
| 112 |
if selected_indices:
|
| 113 |
+
last_selected_lora = loras_state[selected_indices[-1]]
|
| 114 |
new_placeholder = f"Type a prompt for {last_selected_lora['title']}"
|
| 115 |
else:
|
| 116 |
new_placeholder = "Type a prompt after selecting a LoRA"
|
| 117 |
|
| 118 |
+
return gr.update(placeholder=new_placeholder), selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
def remove_lora_1(selected_indices, loras_state):
|
|
|
|
| 121 |
if len(selected_indices) >= 1:
|
| 122 |
selected_indices.pop(0)
|
|
|
|
| 123 |
selected_info_1 = "Select a LoRA 1"
|
| 124 |
selected_info_2 = "Select a LoRA 2"
|
| 125 |
lora_scale_1 = 1.15
|
|
|
|
| 127 |
lora_image_1 = None
|
| 128 |
lora_image_2 = None
|
| 129 |
if len(selected_indices) >= 1:
|
| 130 |
+
lora1 = loras_state[selected_indices[0]]
|
| 131 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 132 |
lora_image_1 = lora1['image']
|
| 133 |
if len(selected_indices) >= 2:
|
| 134 |
+
lora2 = loras_state[selected_indices[1]]
|
| 135 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 136 |
lora_image_2 = lora2['image']
|
| 137 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
| 138 |
|
| 139 |
+
def remove_lora_2(selected_indices, loras_state):
|
|
|
|
| 140 |
if len(selected_indices) >= 2:
|
| 141 |
selected_indices.pop(1)
|
|
|
|
| 142 |
selected_info_1 = "Select a LoRA 1"
|
| 143 |
selected_info_2 = "Select a LoRA 2"
|
| 144 |
lora_scale_1 = 1.15
|
|
|
|
| 146 |
lora_image_1 = None
|
| 147 |
lora_image_2 = None
|
| 148 |
if len(selected_indices) >= 1:
|
| 149 |
+
lora1 = loras_state[selected_indices[0]]
|
| 150 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 151 |
lora_image_1 = lora1['image']
|
| 152 |
if len(selected_indices) >= 2:
|
| 153 |
+
lora2 = loras_state[selected_indices[1]]
|
| 154 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 155 |
lora_image_2 = lora2['image']
|
| 156 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
| 157 |
|
| 158 |
+
def randomize_loras(selected_indices, loras_state):
|
| 159 |
+
if len(loras_state) < 2:
|
| 160 |
raise gr.Error("Not enough LoRAs to randomize.")
|
| 161 |
+
selected_indices = random.sample(range(len(loras_state)), 2)
|
| 162 |
+
lora1 = loras_state[selected_indices[0]]
|
| 163 |
+
lora2 = loras_state[selected_indices[1]]
|
| 164 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 165 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 166 |
lora_scale_1 = 1.15
|
|
|
|
| 170 |
random_prompt = random.choice(prompt_values)
|
| 171 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
|
| 172 |
|
| 173 |
+
def add_custom_lora(custom_lora, selected_indices, current_loras):
|
| 174 |
+
if custom_lora:
|
| 175 |
+
try:
|
| 176 |
+
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
| 177 |
+
print(f"Loaded custom LoRA: {repo}")
|
| 178 |
+
existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
|
| 179 |
+
if existing_item_index is None:
|
| 180 |
+
if repo.endswith(".safetensors") and repo.startswith("http"):
|
| 181 |
+
repo = download_file(repo)
|
| 182 |
+
new_item = {
|
| 183 |
+
"image": image if image else "/home/user/app/custom.png",
|
| 184 |
+
"title": title,
|
| 185 |
+
"repo": repo,
|
| 186 |
+
"weights": path,
|
| 187 |
+
"trigger_word": trigger_word
|
| 188 |
+
}
|
| 189 |
+
print(f"New LoRA: {new_item}")
|
| 190 |
+
existing_item_index = len(current_loras)
|
| 191 |
+
current_loras.append(new_item)
|
| 192 |
+
|
| 193 |
+
# Update gallery
|
| 194 |
+
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
| 195 |
+
# Update selected_indices if there's room
|
| 196 |
+
if len(selected_indices) < 2:
|
| 197 |
+
selected_indices.append(existing_item_index)
|
| 198 |
+
else:
|
| 199 |
+
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
| 200 |
+
|
| 201 |
+
# Update selected_info and images
|
| 202 |
+
selected_info_1 = "Select a LoRA 1"
|
| 203 |
+
selected_info_2 = "Select a LoRA 2"
|
| 204 |
+
lora_scale_1 = 1.15
|
| 205 |
+
lora_scale_2 = 1.15
|
| 206 |
+
lora_image_1 = None
|
| 207 |
+
lora_image_2 = None
|
| 208 |
+
if len(selected_indices) >= 1:
|
| 209 |
+
lora1 = current_loras[selected_indices[0]]
|
| 210 |
+
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
| 211 |
+
lora_image_1 = lora1['image'] if lora1['image'] else None
|
| 212 |
+
if len(selected_indices) >= 2:
|
| 213 |
+
lora2 = current_loras[selected_indices[1]]
|
| 214 |
+
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
| 215 |
+
lora_image_2 = lora2['image'] if lora2['image'] else None
|
| 216 |
+
print("Finished adding custom LoRA")
|
| 217 |
+
return (
|
| 218 |
+
current_loras,
|
| 219 |
+
gr.update(value=gallery_items),
|
| 220 |
+
selected_info_1,
|
| 221 |
+
selected_info_2,
|
| 222 |
+
selected_indices,
|
| 223 |
+
lora_scale_1,
|
| 224 |
+
lora_scale_2,
|
| 225 |
+
lora_image_1,
|
| 226 |
+
lora_image_2
|
| 227 |
+
)
|
| 228 |
+
except Exception as e:
|
| 229 |
+
print(e)
|
| 230 |
+
gr.Warning(str(e))
|
| 231 |
+
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 232 |
+
else:
|
| 233 |
+
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 234 |
+
|
| 235 |
+
def remove_custom_lora(selected_indices, current_loras):
|
| 236 |
+
if current_loras:
|
| 237 |
+
custom_lora_repo = current_loras[-1]['repo']
|
| 238 |
+
# Remove from loras list
|
| 239 |
+
current_loras = current_loras[:-1]
|
| 240 |
+
# Remove from selected_indices if selected
|
| 241 |
+
custom_lora_index = len(current_loras)
|
| 242 |
+
if custom_lora_index in selected_indices:
|
| 243 |
+
selected_indices.remove(custom_lora_index)
|
| 244 |
+
# Update gallery
|
| 245 |
+
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
| 246 |
+
# Update selected_info and images
|
| 247 |
+
selected_info_1 = "Select a LoRA 1"
|
| 248 |
+
selected_info_2 = "Select a LoRA 2"
|
| 249 |
+
lora_scale_1 = 1.15
|
| 250 |
+
lora_scale_2 = 1.15
|
| 251 |
+
lora_image_1 = None
|
| 252 |
+
lora_image_2 = None
|
| 253 |
+
if len(selected_indices) >= 1:
|
| 254 |
+
lora1 = current_loras[selected_indices[0]]
|
| 255 |
+
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 256 |
+
lora_image_1 = lora1['image']
|
| 257 |
+
if len(selected_indices) >= 2:
|
| 258 |
+
lora2 = current_loras[selected_indices[1]]
|
| 259 |
+
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 260 |
+
lora_image_2 = lora2['image']
|
| 261 |
+
return (
|
| 262 |
+
current_loras,
|
| 263 |
+
gr.update(value=gallery_items),
|
| 264 |
+
selected_info_1,
|
| 265 |
+
selected_info_2,
|
| 266 |
+
selected_indices,
|
| 267 |
+
lora_scale_1,
|
| 268 |
+
lora_scale_2,
|
| 269 |
+
lora_image_1,
|
| 270 |
+
lora_image_2
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
@spaces.GPU(duration=70)
|
| 274 |
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
|
| 275 |
print("Generating image...")
|
|
|
|
| 277 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 278 |
with calculateDuration("Generating image"):
|
| 279 |
# Generate image
|
| 280 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(prompt=prompt_mash,
|
|
|
|
| 281 |
num_inference_steps=steps,
|
| 282 |
guidance_scale=cfg_scale,
|
| 283 |
width=width,
|
|
|
|
| 308 |
).images[0]
|
| 309 |
return final_image
|
| 310 |
|
| 311 |
+
@spaces.GPU(duration=70)
|
| 312 |
+
def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state, progress=gr.Progress(track_tqdm=True)):
|
| 313 |
if not selected_indices:
|
| 314 |
raise gr.Error("You must select at least one LoRA before proceeding.")
|
| 315 |
|
| 316 |
+
selected_loras = [loras_state[idx] for idx in selected_indices]
|
| 317 |
|
| 318 |
# Build the prompt with trigger words
|
| 319 |
prepends = []
|
|
|
|
| 432 |
# Assume it's a Hugging Face model path
|
| 433 |
return get_huggingface_safetensors(link)
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
css = '''
|
| 436 |
#gen_btn{height: 100%}
|
| 437 |
#title{text-align: center}
|
|
|
|
| 453 |
#custom_lora_btn{margin-top: auto;margin-bottom: 11px}
|
| 454 |
#random_btn{font-size: 300%}
|
| 455 |
'''
|
| 456 |
+
|
| 457 |
with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
| 458 |
title = gr.HTML(
|
| 459 |
"""<h1><img src="https://i.imgur.com/wMh2Oek.png" alt="LoRA"> LoRA Lab [beta]</h1><br><span style="
|
|
|
|
| 463 |
">Mix and match any FLUX[dev] LoRAs</span>""",
|
| 464 |
elem_id="title",
|
| 465 |
)
|
| 466 |
+
loras_state = gr.State(loras)
|
| 467 |
selected_indices = gr.State([])
|
| 468 |
with gr.Row():
|
| 469 |
with gr.Column(scale=3):
|
|
|
|
| 531 |
|
| 532 |
gallery.select(
|
| 533 |
update_selection,
|
| 534 |
+
inputs=[selected_indices, loras_state, width, height],
|
| 535 |
+
outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2])
|
|
|
|
| 536 |
remove_button_1.click(
|
| 537 |
remove_lora_1,
|
| 538 |
+
inputs=[selected_indices, loras_state],
|
| 539 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 540 |
)
|
| 541 |
remove_button_2.click(
|
| 542 |
remove_lora_2,
|
| 543 |
+
inputs=[selected_indices, loras_state],
|
| 544 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 545 |
)
|
| 546 |
randomize_button.click(
|
| 547 |
randomize_loras,
|
| 548 |
+
inputs=[selected_indices, loras_state],
|
| 549 |
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, prompt]
|
| 550 |
)
|
| 551 |
add_custom_lora_button.click(
|
| 552 |
add_custom_lora,
|
| 553 |
+
inputs=[custom_lora, selected_indices, loras_state],
|
| 554 |
+
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 555 |
)
|
| 556 |
remove_custom_lora_button.click(
|
| 557 |
remove_custom_lora,
|
| 558 |
+
inputs=[selected_indices, loras_state],
|
| 559 |
+
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 560 |
)
|
| 561 |
gr.on(
|
| 562 |
triggers=[generate_button.click, prompt.submit],
|
| 563 |
fn=run_lora,
|
| 564 |
+
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
|
| 565 |
outputs=[result, seed, progress_bar]
|
| 566 |
)
|
| 567 |
|
| 568 |
app.queue()
|
| 569 |
+
app.launch()
|