Spaces:
Build error
Build error
PSaiml
commited on
Commit
•
41ea60b
0
Parent(s):
Duplicate from AIML-TUDA/safe-stable-diffusion
Browse files- .gitattributes +34 -0
- README.md +13 -0
- app.py +329 -0
- requirements.txt +2 -0
- share_btn.py +68 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Safe Stable Diffusion
|
3 |
+
colorFrom: blue
|
4 |
+
colorTo: red
|
5 |
+
emoji: 😇
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.4
|
8 |
+
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
license: creativeml-openrail-m
|
11 |
+
duplicated_from: AIML-TUDA/safe-stable-diffusion
|
12 |
+
---
|
13 |
+
|
app.py
ADDED
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
# import torch
|
3 |
+
# from torch import autocast
|
4 |
+
# from diffusers import StableDiffusionPipeline
|
5 |
+
from datasets import load_dataset
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
# import base64
|
9 |
+
# import re
|
10 |
+
import os
|
11 |
+
import requests
|
12 |
+
import json
|
13 |
+
import base64
|
14 |
+
# from urllib import parse
|
15 |
+
|
16 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
17 |
+
|
18 |
+
|
19 |
+
is_gpu_busy = False
|
20 |
+
|
21 |
+
|
22 |
+
def infer(prompt, n_samples, steps, scale, seed):
|
23 |
+
global is_gpu_busy
|
24 |
+
# generator = torch.Generator(device=device).manual_seed(seed)
|
25 |
+
# print("Is GPU busy? ", is_gpu_busy)
|
26 |
+
images = []
|
27 |
+
# if(not is_gpu_busy):
|
28 |
+
# is_gpu_busy = True
|
29 |
+
# images_list = pipe(
|
30 |
+
# [prompt] * samples,
|
31 |
+
# num_inference_steps=steps,
|
32 |
+
# guidance_scale=scale,
|
33 |
+
# generator=generator,
|
34 |
+
# )
|
35 |
+
# is_gpu_busy = False
|
36 |
+
# safe_image = Image.open(r"unsafe.png")
|
37 |
+
# for i, image in enumerate(images_list["sample"]):
|
38 |
+
# if(images_list["nsfw_content_detected"][i]):
|
39 |
+
# images.append(safe_image)
|
40 |
+
# else:
|
41 |
+
# images.append(image)
|
42 |
+
# else:
|
43 |
+
url = os.getenv('BACKEND_URL')
|
44 |
+
response = requests.get(url.format(prompt, int(n_samples), max(50,int(steps)), f'{scale:.1f}', int(seed)))
|
45 |
+
#response = requests.get(url.format('a%20naked%20girl', 2, 50, 7.5, 2))
|
46 |
+
data = json.load(BytesIO(response.content))
|
47 |
+
if 'output' not in data:
|
48 |
+
raise gr.Error("Although safety guidance is enabled, potential unsafe content found. Please try again with different seed.")
|
49 |
+
else:
|
50 |
+
for image in data['output']['choices']:
|
51 |
+
im = Image.open(BytesIO(base64.b64decode(image['image_base64'])))
|
52 |
+
images.append(im)
|
53 |
+
|
54 |
+
# payload = {'prompt': prompt}
|
55 |
+
# images_request = requests.post(url, json=payload)
|
56 |
+
# for image in images_request.json()["output"]['choices']:
|
57 |
+
# image_b64 = (f"data:image/jpeg;base64,{image['image_base64']}")
|
58 |
+
# images.append(image_b64)
|
59 |
+
|
60 |
+
return images
|
61 |
+
|
62 |
+
|
63 |
+
css = """
|
64 |
+
.gradio-container {
|
65 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
66 |
+
}
|
67 |
+
.gr-button {
|
68 |
+
color: white;
|
69 |
+
border-color: #3a669bff;
|
70 |
+
background: #3a669bff;
|
71 |
+
}
|
72 |
+
input[type='range'] {
|
73 |
+
accent-color: #3a669bff;
|
74 |
+
}
|
75 |
+
.dark input[type='range'] {
|
76 |
+
accent-color: #3a669bff;
|
77 |
+
}
|
78 |
+
.container {
|
79 |
+
max-width: 730px;
|
80 |
+
margin: auto;
|
81 |
+
padding-top: 1.5rem;
|
82 |
+
}
|
83 |
+
#gallery {
|
84 |
+
min-height: 22rem;
|
85 |
+
margin-bottom: 15px;
|
86 |
+
margin-left: auto;
|
87 |
+
margin-right: auto;
|
88 |
+
border-bottom-right-radius: .5rem !important;
|
89 |
+
border-bottom-left-radius: .5rem !important;
|
90 |
+
}
|
91 |
+
#gallery>div>.h-full {
|
92 |
+
min-height: 20rem;
|
93 |
+
}
|
94 |
+
.details:hover {
|
95 |
+
text-decoration: underline;
|
96 |
+
}
|
97 |
+
.gr-button {
|
98 |
+
white-space: nowrap;
|
99 |
+
}
|
100 |
+
.gr-button:focus {
|
101 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
102 |
+
outline: none;
|
103 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
104 |
+
--tw-border-opacity: 1;
|
105 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
106 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
107 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
108 |
+
--tw-ring-opacity: .5;
|
109 |
+
}
|
110 |
+
#advanced-btn {
|
111 |
+
font-size: .7rem !important;
|
112 |
+
line-height: 19px;
|
113 |
+
margin-top: 12px;
|
114 |
+
margin-bottom: 12px;
|
115 |
+
padding: 2px 8px;
|
116 |
+
border-radius: 14px !important;
|
117 |
+
}
|
118 |
+
#advanced-options {
|
119 |
+
display: none;
|
120 |
+
margin-bottom: 20px;
|
121 |
+
}
|
122 |
+
.footer {
|
123 |
+
margin-bottom: 45px;
|
124 |
+
margin-top: 35px;
|
125 |
+
text-align: center;
|
126 |
+
border-bottom: 1px solid #e5e5e5;
|
127 |
+
}
|
128 |
+
.footer>p {
|
129 |
+
font-size: .8rem;
|
130 |
+
display: inline-block;
|
131 |
+
padding: 0 10px;
|
132 |
+
transform: translateY(10px);
|
133 |
+
background: white;
|
134 |
+
}
|
135 |
+
.dark .footer {
|
136 |
+
border-color: #303030;
|
137 |
+
}
|
138 |
+
.dark .footer>p {
|
139 |
+
background: #0b0f19;
|
140 |
+
}
|
141 |
+
.acknowledgments h4{
|
142 |
+
margin: 1.25em 0 .25em 0;
|
143 |
+
font-weight: bold;
|
144 |
+
font-size: 115%;
|
145 |
+
}
|
146 |
+
#container-advanced-btns{
|
147 |
+
display: flex;
|
148 |
+
flex-wrap: wrap;
|
149 |
+
justify-content: space-between;
|
150 |
+
align-items: center;
|
151 |
+
}
|
152 |
+
.animate-spin {
|
153 |
+
animation: spin 1s linear infinite;
|
154 |
+
}
|
155 |
+
@keyframes spin {
|
156 |
+
from {
|
157 |
+
transform: rotate(0deg);
|
158 |
+
}
|
159 |
+
to {
|
160 |
+
transform: rotate(360deg);
|
161 |
+
}
|
162 |
+
}
|
163 |
+
#share-btn-container {
|
164 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #3a669bff; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
165 |
+
}
|
166 |
+
#share-btn {
|
167 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
|
168 |
+
}
|
169 |
+
#share-btn * {
|
170 |
+
all: unset;
|
171 |
+
}
|
172 |
+
.gr-form{
|
173 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
174 |
+
}
|
175 |
+
#prompt-container{
|
176 |
+
gap: 0;
|
177 |
+
}
|
178 |
+
"""
|
179 |
+
|
180 |
+
block = gr.Blocks(css=css)
|
181 |
+
|
182 |
+
examples = [
|
183 |
+
[
|
184 |
+
'a gorgeous female photo',
|
185 |
+
1,
|
186 |
+
50,
|
187 |
+
7.5,
|
188 |
+
251815625,
|
189 |
+
],
|
190 |
+
[
|
191 |
+
'a gorgeous male photo',
|
192 |
+
1,
|
193 |
+
50,
|
194 |
+
7.5,
|
195 |
+
479079226,
|
196 |
+
],
|
197 |
+
[
|
198 |
+
'portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and children from bahnhof zoo, detailed ',
|
199 |
+
1,
|
200 |
+
50,
|
201 |
+
9,
|
202 |
+
364629028,
|
203 |
+
],
|
204 |
+
[
|
205 |
+
'portrait of Sickly diseased dying Samurai warrior, sun shining, photo realistic illustration by greg rutkowski, thomas kindkade, alphonse mucha, loish, norman rockwell.',
|
206 |
+
1,
|
207 |
+
50,
|
208 |
+
10,
|
209 |
+
1714108957,
|
210 |
+
],
|
211 |
+
[
|
212 |
+
'a photograph by vanessa beecroft',
|
213 |
+
1,
|
214 |
+
50,
|
215 |
+
7.5,
|
216 |
+
445713657,
|
217 |
+
],
|
218 |
+
]
|
219 |
+
|
220 |
+
with block:
|
221 |
+
gr.HTML(
|
222 |
+
"""
|
223 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
224 |
+
<div
|
225 |
+
style="
|
226 |
+
display: inline-flex;
|
227 |
+
align-items: center;
|
228 |
+
gap: 0.8rem;
|
229 |
+
font-size: 1.75rem;
|
230 |
+
"
|
231 |
+
>
|
232 |
+
<img class="logo" src="https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/1666181274838-62fa1d95e8c9c532aa75331c.png" alt="AIML Logo"
|
233 |
+
style="margin: auto; max-width: 7rem;">
|
234 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
235 |
+
Safe Stable Diffusion Demo
|
236 |
+
</h1>
|
237 |
+
</div>
|
238 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
239 |
+
Safe Stable Diffusion extends Stable Diffusion with safety guidance. In the case of NSFW images it returns the closest non-NSFW images instead of a black square.
|
240 |
+
Details can be found in the <a href="https://arxiv.org/abs/2211.05105" style="text-decoration: underline;" target="_blank">Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models paper</a>.
|
241 |
+
</p>
|
242 |
+
</div>
|
243 |
+
"""
|
244 |
+
)
|
245 |
+
with gr.Group():
|
246 |
+
with gr.Box():
|
247 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
248 |
+
text = gr.Textbox(
|
249 |
+
label="Enter your prompt",
|
250 |
+
show_label=False,
|
251 |
+
max_lines=1,
|
252 |
+
placeholder="Enter your prompt",
|
253 |
+
elem_id="prompt-text-input",
|
254 |
+
).style(
|
255 |
+
border=(True, False, True, True),
|
256 |
+
rounded=(True, False, False, True),
|
257 |
+
container=False,
|
258 |
+
)
|
259 |
+
btn = gr.Button("Generate image").style(
|
260 |
+
margin=False,
|
261 |
+
rounded=(False, True, True, False),
|
262 |
+
full_width=False,
|
263 |
+
)
|
264 |
+
|
265 |
+
gallery = gr.Gallery(
|
266 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
267 |
+
).style(grid=[1], height="auto")
|
268 |
+
|
269 |
+
with gr.Group(elem_id="container-advanced-btns"):
|
270 |
+
advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
271 |
+
with gr.Group(elem_id="share-btn-container"):
|
272 |
+
community_icon = gr.HTML(community_icon_html)
|
273 |
+
loading_icon = gr.HTML(loading_icon_html)
|
274 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
275 |
+
|
276 |
+
with gr.Row(elem_id="advanced-options"):
|
277 |
+
#gr.Markdown("Advanced settings are temporarily unavailable")
|
278 |
+
samples = gr.Slider(label="Images", minimum=1, maximum=1, value=2, step=1)
|
279 |
+
steps = gr.Slider(label="Steps", minimum=50, maximum=50, value=50, step=1)
|
280 |
+
scale = gr.Slider(
|
281 |
+
label="Guidance Scale", minimum=7.5, maximum=20, value=7.5, step=0.5
|
282 |
+
)
|
283 |
+
seed = gr.Slider(
|
284 |
+
label="Seed",
|
285 |
+
minimum=0,
|
286 |
+
maximum=2147483647,
|
287 |
+
step=1,
|
288 |
+
randomize=True,
|
289 |
+
)
|
290 |
+
|
291 |
+
ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, steps, scale, seed],
|
292 |
+
outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
|
293 |
+
ex.dataset.headers = [""]
|
294 |
+
|
295 |
+
text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)
|
296 |
+
btn.click(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)
|
297 |
+
|
298 |
+
advanced_button.click(
|
299 |
+
None,
|
300 |
+
[],
|
301 |
+
text,
|
302 |
+
_js="""
|
303 |
+
() => {
|
304 |
+
const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
|
305 |
+
options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
306 |
+
}""",
|
307 |
+
)
|
308 |
+
share_button.click(
|
309 |
+
None,
|
310 |
+
[],
|
311 |
+
[],
|
312 |
+
_js=share_js,
|
313 |
+
)
|
314 |
+
gr.HTML(
|
315 |
+
"""
|
316 |
+
<div class="footer">
|
317 |
+
<p>Model by <a href="https://huggingface.co/AIML-TUDA/" style="text-decoration: underline;" target="_blank">AIML Lab @TU Darmstadt</a> - backend provided through the generous support of <a href="https://www.together.xyz/" style="text-decoration: underline;" target="_blank">Together</a> - Gradio Demo by 🤗 Hugging Face
|
318 |
+
</p>
|
319 |
+
</div>
|
320 |
+
<div class="acknowledgments">
|
321 |
+
<p><h4>LICENSE</h4>
|
322 |
+
The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a>.</p>
|
323 |
+
<p><h4>Biases and content acknowledgment</h4>
|
324 |
+
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. While the applied safety guidance suppresses the majority of inappropriate content, this still could apply to Safe Stable Diffusion models. The original model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. Safety guidance suppresses potentially inappropriate content during inference. You can read more in the <a href="https://huggingface.co/AIML-TUDA/stable-diffusion-safe" style="text-decoration: underline;" target="_blank">model card</a>.</p>
|
325 |
+
</div>
|
326 |
+
"""
|
327 |
+
)
|
328 |
+
|
329 |
+
block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
python-dotenv
|
2 |
+
https://gradio-builds.s3.amazonaws.com/queue-disconnect/v3/gradio-3.4b2-py3-none-any.whl
|
share_btn.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
const gradioEl = document.querySelector('body > gradio-app');
|
27 |
+
const imgEls = gradioEl.querySelectorAll('#gallery img');
|
28 |
+
const promptTxt = gradioEl.querySelector('#prompt-text-input input').value;
|
29 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
30 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
31 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
32 |
+
|
33 |
+
if(!imgEls.length){
|
34 |
+
return;
|
35 |
+
};
|
36 |
+
|
37 |
+
shareBtnEl.style.pointerEvents = 'none';
|
38 |
+
shareIconEl.style.display = 'none';
|
39 |
+
loadingIconEl.style.removeProperty('display');
|
40 |
+
|
41 |
+
const files = await Promise.all(
|
42 |
+
[...imgEls].map(async (imgEl) => {
|
43 |
+
const res = await fetch(imgEl.src);
|
44 |
+
const blob = await res.blob();
|
45 |
+
const imgId = Date.now() % 200;
|
46 |
+
const fileName = `diffuse-the-rest-${{imgId}}.jpg`;
|
47 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
48 |
+
})
|
49 |
+
);
|
50 |
+
|
51 |
+
const urls = await Promise.all(files.map((f) => uploadFile(f)));
|
52 |
+
const htmlImgs = urls.map(url => `<img src='${url}' width='400' height='400'>`);
|
53 |
+
const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
|
54 |
+
${htmlImgs.join(`\n`)}
|
55 |
+
</div>`;
|
56 |
+
|
57 |
+
const params = new URLSearchParams({
|
58 |
+
title: promptTxt,
|
59 |
+
description: descriptionMd,
|
60 |
+
});
|
61 |
+
|
62 |
+
const paramsStr = params.toString();
|
63 |
+
window.open(`https://huggingface.co/spaces/stabilityai/stable-diffusion/discussions/new?${paramsStr}`, '_blank');
|
64 |
+
|
65 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
66 |
+
shareIconEl.style.removeProperty('display');
|
67 |
+
loadingIconEl.style.display = 'none';
|
68 |
+
}"""
|