Francesco commited on
Commit
87fc5ec
1 Parent(s): 716b5f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -330
app.py CHANGED
@@ -2,8 +2,7 @@ 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
@@ -11,345 +10,50 @@ import os
11
  import requests
12
 
13
 
14
- from share_btn import community_icon_html, loading_icon_html, share_js
15
-
16
  model_id = "CompVis/stable-diffusion-v1-4"
17
  device = "cuda"
18
 
19
- #If you are running this code locally, you need to either do a 'huggingface-cli login` or paste your User Access Token from here https://huggingface.co/settings/tokens into the use_auth_token field below.
20
- pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True, revision="fp16", torch_dtype=torch.float16)
 
21
  pipe = pipe.to(device)
22
  torch.backends.cudnn.benchmark = True
23
 
24
- #When running locally, you won`t have access to this, so you can remove this part
25
- #word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
26
- #word_list = word_list_dataset["train"]['text']
27
-
28
  is_gpu_busy = False
29
- def infer(prompt):
30
- global is_gpu_busy
31
- samples = 4
32
- steps = 50
33
- scale = 7.5
34
- #When running locally you can also remove this filter
35
- #generator = torch.Generator(device=device).manual_seed(seed)
36
- #print("Is GPU busy? ", is_gpu_busy)
37
- images = []
38
- if(not is_gpu_busy):
39
- is_gpu_busy = True
40
- with autocast("cuda"):
41
- images = pipe(
42
- [prompt] * samples,
43
- num_inference_steps=steps,
44
- guidance_scale=scale,
45
- #generator=generator,
46
- ).images[0]
47
-
48
- return images
49
-
50
-
51
- css = """
52
- .gradio-container {
53
- font-family: 'IBM Plex Sans', sans-serif;
54
- }
55
- .gr-button {
56
- color: white;
57
- border-color: black;
58
- background: black;
59
- }
60
- input[type='range'] {
61
- accent-color: black;
62
- }
63
- .dark input[type='range'] {
64
- accent-color: #dfdfdf;
65
- }
66
- .container {
67
- max-width: 730px;
68
- margin: auto;
69
- padding-top: 1.5rem;
70
- }
71
- #gallery {
72
- min-height: 22rem;
73
- margin-bottom: 15px;
74
- margin-left: auto;
75
- margin-right: auto;
76
- border-bottom-right-radius: .5rem !important;
77
- border-bottom-left-radius: .5rem !important;
78
- }
79
- #gallery>div>.h-full {
80
- min-height: 20rem;
81
- }
82
- .details:hover {
83
- text-decoration: underline;
84
- }
85
- .gr-button {
86
- white-space: nowrap;
87
- }
88
- .gr-button:focus {
89
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
90
- outline: none;
91
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
92
- --tw-border-opacity: 1;
93
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
94
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
95
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
96
- --tw-ring-opacity: .5;
97
- }
98
- #advanced-btn {
99
- font-size: .7rem !important;
100
- line-height: 19px;
101
- margin-top: 12px;
102
- margin-bottom: 12px;
103
- padding: 2px 8px;
104
- border-radius: 14px !important;
105
- }
106
- #advanced-options {
107
- display: none;
108
- margin-bottom: 20px;
109
- }
110
- .footer {
111
- margin-bottom: 45px;
112
- margin-top: 35px;
113
- text-align: center;
114
- border-bottom: 1px solid #e5e5e5;
115
- }
116
- .footer>p {
117
- font-size: .8rem;
118
- display: inline-block;
119
- padding: 0 10px;
120
- transform: translateY(10px);
121
- background: white;
122
- }
123
- .dark .footer {
124
- border-color: #303030;
125
- }
126
- .dark .footer>p {
127
- background: #0b0f19;
128
- }
129
- .acknowledgments h4{
130
- margin: 1.25em 0 .25em 0;
131
- font-weight: bold;
132
- font-size: 115%;
133
- }
134
- #container-advanced-btns{
135
- display: flex;
136
- flex-wrap: wrap;
137
- justify-content: space-between;
138
- align-items: center;
139
- }
140
- .animate-spin {
141
- animation: spin 1s linear infinite;
142
- }
143
- @keyframes spin {
144
- from {
145
- transform: rotate(0deg);
146
- }
147
- to {
148
- transform: rotate(360deg);
149
- }
150
- }
151
- #share-btn-container {
152
- 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;
153
- }
154
- #share-btn {
155
- 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;
156
- }
157
- #share-btn * {
158
- all: unset;
159
- }
160
- .gr-form{
161
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
162
- }
163
- #prompt-container{
164
- gap: 0;
165
- }
166
- """
167
-
168
- block = gr.Blocks(css=css)
169
 
170
- examples = [
171
- [
172
- 'A high tech solarpunk utopia in the Amazon rainforest',
173
- # 4,
174
- # 45,
175
- # 7.5,
176
- # 1024,
177
- ],
178
- [
179
- 'A pikachu fine dining with a view to the Eiffel Tower',
180
- # 4,
181
- # 45,
182
- # 7,
183
- # 1024,
184
- ],
185
- [
186
- 'A mecha robot in a favela in expressionist style',
187
- # 4,
188
- # 45,
189
- # 7,
190
- # 1024,
191
- ],
192
- [
193
- 'an insect robot preparing a delicious meal',
194
- # 4,
195
- # 45,
196
- # 7,
197
- # 1024,
198
- ],
199
- [
200
- "A small cabin on top of a snowy mountain in the style of Disney, artstation",
201
- # 4,
202
- # 45,
203
- # 7,
204
- # 1024,
205
- ],
206
- ]
207
 
 
 
 
208
 
209
- with block:
210
- gr.HTML(
211
- """
212
- <div style="text-align: center; max-width: 650px; margin: 0 auto;">
213
- <div
214
- style="
215
- display: inline-flex;
216
- align-items: center;
217
- gap: 0.8rem;
218
- font-size: 1.75rem;
219
- "
220
- >
221
- <svg
222
- width="0.65em"
223
- height="0.65em"
224
- viewBox="0 0 115 115"
225
- fill="none"
226
- xmlns="http://www.w3.org/2000/svg"
227
- >
228
- <rect width="23" height="23" fill="white"></rect>
229
- <rect y="69" width="23" height="23" fill="white"></rect>
230
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
231
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
232
- <rect x="46" width="23" height="23" fill="white"></rect>
233
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
234
- <rect x="69" width="23" height="23" fill="black"></rect>
235
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
236
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
237
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
238
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
239
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
240
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
241
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
242
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
243
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
244
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
245
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
246
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
247
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
248
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
249
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
250
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
251
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
252
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
253
- </svg>
254
- <h1 style="font-weight: 900; margin-bottom: 7px;">
255
- Stable Diffusion Demo
256
- </h1>
257
- </div>
258
- <p style="margin-bottom: 10px; font-size: 94%">
259
- Stable Diffusion is a state of the art text-to-image model that generates
260
- images from text.<br>For faster generation and API
261
- access you can try
262
- <a
263
- href="http://beta.dreamstudio.ai/"
264
- style="text-decoration: underline;"
265
- target="_blank"
266
- >DreamStudio Beta</a
267
- >
268
- </p>
269
- </div>
270
- """
271
- )
272
- with gr.Group():
273
- with gr.Box():
274
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
275
- text = gr.Textbox(
276
- label="Enter your prompt",
277
- show_label=False,
278
- max_lines=1,
279
- placeholder="Enter your prompt",
280
- elem_id="prompt-text-input",
281
- ).style(
282
- border=(True, False, True, True),
283
- rounded=(True, False, False, True),
284
- container=False,
285
- )
286
- btn = gr.Button("Generate image").style(
287
- margin=False,
288
- rounded=(False, True, True, False),
289
- full_width=False,
290
- )
291
-
292
- gallery = gr.Gallery(
293
- label="Generated images", show_label=False, elem_id="gallery"
294
- ).style(grid=[2], height="auto")
295
-
296
- with gr.Group(elem_id="container-advanced-btns"):
297
- advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
298
- with gr.Group(elem_id="share-btn-container"):
299
- community_icon = gr.HTML(community_icon_html)
300
- loading_icon = gr.HTML(loading_icon_html)
301
- share_button = gr.Button("Share to community", elem_id="share-btn")
302
 
303
- with gr.Row(elem_id="advanced-options"):
304
- gr.Markdown("Advanced settings are temporarily unavailable")
305
- samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
306
- steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
307
- scale = gr.Slider(
308
- label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
309
- )
310
- seed = gr.Slider(
311
- label="Seed",
312
- minimum=0,
313
- maximum=2147483647,
314
- step=1,
315
- randomize=True,
316
- )
317
 
318
- ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
319
- ex.dataset.headers = [""]
320
 
321
- text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
322
- btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
 
 
 
 
 
 
 
 
 
 
 
323
 
324
- advanced_button.click(
325
- None,
326
- [],
327
- text,
328
- _js="""
329
- () => {
330
- const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
331
- options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
332
- }""",
333
- )
334
- share_button.click(
335
- None,
336
- [],
337
- [],
338
- _js=share_js,
339
- )
340
- gr.HTML(
341
- """
342
- <div class="footer">
343
- <p>Model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</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
344
- </p>
345
- </div>
346
- <div class="acknowledgments">
347
- <p><h4>LICENSE</h4>
348
- 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>
349
- <p><h4>Biases and content acknowledgment</h4>
350
- 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>
351
- </div>
352
- """
353
- )
354
-
355
- block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)
2
  import torch
3
  from torch import autocast
4
  from diffusers import StableDiffusionPipeline
5
+ from PIL import Image
 
6
  from io import BytesIO
7
  import base64
8
  import re
10
  import requests
11
 
12
 
 
 
13
  model_id = "CompVis/stable-diffusion-v1-4"
14
  device = "cuda"
15
 
16
+ pipe = StableDiffusionPipeline.from_pretrained(
17
+ model_id, use_auth_token=True, revision="fp16", torch_dtype=torch.float16
18
+ )
19
  pipe = pipe.to(device)
20
  torch.backends.cudnn.benchmark = True
21
 
 
 
 
 
22
  is_gpu_busy = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ @torch.no_grad()
26
+ def image_generation(prompt, samples=4, steps=25, scale=7.5):
27
+ global is_gpu_busy
28
 
29
+ images = []
30
+ if is_gpu_busy:
31
+ return images
32
+
33
+ is_gpu_busy = True
34
+ with autocast("cuda"):
35
+ images = pipe(
36
+ [prompt] * samples,
37
+ num_inference_steps=steps,
38
+ guidance_scale=scale,
39
+ ).images
40
+ is_gpu_busy = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ return images
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
 
 
44
 
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("# Stable Diffusion demo\nType something and generate images!")
47
+ textbox = gr.Textbox(placeholder="Something cool...", interactive=True)
48
+ with gr.Column(scale=1):
49
+ samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="samples")
50
+ steps = gr.Slider(minimum=10, maximum=50, value=25, step=1, label="steps")
51
+ scale = gr.Slider(minimum=5, maximum=15, value=7.5,step=0.1, label="scale")
52
+ submit = gr.Button("Submit", variant="primary")
53
+ gr.Markdown("Images will appear below")
54
+ with gr.Row():
55
+ gallery = gr.Gallery()
56
+ textbox.submit(image_generation, inputs=[textbox, samples, steps, scale], outputs=[gallery])
57
+ submit.click(image_generation, inputs=[textbox, samples, steps, scale], outputs=[gallery])
58
 
59
+ demo.launch()