HatmanStack commited on
Commit
552a8f8
·
1 Parent(s): cd11bb9

ImageEditor edge space

Browse files
Files changed (2) hide show
  1. app.py +32 -22
  2. functions.py +3 -4
app.py CHANGED
@@ -18,6 +18,13 @@ def update_mask_editor(img):
18
  return None
19
  return create_padded_image(img)
20
 
 
 
 
 
 
 
 
21
  def create_advanced_options():
22
 
23
  negative_text = gr.Textbox(label="Negative Prompt", placeholder="Describe what not to include (1-1024 characters)", max_lines=1)
@@ -44,12 +51,13 @@ with gr.Blocks() as demo:
44
  display: flex !important;
45
  justify-content: center !important;
46
  width: 100% !important;
47
- }
48
-
49
  </style>
50
  """)
51
- gr.Markdown("<h1>AWS Nova Canvas Image Generation</h1>", elem_classes="center-markdown" )
52
-
 
 
53
  with gr.Tab("Text to Image"):
54
  with gr.Column():
55
  gr.Markdown("""
@@ -64,13 +72,15 @@ with gr.Blocks() as demo:
64
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
65
  gr.Button("Generate Image").click(text_to_image, inputs=[prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
66
 
67
-
68
  with gr.Tab("Inpainting"):
69
  with gr.Column():
70
  gr.Markdown("""
71
  Modify specific areas of your image using inpainting. Upload your base image, then specify areas to edit using either
72
  the in-app editing tool to draw masks or the Mask Prompt field to let the model infer the mask. Note that only one masking
73
- method can be used at a time. You can provide an optional prompt to guide how the model fills in masked areas.""", elem_classes="center-markdown")
 
 
74
  mask_image = gr.ImageEditor(
75
  type="pil",
76
  height="100%",
@@ -82,8 +92,11 @@ with gr.Blocks() as demo:
82
  sources = ["upload"],
83
  transforms = None,
84
  layers = False,
85
- label="Draw mask (black areas will be edited)",
 
86
  )
 
 
87
  with gr.Accordion("Optional Mask Prompt", open=False):
88
  mask_prompt = gr.Textbox(label="Mask Prompt", placeholder="Describe regions to edit", max_lines=1)
89
  with gr.Accordion("Advanced Options", open=False):
@@ -95,14 +108,14 @@ with gr.Blocks() as demo:
95
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
96
  gr.Button("Generate Image").click(inpainting, inputs=[mask_image,mask_prompt, prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
97
 
98
-
99
  with gr.Tab("Outpainting"):
100
  with gr.Column():
101
  gr.Markdown("""
102
  Modify areas outside of your image using outpainting. Add transparent padding for a border, and use the crop feature to
103
  position your base image. The model can infer the mask from your Mask Prompt. Choose between precise mask boundaries or
104
  smooth transitions between masked and unmasked areas, and optionally provide a prompt to guide how masked areas are filled.
105
- """, elem_classes="center-markdown")
106
  mask_image = gr.ImageEditor(
107
  type="pil",
108
  height="100%",
@@ -113,8 +126,10 @@ with gr.Blocks() as demo:
113
  show_share_button=False,
114
  sources = ["upload"],
115
  layers = False,
 
116
  label="Crop the Image (transparent areas will be edited)"
117
  )
 
118
  gr.Button("Create Padding").click(fn=update_mask_editor, inputs=[mask_image], outputs=[mask_image])
119
 
120
  with gr.Accordion("Optional Mask Prompt", open=False):
@@ -127,18 +142,15 @@ with gr.Blocks() as demo:
127
  output = gr.Image()
128
  with gr.Row():
129
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
130
- gr.Button("Generate Image").click(outpainting, inputs=[mask_image, mask_prompt, prompt, negative_text, outpainting_mode, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
131
-
132
-
133
-
134
-
135
 
136
  with gr.Tab("Image Variation"):
137
  with gr.Column():
138
  gr.Markdown("""
139
  Create a variation image based on up to 5 other images and a Similarity slider available in options. You can add a prompt to direct the
140
  model (optional). Images should be .png or .jpg.
141
- """, elem_classes="center-markdown")
142
  images = gr.File(type='filepath', label="Input Images", file_count="multiple", file_types=["image"])
143
  with gr.Accordion("Optional Prompt", open=False):
144
  prompt = gr.Textbox(label="Prompt", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
@@ -156,7 +168,7 @@ with gr.Blocks() as demo:
156
  Generate an image conditioned by an input image. You need to add a text prompt to direct the model (required).
157
  You have two modes to control the conditioning,"CANNY" and "SEGMENTATION". CANNY will follow the edges of the conditioning image closely.
158
  SEGMENTATION will follow the layout or shapes of the conditioning image.
159
- """, elem_classes="center-markdown")
160
  condition_image = gr.Image(type='pil', label="Condition Image")
161
  with gr.Accordion("Advanced Options", open=False):
162
  control_mode = gr.Radio(choices=["CANNY_EDGE", "SEGMENTATION"], value="CANNY_EDGE", label="Control Mode")
@@ -176,7 +188,7 @@ with gr.Blocks() as demo:
176
  gr.Markdown("""
177
  Generate an image using a color palette. This mode requires a text prompt and a color list. If you choose to include an image, the subject and style will be used as a reference.
178
  The colors of the image will also be incorporated, along with the colors from the colors list. A generic color list has been provided behind the scenes if one isn't added.
179
- """, elem_classes="center-markdown")
180
  with gr.Row():
181
  with gr.Column(scale=70):
182
  colors = gr.Textbox(label="Colors", placeholder="Enter up to 10 colors as hex values, e.g., #00FF00,#FCF2AB", max_lines=1)
@@ -196,17 +208,17 @@ with gr.Blocks() as demo:
196
  gr.Button("Generate Image").click(color_guided_content, inputs=[prompt, reference_image, negative_text, colors, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
197
 
198
 
199
-
200
  with gr.Tab("Background Removal"):
201
  with gr.Column():
202
  gr.Markdown("""
203
  Remove the background from an image.
204
- """, elem_classes="center-markdown")
205
  image = gr.Image(type='pil', label="Input Image")
206
  error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
207
  output = gr.Image()
208
  gr.Button("Generate Image").click(background_removal, inputs=image, outputs=[output, error_box])
209
-
210
  with gr.Accordion("Tips", open=False):
211
  gr.Markdown("On Inference Speed: Resolution (width/height), and quality all have an impact on Inference Speed.")
212
  gr.Markdown("On Negation: For example, consider the prompt \"a rainy city street at night with no people\". The model might interpret \"people\" as a directive of what to include instead of omit. To generate better results, you could use the prompt \"a rainy city street at night\" with a negative prompt \"people\".")
@@ -235,8 +247,6 @@ with gr.Blocks() as demo:
235
  with gr.Column():
236
  gr.Markdown("""A rugged adventurer's ensemble, crafted for the wild, featuring a khaki jacket adorned with numerous functional pockets, a sun-bleached pith hat with a wide brim, sturdy canvas trousers with reinforced knees, and a pair of weathered leather boots with high-traction soles. Accented with a brass compass pendant and a leather utility belt laden with small tools, the outfit is completed by a pair of aviator sunglasses and a weathered map tucked into a side pocket.""")
237
 
238
-
239
-
240
  if __name__ == "__main__":
241
  demo.launch()
242
 
 
18
  return None
19
  return create_padded_image(img)
20
 
21
+ def update_canvas_size(image):
22
+ img = image['background']
23
+ width, height = img.size
24
+ width_container = max(width, 800)
25
+ height_container = max(height, 800)
26
+ return gr.update(canvas_size=(width, height), height=height_container + 20, width=width_container)
27
+
28
  def create_advanced_options():
29
 
30
  negative_text = gr.Textbox(label="Negative Prompt", placeholder="Describe what not to include (1-1024 characters)", max_lines=1)
 
51
  display: flex !important;
52
  justify-content: center !important;
53
  width: 100% !important;
54
+ }
 
55
  </style>
56
  """)
57
+ gr.Markdown("""
58
+ <h1>AWS Nova Canvas Image Generation</h1><p>Known <a href="https://github.com/gradio-app/gradio/issues/7586">Issue</a>
59
+ with Gradio ImageEditor in Chrome</p>Try Edge if having trouble with High Resource Usage</p>""", elem_classes="center-markdown" )
60
+
61
  with gr.Tab("Text to Image"):
62
  with gr.Column():
63
  gr.Markdown("""
 
72
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
73
  gr.Button("Generate Image").click(text_to_image, inputs=[prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
74
 
75
+
76
  with gr.Tab("Inpainting"):
77
  with gr.Column():
78
  gr.Markdown("""
79
  Modify specific areas of your image using inpainting. Upload your base image, then specify areas to edit using either
80
  the in-app editing tool to draw masks or the Mask Prompt field to let the model infer the mask. Note that only one masking
81
+ method can be used at a time. You can provide an optional prompt to guide how the model fills in masked areas.
82
+ """, elem_classes="center-markdown")
83
+
84
  mask_image = gr.ImageEditor(
85
  type="pil",
86
  height="100%",
 
92
  sources = ["upload"],
93
  transforms = None,
94
  layers = False,
95
+ canvas_size = [450,450],
96
+ label="Draw mask (black areas will be edited)",
97
  )
98
+ mask_image.change(fn=update_canvas_size, inputs=mask_image, outputs=mask_image)
99
+
100
  with gr.Accordion("Optional Mask Prompt", open=False):
101
  mask_prompt = gr.Textbox(label="Mask Prompt", placeholder="Describe regions to edit", max_lines=1)
102
  with gr.Accordion("Advanced Options", open=False):
 
108
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
109
  gr.Button("Generate Image").click(inpainting, inputs=[mask_image,mask_prompt, prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
110
 
111
+
112
  with gr.Tab("Outpainting"):
113
  with gr.Column():
114
  gr.Markdown("""
115
  Modify areas outside of your image using outpainting. Add transparent padding for a border, and use the crop feature to
116
  position your base image. The model can infer the mask from your Mask Prompt. Choose between precise mask boundaries or
117
  smooth transitions between masked and unmasked areas, and optionally provide a prompt to guide how masked areas are filled.
118
+ """, elem_classes="center-markdown")
119
  mask_image = gr.ImageEditor(
120
  type="pil",
121
  height="100%",
 
126
  show_share_button=False,
127
  sources = ["upload"],
128
  layers = False,
129
+ canvas_size = [450,450],
130
  label="Crop the Image (transparent areas will be edited)"
131
  )
132
+ mask_image.change(fn=update_canvas_size, inputs=mask_image, outputs=mask_image)
133
  gr.Button("Create Padding").click(fn=update_mask_editor, inputs=[mask_image], outputs=[mask_image])
134
 
135
  with gr.Accordion("Optional Mask Prompt", open=False):
 
142
  output = gr.Image()
143
  with gr.Row():
144
  gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
145
+ gr.Button("Generate Image").click(outpainting, inputs=[mask_image, mask_prompt, prompt, negative_text, outpainting_mode, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
146
+
 
 
 
147
 
148
  with gr.Tab("Image Variation"):
149
  with gr.Column():
150
  gr.Markdown("""
151
  Create a variation image based on up to 5 other images and a Similarity slider available in options. You can add a prompt to direct the
152
  model (optional). Images should be .png or .jpg.
153
+ """, elem_classes="center-markdown")
154
  images = gr.File(type='filepath', label="Input Images", file_count="multiple", file_types=["image"])
155
  with gr.Accordion("Optional Prompt", open=False):
156
  prompt = gr.Textbox(label="Prompt", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
 
168
  Generate an image conditioned by an input image. You need to add a text prompt to direct the model (required).
169
  You have two modes to control the conditioning,"CANNY" and "SEGMENTATION". CANNY will follow the edges of the conditioning image closely.
170
  SEGMENTATION will follow the layout or shapes of the conditioning image.
171
+ """, elem_classes="center-markdown")
172
  condition_image = gr.Image(type='pil', label="Condition Image")
173
  with gr.Accordion("Advanced Options", open=False):
174
  control_mode = gr.Radio(choices=["CANNY_EDGE", "SEGMENTATION"], value="CANNY_EDGE", label="Control Mode")
 
188
  gr.Markdown("""
189
  Generate an image using a color palette. This mode requires a text prompt and a color list. If you choose to include an image, the subject and style will be used as a reference.
190
  The colors of the image will also be incorporated, along with the colors from the colors list. A generic color list has been provided behind the scenes if one isn't added.
191
+ """, elem_classes="center-markdown")
192
  with gr.Row():
193
  with gr.Column(scale=70):
194
  colors = gr.Textbox(label="Colors", placeholder="Enter up to 10 colors as hex values, e.g., #00FF00,#FCF2AB", max_lines=1)
 
208
  gr.Button("Generate Image").click(color_guided_content, inputs=[prompt, reference_image, negative_text, colors, height, width, quality, cfg_scale, seed], outputs=[output, error_box])
209
 
210
 
211
+
212
  with gr.Tab("Background Removal"):
213
  with gr.Column():
214
  gr.Markdown("""
215
  Remove the background from an image.
216
+ """, elem_classes="center-markdown")
217
  image = gr.Image(type='pil', label="Input Image")
218
  error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
219
  output = gr.Image()
220
  gr.Button("Generate Image").click(background_removal, inputs=image, outputs=[output, error_box])
221
+
222
  with gr.Accordion("Tips", open=False):
223
  gr.Markdown("On Inference Speed: Resolution (width/height), and quality all have an impact on Inference Speed.")
224
  gr.Markdown("On Negation: For example, consider the prompt \"a rainy city street at night with no people\". The model might interpret \"people\" as a directive of what to include instead of omit. To generate better results, you could use the prompt \"a rainy city street at night\" with a negative prompt \"people\".")
 
247
  with gr.Column():
248
  gr.Markdown("""A rugged adventurer's ensemble, crafted for the wild, featuring a khaki jacket adorned with numerous functional pockets, a sun-bleached pith hat with a wide brim, sturdy canvas trousers with reinforced knees, and a pair of weathered leather boots with high-traction soles. Accented with a brass compass pendant and a leather utility belt laden with small tools, the outfit is completed by a pair of aviator sunglasses and a weathered map tucked into a side pocket.""")
249
 
 
 
250
  if __name__ == "__main__":
251
  demo.launch()
252
 
functions.py CHANGED
@@ -96,6 +96,7 @@ def text_to_image(prompt, negative_text=None, height=1024, width=1024, quality="
96
 
97
 
98
  def inpainting(mask_image, mask_prompt=None, text=None, negative_text=None, height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
 
99
  image = process_and_encode_image(mask_image['background'])
100
  if len(image) < 200:
101
  return None, gr.update(visible=True, value=image)
@@ -121,7 +122,7 @@ def inpainting(mask_image, mask_prompt=None, text=None, negative_text=None, heig
121
  result = generate_image(body)
122
 
123
  return check_return(result)
124
-
125
  def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, outpainting_mode="DEFAULT", height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
126
  image = process_and_encode_image(mask_image['background'])
127
  if len(image) < 200:
@@ -136,15 +137,13 @@ def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, out
136
  if mask_image and 'composite' in mask_image:
137
  mask = process_composite_to_mask(mask_image['background'], None)
138
  image = process_composite_to_mask(mask_image['background'], None, True)
139
- image.save("image.png")
140
  image = process_and_encode_image(image)
141
- mask.save("mask.png")
142
 
143
  mask_image = process_and_encode_image(mask)
144
 
145
  out_painting_params = {
146
  "image": image,
147
- "outPaintingMode": outpainting_mode, ## Malformed JSON Error
148
  **({"maskImage": mask_image} if mask_image not in [None, ""] else {}),
149
  **({"maskPrompt": mask_prompt} if mask_prompt not in [None, ""] else {}),
150
  **({"text": text} if text not in [None, ""] else {"text": " "}),
 
96
 
97
 
98
  def inpainting(mask_image, mask_prompt=None, text=None, negative_text=None, height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
99
+
100
  image = process_and_encode_image(mask_image['background'])
101
  if len(image) < 200:
102
  return None, gr.update(visible=True, value=image)
 
122
  result = generate_image(body)
123
 
124
  return check_return(result)
125
+
126
  def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, outpainting_mode="DEFAULT", height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
127
  image = process_and_encode_image(mask_image['background'])
128
  if len(image) < 200:
 
137
  if mask_image and 'composite' in mask_image:
138
  mask = process_composite_to_mask(mask_image['background'], None)
139
  image = process_composite_to_mask(mask_image['background'], None, True)
 
140
  image = process_and_encode_image(image)
 
141
 
142
  mask_image = process_and_encode_image(mask)
143
 
144
  out_painting_params = {
145
  "image": image,
146
+ "outPaintingMode": outpainting_mode,
147
  **({"maskImage": mask_image} if mask_image not in [None, ""] else {}),
148
  **({"maskPrompt": mask_prompt} if mask_prompt not in [None, ""] else {}),
149
  **({"text": text} if text not in [None, ""] else {"text": " "}),