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