multimodalart HF staff commited on
Commit
51b6984
1 Parent(s): 980db22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -6,25 +6,21 @@ import re
6
  import os
7
  import requests
8
 
9
-
10
  from share_btn import community_icon_html, loading_icon_html, share_js
11
 
12
  word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
13
  word_list = word_list_dataset["train"]['text']
14
 
15
  is_gpu_busy = False
16
- def infer(prompt):
17
  global is_gpu_busy
18
- samples = 4
19
- steps = 50
20
- scale = 7.5
21
  for filter in word_list:
22
  if re.search(rf"\b{filter}\b", prompt):
23
  raise gr.Error("Unsafe content found. Please try again with different prompts.")
24
 
25
  images = []
26
  url = os.getenv('JAX_BACKEND_URL')
27
- payload = {'prompt': prompt}
28
  images_request = requests.post(url, json = payload)
29
  for image in images_request.json()["images"]:
30
  image_b64 = (f"data:image/jpeg;base64,{image}")
@@ -145,13 +141,15 @@ css = """
145
  #share-btn-container .wrap {
146
  display: none !important;
147
  }
 
148
  .gr-form{
149
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
150
  }
151
  #prompt-container{
152
  gap: 0;
153
  }
154
- #component-9{margin-top: -19px}
 
155
  .image_duplication{position: absolute; width: 100px; left: 50px}
156
  """
157
 
@@ -160,38 +158,28 @@ block = gr.Blocks(css=css)
160
  examples = [
161
  [
162
  'A high tech solarpunk utopia in the Amazon rainforest',
163
- # 4,
164
- # 45,
165
- # 7.5,
166
- # 1024,
167
  ],
168
  [
169
  'A pikachu fine dining with a view to the Eiffel Tower',
170
- # 4,
171
- # 45,
172
- # 7,
173
- # 1024,
174
  ],
175
  [
176
  'A mecha robot in a favela in expressionist style',
177
- # 4,
178
- # 45,
179
- # 7,
180
- # 1024,
181
  ],
182
  [
183
  'an insect robot preparing a delicious meal',
184
- # 4,
185
- # 45,
186
- # 7,
187
- # 1024,
188
  ],
189
  [
190
  "A small cabin on top of a snowy mountain in the style of Disney, artstation",
191
- # 4,
192
- # 45,
193
- # 7,
194
- # 1024,
195
  ],
196
  ]
197
 
@@ -242,11 +230,11 @@ with block:
242
  <rect x="23" y="69" width="23" height="23" fill="black"></rect>
243
  </svg>
244
  <h1 style="font-weight: 900; margin-bottom: 7px;">
245
- Stable Diffusion 2 Demo
246
  </h1>
247
  </div>
248
  <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
249
- Stable Diffusion 2 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
250
  access you can try
251
  <a
252
  href="http://beta.dreamstudio.ai/"
@@ -261,17 +249,29 @@ with block:
261
  with gr.Group():
262
  with gr.Box():
263
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
264
- text = gr.Textbox(
265
- label="Enter your prompt",
266
- show_label=False,
267
- max_lines=1,
268
- placeholder="Enter your prompt",
269
- elem_id="prompt-text-input",
270
- ).style(
271
- border=(True, False, True, True),
272
- rounded=(True, False, False, True),
273
- container=False,
274
- )
 
 
 
 
 
 
 
 
 
 
 
 
275
  btn = gr.Button("Generate image").style(
276
  margin=False,
277
  rounded=(False, True, True, False),
@@ -289,13 +289,13 @@ with block:
289
  loading_icon = gr.HTML(loading_icon_html)
290
  share_button = gr.Button("Share to community", elem_id="share-btn")
291
 
292
- #with gr.Row(elem_id="advanced-options"):
293
  # gr.Markdown("Advanced settings are temporarily unavailable")
294
  # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
295
  # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
296
- # scale = gr.Slider(
297
- # label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
298
- # )
299
  # seed = gr.Slider(
300
  # label="Seed",
301
  # minimum=0,
@@ -304,11 +304,11 @@ with block:
304
  # randomize=True,
305
  # )
306
 
307
- ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
308
  ex.dataset.headers = [""]
309
-
310
- text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
311
- btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
312
 
313
  #advanced_button.click(
314
  # None,
 
6
  import os
7
  import requests
8
 
 
9
  from share_btn import community_icon_html, loading_icon_html, share_js
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, negative, scale):
16
  global is_gpu_busy
 
 
 
17
  for filter in word_list:
18
  if re.search(rf"\b{filter}\b", prompt):
19
  raise gr.Error("Unsafe content found. Please try again with different prompts.")
20
 
21
  images = []
22
  url = os.getenv('JAX_BACKEND_URL')
23
+ payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
24
  images_request = requests.post(url, json = payload)
25
  for image in images_request.json()["images"]:
26
  image_b64 = (f"data:image/jpeg;base64,{image}")
 
141
  #share-btn-container .wrap {
142
  display: none !important;
143
  }
144
+
145
  .gr-form{
146
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
147
  }
148
  #prompt-container{
149
  gap: 0;
150
  }
151
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
152
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
153
  .image_duplication{position: absolute; width: 100px; left: 50px}
154
  """
155
 
 
158
  examples = [
159
  [
160
  'A high tech solarpunk utopia in the Amazon rainforest',
161
+ 'low quality',
162
+ 9
 
 
163
  ],
164
  [
165
  'A pikachu fine dining with a view to the Eiffel Tower',
166
+ 'low quality',
167
+ 9
 
 
168
  ],
169
  [
170
  'A mecha robot in a favela in expressionist style',
171
+ 'low quality, 3d, photorealistic',
172
+ 9
 
 
173
  ],
174
  [
175
  'an insect robot preparing a delicious meal',
176
+ 'low quality, illustration',
177
+ 9
 
 
178
  ],
179
  [
180
  "A small cabin on top of a snowy mountain in the style of Disney, artstation",
181
+ 'low quality, ugly',
182
+ 9
 
 
183
  ],
184
  ]
185
 
 
230
  <rect x="23" y="69" width="23" height="23" fill="black"></rect>
231
  </svg>
232
  <h1 style="font-weight: 900; margin-bottom: 7px;">
233
+ Stable Diffusion 2.1 Demo
234
  </h1>
235
  </div>
236
  <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
237
+ Stable Diffusion 2.1 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
238
  access you can try
239
  <a
240
  href="http://beta.dreamstudio.ai/"
 
249
  with gr.Group():
250
  with gr.Box():
251
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
252
+ with gr.Column():
253
+ text = gr.Textbox(
254
+ label="Enter your prompt",
255
+ show_label=False,
256
+ max_lines=1,
257
+ placeholder="Enter your prompt",
258
+ elem_id="prompt-text-input",
259
+ ).style(
260
+ border=(True, False, True, True),
261
+ rounded=(True, False, False, True),
262
+ container=False,
263
+ )
264
+ negative = gr.Textbox(
265
+ label="Enter your negative prompt",
266
+ show_label=False,
267
+ max_lines=1,
268
+ placeholder="Enter a negative prompt",
269
+ elem_id="negative-prompt-text-input",
270
+ ).style(
271
+ border=(True, False, True, True),
272
+ rounded=(True, False, False, True),
273
+ container=False,
274
+ )
275
  btn = gr.Button("Generate image").style(
276
  margin=False,
277
  rounded=(False, True, True, False),
 
289
  loading_icon = gr.HTML(loading_icon_html)
290
  share_button = gr.Button("Share to community", elem_id="share-btn")
291
 
292
+ with gr.Accordion("Advanced settings", open=False):
293
  # gr.Markdown("Advanced settings are temporarily unavailable")
294
  # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
295
  # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
296
+ guidance_scale = gr.Slider(
297
+ label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
298
+ )
299
  # seed = gr.Slider(
300
  # label="Seed",
301
  # minimum=0,
 
304
  # randomize=True,
305
  # )
306
 
307
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
308
  ex.dataset.headers = [""]
309
+ negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
310
+ text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
311
+ btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
312
 
313
  #advanced_button.click(
314
  # None,