chokiproai commited on
Commit
983d072
1 Parent(s): d54ae69

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +300 -0
app.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")
32
+ images.append(image_b64)
33
+
34
+ return images
35
+
36
+
37
+ css = """
38
+ .gradio-container {
39
+ font-family: 'IBM Plex Sans', sans-serif;
40
+ }
41
+ .gr-button {
42
+ color: white;
43
+ border-color: black;
44
+ background: black;
45
+ }
46
+ input[type='range'] {
47
+ accent-color: black;
48
+ }
49
+ .dark input[type='range'] {
50
+ accent-color: #dfdfdf;
51
+ }
52
+ .container {
53
+ max-width: 730px;
54
+ margin: auto;
55
+ padding-top: 1.5rem;
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
+ #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
+ }
129
+ @keyframes spin {
130
+ from {
131
+ transform: rotate(0deg);
132
+ }
133
+ to {
134
+ transform: rotate(360deg);
135
+ }
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
+
198
+
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;
206
+ align-items: center;
207
+ gap: 0.8rem;
208
+ font-size: 1.75rem;
209
+ "
210
+ >
211
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
212
+ Stable Diffusion v1.5
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.
217
+ </p>
218
+ </div>
219
+ """
220
+ )
221
+ with gr.Group():
222
+ with gr.Box():
223
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
224
+ text = gr.Textbox(
225
+ label="Enter your prompt",
226
+ show_label=False,
227
+ max_lines=1,
228
+ placeholder="Enter your prompt",
229
+ elem_id="prompt-text-input",
230
+ ).style(
231
+ border=(True, False, True, True),
232
+ rounded=(True, False, False, True),
233
+ container=False,
234
+ )
235
+ btn = gr.Button("Generate image").style(
236
+ margin=False,
237
+ rounded=(False, True, True, False),
238
+ full_width=False,
239
+ )
240
+
241
+ gallery = gr.Gallery(
242
+ label="Generated images", show_label=False, elem_id="gallery"
243
+ ).style(grid=[2], height="auto")
244
+
245
+ with gr.Group(elem_id="container-advanced-btns"):
246
+ advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
247
+ with gr.Group(elem_id="share-btn-container"):
248
+ community_icon = gr.HTML(community_icon_html)
249
+ loading_icon = gr.HTML(loading_icon_html)
250
+ share_button = gr.Button("Share to community", elem_id="share-btn")
251
+
252
+ with gr.Row(elem_id="advanced-options"):
253
+ gr.Markdown("Advanced settings are temporarily unavailable")
254
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
255
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
256
+ scale = gr.Slider(
257
+ label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
258
+ )
259
+ seed = gr.Slider(
260
+ label="Seed",
261
+ minimum=0,
262
+ maximum=2147483647,
263
+ step=1,
264
+ randomize=True,
265
+ )
266
+
267
+ ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery], cache_examples=True, postprocess=False)
268
+ ex.dataset.headers = [""]
269
+
270
+ text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
271
+ btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
272
+
273
+ advanced_button.click(
274
+ None,
275
+ [],
276
+ text,
277
+ _js="""
278
+ () => {
279
+ const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
280
+ options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
281
+ }""",
282
+ )
283
+ share_button.click(
284
+ None,
285
+ [],
286
+ [],
287
+ _js=share_js,
288
+ )
289
+ gr.HTML(
290
+ """
291
+ <div class="footer">
292
+ <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 NgocTuanAI" 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>
293
+ </p>
294
+ </div>
295
+ <div class="acknowledgments">
296
+ </div>
297
+ """
298
+ )
299
+
300
+ block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)