Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,11 @@ import jax.numpy as jnp
|
|
4 |
from diffusers import FlaxStableDiffusionPipeline
|
5 |
from flax.jax_utils import replicate
|
6 |
from flax.training.common_utils import shard
|
|
|
7 |
|
8 |
pipeline, pipeline_params = FlaxStableDiffusionPipeline.from_pretrained(
|
9 |
"bguisard/stable-diffusion-nano",
|
10 |
-
dtype=jnp.float16
|
11 |
-
resume_download=True,
|
12 |
-
use_memory_efficient_attention=True
|
13 |
)
|
14 |
|
15 |
|
@@ -35,27 +34,279 @@ def generate_image(prompt: str, inference_steps: int = 30, prng_seed: int = 0):
|
|
35 |
images = images.reshape((num_samples,) + images.shape[-3:])
|
36 |
images = pipeline.numpy_to_pil(images)
|
37 |
return images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
|
|
4 |
from diffusers import FlaxStableDiffusionPipeline
|
5 |
from flax.jax_utils import replicate
|
6 |
from flax.training.common_utils import shard
|
7 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
8 |
|
9 |
pipeline, pipeline_params = FlaxStableDiffusionPipeline.from_pretrained(
|
10 |
"bguisard/stable-diffusion-nano",
|
11 |
+
dtype=jnp.float16
|
|
|
|
|
12 |
)
|
13 |
|
14 |
|
|
|
34 |
images = images.reshape((num_samples,) + images.shape[-3:])
|
35 |
images = pipeline.numpy_to_pil(images)
|
36 |
return images[0]
|
37 |
+
|
38 |
+
css = """
|
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 |
+
padding-top: 1.5rem;
|
57 |
+
}
|
58 |
+
#gallery {
|
59 |
+
min-height: 22rem;
|
60 |
+
margin-bottom: 15px;
|
61 |
+
margin-left: auto;
|
62 |
+
margin-right: auto;
|
63 |
+
border-bottom-right-radius: .5rem !important;
|
64 |
+
border-bottom-left-radius: .5rem !important;
|
65 |
+
}
|
66 |
+
#gallery>div>.h-full {
|
67 |
+
min-height: 20rem;
|
68 |
+
}
|
69 |
+
.details:hover {
|
70 |
+
text-decoration: underline;
|
71 |
+
}
|
72 |
+
.gr-button {
|
73 |
+
white-space: nowrap;
|
74 |
+
}
|
75 |
+
.gr-button:focus {
|
76 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
77 |
+
outline: none;
|
78 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
79 |
+
--tw-border-opacity: 1;
|
80 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
81 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
82 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
83 |
+
--tw-ring-opacity: .5;
|
84 |
+
}
|
85 |
+
#advanced-btn {
|
86 |
+
font-size: .7rem !important;
|
87 |
+
line-height: 19px;
|
88 |
+
margin-top: 12px;
|
89 |
+
margin-bottom: 12px;
|
90 |
+
padding: 2px 8px;
|
91 |
+
border-radius: 14px !important;
|
92 |
+
}
|
93 |
+
#advanced-options {
|
94 |
+
display: none;
|
95 |
+
margin-bottom: 20px;
|
96 |
+
}
|
97 |
+
.footer {
|
98 |
+
margin-bottom: 45px;
|
99 |
+
margin-top: 35px;
|
100 |
+
text-align: center;
|
101 |
+
border-bottom: 1px solid #e5e5e5;
|
102 |
+
}
|
103 |
+
.footer>p {
|
104 |
+
font-size: .8rem;
|
105 |
+
display: inline-block;
|
106 |
+
padding: 0 10px;
|
107 |
+
transform: translateY(10px);
|
108 |
+
background: white;
|
109 |
+
}
|
110 |
+
.dark .footer {
|
111 |
+
border-color: #303030;
|
112 |
+
}
|
113 |
+
.dark .footer>p {
|
114 |
+
background: #0b0f19;
|
115 |
+
}
|
116 |
+
.acknowledgments h4{
|
117 |
+
margin: 1.25em 0 .25em 0;
|
118 |
+
font-weight: bold;
|
119 |
+
font-size: 115%;
|
120 |
+
}
|
121 |
+
.animate-spin {
|
122 |
+
animation: spin 1s linear infinite;
|
123 |
+
}
|
124 |
+
@keyframes spin {
|
125 |
+
from {
|
126 |
+
transform: rotate(0deg);
|
127 |
+
}
|
128 |
+
to {
|
129 |
+
transform: rotate(360deg);
|
130 |
+
}
|
131 |
+
}
|
132 |
+
#share-btn-container {
|
133 |
+
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;
|
134 |
+
margin-top: 10px;
|
135 |
+
margin-left: auto;
|
136 |
+
}
|
137 |
+
#share-btn {
|
138 |
+
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;
|
139 |
+
}
|
140 |
+
#share-btn * {
|
141 |
+
all: unset;
|
142 |
+
}
|
143 |
+
#share-btn-container div:nth-child(-n+2){
|
144 |
+
width: auto !important;
|
145 |
+
min-height: 0px !important;
|
146 |
+
}
|
147 |
+
#share-btn-container .wrap {
|
148 |
+
display: none !important;
|
149 |
+
}
|
150 |
+
|
151 |
+
.gr-form{
|
152 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
153 |
+
}
|
154 |
+
#prompt-container{
|
155 |
+
gap: 0;
|
156 |
+
}
|
157 |
+
#prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
|
158 |
+
#component-16{border-top-width: 1px!important;margin-top: 1em}
|
159 |
+
.image_duplication{position: absolute; width: 100px; left: 50px}
|
160 |
+
"""
|
161 |
|
162 |
+
block = gr.Blocks(css=css)
|
163 |
|
164 |
+
with block:
|
165 |
+
gr.HTML(
|
166 |
+
"""
|
167 |
+
<div style="text-align: center; margin: 0 auto;">
|
168 |
+
<div
|
169 |
+
style="
|
170 |
+
display: inline-flex;
|
171 |
+
align-items: center;
|
172 |
+
gap: 0.8rem;
|
173 |
+
font-size: 1.75rem;
|
174 |
+
"
|
175 |
+
>
|
176 |
+
<svg
|
177 |
+
width="0.65em"
|
178 |
+
height="0.65em"
|
179 |
+
viewBox="0 0 115 115"
|
180 |
+
fill="none"
|
181 |
+
xmlns="http://www.w3.org/2000/svg"
|
182 |
+
>
|
183 |
+
<rect width="23" height="23" fill="white"></rect>
|
184 |
+
<rect y="69" width="23" height="23" fill="white"></rect>
|
185 |
+
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
186 |
+
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
187 |
+
<rect x="46" width="23" height="23" fill="white"></rect>
|
188 |
+
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
189 |
+
<rect x="69" width="23" height="23" fill="black"></rect>
|
190 |
+
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
191 |
+
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
192 |
+
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
193 |
+
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
194 |
+
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
195 |
+
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
196 |
+
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
197 |
+
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
198 |
+
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
199 |
+
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
200 |
+
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
201 |
+
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
202 |
+
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
203 |
+
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
204 |
+
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
205 |
+
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
206 |
+
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
207 |
+
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
208 |
+
</svg>
|
209 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
210 |
+
Stable Diffusion Nano Demo
|
211 |
+
</h1>
|
212 |
+
</div>
|
213 |
+
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
214 |
+
Stable Diffusion Nano was built during the <a style="text-decoration: underline;" href="https://github.com/huggingface/community-events/tree/main/jax-controlnet-sprint">JAX/Diffusers community sprint 🧨</a> based on Stable Diffusion 2.1 and finetuned on 128x128 images for fast prototyping. <br>
|
215 |
+
</p>
|
216 |
+
</div>
|
217 |
+
"""
|
218 |
+
)
|
219 |
+
with gr.Group():
|
220 |
+
with gr.Box():
|
221 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
222 |
+
with gr.Column():
|
223 |
+
prompt_input = gr.Textbox(
|
224 |
+
label="Enter your prompt",
|
225 |
+
show_label=False,
|
226 |
+
max_lines=1,
|
227 |
+
placeholder="A watercolor painting of a bird",
|
228 |
+
elem_id="prompt-text-input",
|
229 |
+
).style(
|
230 |
+
border=(True, False, True, True),
|
231 |
+
rounded=(True, False, False, True),
|
232 |
+
container=False,
|
233 |
+
)
|
234 |
+
negative = gr.Textbox(
|
235 |
+
label="Enter your negative prompt",
|
236 |
+
show_label=False,
|
237 |
+
max_lines=1,
|
238 |
+
placeholder="Enter a negative prompt",
|
239 |
+
elem_id="negative-prompt-text-input",
|
240 |
+
).style(
|
241 |
+
border=(True, False, True, True),
|
242 |
+
rounded=(True, False, False, True),
|
243 |
+
container=False,
|
244 |
+
)
|
245 |
+
btn = gr.Button("Generate image").style(
|
246 |
+
margin=False,
|
247 |
+
rounded=(False, True, True, False),
|
248 |
+
full_width=False,
|
249 |
+
)
|
250 |
+
|
251 |
+
gallery = gr.Gallery(
|
252 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
253 |
+
).style(grid=[2], height="auto")
|
254 |
+
|
255 |
+
with gr.Group(elem_id="container-advanced-btns"):
|
256 |
+
#advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
257 |
+
with gr.Group(elem_id="share-btn-container"):
|
258 |
+
community_icon = gr.HTML(community_icon_html)
|
259 |
+
loading_icon = gr.HTML(loading_icon_html)
|
260 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
261 |
+
|
262 |
+
with gr.Accordion("Advanced settings", open=False):
|
263 |
+
# gr.Markdown("Advanced settings are temporarily unavailable")
|
264 |
+
# samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
|
265 |
+
# steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
|
266 |
+
inf_steps_input = gr.inputs.Slider(
|
267 |
+
minimum=1, maximum=100, default=25, step=1, label="Inference Steps"
|
268 |
+
)
|
269 |
+
seed_input = gr.inputs.Number(default=0, label="Seed")
|
270 |
+
|
271 |
+
ex = gr.Examples(examples=[["A watercolor painting of a bird", 25, 0],["A watercolor painting of an otter",25,0],["Marvel MCU deadpool, red mask, red shirt, red gloves, black shoulders, black elbow pads, black legs, gold buckle, black belt, black mask, white eyes, black boots, fuji low light color 35mm film, downtown Osaka alley at night out of focus in background, neon lights",25,0]], fn=generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input ],outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=True)
|
272 |
+
ex.dataset.headers = [""]
|
273 |
+
negative.submit(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input], outputs=[gallery], postprocess=False)
|
274 |
+
prompt_input.submit(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input], outputs=[gallery], postprocess=False)
|
275 |
+
btn.click(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input], outputs=[gallery], postprocess=False)
|
276 |
+
|
277 |
+
#advanced_button.click(
|
278 |
+
# None,
|
279 |
+
# [],
|
280 |
+
# prompt_input,
|
281 |
+
# _js="""
|
282 |
+
# () => {
|
283 |
+
# const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
|
284 |
+
# options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
285 |
+
# }""",
|
286 |
+
#)
|
287 |
+
share_button.click(
|
288 |
+
None,
|
289 |
+
[],
|
290 |
+
[],
|
291 |
+
_js=share_js,
|
292 |
+
)
|
293 |
+
gr.HTML(
|
294 |
+
"""
|
295 |
+
<div class="footer">
|
296 |
+
<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
|
297 |
+
</p>
|
298 |
+
</div>
|
299 |
+
"""
|
300 |
+
)
|
301 |
+
with gr.Accordion(label="License", open=False):
|
302 |
+
gr.HTML(
|
303 |
+
"""<div class="acknowledgments">
|
304 |
+
<p><h4>LICENSE</h4>
|
305 |
+
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>
|
306 |
+
<p><h4>Biases and content acknowledgment</h4>
|
307 |
+
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>
|
308 |
+
</div>
|
309 |
+
"""
|
310 |
+
)
|
311 |
|
312 |
+
block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)
|