Kabatubare commited on
Commit
deea868
·
verified ·
1 Parent(s): 9979ce7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -9,7 +9,7 @@ from PIL import Image
9
 
10
  SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", "0") == "1"
11
 
12
- # Constants for Starfleet Command theme
13
  base = "stabilityai/stable-diffusion-xl-base-1.0"
14
  repo = "ByteDance/SDXL-Lightning"
15
  checkpoints = {
@@ -36,21 +36,17 @@ if SAFETY_CHECKER:
36
  )
37
 
38
  def check_nsfw_images(images: list[Image.Image]) -> tuple[list[Image.Image], list[bool]]:
39
- safety_checker_input = feature_extractor(images, return_tensors="pt").to("cuda")
40
  has_nsfw_concepts = safety_checker(
41
  images=images,
42
  clip_input=safety_checker_input.pixel_values.to("cuda"),
43
  )
44
-
45
- return images, has_nsfw_concepts
46
 
47
  @spaces.GPU(enable_queue=True)
48
  def generate_image(prompt, ckpt):
49
  global loaded
50
- print("🌌 Starfleet Command: ", prompt, ckpt)
51
-
52
- checkpoint = checkpoints[ckpt][0]
53
- num_inference_steps = checkpoints[ckpt][1]
54
 
55
  if loaded != num_inference_steps:
56
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps == 1 else "epsilon")
@@ -62,15 +58,12 @@ def generate_image(prompt, ckpt):
62
  if SAFETY_CHECKER:
63
  images, has_nsfw_concepts = check_nsfw_images(results.images)
64
  if any(has_nsfw_concepts):
65
- gr.Alert("🚨 NSFW content detected. Displaying a safe placeholder instead.").show()
66
- return Image.new("RGB", (512, 512), "black")
67
- return images[0]
68
  return results.images[0]
69
 
70
  description = """
71
- 🌌 Welcome to Starfleet Command's Text-to-Image Warp Drive - SDXL-Lightning ⚡.
72
- Utilizing ByteDance's SDXL-Lightning model, this interface allows you to generate high-quality cosmic images from textual prompts.
73
- Embark on your voyage of imagination and see the universe through the lens of advanced AI.
74
  """
75
 
76
  with gr.Blocks(css="style.css") as demo:
@@ -78,13 +71,12 @@ with gr.Blocks(css="style.css") as demo:
78
  gr.Markdown(description)
79
  with gr.Group():
80
  with gr.Row():
81
- prompt = gr.Textbox(label='Your cosmic prompt (English)', scale=8)
82
- ckpt = gr.Dropdown(label='Warp Factor (Inference Speed)', choices=['Warp 1', 'Warp 2', 'Warp 4', 'Warp 8'], value='Warp 4', interactive=True)
83
- submit = gr.Button(text="Engage", scale=1, variant='primary')
84
- img = gr.Image(label='The Universe, As You Envision It')
85
 
86
  prompt.submit(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
87
  submit.click(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
88
 
89
  demo.queue().launch()
90
-
 
9
 
10
  SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", "0") == "1"
11
 
12
+ # Constants
13
  base = "stabilityai/stable-diffusion-xl-base-1.0"
14
  repo = "ByteDance/SDXL-Lightning"
15
  checkpoints = {
 
36
  )
37
 
38
  def check_nsfw_images(images: list[Image.Image]) -> tuple[list[Image.Image], list[bool]]:
39
+ safety_checker_input = feature_extractor(images=[image.convert("RGB") for image in images], return_tensors="pt").to("cuda")
40
  has_nsfw_concepts = safety_checker(
41
  images=images,
42
  clip_input=safety_checker_input.pixel_values.to("cuda"),
43
  )
44
+ return images, has_nsfw_concepts.bool().tolist()
 
45
 
46
  @spaces.GPU(enable_queue=True)
47
  def generate_image(prompt, ckpt):
48
  global loaded
49
+ checkpoint, num_inference_steps = checkpoints[ckpt]
 
 
 
50
 
51
  if loaded != num_inference_steps:
52
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps == 1 else "epsilon")
 
58
  if SAFETY_CHECKER:
59
  images, has_nsfw_concepts = check_nsfw_images(results.images)
60
  if any(has_nsfw_concepts):
61
+ return [Image.new("RGB", (512, 512), "black")]
 
 
62
  return results.images[0]
63
 
64
  description = """
65
+ 🌌 Welcome to the Starfleet Command's Advanced Image Generation Console. Utilizing the cutting-edge SDXL-Lightning model, this terminal allows Starfleet officers and Federation citizens alike to materialize visual representations from textual descriptions at various warp speeds.
66
+ 🖖 Boldly go where no one has gone before - create images as vast as the universe with your imagination.
 
67
  """
68
 
69
  with gr.Blocks(css="style.css") as demo:
 
71
  gr.Markdown(description)
72
  with gr.Group():
73
  with gr.Row():
74
+ prompt = gr.Textbox(label='Your cosmic prompt (English)', placeholder="Describe the universe you wish to visualize...", scale=8)
75
+ ckpt = gr.Dropdown(label='Warp Factor (Inference Speed)', choices=list(checkpoints.keys()), value='Warp 4', interactive=True)
76
+ submit = gr.Button("Engage", scale=1, variant='primary')
77
+ img = gr.Image(label='Your Personalized Universe')
78
 
79
  prompt.submit(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
80
  submit.click(fn=generate_image, inputs=[prompt, ckpt], outputs=img)
81
 
82
  demo.queue().launch()