multimodalart HF staff commited on
Commit
82c73be
1 Parent(s): c5d7772

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -15
app.py CHANGED
@@ -10,7 +10,7 @@ from time import sleep
10
  import copy
11
  import json
12
  import gc
13
-
14
  with open("sdxl_loras.json", "r") as file:
15
  data = json.load(file)
16
  sdxl_loras = [
@@ -23,6 +23,8 @@ with open("sdxl_loras.json", "r") as file:
23
  "is_compatible": item["is_compatible"],
24
  "is_pivotal": item.get("is_pivotal", False),
25
  "text_embedding_weights": item.get("text_embedding_weights", None),
 
 
26
  "is_nc": item.get("is_nc", False)
27
  }
28
  for item in data
@@ -41,6 +43,8 @@ for item in sdxl_loras:
41
  item["saved_name"] = saved_name
42
  item["state_dict"] = state_dict #{k: v.to(device=device, dtype=torch.float16) for k, v in state_dict.items() if torch.is_tensor(v)}
43
 
 
 
44
  vae = AutoencoderKL.from_pretrained(
45
  "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
46
  )
@@ -55,7 +59,7 @@ pipe.to(device)
55
  last_lora = ""
56
  last_merged = False
57
  last_fused = False
58
- def update_selection(selected_state: gr.SelectData):
59
  lora_repo = sdxl_loras[selected_state.index]["repo"]
60
  instance_prompt = sdxl_loras[selected_state.index]["trigger_word"]
61
  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"
@@ -135,7 +139,7 @@ def merge_incompatible_lora(full_path_lora, lora_scale):
135
  del lora_model
136
  gc.collect()
137
 
138
- def run_lora(prompt, negative, lora_scale, selected_state, progress=gr.Progress(track_tqdm=True)):
139
  global last_lora, last_merged, last_fused, pipe
140
 
141
  if negative == "":
@@ -195,6 +199,17 @@ def run_lora(prompt, negative, lora_scale, selected_state, progress=gr.Progress(
195
  gc.collect()
196
  return image, gr.update(visible=True)
197
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  with gr.Blocks(css="custom.css") as demo:
200
  title = gr.HTML(
@@ -203,14 +218,16 @@ with gr.Blocks(css="custom.css") as demo:
203
  )
204
  selected_state = gr.State()
205
  with gr.Row():
206
- gallery = gr.Gallery(
207
- value=[(item["image"], item["title"]) for item in sdxl_loras],
208
- label="SDXL LoRA Gallery",
209
- allow_preview=False,
210
- columns=3,
211
- elem_id="gallery",
212
- show_share_button=False
213
- )
 
 
214
  with gr.Column():
215
  prompt_title = gr.Markdown(
216
  value="### Click on a LoRA in the gallery to select it",
@@ -268,9 +285,15 @@ with gr.Blocks(css="custom.css") as demo:
268
  submit_disclaimer = gr.Markdown(
269
  "This is a curated gallery by me, [apolinário (multimodal.art)](https://twitter.com/multimodalart). I'll try to include as many cool LoRAs as they are submitted! You can [duplicate this Space](https://huggingface.co/spaces/multimodalart/LoraTheExplorer?duplicate=true) to use it privately, and add your own LoRAs by editing `sdxl_loras.json` in the Files tab of your private space."
270
  )
271
-
 
 
 
 
 
272
  gallery.select(
273
  update_selection,
 
274
  outputs=[prompt_title, prompt, prompt, selected_state, use_diffusers, use_uis],
275
  queue=False,
276
  show_progress=False,
@@ -282,7 +305,7 @@ with gr.Blocks(css="custom.css") as demo:
282
  show_progress=False
283
  ).success(
284
  fn=run_lora,
285
- inputs=[prompt, negative, weight, selected_state],
286
  outputs=[result, share_group],
287
  )
288
  button.click(
@@ -296,6 +319,6 @@ with gr.Blocks(css="custom.css") as demo:
296
  outputs=[result, share_group],
297
  )
298
  share_button.click(None, [], [], _js=share_js)
299
-
300
  demo.queue(max_size=20)
301
- demo.launch()
 
10
  import copy
11
  import json
12
  import gc
13
+ import random
14
  with open("sdxl_loras.json", "r") as file:
15
  data = json.load(file)
16
  sdxl_loras = [
 
23
  "is_compatible": item["is_compatible"],
24
  "is_pivotal": item.get("is_pivotal", False),
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
  }
30
  for item in data
 
43
  item["saved_name"] = saved_name
44
  item["state_dict"] = state_dict #{k: v.to(device=device, dtype=torch.float16) for k, v in state_dict.items() if torch.is_tensor(v)}
45
 
46
+ gr_sdxl_loras = gr.State(value=sdxl_loras)
47
+
48
  vae = AutoencoderKL.from_pretrained(
49
  "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
50
  )
 
59
  last_lora = ""
60
  last_merged = False
61
  last_fused = False
62
+ def update_selection(selected_state: gr.SelectData, sdxl_loras):
63
  lora_repo = sdxl_loras[selected_state.index]["repo"]
64
  instance_prompt = sdxl_loras[selected_state.index]["trigger_word"]
65
  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"
 
139
  del lora_model
140
  gc.collect()
141
 
142
+ def run_lora(prompt, negative, lora_scale, selected_state, sdxl_loras, progress=gr.Progress(track_tqdm=True)):
143
  global last_lora, last_merged, last_fused, pipe
144
 
145
  if negative == "":
 
199
  gc.collect()
200
  return image, gr.update(visible=True)
201
 
202
+ def shuffle_gallery(sdxl_loras):
203
+ random.shuffle(sdxl_loras)
204
+ return [(item["image"], item["title"]) for item in sdxl_loras], sdxl_loras
205
+
206
+ def swap_gallery(order, sdxl_loras):
207
+ if(order == "random"):
208
+ return shuffle_gallery(sdxl_loras)
209
+ else:
210
+ sorted_gallery = sorted(sdxl_loras, key=lambda x: x.get(order, 0), reverse=True)
211
+ return [(item["image"], item["title"]) for item in sorted_gallery], sdxl_loras
212
+
213
 
214
  with gr.Blocks(css="custom.css") as demo:
215
  title = gr.HTML(
 
218
  )
219
  selected_state = gr.State()
220
  with gr.Row():
221
+ with gr.Box():
222
+ order_gallery = gr.Radio(choices=["random", "likes", "downloads"], value="random", label="Order by")
223
+ gallery = gr.Gallery(
224
+ value=[(item["image"], item["title"]) for item in sdxl_loras],
225
+ label="SDXL LoRA Gallery",
226
+ allow_preview=False,
227
+ columns=3,
228
+ elem_id="gallery",
229
+ show_share_button=False
230
+ )
231
  with gr.Column():
232
  prompt_title = gr.Markdown(
233
  value="### Click on a LoRA in the gallery to select it",
 
285
  submit_disclaimer = gr.Markdown(
286
  "This is a curated gallery by me, [apolinário (multimodal.art)](https://twitter.com/multimodalart). I'll try to include as many cool LoRAs as they are submitted! You can [duplicate this Space](https://huggingface.co/spaces/multimodalart/LoraTheExplorer?duplicate=true) to use it privately, and add your own LoRAs by editing `sdxl_loras.json` in the Files tab of your private space."
287
  )
288
+ order_gallery.change(
289
+ fn=swap_gallery,
290
+ inputs=[order_gallery, gr_sdxl_loras],
291
+ outputs=[gallery, gr_sdxl_loras],
292
+ queue=False
293
+ )
294
  gallery.select(
295
  update_selection,
296
+ inputs=[gr_sdxl_loras],
297
  outputs=[prompt_title, prompt, prompt, selected_state, use_diffusers, use_uis],
298
  queue=False,
299
  show_progress=False,
 
305
  show_progress=False
306
  ).success(
307
  fn=run_lora,
308
+ inputs=[prompt, negative, weight, selected_state, gr_sdxl_loras],
309
  outputs=[result, share_group],
310
  )
311
  button.click(
 
319
  outputs=[result, share_group],
320
  )
321
  share_button.click(None, [], [], _js=share_js)
322
+ demo.load(fn=shuffle_gallery, inputs=[gr_sdxl_loras], outputs=[gallery, gr_sdxl_loras], queue=False)
323
  demo.queue(max_size=20)
324
+ demo.launch(share=True)