kpasko commited on
Commit
3b52253
1 Parent(s): 429eb30

SLDFKJSDLFKJ

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