hlydecker commited on
Commit
bf7848f
1 Parent(s): d2805ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +352 -0
app.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from PIL import Image
4
+
5
+ import re
6
+ import os
7
+ import requests
8
+
9
+ from share_btn import community_icon_html, loading_icon_html, share_js
10
+
11
+ word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
12
+ word_list = word_list_dataset["train"]['text']
13
+
14
+ is_gpu_busy = False
15
+ def infer(person1, person2, scale):
16
+ global is_gpu_busy
17
+ #FIXME: did somebody say f"prompt" injections? :P
18
+ prompt = f"Cross between {person1} and {person2}"
19
+ for filter in word_list:
20
+ if re.search(rf"\b{filter}\b", prompt):
21
+ raise gr.Error("Unsafe content found. Please try again with different prompts.")
22
+
23
+ images = []
24
+ person_1 = ''
25
+ url = os.getenv('JAX_BACKEND_URL')
26
+ payload = {'prompt': prompt, 'guidance_scale': scale}
27
+ images_request = requests.post(url, json = payload)
28
+ for image in images_request.json()["images"]:
29
+ image_b64 = (f"data:image/jpeg;base64,{image}")
30
+ images.append(image_b64)
31
+
32
+ return images
33
+
34
+
35
+ css = """
36
+ .gradio-container {
37
+ font-family: 'IBM Plex Sans', sans-serif;
38
+ }
39
+ .gr-button {
40
+ color: white;
41
+ border-color: black;
42
+ background: black;
43
+ }
44
+ input[type='range'] {
45
+ accent-color: black;
46
+ }
47
+ .dark input[type='range'] {
48
+ accent-color: #dfdfdf;
49
+ }
50
+ .container {
51
+ max-width: 730px;
52
+ margin: auto;
53
+ padding-top: 1.5rem;
54
+ }
55
+ #gallery {
56
+ min-height: 22rem;
57
+ margin-bottom: 15px;
58
+ margin-left: auto;
59
+ margin-right: auto;
60
+ border-bottom-right-radius: .5rem !important;
61
+ border-bottom-left-radius: .5rem !important;
62
+ }
63
+ #gallery>div>.h-full {
64
+ min-height: 20rem;
65
+ }
66
+ .details:hover {
67
+ text-decoration: underline;
68
+ }
69
+ .gr-button {
70
+ white-space: nowrap;
71
+ }
72
+ .gr-button:focus {
73
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
74
+ outline: none;
75
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
76
+ --tw-border-opacity: 1;
77
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
78
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
79
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
80
+ --tw-ring-opacity: .5;
81
+ }
82
+ #advanced-btn {
83
+ font-size: .7rem !important;
84
+ line-height: 19px;
85
+ margin-top: 12px;
86
+ margin-bottom: 12px;
87
+ padding: 2px 8px;
88
+ border-radius: 14px !important;
89
+ }
90
+ #advanced-options {
91
+ display: none;
92
+ margin-bottom: 20px;
93
+ }
94
+ .footer {
95
+ margin-bottom: 45px;
96
+ margin-top: 35px;
97
+ text-align: center;
98
+ border-bottom: 1px solid #e5e5e5;
99
+ }
100
+ .footer>p {
101
+ font-size: .8rem;
102
+ display: inline-block;
103
+ padding: 0 10px;
104
+ transform: translateY(10px);
105
+ background: white;
106
+ }
107
+ .dark .footer {
108
+ border-color: #303030;
109
+ }
110
+ .dark .footer>p {
111
+ background: #0b0f19;
112
+ }
113
+ .acknowledgments h4{
114
+ margin: 1.25em 0 .25em 0;
115
+ font-weight: bold;
116
+ font-size: 115%;
117
+ }
118
+ .animate-spin {
119
+ animation: spin 1s linear infinite;
120
+ }
121
+ @keyframes spin {
122
+ from {
123
+ transform: rotate(0deg);
124
+ }
125
+ to {
126
+ transform: rotate(360deg);
127
+ }
128
+ }
129
+ #share-btn-container {
130
+ 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;
131
+ margin-top: 10px;
132
+ margin-left: auto;
133
+ }
134
+ #share-btn {
135
+ 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;
136
+ }
137
+ #share-btn * {
138
+ all: unset;
139
+ }
140
+ #share-btn-container div:nth-child(-n+2){
141
+ width: auto !important;
142
+ min-height: 0px !important;
143
+ }
144
+ #share-btn-container .wrap {
145
+ display: none !important;
146
+ }
147
+
148
+ .gr-form{
149
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
150
+ }
151
+ #prompt-container{
152
+ gap: 0;
153
+ }
154
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
155
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
156
+ .image_duplication{position: absolute; width: 100px; left: 50px}
157
+ """
158
+
159
+ block = gr.Blocks(css=css)
160
+
161
+ examples = [
162
+ [
163
+ 'Gollum',
164
+ 'Legolas',
165
+ 9
166
+ ],
167
+ [
168
+ 'Frodo',
169
+ 'Gandalf',
170
+ 9
171
+ ],
172
+ [
173
+ 'Gollum',
174
+ 'Captain Picard',
175
+ 9
176
+ ],
177
+ [
178
+ 'Harry Potter',
179
+ 'Dobby',
180
+ 9
181
+ ],
182
+ [
183
+ "Yoda",
184
+ 'a Borg drone',
185
+ 9
186
+ ],
187
+ ]
188
+
189
+
190
+ with block:
191
+ gr.HTML(
192
+ """
193
+ <div style="text-align: center; margin: 0 auto;">
194
+ <div
195
+ style="
196
+ display: inline-flex;
197
+ align-items: center;
198
+ gap: 0.8rem;
199
+ font-size: 1.75rem;
200
+ "
201
+ >
202
+ <svg
203
+ width="0.65em"
204
+ height="0.65em"
205
+ viewBox="0 0 115 115"
206
+ fill="none"
207
+ xmlns="http://www.w3.org/2000/svg"
208
+ >
209
+ <rect width="23" height="23" fill="white"></rect>
210
+ <rect y="69" width="23" height="23" fill="white"></rect>
211
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
212
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
213
+ <rect x="46" width="23" height="23" fill="white"></rect>
214
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
215
+ <rect x="69" width="23" height="23" fill="black"></rect>
216
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
217
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
218
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
219
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
220
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
221
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
222
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
223
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
224
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
225
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
226
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
227
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
228
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
229
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
230
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
231
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
232
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
233
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
234
+ </svg>
235
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
236
+ Stable Diffusion 2.1 Demo
237
+ </h1>
238
+ </div>
239
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
240
+ 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
241
+ access you can try
242
+ <a
243
+ href="http://beta.dreamstudio.ai/"
244
+ style="text-decoration: underline;"
245
+ target="_blank"
246
+ >DreamStudio Beta</a
247
+ >.</a>
248
+ </p>
249
+ </div>
250
+ """
251
+ )
252
+ with gr.Group():
253
+ with gr.Box():
254
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
255
+ with gr.Column():
256
+ person1 = gr.Textbox(
257
+ label="First Person",
258
+ show_label=False,
259
+ max_lines=1,
260
+ placeholder="First person",
261
+ elem_id="person1-text-input",
262
+ ).style(
263
+ border=(True, False, True, True),
264
+ rounded=(True, False, False, True),
265
+ container=False,
266
+ )
267
+ person2 = gr.Textbox(
268
+ label="Second Person",
269
+ show_label=False,
270
+ max_lines=1,
271
+ placeholder="Second person",
272
+ elem_id="person2-text-input",
273
+ ).style(
274
+ border=(True, False, True, True),
275
+ rounded=(True, False, False, True),
276
+ container=False,
277
+ )
278
+ btn = gr.Button("Generate image").style(
279
+ margin=False,
280
+ rounded=(False, True, True, False),
281
+ full_width=False,
282
+ )
283
+
284
+ gallery = gr.Gallery(
285
+ label="Generated images", show_label=False, elem_id="gallery"
286
+ ).style(grid=[2], height="auto")
287
+
288
+ with gr.Group(elem_id="container-advanced-btns"):
289
+ #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
290
+ with gr.Group(elem_id="share-btn-container"):
291
+ community_icon = gr.HTML(community_icon_html)
292
+ loading_icon = gr.HTML(loading_icon_html)
293
+ share_button = gr.Button("Share to community", elem_id="share-btn")
294
+
295
+ with gr.Accordion("Advanced settings", open=False):
296
+ # gr.Markdown("Advanced settings are temporarily unavailable")
297
+ # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
298
+ # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
299
+ guidance_scale = gr.Slider(
300
+ label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
301
+ )
302
+ # seed = gr.Slider(
303
+ # label="Seed",
304
+ # minimum=0,
305
+ # maximum=2147483647,
306
+ # step=1,
307
+ # randomize=True,
308
+ # )
309
+
310
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[person1, person2, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
311
+ ex.dataset.headers = [""]
312
+ negative.submit(infer, inputs=[person1, person2, guidance_scale], outputs=[gallery], postprocess=False)
313
+ text.submit(infer, inputs=[person1, person2, guidance_scale], outputs=[gallery], postprocess=False)
314
+ btn.click(infer, inputs=[person1, person2, guidance_scale], outputs=[gallery], postprocess=False)
315
+
316
+ #advanced_button.click(
317
+ # None,
318
+ # [],
319
+ # text,
320
+ # _js="""
321
+ # () => {
322
+ # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
323
+ # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
324
+ # }""",
325
+ #)
326
+ share_button.click(
327
+ None,
328
+ [],
329
+ [],
330
+ _js=share_js,
331
+ )
332
+ gr.HTML(
333
+ """
334
+ <div class="footer">
335
+ <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
336
+ </p>
337
+ </div>
338
+ """
339
+ )
340
+ with gr.Accordion(label="License", open=False):
341
+ gr.HTML(
342
+ """<div class="acknowledgments">
343
+ <p><h4>LICENSE</h4>
344
+ 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>
345
+ <p><h4>Biases and content acknowledgment</h4>
346
+ 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>
347
+ </div>
348
+ """
349
+ )
350
+
351
+
352
+ block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)