CyranoB Fabrice-TIERCELIN commited on
Commit
3152b9c
1 Parent(s): e4e61fc

Add Negative Prompt parameter (#1)

Browse files

- Add Negative Prompt parameter (55651a7478bac3cb8b35d2b2f0aba8ce45b2d5b2)


Co-authored-by: Fabrice TIERCELIN <[email protected]>

Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -55,20 +55,20 @@ def calculate_resolution(aspect_ratio, mode='landscape', total_pixels=1024*1024,
55
 
56
  # Example prompts with ckpt, aspect, and mode
57
  examples = [
58
- {"prompt": "A futuristic cityscape at sunset", "ckpt": "4-Step", "aspect": "16:9", "mode": "landscape"},
59
- {"prompt": "pair of shoes made of dried fruit skins, 3d render, bright colours, clean composition, beautiful artwork, logo", "ckpt": "2-Step", "aspect": "1:1", "mode": "portrait"},
60
- {"prompt": "A portrait of a robot in the style of Renaissance art", "ckpt": "2-Step", "aspect": "1:1", "mode": "portrait"},
61
- {"prompt": "full body of alien shaped like woman, big golden eyes, mars planet, photo, digital art, fantasy", "ckpt": "4-Step", "aspect": "1:1", "mode": "portrait"},
62
- {"prompt": "A serene landscape with mountains and a river", "ckpt": "8-Step", "aspect": "3:2", "mode": "landscape"},
63
- {"prompt": "post-apocalyptic wasteland, the most delicate beautiful flower with green leaves growing from dust and rubble, vibrant colours, cinematic", "ckpt": "8-Step", "aspect": "16:9", "mode": "landscape"}
64
  ]
65
  # Define a function to set the example inputs
66
  def set_example(selected_prompt):
67
  # Find the example that matches the selected prompt
68
  for example in examples:
69
  if example["prompt"] == selected_prompt:
70
- return example["prompt"], example["ckpt"], example["aspect"], example["mode"]
71
- return None, None, None, None # Default values if not found
72
 
73
  # Check if CUDA is available (GPU support), and set the appropriate device
74
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -114,7 +114,7 @@ def check_nsfw_images(
114
 
115
  # Function
116
  @spaces.GPU(enable_queue=True)
117
- def generate_image(prompt, ckpt, aspect_ratio, mode):
118
  width, height = calculate_resolution(aspect_ratio, mode) # Calculate resolution based on the aspect ratio
119
  checkpoint = checkpoints[ckpt][0]
120
  num_inference_steps = checkpoints[ckpt][1]
@@ -127,7 +127,7 @@ def generate_image(prompt, ckpt, aspect_ratio, mode):
127
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
128
 
129
  pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device=device))
130
- results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0, width=width, height=height )
131
 
132
  if SAFETY_CHECKER:
133
  images, has_nsfw_concepts = check_nsfw_images(results.images)
@@ -152,6 +152,8 @@ with gr.Blocks(css="style.css") as demo:
152
  with gr.Group():
153
  with gr.Row():
154
  prompt = gr.Textbox(label='Enter you image prompt:', scale=8)
 
 
155
  with gr.Row():
156
  ckpt = gr.Dropdown(label='Select inference steps',choices=['1-Step', '2-Step', '4-Step', '8-Step'], value='4-Step', interactive=True)
157
  aspect = gr.Dropdown(label='Aspect Ratio', choices=list(aspect_ratios.keys()), value='1:1', interactive=True)
@@ -161,15 +163,15 @@ with gr.Blocks(css="style.css") as demo:
161
  img = gr.Image(label='SDXL-Lightning Generated Image')
162
 
163
  prompt.submit(fn=generate_image,
164
- inputs=[prompt, ckpt, aspect, mode],
165
  outputs=img,
166
  )
167
  submit.click(fn=generate_image,
168
- inputs=[prompt, ckpt, aspect, mode],
169
  outputs=img,
170
  )
171
  # Dropdown for selecting examples
172
  example_dropdown = gr.Dropdown(label='Select an Example', choices=[e["prompt"] for e in examples])
173
- example_dropdown.change(fn=set_example, inputs=example_dropdown, outputs=[prompt, ckpt, aspect, mode])
174
 
175
  demo.queue().launch()
 
55
 
56
  # Example prompts with ckpt, aspect, and mode
57
  examples = [
58
+ {"prompt": "A futuristic cityscape at sunset", "negative_prompt": "Ugly", "ckpt": "4-Step", "aspect": "16:9", "mode": "landscape"},
59
+ {"prompt": "pair of shoes made of dried fruit skins, 3d render, bright colours, clean composition, beautiful artwork, logo", "negative_prompt": "Ugly", "ckpt": "2-Step", "aspect": "1:1", "mode": "portrait"},
60
+ {"prompt": "A portrait of a robot in the style of Renaissance art", "negative_prompt": "Ugly", "ckpt": "2-Step", "aspect": "1:1", "mode": "portrait"},
61
+ {"prompt": "full body of alien shaped like woman, big golden eyes, mars planet, photo, digital art, fantasy", "negative_prompt": "Ugly", "ckpt": "4-Step", "aspect": "1:1", "mode": "portrait"},
62
+ {"prompt": "A serene landscape with mountains and a river", "negative_prompt": "Ugly", "ckpt": "8-Step", "aspect": "3:2", "mode": "landscape"},
63
+ {"prompt": "post-apocalyptic wasteland, the most delicate beautiful flower with green leaves growing from dust and rubble, vibrant colours, cinematic", "negative_prompt": "Ugly", "ckpt": "8-Step", "aspect": "16:9", "mode": "landscape"}
64
  ]
65
  # Define a function to set the example inputs
66
  def set_example(selected_prompt):
67
  # Find the example that matches the selected prompt
68
  for example in examples:
69
  if example["prompt"] == selected_prompt:
70
+ return example["prompt"], example["negative_prompt"], example["ckpt"], example["aspect"], example["mode"]
71
+ return None, None, None, None, None # Default values if not found
72
 
73
  # Check if CUDA is available (GPU support), and set the appropriate device
74
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
114
 
115
  # Function
116
  @spaces.GPU(enable_queue=True)
117
+ def generate_image(prompt, negative_prompt, ckpt, aspect_ratio, mode):
118
  width, height = calculate_resolution(aspect_ratio, mode) # Calculate resolution based on the aspect ratio
119
  checkpoint = checkpoints[ckpt][0]
120
  num_inference_steps = checkpoints[ckpt][1]
 
127
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
128
 
129
  pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device=device))
130
+ results = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, guidance_scale=0, width=width, height=height )
131
 
132
  if SAFETY_CHECKER:
133
  images, has_nsfw_concepts = check_nsfw_images(results.images)
 
152
  with gr.Group():
153
  with gr.Row():
154
  prompt = gr.Textbox(label='Enter you image prompt:', scale=8)
155
+ with gr.Row():
156
+ negative_prompt = gr.Textbox(label='Optional negative prompt:', scale=8)
157
  with gr.Row():
158
  ckpt = gr.Dropdown(label='Select inference steps',choices=['1-Step', '2-Step', '4-Step', '8-Step'], value='4-Step', interactive=True)
159
  aspect = gr.Dropdown(label='Aspect Ratio', choices=list(aspect_ratios.keys()), value='1:1', interactive=True)
 
163
  img = gr.Image(label='SDXL-Lightning Generated Image')
164
 
165
  prompt.submit(fn=generate_image,
166
+ inputs=[prompt, negative_prompt, ckpt, aspect, mode],
167
  outputs=img,
168
  )
169
  submit.click(fn=generate_image,
170
+ inputs=[prompt, negative_prompt, ckpt, aspect, mode],
171
  outputs=img,
172
  )
173
  # Dropdown for selecting examples
174
  example_dropdown = gr.Dropdown(label='Select an Example', choices=[e["prompt"] for e in examples])
175
+ example_dropdown.change(fn=set_example, inputs=example_dropdown, outputs=[prompt, negative_prompt, ckpt, aspect, mode])
176
 
177
  demo.queue().launch()