Spaces:
Paused
Paused
Commit
•
b6a8c7c
1
Parent(s):
39eaa3c
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import copy
|
|
10 |
import json
|
11 |
import gc
|
12 |
import random
|
|
|
13 |
with open("sdxl_loras.json", "r") as file:
|
14 |
data = json.load(file)
|
15 |
sdxl_loras_raw = [
|
@@ -24,7 +25,8 @@ with open("sdxl_loras.json", "r") as file:
|
|
24 |
"text_embedding_weights": item.get("text_embedding_weights", None),
|
25 |
"likes": item.get("likes", 0),
|
26 |
"downloads": item.get("downloads", 0),
|
27 |
-
"is_nc": item.get("is_nc", False)
|
|
|
28 |
}
|
29 |
for item in data
|
30 |
]
|
@@ -46,6 +48,11 @@ for item in sdxl_loras_raw:
|
|
46 |
"state_dict": state_dict
|
47 |
}
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
vae = AutoencoderKL.from_pretrained(
|
50 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
51 |
)
|
@@ -60,7 +67,14 @@ pipe.to(device)
|
|
60 |
last_lora = ""
|
61 |
last_merged = False
|
62 |
last_fused = False
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
lora_repo = sdxl_loras[selected_state.index]["repo"]
|
65 |
instance_prompt = sdxl_loras[selected_state.index]["trigger_word"]
|
66 |
new_placeholder = "Type a prompt. This LoRA applies for all prompts, no need for a trigger word" if instance_prompt == "" else "Type a prompt to use your selected LoRA"
|
@@ -103,6 +117,12 @@ def update_selection(selected_state: gr.SelectData, sdxl_loras):
|
|
103 |
- [SD.Next guide](https://github.com/vladmandic/automatic)
|
104 |
- [AUTOMATIC1111 guide](https://stable-diffusion-art.com/lora/)
|
105 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return (
|
107 |
updated_text,
|
108 |
instance_prompt,
|
@@ -110,6 +130,7 @@ def update_selection(selected_state: gr.SelectData, sdxl_loras):
|
|
110 |
selected_state,
|
111 |
use_with_diffusers,
|
112 |
use_with_uis,
|
|
|
113 |
)
|
114 |
|
115 |
|
@@ -140,9 +161,17 @@ def merge_incompatible_lora(full_path_lora, lora_scale):
|
|
140 |
del lora_model
|
141 |
gc.collect()
|
142 |
|
143 |
-
def run_lora(prompt, negative, lora_scale, selected_state, sdxl_loras, progress=gr.Progress(track_tqdm=True)):
|
144 |
global last_lora, last_merged, last_fused, pipe
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
if negative == "":
|
147 |
negative = None
|
148 |
|
@@ -212,17 +241,22 @@ def swap_gallery(order, sdxl_loras):
|
|
212 |
sorted_gallery = sorted(sdxl_loras, key=lambda x: x.get(order, 0), reverse=True)
|
213 |
return [(item["image"], item["title"]) for item in sorted_gallery], sorted_gallery
|
214 |
|
|
|
|
|
215 |
|
216 |
with gr.Blocks(css="custom.css") as demo:
|
217 |
gr_sdxl_loras = gr.State(value=sdxl_loras_raw)
|
|
|
218 |
title = gr.HTML(
|
219 |
"""<h1><img src="https://i.imgur.com/vT48NAO.png" alt="LoRA"> LoRA the Explorer</h1>""",
|
220 |
elem_id="title",
|
221 |
)
|
222 |
selected_state = gr.State()
|
223 |
with gr.Row(elem_id="main_app"):
|
224 |
-
with gr.
|
225 |
order_gallery = gr.Radio(choices=["random", "likes"], value="random", label="Order by", elem_id="order_radio")
|
|
|
|
|
226 |
gallery = gr.Gallery(
|
227 |
#value=[(item["image"], item["title"]) for item in sdxl_loras],
|
228 |
label="SDXL LoRA Gallery",
|
@@ -251,7 +285,6 @@ with gr.Blocks(css="custom.css") as demo:
|
|
251 |
with gr.Accordion("Advanced options", open=False):
|
252 |
negative = gr.Textbox(label="Negative Prompt")
|
253 |
weight = gr.Slider(0, 10, value=0.8, step=0.1, label="LoRA weight")
|
254 |
-
|
255 |
with gr.Column(elem_id="extra_info"):
|
256 |
with gr.Accordion(
|
257 |
"Use it with: 🧨 diffusers, ComfyUI, Invoke AI, SD.Next, AUTO1111",
|
@@ -265,7 +298,7 @@ with gr.Blocks(css="custom.css") as demo:
|
|
265 |
submit_title = gr.Markdown(
|
266 |
"### Streamlined submission coming soon! Until then [suggest your LoRA in the community tab](https://huggingface.co/spaces/multimodalart/LoraTheExplorer/discussions) 🤗"
|
267 |
)
|
268 |
-
with gr.
|
269 |
submit_source = gr.Radio(
|
270 |
["Hugging Face", "CivitAI"],
|
271 |
label="LoRA source",
|
@@ -298,7 +331,14 @@ with gr.Blocks(css="custom.css") as demo:
|
|
298 |
gallery.select(
|
299 |
fn=update_selection,
|
300 |
inputs=[gr_sdxl_loras],
|
301 |
-
outputs=[prompt_title, prompt, prompt, selected_state, use_diffusers, use_uis],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
queue=False,
|
303 |
show_progress=False
|
304 |
)
|
@@ -309,7 +349,7 @@ with gr.Blocks(css="custom.css") as demo:
|
|
309 |
show_progress=False
|
310 |
).success(
|
311 |
fn=run_lora,
|
312 |
-
inputs=[prompt, negative, weight, selected_state, gr_sdxl_loras],
|
313 |
outputs=[result, share_group],
|
314 |
)
|
315 |
button.click(
|
@@ -319,10 +359,10 @@ with gr.Blocks(css="custom.css") as demo:
|
|
319 |
show_progress=False
|
320 |
).success(
|
321 |
fn=run_lora,
|
322 |
-
inputs=[prompt, negative, weight, selected_state, gr_sdxl_loras],
|
323 |
outputs=[result, share_group],
|
324 |
)
|
325 |
-
share_button.click(None, [], [],
|
326 |
-
demo.load(fn=shuffle_gallery, inputs=[gr_sdxl_loras], outputs=[gallery, gr_sdxl_loras], queue=False)
|
327 |
demo.queue(max_size=20)
|
328 |
-
demo.launch()
|
|
|
10 |
import json
|
11 |
import gc
|
12 |
import random
|
13 |
+
from urllib.parse import quote
|
14 |
with open("sdxl_loras.json", "r") as file:
|
15 |
data = json.load(file)
|
16 |
sdxl_loras_raw = [
|
|
|
25 |
"text_embedding_weights": item.get("text_embedding_weights", None),
|
26 |
"likes": item.get("likes", 0),
|
27 |
"downloads": item.get("downloads", 0),
|
28 |
+
"is_nc": item.get("is_nc", False),
|
29 |
+
"new": item.get("new", False),
|
30 |
}
|
31 |
for item in data
|
32 |
]
|
|
|
48 |
"state_dict": state_dict
|
49 |
}
|
50 |
|
51 |
+
sdxl_loras_raw_new = [item for item in sdxl_loras_raw if item.get("new") == True]
|
52 |
+
|
53 |
+
sdxl_loras_raw = [item for item in sdxl_loras_raw if item.get("new") != True]
|
54 |
+
|
55 |
+
|
56 |
vae = AutoencoderKL.from_pretrained(
|
57 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
58 |
)
|
|
|
67 |
last_lora = ""
|
68 |
last_merged = False
|
69 |
last_fused = False
|
70 |
+
js = '''
|
71 |
+
var button = document.getElementById('button');
|
72 |
+
// Add a click event listener to the button
|
73 |
+
button.addEventListener('click', function() {
|
74 |
+
element.classList.add('selected');
|
75 |
+
});
|
76 |
+
'''
|
77 |
+
def update_selection(selected_state: gr.SelectData, sdxl_loras, is_new=False):
|
78 |
lora_repo = sdxl_loras[selected_state.index]["repo"]
|
79 |
instance_prompt = sdxl_loras[selected_state.index]["trigger_word"]
|
80 |
new_placeholder = "Type a prompt. This LoRA applies for all prompts, no need for a trigger word" if instance_prompt == "" else "Type a prompt to use your selected LoRA"
|
|
|
117 |
- [SD.Next guide](https://github.com/vladmandic/automatic)
|
118 |
- [AUTOMATIC1111 guide](https://stable-diffusion-art.com/lora/)
|
119 |
'''
|
120 |
+
if(is_new):
|
121 |
+
if(selected_state.index == 0):
|
122 |
+
selected_state.index = -9999
|
123 |
+
else:
|
124 |
+
selected_state.index *= -1
|
125 |
+
|
126 |
return (
|
127 |
updated_text,
|
128 |
instance_prompt,
|
|
|
130 |
selected_state,
|
131 |
use_with_diffusers,
|
132 |
use_with_uis,
|
133 |
+
gr.Gallery(selected_index=None)
|
134 |
)
|
135 |
|
136 |
|
|
|
161 |
del lora_model
|
162 |
gc.collect()
|
163 |
|
164 |
+
def run_lora(prompt, negative, lora_scale, selected_state, sdxl_loras, sdxl_loras_new, progress=gr.Progress(track_tqdm=True)):
|
165 |
global last_lora, last_merged, last_fused, pipe
|
166 |
+
print("Index when running ", selected_state.index)
|
167 |
+
if(selected_state.index < 0):
|
168 |
+
if(selected_state.index == -9999):
|
169 |
+
selected_state.index = 0
|
170 |
+
else:
|
171 |
+
selected_state.index *= -1
|
172 |
+
sdxl_loras = sdxl_loras_new
|
173 |
+
print("Selected State: ", selected_state.index)
|
174 |
+
print(sdxl_loras[selected_state.index]["repo"])
|
175 |
if negative == "":
|
176 |
negative = None
|
177 |
|
|
|
241 |
sorted_gallery = sorted(sdxl_loras, key=lambda x: x.get(order, 0), reverse=True)
|
242 |
return [(item["image"], item["title"]) for item in sorted_gallery], sorted_gallery
|
243 |
|
244 |
+
def deselect():
|
245 |
+
return gr.Gallery(selected_index=None)
|
246 |
|
247 |
with gr.Blocks(css="custom.css") as demo:
|
248 |
gr_sdxl_loras = gr.State(value=sdxl_loras_raw)
|
249 |
+
gr_sdxl_loras_new = gr.State(value=sdxl_loras_raw_new)
|
250 |
title = gr.HTML(
|
251 |
"""<h1><img src="https://i.imgur.com/vT48NAO.png" alt="LoRA"> LoRA the Explorer</h1>""",
|
252 |
elem_id="title",
|
253 |
)
|
254 |
selected_state = gr.State()
|
255 |
with gr.Row(elem_id="main_app"):
|
256 |
+
with gr.Group(elem_id="gallery_box"):
|
257 |
order_gallery = gr.Radio(choices=["random", "likes"], value="random", label="Order by", elem_id="order_radio")
|
258 |
+
selected_loras = gr.Gallery(label="Selected LoRAs", height=80, show_share_button=False, visible=False, elem_id="gallery_selected", )
|
259 |
+
new_gallery = gr.Gallery(label="New LoRAs", elem_id="gallery_new", columns=3, value=[(item["image"], item["title"]) for item in sdxl_loras_raw_new], allow_preview=False, show_share_button=False)
|
260 |
gallery = gr.Gallery(
|
261 |
#value=[(item["image"], item["title"]) for item in sdxl_loras],
|
262 |
label="SDXL LoRA Gallery",
|
|
|
285 |
with gr.Accordion("Advanced options", open=False):
|
286 |
negative = gr.Textbox(label="Negative Prompt")
|
287 |
weight = gr.Slider(0, 10, value=0.8, step=0.1, label="LoRA weight")
|
|
|
288 |
with gr.Column(elem_id="extra_info"):
|
289 |
with gr.Accordion(
|
290 |
"Use it with: 🧨 diffusers, ComfyUI, Invoke AI, SD.Next, AUTO1111",
|
|
|
298 |
submit_title = gr.Markdown(
|
299 |
"### Streamlined submission coming soon! Until then [suggest your LoRA in the community tab](https://huggingface.co/spaces/multimodalart/LoraTheExplorer/discussions) 🤗"
|
300 |
)
|
301 |
+
with gr.Group(elem_id="soon"):
|
302 |
submit_source = gr.Radio(
|
303 |
["Hugging Face", "CivitAI"],
|
304 |
label="LoRA source",
|
|
|
331 |
gallery.select(
|
332 |
fn=update_selection,
|
333 |
inputs=[gr_sdxl_loras],
|
334 |
+
outputs=[prompt_title, prompt, prompt, selected_state, use_diffusers, use_uis, new_gallery],
|
335 |
+
queue=False,
|
336 |
+
show_progress=False
|
337 |
+
)
|
338 |
+
new_gallery.select(
|
339 |
+
fn=update_selection,
|
340 |
+
inputs=[gr_sdxl_loras_new, gr.State(True)],
|
341 |
+
outputs=[prompt_title, prompt, prompt, selected_state, use_diffusers, use_uis, gallery],
|
342 |
queue=False,
|
343 |
show_progress=False
|
344 |
)
|
|
|
349 |
show_progress=False
|
350 |
).success(
|
351 |
fn=run_lora,
|
352 |
+
inputs=[prompt, negative, weight, selected_state, gr_sdxl_loras, gr_sdxl_loras_new],
|
353 |
outputs=[result, share_group],
|
354 |
)
|
355 |
button.click(
|
|
|
359 |
show_progress=False
|
360 |
).success(
|
361 |
fn=run_lora,
|
362 |
+
inputs=[prompt, negative, weight, selected_state, gr_sdxl_loras, gr_sdxl_loras_new],
|
363 |
outputs=[result, share_group],
|
364 |
)
|
365 |
+
share_button.click(None, [], [], js=share_js)
|
366 |
+
demo.load(fn=shuffle_gallery, inputs=[gr_sdxl_loras], outputs=[gallery, gr_sdxl_loras], queue=False, js=js)
|
367 |
demo.queue(max_size=20)
|
368 |
+
demo.launch(share=True)
|