Spaces:
Paused
Paused
Commit
•
5715833
1
Parent(s):
8df8f4d
Update a few things
Browse files- app.py +119 -45
- custom.css +21 -0
- lora.png +0 -0
- share_btn.py +75 -0
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
4 |
from huggingface_hub import hf_hub_download
|
|
|
5 |
import lora
|
6 |
from time import sleep
|
7 |
import copy
|
@@ -24,14 +25,7 @@ saved_names = [
|
|
24 |
hf_hub_download(repo_id, filename) for _, _, repo_id, _, filename, _ in sdxl_loras
|
25 |
]
|
26 |
|
27 |
-
device = "cuda"
|
28 |
-
|
29 |
-
def update_selection(selected_state: gr.SelectData):
|
30 |
-
lora_repo = sdxl_loras[selected_state.index][2]
|
31 |
-
instance_prompt = sdxl_loras[selected_state.index][3]
|
32 |
-
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo})"
|
33 |
-
return updated_text, instance_prompt, selected_state
|
34 |
-
|
35 |
|
36 |
vae = AutoencoderKL.from_pretrained(
|
37 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
@@ -48,8 +42,57 @@ last_lora = ""
|
|
48 |
last_merged = False
|
49 |
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def run_lora(prompt, negative, weight, selected_state):
|
52 |
global last_lora, last_merged, pipe
|
|
|
|
|
|
|
|
|
53 |
if not selected_state:
|
54 |
raise gr.Error("You must select a LoRA")
|
55 |
repo_name = sdxl_loras[selected_state.index][2]
|
@@ -73,8 +116,7 @@ def run_lora(prompt, negative, weight, selected_state):
|
|
73 |
multiplier = float(weight)
|
74 |
else:
|
75 |
multiplier = 1.0
|
76 |
-
|
77 |
-
#multiplier = torch.tensor([multiplier], dtype=torch.float16, device=device)
|
78 |
lora_model, weights_sd = lora.create_network_from_weights(
|
79 |
multiplier,
|
80 |
full_path_lora,
|
@@ -83,9 +125,6 @@ def run_lora(prompt, negative, weight, selected_state):
|
|
83 |
pipe.unet,
|
84 |
for_inference=True,
|
85 |
)
|
86 |
-
#lora_model = lora_model.to("cuda")
|
87 |
-
#lora_model.apply_to(pipe.text_encoder, pipe.unet)
|
88 |
-
lora_model = lora_model.to("cuda")
|
89 |
lora_model.merge_to(
|
90 |
pipe.text_encoder, pipe.unet, weights_sd, torch.float16, "cuda"
|
91 |
)
|
@@ -94,26 +133,22 @@ def run_lora(prompt, negative, weight, selected_state):
|
|
94 |
image = pipe(
|
95 |
prompt=prompt,
|
96 |
negative_prompt=negative,
|
|
|
|
|
97 |
num_inference_steps=20,
|
98 |
guidance_scale=7.5,
|
99 |
cross_attention_kwargs=cross_attention_kwargs,
|
100 |
).images[0]
|
101 |
last_lora = repo_name
|
102 |
-
return image
|
103 |
|
104 |
|
105 |
-
css
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
#gallery{display:flex}
|
112 |
-
#gallery .grid-wrap{min-height: 100%;}
|
113 |
-
"""
|
114 |
-
|
115 |
-
with gr.Blocks(css=css) as demo:
|
116 |
-
title = gr.Markdown("# LoRA the Explorer 🔎", elem_id="title")
|
117 |
with gr.Row():
|
118 |
gallery = gr.Gallery(
|
119 |
value=[(a, b) for a, b, _, _, _, _ in sdxl_loras],
|
@@ -124,39 +159,78 @@ with gr.Blocks(css=css) as demo:
|
|
124 |
)
|
125 |
with gr.Column():
|
126 |
prompt_title = gr.Markdown(
|
127 |
-
value="### Click on a LoRA in the gallery to select it",
|
|
|
|
|
128 |
)
|
129 |
with gr.Row():
|
130 |
prompt = gr.Textbox(label="Prompt", elem_id="prompt")
|
131 |
button = gr.Button("Run", elem_id="run_button")
|
132 |
-
result = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
with gr.Accordion("Advanced options", open=False):
|
134 |
negative = gr.Textbox(label="Negative Prompt")
|
135 |
-
weight = gr.Slider(0,
|
136 |
-
|
137 |
-
|
138 |
-
with gr.
|
139 |
-
with
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
with gr.
|
144 |
-
gr.Markdown("")
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
gallery.select(
|
149 |
update_selection,
|
150 |
-
outputs=[prompt_title, prompt, selected_state],
|
151 |
queue=False,
|
152 |
show_progress=False,
|
153 |
)
|
154 |
prompt.submit(
|
155 |
-
fn=run_lora,
|
|
|
|
|
156 |
)
|
157 |
button.click(
|
158 |
-
fn=run_lora,
|
|
|
|
|
159 |
)
|
160 |
-
|
161 |
|
162 |
demo.launch()
|
|
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
6 |
import lora
|
7 |
from time import sleep
|
8 |
import copy
|
|
|
25 |
hf_hub_download(repo_id, filename) for _, _, repo_id, _, filename, _ in sdxl_loras
|
26 |
]
|
27 |
|
28 |
+
device = "cuda" # replace this to `mps` if on a MacOS Silicon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
vae = AutoencoderKL.from_pretrained(
|
31 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
|
|
42 |
last_merged = False
|
43 |
|
44 |
|
45 |
+
def update_selection(selected_state: gr.SelectData):
|
46 |
+
lora_repo = sdxl_loras[selected_state.index][2]
|
47 |
+
instance_prompt = sdxl_loras[selected_state.index][3]
|
48 |
+
weight_name = sdxl_loras[selected_state.index][4]
|
49 |
+
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo})"
|
50 |
+
use_with_diffusers = f"""
|
51 |
+
## Using [`{lora_repo}`](https://huggingface.co/{lora_repo})
|
52 |
+
|
53 |
+
## Use it with diffusers:
|
54 |
+
|
55 |
+
```python
|
56 |
+
from diffusers import StableDiffusionXLPipeline
|
57 |
+
import torch
|
58 |
+
|
59 |
+
model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
60 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
61 |
+
pipe.to("cuda")
|
62 |
+
pipe.load_lora_weights("{lora_repo}", weight_name={weight_name})
|
63 |
+
|
64 |
+
|
65 |
+
prompt = "{instance_prompt}..."
|
66 |
+
lora_weight = 0.5
|
67 |
+
image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5, cross_attention_kwargs={{"scale":lora_weight}}).images[0]
|
68 |
+
image.save("image.png")
|
69 |
+
```
|
70 |
+
"""
|
71 |
+
use_with_uis = f"""
|
72 |
+
## Use it with Comfy UI, Invoke AI, SD.Next, AUTO1111:
|
73 |
+
|
74 |
+
### Download the `*.safetensors` weights of [here](https://huggingface.co/{lora_repo}/resolve/main/{weight_name})
|
75 |
+
|
76 |
+
- [ComfyUI guide](https://comfyanonymous.github.io/ComfyUI_examples/lora/)
|
77 |
+
- [Invoke AI guide](https://invoke-ai.github.io/InvokeAI/features/CONCEPTS/?h=lora#using-loras)
|
78 |
+
- [SD.Next guide](https://github.com/vladmandic/automatic)
|
79 |
+
- [AUTOMATIC1111 guide](https://stable-diffusion-art.com/lora/)
|
80 |
+
"""
|
81 |
+
return (
|
82 |
+
updated_text,
|
83 |
+
instance_prompt,
|
84 |
+
selected_state,
|
85 |
+
use_with_diffusers,
|
86 |
+
use_with_uis,
|
87 |
+
)
|
88 |
+
|
89 |
+
|
90 |
def run_lora(prompt, negative, weight, selected_state):
|
91 |
global last_lora, last_merged, pipe
|
92 |
+
|
93 |
+
if negative == "":
|
94 |
+
negative = None
|
95 |
+
|
96 |
if not selected_state:
|
97 |
raise gr.Error("You must select a LoRA")
|
98 |
repo_name = sdxl_loras[selected_state.index][2]
|
|
|
116 |
multiplier = float(weight)
|
117 |
else:
|
118 |
multiplier = 1.0
|
119 |
+
|
|
|
120 |
lora_model, weights_sd = lora.create_network_from_weights(
|
121 |
multiplier,
|
122 |
full_path_lora,
|
|
|
125 |
pipe.unet,
|
126 |
for_inference=True,
|
127 |
)
|
|
|
|
|
|
|
128 |
lora_model.merge_to(
|
129 |
pipe.text_encoder, pipe.unet, weights_sd, torch.float16, "cuda"
|
130 |
)
|
|
|
133 |
image = pipe(
|
134 |
prompt=prompt,
|
135 |
negative_prompt=negative,
|
136 |
+
width=768,
|
137 |
+
height=768,
|
138 |
num_inference_steps=20,
|
139 |
guidance_scale=7.5,
|
140 |
cross_attention_kwargs=cross_attention_kwargs,
|
141 |
).images[0]
|
142 |
last_lora = repo_name
|
143 |
+
return image, gr.update(visible=True)
|
144 |
|
145 |
|
146 |
+
with gr.Blocks(css="custom.css") as demo:
|
147 |
+
title = gr.HTML(
|
148 |
+
"""<h1><img src="https://i.imgur.com/vT48NAO.png" alt="LoRA"> LoRA the Explorer</h1>""",
|
149 |
+
elem_id="title",
|
150 |
+
)
|
151 |
+
selected_state = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
with gr.Row():
|
153 |
gallery = gr.Gallery(
|
154 |
value=[(a, b) for a, b, _, _, _, _ in sdxl_loras],
|
|
|
159 |
)
|
160 |
with gr.Column():
|
161 |
prompt_title = gr.Markdown(
|
162 |
+
value="### Click on a LoRA in the gallery to select it",
|
163 |
+
visible=True,
|
164 |
+
elem_id="selected_lora",
|
165 |
)
|
166 |
with gr.Row():
|
167 |
prompt = gr.Textbox(label="Prompt", elem_id="prompt")
|
168 |
button = gr.Button("Run", elem_id="run_button")
|
169 |
+
result = gr.Image(
|
170 |
+
interactive=False, label="Generated Image", elem_id="result-image"
|
171 |
+
)
|
172 |
+
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
173 |
+
community_icon = gr.HTML(community_icon_html)
|
174 |
+
loading_icon = gr.HTML(loading_icon_html)
|
175 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
176 |
with gr.Accordion("Advanced options", open=False):
|
177 |
negative = gr.Textbox(label="Negative Prompt")
|
178 |
+
weight = gr.Slider(0, 10, value=1, step=0.1, label="LoRA weight")
|
179 |
+
|
180 |
+
with gr.Column(elem_id="extra_info"):
|
181 |
+
with gr.Accordion(
|
182 |
+
"Use it with: 🧨 diffusers, ComfyUI, Invoke AI, SD.Next, AUTO1111",
|
183 |
+
open=False,
|
184 |
+
elem_id="accordion",
|
185 |
+
):
|
186 |
+
with gr.Row():
|
187 |
+
use_diffusers = gr.Markdown("""## Select a LoRA first 🤗""")
|
188 |
+
use_uis = gr.Markdown()
|
189 |
+
with gr.Accordion("Submit a LoRA! 📥", open=False):
|
190 |
+
submit_title = gr.Markdown(
|
191 |
+
"### Streamlined submission coming soon! Until then [suggest your LoRA in the community tab](https://huggingface.co/spaces/multimodalart/LoraTheExplorer/discussions) 🤗"
|
192 |
+
)
|
193 |
+
with gr.Box(elem_id="soon"):
|
194 |
+
submit_source = gr.Radio(
|
195 |
+
["Hugging Face", "CivitAI"],
|
196 |
+
label="LoRA source",
|
197 |
+
value="Hugging Face",
|
198 |
+
)
|
199 |
+
with gr.Row():
|
200 |
+
submit_source_hf = gr.Textbox(
|
201 |
+
label="Hugging Face Model Repo",
|
202 |
+
info="In the format `username/model_id`",
|
203 |
+
)
|
204 |
+
submit_safetensors_hf = gr.Textbox(
|
205 |
+
label="Safetensors filename",
|
206 |
+
info="The filename `*.safetensors` in the model repo",
|
207 |
+
)
|
208 |
+
with gr.Row():
|
209 |
+
submit_trigger_word_hf = gr.Textbox(label="Trigger word")
|
210 |
+
submit_image = gr.Image(
|
211 |
+
label="Example image (optional if the repo already contains images)"
|
212 |
+
)
|
213 |
+
submit_button = gr.Button("Submit!")
|
214 |
+
submit_disclaimer = gr.Markdown(
|
215 |
+
"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."
|
216 |
+
)
|
217 |
+
|
218 |
gallery.select(
|
219 |
update_selection,
|
220 |
+
outputs=[prompt_title, prompt, selected_state, use_diffusers, use_uis],
|
221 |
queue=False,
|
222 |
show_progress=False,
|
223 |
)
|
224 |
prompt.submit(
|
225 |
+
fn=run_lora,
|
226 |
+
inputs=[prompt, negative, weight, selected_state],
|
227 |
+
outputs=[result, share_group],
|
228 |
)
|
229 |
button.click(
|
230 |
+
fn=run_lora,
|
231 |
+
inputs=[prompt, negative, weight, selected_state],
|
232 |
+
outputs=[result, share_group],
|
233 |
)
|
234 |
+
share_button.click(None, [], [], _js=share_js)
|
235 |
|
236 |
demo.launch()
|
custom.css
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#title{text-align: center;margin-bottom: 0.5em;}
|
2 |
+
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
3 |
+
#title img{width: 100px; margin-right: 0.5em}
|
4 |
+
#prompt textarea{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
|
5 |
+
#run_button{position:absolute;margin-top: 38px;right: 0;margin-right: 0.8em;border-bottom-left-radius: 0px;
|
6 |
+
border-top-left-radius: 0px;}
|
7 |
+
#gallery{display:flex}
|
8 |
+
#gallery .grid-wrap{min-height: 100%;}
|
9 |
+
#accordion code{word-break: break-all;word-wrap: break-word;white-space: pre-wrap}
|
10 |
+
#soon{opacity: 0.55; pointer-events: none}
|
11 |
+
#soon button{width: 100%}
|
12 |
+
#share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
|
13 |
+
div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
|
14 |
+
#share-btn-container:hover {background-color: #060606}
|
15 |
+
#share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
|
16 |
+
#share-btn * {all: unset}
|
17 |
+
#share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
|
18 |
+
#share-btn-container .wrap {display: none !important}
|
19 |
+
#share-btn-container.hidden {display: none!important}
|
20 |
+
#extra_info{margin-top: 1em}
|
21 |
+
.pending .min {min-height: auto}
|
lora.png
ADDED
share_btn.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
|
2 |
+
<path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
|
3 |
+
<path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
|
4 |
+
</svg>"""
|
5 |
+
|
6 |
+
loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
|
7 |
+
style="color: #ffffff;
|
8 |
+
"
|
9 |
+
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
10 |
+
|
11 |
+
share_js = """async () => {
|
12 |
+
async function uploadFile(file){
|
13 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
14 |
+
const response = await fetch(UPLOAD_URL, {
|
15 |
+
method: 'POST',
|
16 |
+
headers: {
|
17 |
+
'Content-Type': file.type,
|
18 |
+
'X-Requested-With': 'XMLHttpRequest',
|
19 |
+
},
|
20 |
+
body: file, /// <- File inherits from Blob
|
21 |
+
});
|
22 |
+
const url = await response.text();
|
23 |
+
return url;
|
24 |
+
}
|
25 |
+
|
26 |
+
async function getInputImgFile(imgEl){
|
27 |
+
const res = await fetch(imgEl.src);
|
28 |
+
const blob = await res.blob();
|
29 |
+
const imgId = Date.now() % 200;
|
30 |
+
const isPng = imgEl.src.startsWith(`data:image/png`);
|
31 |
+
if(isPng){
|
32 |
+
const fileName = `sd-perception-${{imgId}}.png`;
|
33 |
+
return new File([blob], fileName, { type: 'image/png' });
|
34 |
+
}else{
|
35 |
+
const fileName = `sd-perception-${{imgId}}.jpg`;
|
36 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
|
41 |
+
const selectedLoRA = gradioEl.querySelector('#selected_lora');
|
42 |
+
const inputPrompt = gradioEl.querySelector('#prompt textarea').value;
|
43 |
+
const outputImgEl = gradioEl.querySelector('#result-image img');
|
44 |
+
|
45 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
46 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
47 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
48 |
+
|
49 |
+
shareBtnEl.style.pointerEvents = 'none';
|
50 |
+
shareIconEl.style.display = 'none';
|
51 |
+
loadingIconEl.style.removeProperty('display');
|
52 |
+
|
53 |
+
const inputFile = await getInputImgFile(outputImgEl);
|
54 |
+
const urlInputImg = await uploadFile(inputFile);
|
55 |
+
|
56 |
+
const descriptionMd = `
|
57 |
+
|
58 |
+
### ${selectedLoRA}
|
59 |
+
|
60 |
+
### Prompt
|
61 |
+
${inputPrompt}
|
62 |
+
|
63 |
+
#### Generated Image:
|
64 |
+
<img src="${outputImgEl}" />
|
65 |
+
`;
|
66 |
+
const params = new URLSearchParams({
|
67 |
+
title: "${inputPrompt}",
|
68 |
+
preview: true
|
69 |
+
});
|
70 |
+
const paramsStr = params.toString();
|
71 |
+
window.open(`https://huggingface.co/spaces/multimodalart/lora-the-explorer/discussions/new?${paramsStr}`, '_blank');
|
72 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
73 |
+
shareIconEl.style.removeProperty('display');
|
74 |
+
loadingIconEl.style.display = 'none';
|
75 |
+
}"""
|