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