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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from datasets import load_dataset
|
3 |
+
from PIL import Image
|
4 |
+
import re
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
+
|
8 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
9 |
+
|
10 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
11 |
+
device = "cuda"
|
12 |
+
|
13 |
+
word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
|
14 |
+
word_list = word_list_dataset["train"]['text']
|
15 |
+
|
16 |
+
is_gpu_busy = False
|
17 |
+
def infer(prompt):
|
18 |
+
global is_gpu_busy
|
19 |
+
samples = 4
|
20 |
+
steps = 50
|
21 |
+
scale = 7.5
|
22 |
+
for filter in word_list:
|
23 |
+
if re.search(rf"\b{filter}\b", prompt):
|
24 |
+
raise gr.Error("Unsafe content found. Please try again with different prompts.")
|
25 |
+
|
26 |
+
images = []
|
27 |
+
url = os.getenv('JAX_BACKEND_URL')
|
28 |
+
payload = {'prompt': prompt}
|
29 |
+
images_request = requests.post(url, json = payload)
|
30 |
+
for image in images_request.json()["images"]:
|
31 |
+
image_b64 = (f"data:image/jpeg;base64,{image}")
|
32 |
+
images.append(image_b64)
|
33 |
+
|
34 |
+
return images
|
35 |
+
|
36 |
+
|
37 |
+
css = """
|
38 |
+
.gradio-container {
|
39 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
40 |
+
}
|
41 |
+
.gr-button {
|
42 |
+
color: white;
|
43 |
+
border-color: black;
|
44 |
+
background: black;
|
45 |
+
}
|
46 |
+
input[type='range'] {
|
47 |
+
accent-color: black;
|
48 |
+
}
|
49 |
+
.dark input[type='range'] {
|
50 |
+
accent-color: #dfdfdf;
|
51 |
+
}
|
52 |
+
.container {
|
53 |
+
max-width: 730px;
|
54 |
+
margin: auto;
|
55 |
+
padding-top: 1.5rem;
|
56 |
+
}
|
57 |
+
#gallery {
|
58 |
+
min-height: 22rem;
|
59 |
+
margin-bottom: 15px;
|
60 |
+
margin-left: auto;
|
61 |
+
margin-right: auto;
|
62 |
+
border-bottom-right-radius: .5rem !important;
|
63 |
+
border-bottom-left-radius: .5rem !important;
|
64 |
+
}
|
65 |
+
#gallery>div>.h-full {
|
66 |
+
min-height: 20rem;
|
67 |
+
}
|
68 |
+
.details:hover {
|
69 |
+
text-decoration: underline;
|
70 |
+
}
|
71 |
+
.gr-button {
|
72 |
+
white-space: nowrap;
|
73 |
+
}
|
74 |
+
.gr-button:focus {
|
75 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
76 |
+
outline: none;
|
77 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
78 |
+
--tw-border-opacity: 1;
|
79 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
80 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
81 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
82 |
+
--tw-ring-opacity: .5;
|
83 |
+
}
|
84 |
+
#advanced-btn {
|
85 |
+
font-size: .7rem !important;
|
86 |
+
line-height: 19px;
|
87 |
+
margin-top: 12px;
|
88 |
+
margin-bottom: 12px;
|
89 |
+
padding: 2px 8px;
|
90 |
+
border-radius: 14px !important;
|
91 |
+
}
|
92 |
+
#advanced-options {
|
93 |
+
display: none;
|
94 |
+
margin-bottom: 20px;
|
95 |
+
}
|
96 |
+
.footer {
|
97 |
+
margin-bottom: 45px;
|
98 |
+
margin-top: 35px;
|
99 |
+
text-align: center;
|
100 |
+
border-bottom: 1px solid #e5e5e5;
|
101 |
+
}
|
102 |
+
.footer>p {
|
103 |
+
font-size: .8rem;
|
104 |
+
display: inline-block;
|
105 |
+
padding: 0 10px;
|
106 |
+
transform: translateY(10px);
|
107 |
+
background: white;
|
108 |
+
}
|
109 |
+
.dark .footer {
|
110 |
+
border-color: #303030;
|
111 |
+
}
|
112 |
+
.dark .footer>p {
|
113 |
+
background: #0b0f19;
|
114 |
+
}
|
115 |
+
.acknowledgments h4{
|
116 |
+
margin: 1.25em 0 .25em 0;
|
117 |
+
font-weight: bold;
|
118 |
+
font-size: 115%;
|
119 |
+
}
|
120 |
+
#container-advanced-btns{
|
121 |
+
display: flex;
|
122 |
+
flex-wrap: wrap;
|
123 |
+
justify-content: space-between;
|
124 |
+
align-items: center;
|
125 |
+
}
|
126 |
+
.animate-spin {
|
127 |
+
animation: spin 1s linear infinite;
|
128 |
+
}
|
129 |
+
@keyframes spin {
|
130 |
+
from {
|
131 |
+
transform: rotate(0deg);
|
132 |
+
}
|
133 |
+
to {
|
134 |
+
transform: rotate(360deg);
|
135 |
+
}
|
136 |
+
}
|
137 |
+
#share-btn-container {
|
138 |
+
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;
|
139 |
+
}
|
140 |
+
#share-btn {
|
141 |
+
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;
|
142 |
+
}
|
143 |
+
#share-btn * {
|
144 |
+
all: unset;
|
145 |
+
}
|
146 |
+
.gr-form{
|
147 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
148 |
+
}
|
149 |
+
#prompt-container{
|
150 |
+
gap: 0;
|
151 |
+
}
|
152 |
+
#share-btn-container div:nth-child(-n+2){
|
153 |
+
width: auto !important;
|
154 |
+
min-height: 0px !important;
|
155 |
+
}
|
156 |
+
"""
|
157 |
+
|
158 |
+
block = gr.Blocks(css=css)
|
159 |
+
|
160 |
+
examples = [
|
161 |
+
[
|
162 |
+
'The spirit of a tamagotchi wandering in the city of Paris',
|
163 |
+
# 4,
|
164 |
+
# 45,
|
165 |
+
# 7.5,
|
166 |
+
# 1024,
|
167 |
+
],
|
168 |
+
[
|
169 |
+
'A delicious ceviche cheesecake slice',
|
170 |
+
# 4,
|
171 |
+
# 45,
|
172 |
+
# 7,
|
173 |
+
# 1024,
|
174 |
+
],
|
175 |
+
[
|
176 |
+
'A pao de queijo foodcart in front of a japanese castle',
|
177 |
+
# 4,
|
178 |
+
# 45,
|
179 |
+
# 7,
|
180 |
+
# 1024,
|
181 |
+
],
|
182 |
+
[
|
183 |
+
'alone in the amusement park by Edward Hopper',
|
184 |
+
# 4,
|
185 |
+
# 45,
|
186 |
+
# 7,
|
187 |
+
# 1024,
|
188 |
+
],
|
189 |
+
[
|
190 |
+
"A large cabin on top of a sunny mountain in the style of Dreamworks, artstation",
|
191 |
+
# 4,
|
192 |
+
# 45,
|
193 |
+
# 7,
|
194 |
+
# 1024,
|
195 |
+
],
|
196 |
+
]
|
197 |
+
|
198 |
+
|
199 |
+
with block:
|
200 |
+
gr.HTML(
|
201 |
+
"""
|
202 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto; padding-top: 7px;">
|
203 |
+
<div
|
204 |
+
style="
|
205 |
+
display: inline-flex;
|
206 |
+
align-items: center;
|
207 |
+
gap: 0.8rem;
|
208 |
+
font-size: 1.75rem;
|
209 |
+
"
|
210 |
+
>
|
211 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
212 |
+
Stable Diffusion v1-5 Demo
|
213 |
+
</h1>
|
214 |
+
</div>
|
215 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
216 |
+
Stable Diffusion v1-5 is the latest version of the state of the art text-to-image model.<br>For faster generation you can try
|
217 |
+
<a href="https://app.runwayml.com/ai-tools/text-to-image"
|
218 |
+
style="text-decoration: underline;" target="_blank">text to image tool at Runway.</a>
|
219 |
+
</p>
|
220 |
+
</div>
|
221 |
+
"""
|
222 |
+
)
|
223 |
+
with gr.Group():
|
224 |
+
with gr.Box():
|
225 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
226 |
+
text = gr.Textbox(
|
227 |
+
label="Enter your prompt",
|
228 |
+
show_label=False,
|
229 |
+
max_lines=1,
|
230 |
+
placeholder="Enter your prompt",
|
231 |
+
elem_id="prompt-text-input",
|
232 |
+
).style(
|
233 |
+
border=(True, False, True, True),
|
234 |
+
rounded=(True, False, False, True),
|
235 |
+
container=False,
|
236 |
+
)
|
237 |
+
btn = gr.Button("Generate image").style(
|
238 |
+
margin=False,
|
239 |
+
rounded=(False, True, True, False),
|
240 |
+
full_width=False,
|
241 |
+
)
|
242 |
+
|
243 |
+
gallery = gr.Gallery(
|
244 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
245 |
+
).style(grid=[2], height="auto")
|
246 |
+
|
247 |
+
with gr.Group(elem_id="container-advanced-btns"):
|
248 |
+
advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
249 |
+
with gr.Group(elem_id="share-btn-container"):
|
250 |
+
community_icon = gr.HTML(community_icon_html)
|
251 |
+
loading_icon = gr.HTML(loading_icon_html)
|
252 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
253 |
+
|
254 |
+
with gr.Row(elem_id="advanced-options"):
|
255 |
+
gr.Markdown("Advanced settings are temporarily unavailable")
|
256 |
+
samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
|
257 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
|
258 |
+
scale = gr.Slider(
|
259 |
+
label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
|
260 |
+
)
|
261 |
+
seed = gr.Slider(
|
262 |
+
label="Seed",
|
263 |
+
minimum=0,
|
264 |
+
maximum=2147483647,
|
265 |
+
step=1,
|
266 |
+
randomize=True,
|
267 |
+
)
|
268 |
+
|
269 |
+
ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery], cache_examples=True, postprocess=False)
|
270 |
+
ex.dataset.headers = [""]
|
271 |
+
|
272 |
+
text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
|
273 |
+
btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
|
274 |
+
|
275 |
+
advanced_button.click(
|
276 |
+
None,
|
277 |
+
[],
|
278 |
+
text,
|
279 |
+
_js="""
|
280 |
+
() => {
|
281 |
+
const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
|
282 |
+
options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
283 |
+
}""",
|
284 |
+
)
|
285 |
+
share_button.click(
|
286 |
+
None,
|
287 |
+
[],
|
288 |
+
[],
|
289 |
+
_js=share_js,
|
290 |
+
)
|
291 |
+
gr.HTML(
|
292 |
+
"""
|
293 |
+
<div class="footer">
|
294 |
+
<p>Model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://runwayml.com/" style="text-decoration: underline;" target="_blank">Runway</a> supported by <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
|
295 |
+
</p>
|
296 |
+
</div>
|
297 |
+
<div class="acknowledgments">
|
298 |
+
<p><h4>LICENSE</h4>
|
299 |
+
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>
|
300 |
+
<p><h4>Biases and content acknowledgment</h4>
|
301 |
+
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/runwayml/stable-diffusion-v1-5" style="text-decoration: underline;" target="_blank">model card</a></p>
|
302 |
+
</div>
|
303 |
+
"""
|
304 |
+
)
|
305 |
+
|
306 |
+
block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)
|