John6666 commited on
Commit
d36e3ba
·
verified ·
1 Parent(s): a24f054

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +381 -379
app.py CHANGED
@@ -1,380 +1,382 @@
1
- import gradio as gr
2
- from datasets import load_dataset
3
- from PIL import Image
4
- from huggingface_hub import InferenceClient
5
-
6
- import re
7
- import os
8
- import requests
9
- import uuid
10
- import base64
11
- import random
12
-
13
- from share_btn import community_icon_html, loading_icon_html, share_js
14
-
15
- MODELS = ["stabilityai/stable-diffusion-2-1", "stabilityai/stable-diffusion-2"]
16
-
17
- #word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt")
18
- #word_list = word_list_dataset["train"]['text']
19
-
20
- #is_gpu_busy = False
21
- def infer(prompt, negative, scale, model_id=MODELS[0], hf_token="", images=[]):
22
- #global is_gpu_busy
23
- #for filter in word_list:
24
- # if re.search(rf"\b{filter}\b", prompt):
25
- # print(filter)
26
- # print(prompt)
27
- # raise gr.Error("Unsafe content found. Please try again with different prompts.")
28
-
29
- #images = []
30
- #url = os.getenv('JAX_BACKEND_URL')
31
- #print(url)
32
- if images is None: images = []
33
- if not hf_token: hf_token = os.getenv("HF_TOKEN", None)
34
- client = InferenceClient(model=model_id, token=hf_token)
35
- image = client.text_to_image(prompt=prompt, negative_prompt=negative, guidance_scale=scale)
36
- if image: images.append(image)
37
- #payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
38
- #images_request = requests.post(url, json = payload)
39
- #for image in images_request.json()["images"]:
40
- # file_path = f"{uuid.uuid4()}.jpg"
41
- # with open(file_path, "wb") as f:
42
- # f.write(base64.b64decode(image))
43
- # images.append(file_path)
44
-
45
- return images
46
-
47
- def gen_random_seed():
48
- return random.randint(0, 2147483647)
49
-
50
- def infer_advance(prompt, negative, scale, width, height, steps, seed, samples, model_id=MODELS[0], hf_token="", images=[]):
51
- if images is None: images = []
52
- if not hf_token: hf_token = os.getenv("HF_TOKEN", None)
53
- client = InferenceClient(model=model_id, token=hf_token)
54
- for i in range(samples):
55
- image = client.text_to_image(prompt=prompt, negative_prompt=negative, width=width, height=height,
56
- guidance_scale=scale, num_inference_steps=steps, seed=seed if i==0 else gen_random_seed())
57
- if image: images.append(image)
58
- yield images
59
-
60
- css = """
61
- .gradio-container {
62
- max-width: 768px !important;
63
- }
64
- .gradio-container {
65
- font-family: 'IBM Plex Sans', sans-serif;
66
- }
67
- .gr-button {
68
- color: white;
69
- border-color: black;
70
- background: black;
71
- }
72
- input[type='range'] {
73
- accent-color: black;
74
- }
75
- .dark input[type='range'] {
76
- accent-color: #dfdfdf;
77
- }
78
- .container {
79
- max-width: 730px;
80
- margin: auto;
81
- }
82
- #gallery {
83
- min-height: 22rem;
84
- margin-bottom: 15px;
85
- margin-left: auto;
86
- margin-right: auto;
87
- border-bottom-right-radius: .5rem !important;
88
- border-bottom-left-radius: .5rem !important;
89
- }
90
- #gallery>div>.h-full {
91
- min-height: 20rem;
92
- }
93
- .details:hover {
94
- text-decoration: underline;
95
- }
96
- .gr-button {
97
- white-space: nowrap;
98
- }
99
- .gr-button:focus {
100
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
101
- outline: none;
102
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
103
- --tw-border-opacity: 1;
104
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
105
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
106
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
107
- --tw-ring-opacity: .5;
108
- }
109
- #advanced-btn {
110
- font-size: .7rem !important;
111
- line-height: 19px;
112
- margin-top: 12px;
113
- margin-bottom: 12px;
114
- padding: 2px 8px;
115
- border-radius: 14px !important;
116
- }
117
- #advanced-options {
118
- display: none;
119
- margin-bottom: 20px;
120
- }
121
- .footer {
122
- margin-bottom: 45px;
123
- margin-top: 35px;
124
- text-align: center;
125
- border-bottom: 1px solid #e5e5e5;
126
- }
127
- .footer>p {
128
- font-size: .8rem;
129
- display: inline-block;
130
- padding: 0 10px;
131
- transform: translateY(10px);
132
- background: white;
133
- }
134
- .dark .footer {
135
- border-color: #303030;
136
- }
137
- .dark .footer>p {
138
- background: #0b0f19;
139
- }
140
- .acknowledgments h4{
141
- margin: 1.25em 0 .25em 0;
142
- font-weight: bold;
143
- font-size: 115%;
144
- }
145
- .animate-spin {
146
- animation: spin 1s linear infinite;
147
- }
148
- @keyframes spin {
149
- from {
150
- transform: rotate(0deg);
151
- }
152
- to {
153
- transform: rotate(360deg);
154
- }
155
- }
156
- #share-btn-container {
157
- 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;
158
- margin-top: 10px;
159
- margin-left: auto;
160
- }
161
- #share-btn-container .styler{
162
- background-color: #000000;
163
- }
164
- #share-btn {
165
- 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;
166
- }
167
- #share-btn * {
168
- all: unset;
169
- }
170
- #share-btn-container div:nth-child(-n+2){
171
- width: auto !important;
172
- min-height: 0px !important;
173
- }
174
- #share-btn-container .wrap {
175
- display: none !important;
176
- }
177
-
178
- .gr-form{
179
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
180
- }
181
- #prompt-container{
182
- gap: 0;
183
- }
184
- #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
185
- #component-16{border-top-width: 1px!important;margin-top: 1em}
186
- .image_duplication{position: absolute; width: 100px; left: 50px}
187
- button{height: 100%}
188
- .info { align-items: center; text-align: center; }
189
- """
190
-
191
- block = gr.Blocks(css=css)
192
-
193
- examples = [
194
- [
195
- 'A high tech solarpunk utopia in the Amazon rainforest',
196
- 'low quality',
197
- 9
198
- ],
199
- [
200
- 'A pikachu fine dining with a view to the Eiffel Tower',
201
- 'low quality',
202
- 9
203
- ],
204
- [
205
- 'A mecha robot in a favela in expressionist style',
206
- 'low quality, 3d, photorealistic',
207
- 9
208
- ],
209
- [
210
- 'an insect robot preparing a delicious meal',
211
- 'low quality, illustration',
212
- 9
213
- ],
214
- [
215
- "A small cabin on top of a snowy mountain in the style of Disney, artstation",
216
- 'low quality, ugly',
217
- 9
218
- ],
219
- ]
220
-
221
-
222
- with block:
223
- gr.HTML(
224
- """
225
- <div style="text-align: center; margin: 0 auto;">
226
- <div
227
- style="
228
- display: inline-flex;
229
- align-items: center;
230
- gap: 0.8rem;
231
- font-size: 1.75rem;
232
- "
233
- >
234
- <svg
235
- width="0.65em"
236
- height="0.65em"
237
- viewBox="0 0 115 115"
238
- fill="none"
239
- xmlns="http://www.w3.org/2000/svg"
240
- >
241
- <rect width="23" height="23" fill="white"></rect>
242
- <rect y="69" width="23" height="23" fill="white"></rect>
243
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
244
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
245
- <rect x="46" width="23" height="23" fill="white"></rect>
246
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
247
- <rect x="69" width="23" height="23" fill="black"></rect>
248
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
249
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
250
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
251
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
252
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
253
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
254
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
255
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
256
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
257
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
258
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
259
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
260
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
261
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
262
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
263
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
264
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
265
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
266
- </svg>
267
- <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
268
- Stable Diffusion 2.1 Demo
269
- </h1>
270
- </div>
271
- <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
272
- Stable Diffusion 2.1 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
273
- access you can try
274
- <a
275
- href="http://beta.dreamstudio.ai/"
276
- style="text-decoration: underline;"
277
- target="_blank"
278
- >DreamStudio Beta</a
279
- >.</a>
280
- </p>
281
- </div>
282
- """
283
- )
284
- with gr.Group():
285
- with gr.Row(elem_id="prompt-container"):
286
- with gr.Column(scale=3):
287
- text = gr.Textbox(
288
- label="Enter your prompt",
289
- show_label=False,
290
- max_lines=1,
291
- placeholder="Enter your prompt",
292
- elem_id="prompt-text-input",
293
- )
294
- negative = gr.Textbox(
295
- label="Enter your negative prompt",
296
- show_label=False,
297
- max_lines=1,
298
- placeholder="Enter a negative prompt",
299
- elem_id="negative-prompt-text-input",
300
- )
301
- with gr.Column(scale=1, min_width=150):
302
- btn = gr.Button("Generate image")
303
-
304
- gallery = gr.Gallery(
305
- label="Generated images", show_label=False, elem_id="gallery",
306
- )
307
-
308
- with gr.Group(elem_id="container-advanced-btns"):
309
- #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
310
- with gr.Group(elem_id="share-btn-container"):
311
- community_icon = gr.HTML(community_icon_html)
312
- loading_icon = gr.HTML(loading_icon_html)
313
- share_button = gr.Button("Share to community", elem_id="share-btn")
314
-
315
- with gr.Accordion("Advanced settings", open=False):
316
- # gr.Markdown("Advanced settings are temporarily unavailable")
317
- model_id = gr.Dropdown(label="Model", choices=MODELS, value=MODELS[0], allow_custom_value=True)
318
- samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
319
- with gr.Row():
320
- width = gr.Slider(label="Width", minimum=256, maximum=2048, step=32, value=768)
321
- height = gr.Slider(label="Height", minimum=256, maximum=2048, step=32, value=768)
322
- steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
323
- guidance_scale = gr.Slider(
324
- label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
325
- )
326
- seed = gr.Slider(
327
- label="Seed",
328
- minimum=0,
329
- maximum=2147483647,
330
- step=1,
331
- randomize=True,
332
- )
333
- with gr.Column():
334
- hf_token = gr.Textbox(label="Your HF read token (Optional)", placeholder="hf_...", value="", max_lines=1)
335
- gr.Markdown("Your token is available at [hf.co/settings/tokens](https://huggingface.co/settings/tokens).", elem_classes="info")
336
- advanced_button = gr.Button("Generate images (advanced)")
337
-
338
- ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
339
- ex.dataset.headers = [""]
340
- negative.submit(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
341
- text.submit(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
342
- btn.click(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
343
- advanced_button.click(infer_advance, inputs=[text, negative, guidance_scale, width, height, steps, seed, samples, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
344
-
345
- #advanced_button.click(
346
- # None,
347
- # [],
348
- # text,
349
- # _js="""
350
- # () => {
351
- # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
352
- # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
353
- # }""",
354
- #)
355
- share_button.click(
356
- None,
357
- [],
358
- [],
359
- js=share_js,
360
- )
361
- gr.HTML(
362
- """
363
- <div class="footer">
364
- <p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a> - Gradio Demo by 🤗 Hugging Face
365
- </p>
366
- </div>
367
- """
368
- )
369
- with gr.Accordion(label="License", open=False):
370
- gr.HTML(
371
- """<div class="acknowledgments">
372
- <p><h4>LICENSE</h4>
373
- 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>
374
- <p><h4>Biases and content acknowledgment</h4>
375
- 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>
376
- </div>
377
- """
378
- )
379
-
 
 
380
  block.queue().launch(max_threads=150, show_error=True)
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from PIL import Image
4
+ from huggingface_hub import InferenceClient
5
+
6
+ import re
7
+ import os
8
+ import requests
9
+ import uuid
10
+ import base64
11
+ import random
12
+
13
+ from share_btn import community_icon_html, loading_icon_html, share_js
14
+
15
+ MODELS = ["stabilityai/stable-diffusion-2-1", "stabilityai/stable-diffusion-2"]
16
+
17
+ #word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt")
18
+ #word_list = word_list_dataset["train"]['text']
19
+
20
+ #is_gpu_busy = False
21
+ def infer(prompt, negative, scale, model_id=MODELS[0], hf_token="", images=[]):
22
+ #global is_gpu_busy
23
+ #for filter in word_list:
24
+ # if re.search(rf"\b{filter}\b", prompt):
25
+ # print(filter)
26
+ # print(prompt)
27
+ # raise gr.Error("Unsafe content found. Please try again with different prompts.")
28
+
29
+ #images = []
30
+ #url = os.getenv('JAX_BACKEND_URL')
31
+ #print(url)
32
+ if images is None: images = []
33
+ if not hf_token: hf_token = os.getenv("HF_TOKEN", None)
34
+ client = InferenceClient(model=model_id, token=hf_token)
35
+ for i in range(4):
36
+ image = client.text_to_image(prompt=prompt, negative_prompt=negative, guidance_scale=scale)
37
+ if image: images.append(image)
38
+ yield images
39
+ #payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
40
+ #images_request = requests.post(url, json = payload)
41
+ #for image in images_request.json()["images"]:
42
+ # file_path = f"{uuid.uuid4()}.jpg"
43
+ # with open(file_path, "wb") as f:
44
+ # f.write(base64.b64decode(image))
45
+ # images.append(file_path)
46
+
47
+ #return images
48
+
49
+ def gen_random_seed():
50
+ return random.randint(0, 2147483647)
51
+
52
+ def infer_advance(prompt, negative, scale, width, height, steps, seed, samples, model_id=MODELS[0], hf_token="", images=[]):
53
+ if images is None: images = []
54
+ if not hf_token: hf_token = os.getenv("HF_TOKEN", None)
55
+ client = InferenceClient(model=model_id, token=hf_token)
56
+ for i in range(samples):
57
+ image = client.text_to_image(prompt=prompt, negative_prompt=negative, width=width, height=height,
58
+ guidance_scale=scale, num_inference_steps=steps, seed=seed if i==0 else gen_random_seed())
59
+ if image: images.append(image)
60
+ yield images
61
+
62
+ css = """
63
+ .gradio-container {
64
+ max-width: 768px !important;
65
+ }
66
+ .gradio-container {
67
+ font-family: 'IBM Plex Sans', sans-serif;
68
+ }
69
+ .gr-button {
70
+ color: white;
71
+ border-color: black;
72
+ background: black;
73
+ }
74
+ input[type='range'] {
75
+ accent-color: black;
76
+ }
77
+ .dark input[type='range'] {
78
+ accent-color: #dfdfdf;
79
+ }
80
+ .container {
81
+ max-width: 730px;
82
+ margin: auto;
83
+ }
84
+ #gallery {
85
+ min-height: 22rem;
86
+ margin-bottom: 15px;
87
+ margin-left: auto;
88
+ margin-right: auto;
89
+ border-bottom-right-radius: .5rem !important;
90
+ border-bottom-left-radius: .5rem !important;
91
+ }
92
+ #gallery>div>.h-full {
93
+ min-height: 20rem;
94
+ }
95
+ .details:hover {
96
+ text-decoration: underline;
97
+ }
98
+ .gr-button {
99
+ white-space: nowrap;
100
+ }
101
+ .gr-button:focus {
102
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
103
+ outline: none;
104
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
105
+ --tw-border-opacity: 1;
106
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
107
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
108
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
109
+ --tw-ring-opacity: .5;
110
+ }
111
+ #advanced-btn {
112
+ font-size: .7rem !important;
113
+ line-height: 19px;
114
+ margin-top: 12px;
115
+ margin-bottom: 12px;
116
+ padding: 2px 8px;
117
+ border-radius: 14px !important;
118
+ }
119
+ #advanced-options {
120
+ display: none;
121
+ margin-bottom: 20px;
122
+ }
123
+ .footer {
124
+ margin-bottom: 45px;
125
+ margin-top: 35px;
126
+ text-align: center;
127
+ border-bottom: 1px solid #e5e5e5;
128
+ }
129
+ .footer>p {
130
+ font-size: .8rem;
131
+ display: inline-block;
132
+ padding: 0 10px;
133
+ transform: translateY(10px);
134
+ background: white;
135
+ }
136
+ .dark .footer {
137
+ border-color: #303030;
138
+ }
139
+ .dark .footer>p {
140
+ background: #0b0f19;
141
+ }
142
+ .acknowledgments h4{
143
+ margin: 1.25em 0 .25em 0;
144
+ font-weight: bold;
145
+ font-size: 115%;
146
+ }
147
+ .animate-spin {
148
+ animation: spin 1s linear infinite;
149
+ }
150
+ @keyframes spin {
151
+ from {
152
+ transform: rotate(0deg);
153
+ }
154
+ to {
155
+ transform: rotate(360deg);
156
+ }
157
+ }
158
+ #share-btn-container {
159
+ 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;
160
+ margin-top: 10px;
161
+ margin-left: auto;
162
+ }
163
+ #share-btn-container .styler{
164
+ background-color: #000000;
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;right:0;
168
+ }
169
+ #share-btn * {
170
+ all: unset;
171
+ }
172
+ #share-btn-container div:nth-child(-n+2){
173
+ width: auto !important;
174
+ min-height: 0px !important;
175
+ }
176
+ #share-btn-container .wrap {
177
+ display: none !important;
178
+ }
179
+
180
+ .gr-form{
181
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
182
+ }
183
+ #prompt-container{
184
+ gap: 0;
185
+ }
186
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
187
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
188
+ .image_duplication{position: absolute; width: 100px; left: 50px}
189
+ button{height: 100%}
190
+ .info { align-items: center; text-align: center; }
191
+ """
192
+
193
+ block = gr.Blocks(css=css)
194
+
195
+ examples = [
196
+ [
197
+ 'A high tech solarpunk utopia in the Amazon rainforest',
198
+ 'low quality',
199
+ 9
200
+ ],
201
+ [
202
+ 'A pikachu fine dining with a view to the Eiffel Tower',
203
+ 'low quality',
204
+ 9
205
+ ],
206
+ [
207
+ 'A mecha robot in a favela in expressionist style',
208
+ 'low quality, 3d, photorealistic',
209
+ 9
210
+ ],
211
+ [
212
+ 'an insect robot preparing a delicious meal',
213
+ 'low quality, illustration',
214
+ 9
215
+ ],
216
+ [
217
+ "A small cabin on top of a snowy mountain in the style of Disney, artstation",
218
+ 'low quality, ugly',
219
+ 9
220
+ ],
221
+ ]
222
+
223
+
224
+ with block:
225
+ gr.HTML(
226
+ """
227
+ <div style="text-align: center; margin: 0 auto;">
228
+ <div
229
+ style="
230
+ display: inline-flex;
231
+ align-items: center;
232
+ gap: 0.8rem;
233
+ font-size: 1.75rem;
234
+ "
235
+ >
236
+ <svg
237
+ width="0.65em"
238
+ height="0.65em"
239
+ viewBox="0 0 115 115"
240
+ fill="none"
241
+ xmlns="http://www.w3.org/2000/svg"
242
+ >
243
+ <rect width="23" height="23" fill="white"></rect>
244
+ <rect y="69" width="23" height="23" fill="white"></rect>
245
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
246
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
247
+ <rect x="46" width="23" height="23" fill="white"></rect>
248
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
249
+ <rect x="69" width="23" height="23" fill="black"></rect>
250
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
251
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
252
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
253
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
254
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
255
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
256
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
257
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
258
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
259
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
260
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
261
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
262
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
263
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
264
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
265
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
266
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
267
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
268
+ </svg>
269
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
270
+ Stable Diffusion 2.1 Demo
271
+ </h1>
272
+ </div>
273
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
274
+ Stable Diffusion 2.1 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
275
+ access you can try
276
+ <a
277
+ href="http://beta.dreamstudio.ai/"
278
+ style="text-decoration: underline;"
279
+ target="_blank"
280
+ >DreamStudio Beta</a
281
+ >.</a>
282
+ </p>
283
+ </div>
284
+ """
285
+ )
286
+ with gr.Group():
287
+ with gr.Row(elem_id="prompt-container"):
288
+ with gr.Column(scale=3):
289
+ text = gr.Textbox(
290
+ label="Enter your prompt",
291
+ show_label=False,
292
+ max_lines=1,
293
+ placeholder="Enter your prompt",
294
+ elem_id="prompt-text-input",
295
+ )
296
+ negative = gr.Textbox(
297
+ label="Enter your negative prompt",
298
+ show_label=False,
299
+ max_lines=1,
300
+ placeholder="Enter a negative prompt",
301
+ elem_id="negative-prompt-text-input",
302
+ )
303
+ with gr.Column(scale=1, min_width=150):
304
+ btn = gr.Button("Generate image")
305
+
306
+ gallery = gr.Gallery(
307
+ label="Generated images", show_label=False, elem_id="gallery",
308
+ )
309
+
310
+ with gr.Group(elem_id="container-advanced-btns"):
311
+ #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
312
+ with gr.Group(elem_id="share-btn-container"):
313
+ community_icon = gr.HTML(community_icon_html)
314
+ loading_icon = gr.HTML(loading_icon_html)
315
+ share_button = gr.Button("Share to community", elem_id="share-btn")
316
+
317
+ with gr.Accordion("Advanced settings", open=False):
318
+ # gr.Markdown("Advanced settings are temporarily unavailable")
319
+ model_id = gr.Dropdown(label="Model", choices=MODELS, value=MODELS[0], allow_custom_value=True)
320
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
321
+ with gr.Row():
322
+ width = gr.Slider(label="Width", minimum=256, maximum=2048, step=32, value=768)
323
+ height = gr.Slider(label="Height", minimum=256, maximum=2048, step=32, value=768)
324
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
325
+ guidance_scale = gr.Slider(
326
+ label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
327
+ )
328
+ seed = gr.Slider(
329
+ label="Seed",
330
+ minimum=0,
331
+ maximum=2147483647,
332
+ step=1,
333
+ randomize=True,
334
+ )
335
+ with gr.Column():
336
+ hf_token = gr.Textbox(label="Your HF read token (Optional)", placeholder="hf_...", value="", max_lines=1)
337
+ gr.Markdown("Your token is available at [hf.co/settings/tokens](https://huggingface.co/settings/tokens).", elem_classes="info")
338
+ advanced_button = gr.Button("Generate images (advanced)")
339
+
340
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
341
+ ex.dataset.headers = [""]
342
+ negative.submit(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
343
+ text.submit(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
344
+ btn.click(infer, inputs=[text, negative, guidance_scale, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
345
+ advanced_button.click(infer_advance, inputs=[text, negative, guidance_scale, width, height, steps, seed, samples, model_id, hf_token, gallery], outputs=[gallery], concurrency_limit=80)
346
+
347
+ #advanced_button.click(
348
+ # None,
349
+ # [],
350
+ # text,
351
+ # _js="""
352
+ # () => {
353
+ # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
354
+ # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
355
+ # }""",
356
+ #)
357
+ share_button.click(
358
+ None,
359
+ [],
360
+ [],
361
+ js=share_js,
362
+ )
363
+ gr.HTML(
364
+ """
365
+ <div class="footer">
366
+ <p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a> - Gradio Demo by 🤗 Hugging Face
367
+ </p>
368
+ </div>
369
+ """
370
+ )
371
+ with gr.Accordion(label="License", open=False):
372
+ gr.HTML(
373
+ """<div class="acknowledgments">
374
+ <p><h4>LICENSE</h4>
375
+ 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>
376
+ <p><h4>Biases and content acknowledgment</h4>
377
+ 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>
378
+ </div>
379
+ """
380
+ )
381
+
382
  block.queue().launch(max_threads=150, show_error=True)