pengdaqian commited on
Commit
134c419
1 Parent(s): 2a589fd
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from datasets import load_dataset
3
- from PIL import Image
4
 
5
  import re
6
  import os
@@ -11,23 +11,27 @@ from share_btn import community_icon_html, loading_icon_html, share_js
11
  word_list_dataset = load_dataset("Gustavosta/Stable-Diffusion-Prompts")
12
  word_list = word_list_dataset["train"]['Prompt']
13
 
14
-
15
  from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
16
  import torch
17
 
18
  model_id = "stabilityai/stable-diffusion-2-1-base"
19
 
20
  scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
21
- pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
22
-
 
 
 
23
 
24
  is_gpu_busy = False
 
 
25
  def infer(prompt, negative, scale):
26
  global is_gpu_busy
27
  for filter in word_list:
28
  if re.search(rf"\b{filter}\b", prompt):
29
  raise gr.Error("Unsafe content found. Please try again with different prompts.")
30
-
31
  images = []
32
  generator = torch.manual_seed(68781279)
33
  payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
@@ -40,10 +44,10 @@ def infer(prompt, negative, scale):
40
  width=768).images[0]
41
 
42
  images.append(image)
43
-
44
  return images
45
-
46
-
47
  css = """
48
  .gradio-container {
49
  font-family: 'IBM Plex Sans', sans-serif;
@@ -198,7 +202,6 @@ examples = [
198
  ],
199
  ]
200
 
201
-
202
  with block:
203
  gr.HTML(
204
  """
@@ -298,19 +301,19 @@ with block:
298
  ).style(grid=[2], height="auto")
299
 
300
  with gr.Group(elem_id="container-advanced-btns"):
301
- #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
302
  with gr.Group(elem_id="share-btn-container"):
303
  community_icon = gr.HTML(community_icon_html)
304
  loading_icon = gr.HTML(loading_icon_html)
305
  share_button = gr.Button("Share to community", elem_id="share-btn")
306
 
307
  with gr.Accordion("Advanced settings", open=False):
308
- # gr.Markdown("Advanced settings are temporarily unavailable")
309
- # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
310
- # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
311
- guidance_scale = gr.Slider(
312
  label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
313
- )
314
  # seed = gr.Slider(
315
  # label="Seed",
316
  # minimum=0,
@@ -319,13 +322,14 @@ with block:
319
  # randomize=True,
320
  # )
321
 
322
- ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
 
323
  ex.dataset.headers = [""]
324
  negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
325
  text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
326
  btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
327
-
328
- #advanced_button.click(
329
  # None,
330
  # [],
331
  # text,
@@ -334,13 +338,12 @@ with block:
334
  # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
335
  # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
336
  # }""",
337
- #)
338
  share_button.click(
339
  None,
340
  [],
341
  [],
342
  _js=share_js,
343
  )
344
-
345
 
346
- block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)
 
1
  import gradio as gr
2
  from datasets import load_dataset
3
+ from PIL import Image
4
 
5
  import re
6
  import os
 
11
  word_list_dataset = load_dataset("Gustavosta/Stable-Diffusion-Prompts")
12
  word_list = word_list_dataset["train"]['Prompt']
13
 
 
14
  from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
15
  import torch
16
 
17
  model_id = "stabilityai/stable-diffusion-2-1-base"
18
 
19
  scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
20
+ pipe = StableDiffusionPipeline.from_pretrained(
21
+ model_id,
22
+ scheduler=scheduler,
23
+ safety_checker=None,
24
+ torch_dtype=torch.float16)
25
 
26
  is_gpu_busy = False
27
+
28
+
29
  def infer(prompt, negative, scale):
30
  global is_gpu_busy
31
  for filter in word_list:
32
  if re.search(rf"\b{filter}\b", prompt):
33
  raise gr.Error("Unsafe content found. Please try again with different prompts.")
34
+
35
  images = []
36
  generator = torch.manual_seed(68781279)
37
  payload = {'prompt': prompt, 'negative_prompt': negative, 'guidance_scale': scale}
 
44
  width=768).images[0]
45
 
46
  images.append(image)
47
+
48
  return images
49
+
50
+
51
  css = """
52
  .gradio-container {
53
  font-family: 'IBM Plex Sans', sans-serif;
 
202
  ],
203
  ]
204
 
 
205
  with block:
206
  gr.HTML(
207
  """
 
301
  ).style(grid=[2], height="auto")
302
 
303
  with gr.Group(elem_id="container-advanced-btns"):
304
+ # advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
305
  with gr.Group(elem_id="share-btn-container"):
306
  community_icon = gr.HTML(community_icon_html)
307
  loading_icon = gr.HTML(loading_icon_html)
308
  share_button = gr.Button("Share to community", elem_id="share-btn")
309
 
310
  with gr.Accordion("Advanced settings", open=False):
311
+ # gr.Markdown("Advanced settings are temporarily unavailable")
312
+ # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
313
+ # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
314
+ guidance_scale = gr.Slider(
315
  label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
316
+ )
317
  # seed = gr.Slider(
318
  # label="Seed",
319
  # minimum=0,
 
322
  # randomize=True,
323
  # )
324
 
325
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale],
326
+ outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
327
  ex.dataset.headers = [""]
328
  negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
329
  text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
330
  btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
331
+
332
+ # advanced_button.click(
333
  # None,
334
  # [],
335
  # text,
 
338
  # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
339
  # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
340
  # }""",
341
+ # )
342
  share_button.click(
343
  None,
344
  [],
345
  [],
346
  _js=share_js,
347
  )
 
348
 
349
+ block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)