Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,31 @@
|
|
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 |
word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
|
10 |
word_list = word_list_dataset["train"]['text']
|
11 |
|
12 |
is_gpu_busy = False
|
13 |
-
def infer(prompt
|
14 |
global is_gpu_busy
|
|
|
|
|
|
|
15 |
for filter in word_list:
|
16 |
if re.search(rf"\b{filter}\b", prompt):
|
17 |
raise gr.Error("Unsafe content found. Please try again with different prompts.")
|
18 |
|
19 |
images = []
|
20 |
url = os.getenv('JAX_BACKEND_URL')
|
21 |
-
payload = {'prompt': prompt
|
22 |
images_request = requests.post(url, json = payload)
|
23 |
for image in images_request.json()["images"]:
|
24 |
image_b64 = (f"data:image/jpeg;base64,{image}")
|
@@ -110,6 +117,12 @@ css = """
|
|
110 |
font-weight: bold;
|
111 |
font-size: 115%;
|
112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
.animate-spin {
|
114 |
animation: spin 1s linear infinite;
|
115 |
}
|
@@ -123,61 +136,62 @@ css = """
|
|
123 |
}
|
124 |
#share-btn-container {
|
125 |
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;
|
126 |
-
margin-top: 10px;
|
127 |
-
margin-left: auto;
|
128 |
}
|
129 |
#share-btn {
|
130 |
-
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;
|
131 |
}
|
132 |
#share-btn * {
|
133 |
all: unset;
|
134 |
}
|
135 |
-
#share-btn-container div:nth-child(-n+2){
|
136 |
-
width: auto !important;
|
137 |
-
min-height: 0px !important;
|
138 |
-
}
|
139 |
-
#share-btn-container .wrap {
|
140 |
-
display: none !important;
|
141 |
-
}
|
142 |
-
|
143 |
.gr-form{
|
144 |
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
145 |
}
|
146 |
#prompt-container{
|
147 |
gap: 0;
|
148 |
}
|
149 |
-
#
|
150 |
-
|
151 |
-
|
|
|
152 |
"""
|
153 |
|
154 |
block = gr.Blocks(css=css)
|
155 |
|
156 |
examples = [
|
157 |
[
|
158 |
-
'
|
159 |
-
|
160 |
-
|
|
|
|
|
161 |
],
|
162 |
[
|
163 |
-
'A
|
164 |
-
|
165 |
-
|
|
|
|
|
166 |
],
|
167 |
[
|
168 |
-
'A
|
169 |
-
|
170 |
-
|
|
|
|
|
171 |
],
|
172 |
[
|
173 |
-
'
|
174 |
-
|
175 |
-
|
|
|
|
|
176 |
],
|
177 |
[
|
178 |
-
"A
|
179 |
-
|
180 |
-
|
|
|
|
|
181 |
],
|
182 |
]
|
183 |
|
@@ -185,7 +199,7 @@ examples = [
|
|
185 |
with block:
|
186 |
gr.HTML(
|
187 |
"""
|
188 |
-
<div style="text-align: center; margin: 0 auto;">
|
189 |
<div
|
190 |
style="
|
191 |
display: inline-flex;
|
@@ -194,52 +208,14 @@ with block:
|
|
194 |
font-size: 1.75rem;
|
195 |
"
|
196 |
>
|
197 |
-
<
|
198 |
-
|
199 |
-
height="0.65em"
|
200 |
-
viewBox="0 0 115 115"
|
201 |
-
fill="none"
|
202 |
-
xmlns="http://www.w3.org/2000/svg"
|
203 |
-
>
|
204 |
-
<rect width="23" height="23" fill="white"></rect>
|
205 |
-
<rect y="69" width="23" height="23" fill="white"></rect>
|
206 |
-
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
207 |
-
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
208 |
-
<rect x="46" width="23" height="23" fill="white"></rect>
|
209 |
-
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
210 |
-
<rect x="69" width="23" height="23" fill="black"></rect>
|
211 |
-
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
212 |
-
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
213 |
-
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
214 |
-
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
215 |
-
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
216 |
-
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
217 |
-
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
218 |
-
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
219 |
-
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
220 |
-
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
221 |
-
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
222 |
-
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
223 |
-
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
224 |
-
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
225 |
-
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
226 |
-
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
227 |
-
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
228 |
-
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
229 |
-
</svg>
|
230 |
-
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
231 |
-
Stable Diffusion 2.1 Demo
|
232 |
</h1>
|
233 |
</div>
|
234 |
-
<p style="margin-bottom: 10px; font-size: 94
|
235 |
-
Stable Diffusion
|
236 |
-
|
237 |
-
|
238 |
-
href="http://beta.dreamstudio.ai/"
|
239 |
-
style="text-decoration: underline;"
|
240 |
-
target="_blank"
|
241 |
-
>DreamStudio Beta</a
|
242 |
-
>.</a>
|
243 |
</p>
|
244 |
</div>
|
245 |
"""
|
@@ -247,29 +223,17 @@ with block:
|
|
247 |
with gr.Group():
|
248 |
with gr.Box():
|
249 |
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
)
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
)
|
262 |
-
negative = gr.Textbox(
|
263 |
-
label="Enter your negative prompt",
|
264 |
-
show_label=False,
|
265 |
-
max_lines=1,
|
266 |
-
placeholder="Enter a negative prompt",
|
267 |
-
elem_id="negative-prompt-text-input",
|
268 |
-
).style(
|
269 |
-
border=(True, False, True, True),
|
270 |
-
rounded=(True, False, False, True),
|
271 |
-
container=False,
|
272 |
-
)
|
273 |
btn = gr.Button("Generate image").style(
|
274 |
margin=False,
|
275 |
rounded=(False, True, True, False),
|
@@ -281,33 +245,62 @@ with block:
|
|
281 |
).style(grid=[2], height="auto")
|
282 |
|
283 |
with gr.Group(elem_id="container-advanced-btns"):
|
284 |
-
|
285 |
with gr.Group(elem_id="share-btn-container"):
|
286 |
community_icon = gr.HTML(community_icon_html)
|
287 |
loading_icon = gr.HTML(loading_icon_html)
|
288 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
289 |
|
290 |
-
with gr.
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
label="Guidance Scale", minimum=0, maximum=50, value=
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
|
305 |
-
ex = gr.Examples(examples=examples, fn=infer, inputs=
|
306 |
ex.dataset.headers = [""]
|
307 |
-
|
308 |
-
text.submit(infer, inputs=
|
309 |
-
btn.click(infer, inputs=
|
310 |
-
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
|
313 |
-
block.queue(concurrency_count=
|
|
|
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}")
|
|
|
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 |
}
|
|
|
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 |
|
|
|
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;
|
|
|
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 |
"""
|
|
|
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),
|
|
|
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)
|