Tritkoman multimodalart HF staff commited on
Commit
10af6a7
0 Parent(s):

Duplicate from stabilityai/stable-diffusion

Browse files

Co-authored-by: Multimodal AI art <multimodalart@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .gitattributes +31 -0
  2. README.md +14 -0
  3. app.py +346 -0
  4. requirements.txt +10 -0
  5. share_btn.py +60 -0
.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Stable Diffusion 2
3
+ emoji: 🔥
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.11.0
8
+ app_file: app.py
9
+ pinned: true
10
+ license: mit
11
+ duplicated_from: stabilityai/stable-diffusion
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ from imwatermark import WatermarkEncoder
5
+ import numpy as np
6
+ from PIL import Image
7
+ import re
8
+ from datasets import load_dataset
9
+ from diffusers import DiffusionPipeline, EulerDiscreteScheduler
10
+
11
+ from share_btn import community_icon_html, loading_icon_html, share_js
12
+
13
+ REPO_ID = "stabilityai/stable-diffusion-2"
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ wm = "SDV2"
17
+ wm_encoder = WatermarkEncoder()
18
+ wm_encoder.set_watermark('bytes', wm.encode('utf-8'))
19
+ def put_watermark(img, wm_encoder=None):
20
+ if wm_encoder is not None:
21
+ img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
22
+ img = wm_encoder.encode(img, 'dwtDct')
23
+ img = Image.fromarray(img[:, :, ::-1])
24
+ return img
25
+
26
+ repo_id = "stabilityai/stable-diffusion-2"
27
+ scheduler = EulerDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler", prediction_type="v_prediction")
28
+ pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16", scheduler=scheduler)
29
+ pipe = pipe.to(device)
30
+ pipe.enable_xformers_memory_efficient_attention()
31
+
32
+ #If you have duplicated this Space or is running locally, you can remove this part
33
+ word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
34
+ word_list = word_list_dataset["train"]['text']
35
+
36
+ def infer(prompt, samples, steps, scale, seed):
37
+ #If you have duplicated this Space or is running locally, you can remove this part
38
+ for filter in word_list:
39
+ if re.search(rf"\b{filter}\b", prompt):
40
+ raise gr.Error("Unsafe content found. Please try again with different prompts.")
41
+ generator = torch.Generator(device=device).manual_seed(seed)
42
+ images = pipe(prompt, width=768, height=768, num_inference_steps=steps, guidance_scale=scale, num_images_per_prompt=samples, generator=generator).images
43
+ images_watermarked = []
44
+ for image in images:
45
+ image = put_watermark(image, wm_encoder)
46
+ images_watermarked.append(image)
47
+ return images_watermarked
48
+
49
+ css = """
50
+ .gradio-container {
51
+ font-family: 'IBM Plex Sans', sans-serif;
52
+ }
53
+ .gr-button {
54
+ color: white;
55
+ border-color: black;
56
+ background: black;
57
+ }
58
+ input[type='range'] {
59
+ accent-color: black;
60
+ }
61
+ .dark input[type='range'] {
62
+ accent-color: #dfdfdf;
63
+ }
64
+ .container {
65
+ max-width: 730px;
66
+ margin: auto;
67
+ padding-top: 1.5rem;
68
+ }
69
+ #gallery {
70
+ min-height: 22rem;
71
+ margin-bottom: 15px;
72
+ margin-left: auto;
73
+ margin-right: auto;
74
+ border-bottom-right-radius: .5rem !important;
75
+ border-bottom-left-radius: .5rem !important;
76
+ }
77
+ #gallery>div>.h-full {
78
+ min-height: 20rem;
79
+ }
80
+ .details:hover {
81
+ text-decoration: underline;
82
+ }
83
+ .gr-button {
84
+ white-space: nowrap;
85
+ }
86
+ .gr-button:focus {
87
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
88
+ outline: none;
89
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
90
+ --tw-border-opacity: 1;
91
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
92
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
93
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
94
+ --tw-ring-opacity: .5;
95
+ }
96
+ #advanced-btn {
97
+ font-size: .7rem !important;
98
+ line-height: 19px;
99
+ margin-top: 12px;
100
+ margin-bottom: 12px;
101
+ padding: 2px 8px;
102
+ border-radius: 14px !important;
103
+ }
104
+ #advanced-options {
105
+ display: none;
106
+ margin-bottom: 20px;
107
+ }
108
+ .footer {
109
+ margin-bottom: 45px;
110
+ margin-top: 35px;
111
+ text-align: center;
112
+ border-bottom: 1px solid #e5e5e5;
113
+ }
114
+ .footer>p {
115
+ font-size: .8rem;
116
+ display: inline-block;
117
+ padding: 0 10px;
118
+ transform: translateY(10px);
119
+ background: white;
120
+ }
121
+ .dark .footer {
122
+ border-color: #303030;
123
+ }
124
+ .dark .footer>p {
125
+ background: #0b0f19;
126
+ }
127
+ .acknowledgments h4{
128
+ margin: 1.25em 0 .25em 0;
129
+ font-weight: bold;
130
+ font-size: 115%;
131
+ }
132
+ .animate-spin {
133
+ animation: spin 1s linear infinite;
134
+ }
135
+ @keyframes spin {
136
+ from {
137
+ transform: rotate(0deg);
138
+ }
139
+ to {
140
+ transform: rotate(360deg);
141
+ }
142
+ }
143
+ #share-btn-container {
144
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
145
+ margin-top: 10px;
146
+ margin-left: auto;
147
+ }
148
+ #share-btn {
149
+ 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;right:0;
150
+ }
151
+ #share-btn * {
152
+ all: unset;
153
+ }
154
+ #share-btn-container div:nth-child(-n+2){
155
+ width: auto !important;
156
+ min-height: 0px !important;
157
+ }
158
+ #share-btn-container .wrap {
159
+ display: none !important;
160
+ }
161
+ .gr-form{
162
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
163
+ }
164
+ #prompt-container{
165
+ gap: 0;
166
+ }
167
+ #component-9{margin-top: -19px}
168
+ .image_duplication{position: absolute; width: 100px; left: 50px}
169
+ """
170
+
171
+ block = gr.Blocks(css=css)
172
+
173
+ examples = [
174
+ [
175
+ 'A high tech solarpunk utopia in the Amazon rainforest',
176
+ 4,
177
+ 25,
178
+ 9,
179
+ 1024,
180
+ ],
181
+ [
182
+ 'A pikachu fine dining with a view to the Eiffel Tower',
183
+ 4,
184
+ 25,
185
+ 9,
186
+ 1024,
187
+ ],
188
+ [
189
+ 'A mecha robot in a favela in expressionist style',
190
+ 4,
191
+ 25,
192
+ 9,
193
+ 1024,
194
+ ],
195
+ [
196
+ 'an insect robot preparing a delicious meal',
197
+ 4,
198
+ 25,
199
+ 9,
200
+ 1024,
201
+ ],
202
+ [
203
+ "A small cabin on top of a snowy mountain in the style of Disney, artstation",
204
+ 4,
205
+ 25,
206
+ 9,
207
+ 1024,
208
+ ],
209
+ ]
210
+
211
+ with block:
212
+ gr.HTML(
213
+ """
214
+ <div style="text-align: center; margin: 0 auto;">
215
+ <div
216
+ style="
217
+ display: inline-flex;
218
+ align-items: center;
219
+ gap: 0.8rem;
220
+ font-size: 1.75rem;
221
+ "
222
+ >
223
+ <svg
224
+ width="0.65em"
225
+ height="0.65em"
226
+ viewBox="0 0 115 115"
227
+ fill="none"
228
+ xmlns="http://www.w3.org/2000/svg"
229
+ >
230
+ <rect width="23" height="23" fill="white"></rect>
231
+ <rect y="69" width="23" height="23" fill="white"></rect>
232
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
233
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
234
+ <rect x="46" width="23" height="23" fill="white"></rect>
235
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
236
+ <rect x="69" width="23" height="23" fill="black"></rect>
237
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
238
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
239
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
240
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
241
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
242
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
243
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
244
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
245
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
246
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
247
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
248
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
249
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
250
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
251
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
252
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
253
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
254
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
255
+ </svg>
256
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
257
+ Stable Diffusion 2 Demo
258
+ </h1>
259
+ </div>
260
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
261
+ Stable Diffusion 2 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
262
+ access you can try
263
+ <a
264
+ href="http://beta.dreamstudio.ai/"
265
+ style="text-decoration: underline;"
266
+ target="_blank"
267
+ >DreamStudio Beta</a
268
+ >. To skip the queue you can <a style="display:inline-block;width: 123px;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion?duplicate=true"><img style="width: 113px;margin-top: -13px;position: absolute;" src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
269
+ </p>
270
+ </div>
271
+ """
272
+ )
273
+ with gr.Group():
274
+ with gr.Box():
275
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
276
+ text = gr.Textbox(
277
+ label="Enter your prompt",
278
+ show_label=False,
279
+ max_lines=1,
280
+ placeholder="Enter your prompt",
281
+ elem_id="prompt-text-input",
282
+ ).style(
283
+ border=(True, False, True, True),
284
+ rounded=(True, False, False, True),
285
+ container=False,
286
+ )
287
+ btn = gr.Button("Generate image").style(
288
+ margin=False,
289
+ rounded=(False, True, True, False),
290
+ full_width=False,
291
+ )
292
+
293
+ gallery = gr.Gallery(
294
+ label="Generated images", show_label=False, elem_id="gallery"
295
+ ).style(grid=[2], height="auto")
296
+
297
+
298
+
299
+ with gr.Accordion("Custom options", open=False):
300
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
301
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=25, step=1)
302
+ scale = gr.Slider(
303
+ label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
304
+ )
305
+ seed = gr.Slider(
306
+ label="Seed",
307
+ minimum=0,
308
+ maximum=2147483647,
309
+ step=1,
310
+ randomize=True,
311
+ )
312
+
313
+ with gr.Group():
314
+ with gr.Group(elem_id="share-btn-container"):
315
+ community_icon = gr.HTML(community_icon_html)
316
+ loading_icon = gr.HTML(loading_icon_html)
317
+ share_button = gr.Button("Share to community", elem_id="share-btn")
318
+
319
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery], cache_examples=False)
320
+ ex.dataset.headers = [""]
321
+
322
+ text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery])
323
+ btn.click(infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery])
324
+
325
+ share_button.click(
326
+ None,
327
+ [],
328
+ [],
329
+ _js=share_js,
330
+ )
331
+ gr.HTML(
332
+ """
333
+ <div class="footer">
334
+ <p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a> - Gradio Demo by 🤗 Hugging Face using the <a href="https://github.com/huggingface/diffusers" style="text-decoration: underline;" target="_blank">🧨 diffusers library</a>
335
+ </p>
336
+ </div>
337
+ <div class="acknowledgments">
338
+ <p><h4>LICENSE</h4>
339
+ The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</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>
340
+ <p><h4>Biases and content acknowledgment</h4>
341
+ 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. The 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. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
342
+ </div>
343
+ """
344
+ )
345
+
346
+ block.queue(concurrency_count=1, max_size=50).launch(max_threads=150)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch==1.13.0
3
+ torchvision
4
+ git+https://github.com/huggingface/diffusers.git@30f6f44
5
+ transformers
6
+ accelerate
7
+ ftfy
8
+ python-dotenv
9
+ invisible-watermark
10
+ https://github.com/apolinario/xformers/releases/download/0.0.3/xformers-0.0.14.dev0-cp38-cp38-linux_x86_64.whl
share_btn.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ const gradioEl = document.querySelector('body > gradio-app');
26
+ const imgEls = gradioEl.querySelectorAll('#gallery img');
27
+ const promptTxt = gradioEl.querySelector('#prompt-text-input input').value;
28
+ const shareBtnEl = gradioEl.querySelector('#share-btn');
29
+ const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
30
+ const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
31
+ if(!imgEls.length){
32
+ return;
33
+ };
34
+ shareBtnEl.style.pointerEvents = 'none';
35
+ shareIconEl.style.display = 'none';
36
+ loadingIconEl.style.removeProperty('display');
37
+ const files = await Promise.all(
38
+ [...imgEls].map(async (imgEl) => {
39
+ const res = await fetch(imgEl.src);
40
+ const blob = await res.blob();
41
+ const imgId = Date.now() % 200;
42
+ const fileName = `diffuse-the-rest-${{imgId}}.jpg`;
43
+ return new File([blob], fileName, { type: 'image/jpeg' });
44
+ })
45
+ );
46
+ const urls = await Promise.all(files.map((f) => uploadFile(f)));
47
+ const htmlImgs = urls.map(url => `<img src='${url}' width='400' height='400'>`);
48
+ const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
49
+ ${htmlImgs.join(`\n`)}
50
+ </div>`;
51
+ const params = new URLSearchParams({
52
+ title: promptTxt,
53
+ description: descriptionMd,
54
+ });
55
+ const paramsStr = params.toString();
56
+ window.open(`https://huggingface.co/spaces/stabilityai/stable-diffusion/discussions/new?${paramsStr}`, '_blank');
57
+ shareBtnEl.style.removeProperty('pointer-events');
58
+ shareIconEl.style.removeProperty('display');
59
+ loadingIconEl.style.display = 'none';
60
+ }"""