Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,325 +0,0 @@
|
|
| 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 |
-
import uuid
|
| 8 |
-
import base64
|
| 9 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 10 |
-
word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt")
|
| 11 |
-
word_list = word_list_dataset["train"]['text']
|
| 12 |
-
is_gpu_busy = False
|
| 13 |
-
def infer(prompt, negative, scale):
|
| 14 |
-
global is_gpu_busy
|
| 15 |
-
for filter in word_list:
|
| 16 |
-
if re.search(rf"\b{filter}\b", prompt):
|
| 17 |
-
print(filter)
|
| 18 |
-
print(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 |
-
print(url)
|
| 24 |
-
payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
|
| 25 |
-
images_request = requests.post(url, json = payload)
|
| 26 |
-
for image in images_request.json()["images"]:
|
| 27 |
-
file_path = f"{uuid.uuid4()}.jpg"
|
| 28 |
-
with open(file_path, "wb") as f:
|
| 29 |
-
f.write(base64.b64decode(image))
|
| 30 |
-
images.append(file_path)
|
| 31 |
-
|
| 32 |
-
return images
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
css = """
|
| 36 |
-
.gradio-container {
|
| 37 |
-
max-width: 768px !important;
|
| 38 |
-
}
|
| 39 |
-
.gradio-container {
|
| 40 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 41 |
-
}
|
| 42 |
-
.gr-button {
|
| 43 |
-
color: white;
|
| 44 |
-
border-color: black;
|
| 45 |
-
background: black;
|
| 46 |
-
}
|
| 47 |
-
input[type='range'] {
|
| 48 |
-
accent-color: black;
|
| 49 |
-
}
|
| 50 |
-
.dark input[type='range'] {
|
| 51 |
-
accent-color: #dfdfdf;
|
| 52 |
-
}
|
| 53 |
-
.container {
|
| 54 |
-
max-width: 730px;
|
| 55 |
-
margin: auto;
|
| 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 |
-
.animate-spin {
|
| 121 |
-
animation: spin 1s linear infinite;
|
| 122 |
-
}
|
| 123 |
-
@keyframes spin {
|
| 124 |
-
from {
|
| 125 |
-
transform: rotate(0deg);
|
| 126 |
-
}
|
| 127 |
-
to {
|
| 128 |
-
transform: rotate(360deg);
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
-
#share-btn-container {
|
| 132 |
-
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;
|
| 133 |
-
margin-top: 10px;
|
| 134 |
-
margin-left: auto;
|
| 135 |
-
}
|
| 136 |
-
#share-btn-container .styler{
|
| 137 |
-
background-color: #000000;
|
| 138 |
-
}
|
| 139 |
-
#share-btn {
|
| 140 |
-
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;
|
| 141 |
-
}
|
| 142 |
-
#share-btn * {
|
| 143 |
-
all: unset;
|
| 144 |
-
}
|
| 145 |
-
#share-btn-container div:nth-child(-n+2){
|
| 146 |
-
width: auto !important;
|
| 147 |
-
min-height: 0px !important;
|
| 148 |
-
}
|
| 149 |
-
#share-btn-container .wrap {
|
| 150 |
-
display: none !important;
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
.gr-form{
|
| 154 |
-
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
| 155 |
-
}
|
| 156 |
-
#prompt-container{
|
| 157 |
-
gap: 0;
|
| 158 |
-
}
|
| 159 |
-
#prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
|
| 160 |
-
#component-16{border-top-width: 1px!important;margin-top: 1em}
|
| 161 |
-
.image_duplication{position: absolute; width: 100px; left: 50px}
|
| 162 |
-
button{height: 100%}
|
| 163 |
-
"""
|
| 164 |
-
block = gr.Blocks(css=css)
|
| 165 |
-
examples = [
|
| 166 |
-
[
|
| 167 |
-
'A high tech solarpunk utopia in the Amazon rainforest',
|
| 168 |
-
'low quality',
|
| 169 |
-
9
|
| 170 |
-
],
|
| 171 |
-
[
|
| 172 |
-
'A pikachu fine dining with a view to the Eiffel Tower',
|
| 173 |
-
'low quality',
|
| 174 |
-
9
|
| 175 |
-
],
|
| 176 |
-
[
|
| 177 |
-
'A mecha robot in a favela in expressionist style',
|
| 178 |
-
'low quality, 3d, photorealistic',
|
| 179 |
-
9
|
| 180 |
-
],
|
| 181 |
-
[
|
| 182 |
-
'an insect robot preparing a delicious meal',
|
| 183 |
-
'low quality, illustration',
|
| 184 |
-
9
|
| 185 |
-
],
|
| 186 |
-
[
|
| 187 |
-
"A small cabin on top of a snowy mountain in the style of Disney, artstation",
|
| 188 |
-
'low quality, ugly',
|
| 189 |
-
9
|
| 190 |
-
],
|
| 191 |
-
]
|
| 192 |
-
with block:
|
| 193 |
-
gr.HTML(
|
| 194 |
-
"""
|
| 195 |
-
<div style="text-align: center; margin: 0 auto;">
|
| 196 |
-
<div
|
| 197 |
-
style="
|
| 198 |
-
display: inline-flex;
|
| 199 |
-
align-items: center;
|
| 200 |
-
gap: 0.8rem;
|
| 201 |
-
font-size: 1.75rem;
|
| 202 |
-
"
|
| 203 |
-
>
|
| 204 |
-
<svg
|
| 205 |
-
width="0.65em"
|
| 206 |
-
height="0.65em"
|
| 207 |
-
viewBox="0 0 115 115"
|
| 208 |
-
fill="none"
|
| 209 |
-
xmlns="http://www.w3.org/2000/svg"
|
| 210 |
-
>
|
| 211 |
-
<rect width="23" height="23" fill="white"></rect>
|
| 212 |
-
<rect y="69" width="23" height="23" fill="white"></rect>
|
| 213 |
-
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
| 214 |
-
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 215 |
-
<rect x="46" width="23" height="23" fill="white"></rect>
|
| 216 |
-
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
| 217 |
-
<rect x="69" width="23" height="23" fill="black"></rect>
|
| 218 |
-
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
| 219 |
-
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
| 220 |
-
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
| 221 |
-
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
| 222 |
-
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
| 223 |
-
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 224 |
-
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
| 225 |
-
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 226 |
-
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
| 227 |
-
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
| 228 |
-
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
| 229 |
-
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
| 230 |
-
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
| 231 |
-
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
| 232 |
-
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
| 233 |
-
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
| 234 |
-
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
| 235 |
-
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
| 236 |
-
</svg>
|
| 237 |
-
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
| 238 |
-
Stable Diffusion 2.1 Demo
|
| 239 |
-
</h1>
|
| 240 |
-
</div>
|
| 241 |
-
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
| 242 |
-
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
|
| 243 |
-
access you can try
|
| 244 |
-
<a
|
| 245 |
-
href="http://beta.dreamstudio.ai/"
|
| 246 |
-
style="text-decoration: underline;"
|
| 247 |
-
target="_blank"
|
| 248 |
-
>DreamStudio Beta</a
|
| 249 |
-
>.</a>
|
| 250 |
-
</p>
|
| 251 |
-
</div>
|
| 252 |
-
"""
|
| 253 |
-
)
|
| 254 |
-
with gr.Group():
|
| 255 |
-
with gr.Row(elem_id="prompt-container"):
|
| 256 |
-
with gr.Column(scale=3):
|
| 257 |
-
text = gr.Textbox(
|
| 258 |
-
label="Enter your prompt",
|
| 259 |
-
show_label=False,
|
| 260 |
-
max_lines=1,
|
| 261 |
-
placeholder="Enter your prompt",
|
| 262 |
-
elem_id="prompt-text-input",
|
| 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 |
-
)
|
| 271 |
-
with gr.Column(scale=1, min_width=150):
|
| 272 |
-
btn = gr.Button("Generate image")
|
| 273 |
-
gallery = gr.Gallery(
|
| 274 |
-
label="Generated images", show_label=False, elem_id="gallery"
|
| 275 |
-
)
|
| 276 |
-
with gr.Group(elem_id="container-advanced-btns"):
|
| 277 |
-
#advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
| 278 |
-
with gr.Group(elem_id="share-btn-container"):
|
| 279 |
-
community_icon = gr.HTML(community_icon_html)
|
| 280 |
-
loading_icon = gr.HTML(loading_icon_html)
|
| 281 |
-
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 282 |
-
with gr.Accordion("Advanced settings", open=False):
|
| 283 |
-
# gr.Markdown("Advanced settings are temporarily unavailable")
|
| 284 |
-
# samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
|
| 285 |
-
# steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
|
| 286 |
-
guidance_scale = gr.Slider(
|
| 287 |
-
label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
|
| 288 |
-
)
|
| 289 |
-
# seed = gr.Slider(
|
| 290 |
-
# label="Seed",
|
| 291 |
-
# minimum=0,
|
| 292 |
-
# maximum=2147483647,
|
| 293 |
-
# step=1,
|
| 294 |
-
# randomize=True,
|
| 295 |
-
# )
|
| 296 |
-
ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
|
| 297 |
-
ex.dataset.headers = [""]
|
| 298 |
-
negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], concurrency_limit=80)
|
| 299 |
-
text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], concurrency_limit=80)
|
| 300 |
-
btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], concurrency_limit=80)
|
| 301 |
-
|
| 302 |
-
#advanced_button.click(
|
| 303 |
-
# None,
|
| 304 |
-
# [],
|
| 305 |
-
# text,
|
| 306 |
-
# _js="""
|
| 307 |
-
# () => {
|
| 308 |
-
# const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
|
| 309 |
-
# options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
| 310 |
-
# }""",
|
| 311 |
-
#)
|
| 312 |
-
share_button.click(
|
| 313 |
-
None,
|
| 314 |
-
[],
|
| 315 |
-
[],
|
| 316 |
-
js=share_js,
|
| 317 |
-
)
|
| 318 |
-
gr.HTML(
|
| 319 |
-
"""
|
| 320 |
-
<div class="footer">
|
| 321 |
-
<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
|
| 322 |
-
</p>
|
| 323 |
-
</div>
|
| 324 |
-
"""
|
| 325 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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