Spaces:
Runtime error
Runtime error
Update app.py
#2
by
telcom - opened
app.py
CHANGED
|
@@ -1,266 +1,221 @@
|
|
| 1 |
-
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
|
| 10 |
import os
|
| 11 |
-
import requests
|
| 12 |
import json
|
| 13 |
import base64
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 17 |
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
url = os.getenv('BACKEND_URL_SAFE_NEW')
|
| 23 |
-
token = os.getenv('BACKEND_TOKEN')
|
| 24 |
-
user = os.getenv('BACKEND_USER')
|
| 25 |
-
res = requests.post(url, json={
|
| 26 |
"model": "togethercomputer/UniversalSD",
|
| 27 |
"prompt": prompt,
|
| 28 |
-
"n": n_samples,
|
| 29 |
"mode": mode,
|
| 30 |
-
"steps": steps,
|
| 31 |
-
"seed": seed,
|
| 32 |
-
"guidance_scale": scale,
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return res
|
| 38 |
|
| 39 |
-
def infer(prompt, n_samples, steps, scale, seed):
|
| 40 |
-
global is_gpu_busy
|
| 41 |
-
# generator = torch.Generator(device=device).manual_seed(seed)
|
| 42 |
-
# print("Is GPU busy? ", is_gpu_busy)
|
| 43 |
-
images = []
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
if 'output' not in data:
|
| 52 |
-
raise gr.Error("An error occurred.")
|
| 53 |
-
else:
|
| 54 |
-
if data['output']['result_type'] == "error":
|
| 55 |
-
raise gr.Error(data['output']['value'])
|
| 56 |
-
for image in data['output']['choices']:
|
| 57 |
-
im = Image.open(BytesIO(base64.b64decode(image['image_base64'])))
|
| 58 |
-
images.append(im)
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
images.append(im)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return images
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
css = """
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
font-size: .8rem;
|
| 139 |
-
display: inline-block;
|
| 140 |
-
padding: 0 10px;
|
| 141 |
-
transform: translateY(10px);
|
| 142 |
-
background: white;
|
| 143 |
-
}
|
| 144 |
-
.dark .footer {
|
| 145 |
-
border-color: #303030;
|
| 146 |
-
}
|
| 147 |
-
.dark .footer>p {
|
| 148 |
-
background: #0b0f19;
|
| 149 |
-
}
|
| 150 |
-
.acknowledgments h4{
|
| 151 |
-
margin: 1.25em 0 .25em 0;
|
| 152 |
-
font-weight: bold;
|
| 153 |
-
font-size: 115%;
|
| 154 |
-
}
|
| 155 |
-
#container-advanced-btns{
|
| 156 |
-
display: flex;
|
| 157 |
-
flex-wrap: wrap;
|
| 158 |
-
justify-content: space-between;
|
| 159 |
-
align-items: center;
|
| 160 |
-
}
|
| 161 |
-
.animate-spin {
|
| 162 |
-
animation: spin 1s linear infinite;
|
| 163 |
-
}
|
| 164 |
-
@keyframes spin {
|
| 165 |
-
from {
|
| 166 |
-
transform: rotate(0deg);
|
| 167 |
-
}
|
| 168 |
-
to {
|
| 169 |
-
transform: rotate(360deg);
|
| 170 |
-
}
|
| 171 |
-
}
|
| 172 |
-
#share-btn-container {
|
| 173 |
-
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #3a669bff; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
| 174 |
-
}
|
| 175 |
-
#share-btn {
|
| 176 |
-
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;
|
| 177 |
-
}
|
| 178 |
-
#share-btn * {
|
| 179 |
-
all: unset;
|
| 180 |
-
}
|
| 181 |
-
.gr-form{
|
| 182 |
-
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
| 183 |
-
}
|
| 184 |
-
#prompt-container{
|
| 185 |
-
gap: 0;
|
| 186 |
-
}
|
| 187 |
"""
|
| 188 |
|
| 189 |
-
block = gr.Blocks(css=css)
|
| 190 |
-
|
| 191 |
examples = [
|
| 192 |
-
[
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
],
|
| 199 |
-
[
|
| 200 |
-
'a gorgeous female photo',
|
| 201 |
-
1,
|
| 202 |
-
50,
|
| 203 |
-
7.5,
|
| 204 |
-
733664822,
|
| 205 |
-
],
|
| 206 |
-
[
|
| 207 |
-
'a gorgeous male photo',
|
| 208 |
-
1,
|
| 209 |
-
50,
|
| 210 |
-
7.5,
|
| 211 |
-
881355,
|
| 212 |
-
],
|
| 213 |
-
[
|
| 214 |
-
'the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c. leyendecker',
|
| 215 |
-
1,
|
| 216 |
-
50,
|
| 217 |
-
7.5,
|
| 218 |
-
557645701
|
| 219 |
-
],
|
| 220 |
-
[
|
| 221 |
-
'portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and children from bahnhof zoo, detailed ',
|
| 222 |
-
1,
|
| 223 |
-
50,
|
| 224 |
-
9,
|
| 225 |
-
1115417309,
|
| 226 |
-
],
|
| 227 |
-
[
|
| 228 |
-
'portrait of Sickly diseased dying Samurai warrior, sun shining, photo realistic illustration by greg rutkowski, thomas kindkade, alphonse mucha, loish, norman rockwell.',
|
| 229 |
-
1,
|
| 230 |
-
50,
|
| 231 |
-
10,
|
| 232 |
-
1714108957,
|
| 233 |
-
]
|
| 234 |
]
|
| 235 |
|
|
|
|
|
|
|
| 236 |
with block:
|
| 237 |
gr.HTML(
|
| 238 |
"""
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
<p style="margin-bottom: 10px; font-size: 94%">
|
| 255 |
-
Safe Stable Diffusion extends Stable Diffusion with safety guidance. In the case of NSFW images it returns the closest non-NSFW images instead of a black square.
|
| 256 |
-
Details can be found in the <a href="https://arxiv.org/abs/2211.05105" style="text-decoration: underline;" target="_blank">Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models paper</a>.
|
| 257 |
-
</p>
|
| 258 |
-
</div>
|
| 259 |
"""
|
| 260 |
)
|
|
|
|
| 261 |
with gr.Group():
|
| 262 |
with gr.Box():
|
| 263 |
-
with gr.Row(elem_id="prompt-container")
|
| 264 |
text = gr.Textbox(
|
| 265 |
label="Enter your prompt",
|
| 266 |
show_label=False,
|
|
@@ -279,7 +234,9 @@ with block:
|
|
| 279 |
)
|
| 280 |
|
| 281 |
gallery = gr.Gallery(
|
| 282 |
-
label="Left: Stable Diffusion, Right: Safe Stable Diffusion",
|
|
|
|
|
|
|
| 283 |
).style(grid=[2], height="auto")
|
| 284 |
|
| 285 |
with gr.Group(elem_id="container-advanced-btns"):
|
|
@@ -290,22 +247,19 @@ with block:
|
|
| 290 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 291 |
|
| 292 |
with gr.Row(elem_id="advanced-options"):
|
| 293 |
-
#gr.Markdown("Advanced settings are temporarily unavailable")
|
| 294 |
samples = gr.Slider(label="Images", minimum=1, maximum=1, value=1, step=1)
|
| 295 |
steps = gr.Slider(label="Steps", minimum=50, maximum=50, value=50, step=1)
|
| 296 |
-
scale = gr.Slider(
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, steps, scale, seed],
|
| 308 |
-
outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
|
| 309 |
ex.dataset.headers = [""]
|
| 310 |
|
| 311 |
text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)
|
|
@@ -321,25 +275,21 @@ with block:
|
|
| 321 |
options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
| 322 |
}""",
|
| 323 |
)
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
[],
|
| 328 |
-
_js=share_js,
|
| 329 |
-
)
|
| 330 |
gr.HTML(
|
| 331 |
"""
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
</
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
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. While the applied safety guidance suppresses the majority of inappropriate content, this still could apply to Safe Stable Diffusion models. The original 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. Safety guidance suppresses potentially inappropriate content during inference. You can read more in the <a href="https://huggingface.co/AIML-TUDA/stable-diffusion-safe" style="text-decoration: underline;" target="_blank">model card</a>.</p>
|
| 341 |
-
</div>
|
| 342 |
-
"""
|
| 343 |
)
|
| 344 |
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import requests
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 11 |
|
| 12 |
|
| 13 |
+
def safe_sd(prompt: str, n_samples: int, steps: int, scale: float, seed: int, mode: str):
|
| 14 |
+
url = os.getenv("BACKEND_URL_SAFE_NEW")
|
| 15 |
+
token = os.getenv("BACKEND_TOKEN")
|
| 16 |
+
user = os.getenv("BACKEND_USER")
|
| 17 |
+
|
| 18 |
+
if not url or not token or not user:
|
| 19 |
+
raise gr.Error(
|
| 20 |
+
"Backend is not configured. Please set BACKEND_URL_SAFE_NEW, BACKEND_TOKEN, BACKEND_USER."
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"model": "togethercomputer/UniversalSD",
|
| 25 |
"prompt": prompt,
|
| 26 |
+
"n": int(n_samples),
|
| 27 |
"mode": mode,
|
| 28 |
+
"steps": int(steps),
|
| 29 |
+
"seed": int(seed),
|
| 30 |
+
"guidance_scale": float(scale),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
headers = {"Authorization": token, "User-Agent": user}
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
res = requests.post(url, json=payload, headers=headers, timeout=180)
|
| 37 |
+
except requests.RequestException as e:
|
| 38 |
+
raise gr.Error(f"Backend request failed: {e}")
|
| 39 |
+
|
| 40 |
+
if res.status_code != 200:
|
| 41 |
+
txt = res.text if isinstance(res.text, str) else str(res.content)
|
| 42 |
+
raise gr.Error(f"Backend error HTTP {res.status_code}: {txt[:400]}")
|
| 43 |
+
|
| 44 |
return res
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def _parse_images_from_response(response: requests.Response):
|
| 48 |
+
# Prefer response.json(); fall back to raw decode if content-type is odd.
|
| 49 |
+
try:
|
| 50 |
+
data = response.json()
|
| 51 |
+
except Exception:
|
| 52 |
+
try:
|
| 53 |
+
data = json.loads(response.content.decode("utf-8", errors="replace"))
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise gr.Error(f"Could not parse backend JSON: {e}")
|
| 56 |
|
| 57 |
+
if "output" not in data:
|
| 58 |
+
raise gr.Error("Backend response missing 'output' field.")
|
| 59 |
|
| 60 |
+
out = data["output"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
if isinstance(out, dict) and out.get("result_type") == "error":
|
| 63 |
+
raise gr.Error(out.get("value", "Backend returned an error."))
|
| 64 |
|
| 65 |
+
choices = out.get("choices", [])
|
| 66 |
+
images = []
|
| 67 |
+
|
| 68 |
+
for c in choices:
|
| 69 |
+
b64 = c.get("image_base64")
|
| 70 |
+
if not b64:
|
| 71 |
+
continue
|
| 72 |
+
try:
|
| 73 |
+
raw = base64.b64decode(b64)
|
| 74 |
+
im = Image.open(BytesIO(raw)).convert("RGB")
|
| 75 |
images.append(im)
|
| 76 |
+
except Exception:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
if not images:
|
| 80 |
+
raise gr.Error("No images returned from backend.")
|
| 81 |
+
|
| 82 |
return images
|
| 83 |
|
| 84 |
|
| 85 |
+
def infer(prompt, n_samples, steps, scale, seed):
|
| 86 |
+
if not prompt or not str(prompt).strip():
|
| 87 |
+
raise gr.Error("Empty prompt. Please provide a prompt.")
|
| 88 |
+
|
| 89 |
+
n_samples = int(n_samples)
|
| 90 |
+
steps = int(steps)
|
| 91 |
+
scale = float(scale)
|
| 92 |
+
seed = int(seed)
|
| 93 |
+
|
| 94 |
+
# Left: SD (mode text2img), Right: Safe SD (mode safe_text2img)
|
| 95 |
+
sd_resp = safe_sd(prompt, n_samples, max(50, steps), scale, seed, mode="text2img")
|
| 96 |
+
sd_images = _parse_images_from_response(sd_resp)
|
| 97 |
+
|
| 98 |
+
safe_resp = safe_sd(prompt, n_samples, max(50, steps), scale, seed, mode="safe_text2img")
|
| 99 |
+
safe_images = _parse_images_from_response(safe_resp)
|
| 100 |
+
|
| 101 |
+
# Interleave: [sd1, safe1, sd2, safe2, ...]
|
| 102 |
+
merged = []
|
| 103 |
+
k = min(len(sd_images), len(safe_images))
|
| 104 |
+
for i in range(k):
|
| 105 |
+
merged.append(sd_images[i])
|
| 106 |
+
merged.append(safe_images[i])
|
| 107 |
+
|
| 108 |
+
if len(sd_images) > k:
|
| 109 |
+
merged.extend(sd_images[k:])
|
| 110 |
+
if len(safe_images) > k:
|
| 111 |
+
merged.extend(safe_images[k:])
|
| 112 |
+
|
| 113 |
+
return merged
|
| 114 |
+
|
| 115 |
+
|
| 116 |
css = """
|
| 117 |
+
.gradio-container { font-family: 'IBM Plex Sans', sans-serif; }
|
| 118 |
+
.gr-button { color: white; border-color: #3a669bff; background: #3a669bff; }
|
| 119 |
+
input[type='range'] { accent-color: #3a669bff; }
|
| 120 |
+
.dark input[type='range'] { accent-color: #3a669bff; }
|
| 121 |
+
.container { max-width: 730px; margin: auto; padding-top: 1.5rem; }
|
| 122 |
+
#gallery {
|
| 123 |
+
min-height: 22rem;
|
| 124 |
+
margin-bottom: 15px;
|
| 125 |
+
margin-left: auto;
|
| 126 |
+
margin-right: auto;
|
| 127 |
+
border-bottom-right-radius: .5rem !important;
|
| 128 |
+
border-bottom-left-radius: .5rem !important;
|
| 129 |
+
}
|
| 130 |
+
#gallery>div>.h-full { min-height: 20rem; }
|
| 131 |
+
.details:hover { text-decoration: underline; }
|
| 132 |
+
.gr-button { white-space: nowrap; }
|
| 133 |
+
#advanced-btn {
|
| 134 |
+
font-size: .7rem !important;
|
| 135 |
+
line-height: 19px;
|
| 136 |
+
margin-top: 12px;
|
| 137 |
+
margin-bottom: 12px;
|
| 138 |
+
padding: 2px 8px;
|
| 139 |
+
border-radius: 14px !important;
|
| 140 |
+
}
|
| 141 |
+
#advanced-options { display: none; margin-bottom: 20px; }
|
| 142 |
+
.footer {
|
| 143 |
+
margin-bottom: 45px;
|
| 144 |
+
margin-top: 35px;
|
| 145 |
+
text-align: center;
|
| 146 |
+
border-bottom: 1px solid #e5e5e5;
|
| 147 |
+
}
|
| 148 |
+
.footer>p {
|
| 149 |
+
font-size: .8rem;
|
| 150 |
+
display: inline-block;
|
| 151 |
+
padding: 0 10px;
|
| 152 |
+
transform: translateY(10px);
|
| 153 |
+
background: white;
|
| 154 |
+
}
|
| 155 |
+
.dark .footer { border-color: #303030; }
|
| 156 |
+
.dark .footer>p { background: #0b0f19; }
|
| 157 |
+
.acknowledgments h4 { margin: 1.25em 0 .25em 0; font-weight: bold; font-size: 115%; }
|
| 158 |
+
#container-advanced-btns { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; }
|
| 159 |
+
#share-btn-container {
|
| 160 |
+
display: flex;
|
| 161 |
+
padding-left: 0.5rem !important;
|
| 162 |
+
padding-right: 0.5rem !important;
|
| 163 |
+
background-color: #3a669bff;
|
| 164 |
+
justify-content: center;
|
| 165 |
+
align-items: center;
|
| 166 |
+
border-radius: 9999px !important;
|
| 167 |
+
width: 13rem;
|
| 168 |
+
}
|
| 169 |
+
#share-btn {
|
| 170 |
+
all: initial;
|
| 171 |
+
color: #ffffff;
|
| 172 |
+
font-weight: 600;
|
| 173 |
+
cursor: pointer;
|
| 174 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 175 |
+
margin-left: 0.5rem !important;
|
| 176 |
+
padding-top: 0.25rem !important;
|
| 177 |
+
padding-bottom: 0.25rem !important;
|
| 178 |
+
}
|
| 179 |
+
#share-btn * { all: unset; }
|
| 180 |
+
.gr-form { flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0; }
|
| 181 |
+
#prompt-container { gap: 0; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
"""
|
| 183 |
|
|
|
|
|
|
|
| 184 |
examples = [
|
| 185 |
+
["a photograph by vanessa beecroft", 1, 50, 7.5, 24803839],
|
| 186 |
+
["a gorgeous female photo", 1, 50, 7.5, 733664822],
|
| 187 |
+
["a gorgeous male photo", 1, 50, 7.5, 881355],
|
| 188 |
+
["the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c. leyendecker", 1, 50, 7.5, 557645701],
|
| 189 |
+
["portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and children from bahnhof zoo, detailed", 1, 50, 9, 1115417309],
|
| 190 |
+
["portrait of Sickly diseased dying Samurai warrior, sun shining, photo realistic illustration by greg rutkowski, thomas kindkade, alphonse mucha, loish, norman rockwell.", 1, 50, 10, 1714108957],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
]
|
| 192 |
|
| 193 |
+
block = gr.Blocks(css=css)
|
| 194 |
+
|
| 195 |
with block:
|
| 196 |
gr.HTML(
|
| 197 |
"""
|
| 198 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
| 199 |
+
<div style="display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;">
|
| 200 |
+
<img class="logo"
|
| 201 |
+
src="https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/1666181274838-62fa1d95e8c9c532aa75331c.png"
|
| 202 |
+
alt="AIML Logo"
|
| 203 |
+
style="margin: auto; max-width: 7rem;">
|
| 204 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
| 205 |
+
Stable Diffusion vs. Safe Stable Diffusion
|
| 206 |
+
</h1>
|
| 207 |
+
</div>
|
| 208 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 209 |
+
Safe Stable Diffusion extends Stable Diffusion with safety guidance. In the case of NSFW images it returns the closest non-NSFW images instead of a black square.
|
| 210 |
+
Details can be found in the <a href="https://arxiv.org/abs/2211.05105" style="text-decoration: underline;" target="_blank">paper</a>.
|
| 211 |
+
</p>
|
| 212 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
"""
|
| 214 |
)
|
| 215 |
+
|
| 216 |
with gr.Group():
|
| 217 |
with gr.Box():
|
| 218 |
+
with gr.Row(elem_id="prompt-container"):
|
| 219 |
text = gr.Textbox(
|
| 220 |
label="Enter your prompt",
|
| 221 |
show_label=False,
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
gallery = gr.Gallery(
|
| 237 |
+
label="Left: Stable Diffusion, Right: Safe Stable Diffusion",
|
| 238 |
+
show_label=True,
|
| 239 |
+
elem_id="gallery",
|
| 240 |
).style(grid=[2], height="auto")
|
| 241 |
|
| 242 |
with gr.Group(elem_id="container-advanced-btns"):
|
|
|
|
| 247 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 248 |
|
| 249 |
with gr.Row(elem_id="advanced-options"):
|
|
|
|
| 250 |
samples = gr.Slider(label="Images", minimum=1, maximum=1, value=1, step=1)
|
| 251 |
steps = gr.Slider(label="Steps", minimum=50, maximum=50, value=50, step=1)
|
| 252 |
+
scale = gr.Slider(label="Guidance Scale", minimum=7.5, maximum=20, value=7.5, step=0.5)
|
| 253 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True)
|
| 254 |
+
|
| 255 |
+
# IMPORTANT: infer() returns ONLY images, so Examples must output ONLY gallery.
|
| 256 |
+
ex = gr.Examples(
|
| 257 |
+
examples=examples,
|
| 258 |
+
fn=infer,
|
| 259 |
+
inputs=[text, samples, steps, scale, seed],
|
| 260 |
+
outputs=gallery,
|
| 261 |
+
cache_examples=False,
|
| 262 |
+
)
|
|
|
|
|
|
|
| 263 |
ex.dataset.headers = [""]
|
| 264 |
|
| 265 |
text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)
|
|
|
|
| 275 |
options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
| 276 |
}""",
|
| 277 |
)
|
| 278 |
+
|
| 279 |
+
share_button.click(None, [], [], _js=share_js)
|
| 280 |
+
|
|
|
|
|
|
|
|
|
|
| 281 |
gr.HTML(
|
| 282 |
"""
|
| 283 |
+
<div class="footer">
|
| 284 |
+
<p>
|
| 285 |
+
Model by <a href="https://huggingface.co/AIML-TUDA/" style="text-decoration: underline;" target="_blank">AIML Lab @TU Darmstadt</a>
|
| 286 |
+
- backend provided through the generous support of <a href="https://www.together.xyz/" style="text-decoration: underline;" target="_blank">Together</a>
|
| 287 |
+
- Gradio Demo by 🤗 Hugging Face
|
| 288 |
+
</p>
|
| 289 |
+
</div>
|
| 290 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 291 |
)
|
| 292 |
|
| 293 |
+
# Old-gradio safe pattern:
|
| 294 |
+
block.queue()
|
| 295 |
+
block.launch()
|