File size: 14,787 Bytes
76f94b4
ab6cb7b
 
76f94b4
ab6cb7b
 
 
 
 
 
 
 
76f94b4
ab6cb7b
76f94b4
337cf72
 
 
 
 
ab6cb7b
1963e4b
ab6cb7b
 
 
 
 
 
 
76f94b4
 
 
1963e4b
 
8a17e3a
 
 
1963e4b
8a17e3a
1963e4b
 
4cacb08
 
 
 
 
 
337cf72
1963e4b
 
8956ae5
76f94b4
 
 
ab6cb7b
8956ae5
337cf72
ab6cb7b
 
4cacb08
76f94b4
1963e4b
ab6cb7b
911f98c
76f94b4
 
 
1963e4b
8956ae5
 
 
337cf72
 
 
 
 
 
 
 
1c930ab
 
 
337cf72
 
 
 
 
 
4cacb08
76f94b4
1963e4b
ab6cb7b
1963e4b
337cf72
76f94b4
 
 
1963e4b
8956ae5
 
 
337cf72
 
 
 
 
 
1c930ab
337cf72
 
1c930ab
 
 
337cf72
 
 
 
 
 
 
4cacb08
76f94b4
1963e4b
 
ab6cb7b
1963e4b
337cf72
76f94b4
 
 
1963e4b
8956ae5
 
337cf72
1963e4b
ab6cb7b
 
 
4cacb08
76f94b4
1963e4b
 
ab6cb7b
1963e4b
911f98c
76f94b4
 
 
1963e4b
ab6cb7b
 
 
337cf72
1963e4b
ab6cb7b
 
4cacb08
76f94b4
1963e4b
 
 
ab6cb7b
911f98c
76f94b4
8a17e3a
76f94b4
1963e4b
337cf72
 
 
 
f00a818
337cf72
f00a818
 
 
 
059f429
 
a967c34
 
4cacb08
76f94b4
1963e4b
ab6cb7b
911f98c
76f94b4
 
 
1963e4b
 
337cf72
76f94b4
4cacb08
76f94b4
911f98c
76f94b4
ab6cb7b
 
 
4b6b3ad
911f98c
8856d1e
911f98c
1d047e4
 
 
8856d1e
1d047e4
 
911f98c
1d047e4
 
 
8856d1e
5166e47
 
 
 
 
 
8856d1e
1d047e4
 
911f98c
 
 
76f94b4
ab6cb7b
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import gradio as gr
from functions import *
from dataclasses import dataclass

@dataclass
class Config:
    min_size: int = 256
    max_size: int = 2048
    step_size: int = 64
    default_size: int = 1024
    default_cfg_scale: float = 8.0
    default_seed: int = 8

config = Config()

def update_mask_editor(img):
    if img['background'] is None:
        return None
    return create_padded_image(img)

def create_advanced_options():
    
    negative_text = gr.Textbox(label="Negative Prompt", placeholder="Describe what not to include (1-1024 characters)", max_lines=1)
    width = gr.Slider(minimum=config.min_size, maximum=config.max_size, step=config.step_size, value=config.default_size, label="Width")
    height = gr.Slider(minimum=config.min_size, maximum=config.max_size, step=config.step_size, value=config.default_size, label="Height")
    quality = gr.Radio(choices=["standard", "premium"], value="standard", label="Quality")
    cfg_scale = gr.Slider(minimum=1.0, maximum=20.0, step=0.1, value=config.default_cfg_scale, label="CFG Scale")
    seed = gr.Slider(minimum=1, maximum=2000, step=1, value=config.default_seed, label="Seed")
    return negative_text, width, height, quality, cfg_scale, seed

# Gradio Interface
with gr.Blocks() as demo:
    gr.HTML("""
    <style>
        ::-webkit-scrollbar {
            display: none; 
        }
        #component-0 {
            max-width: 800px;
            margin: 0 auto; 
        }
        .center-markdown {
            text-align: center !important;
            display: flex !important;
            justify-content: center !important;
            width: 100% !important;
        }
        
    </style>
    """)
    gr.Markdown("<h1>AWS Nova Canvas Image Generation</h1>", elem_classes="center-markdown" )

    with gr.Tab("Text to Image"):
        with gr.Column():
            gr.Markdown("""
                Generate an image from a text prompt using the AWS Nova Canvas model.
            """, elem_classes="center-markdown")
            prompt = gr.Textbox(label="Prompt", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
            gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            gr.Button("Generate").click(text_to_image, inputs=[prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Inpainting"):
        with gr.Column():
            gr.Markdown("""
            Modify specific areas of your image using inpainting. Upload your base image, then specify areas to edit using either 
            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 
            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")
            mask_image = gr.ImageEditor(
                type="pil",
                height="100%",
                width="100%",
                crop_size="1:1",
                brush={"color": "#000000", "radius": 25},
                show_download_button=False,
                show_share_button=False,
                sources = ["upload"],
                transforms = None,
                layers = False,
                label="Draw mask (black areas will be edited)",
            )
            with gr.Accordion("Optional Mask Prompt", open=False):
                mask_prompt = gr.Textbox(label="Mask Prompt", placeholder="Describe regions to edit", max_lines=1)
            prompt = gr.Textbox(label="Optional Prompt", placeholder="Describe what to generate (1-1024 characters) in the masked area", max_lines=4)
            gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            
            gr.Button("Generate").click(inpainting, inputs=[mask_image,mask_prompt, prompt, negative_text, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Outpainting"):
        with gr.Column():
            gr.Markdown("""
                Modify areas outside of your image using outpainting. Add transparent padding for a border, and use the crop feature to 
                position your base image. The model can infer the mask from your Mask Prompt. Choose between precise mask boundaries or 
                smooth transitions between masked and unmasked areas, and optionally provide a prompt to guide how masked areas are filled.
            """, elem_classes="center-markdown")
            mask_image = gr.ImageEditor(
                type="pil",
                height="100%",
                width="100%",
                crop_size="1:1",
                brush=False,
                show_download_button=False,
                show_share_button=False,
                sources = ["upload"],
                layers = False,
                label="Crop the Image (transparent areas will be edited)"
            )
            gr.Button("Create Padding").click(fn=update_mask_editor, inputs=[mask_image], outputs=[mask_image])
            
            with gr.Accordion("Optional Mask Prompt", open=False):
                mask_prompt = gr.Textbox(label="Mask Prompt", placeholder="Describe regions to edit", max_lines=1)
            prompt = gr.Textbox(label="Prompt", placeholder="Describe what to generate (1-1024 characters)", max_lines=4)
            gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                outpainting_mode = gr.Radio(choices=["DEFAULT", "PRECISE"], value="DEFAULT", label="Outpainting Mode")
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            
            gr.Button("Generate").click(outpainting, inputs=[mask_image, mask_prompt, prompt, negative_text, outpainting_mode, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Image Variation"):
        with gr.Column():
            gr.Markdown("""
                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 
                model (optional).  Images should be .png or .jpg.  
            """, elem_classes="center-markdown")
            images = gr.File(type='filepath', label="Input Images", file_count="multiple", file_types=["image"])
            with gr.Accordion("Optional Prompt", open=False):
                prompt = gr.Textbox(label="Prompt", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
                gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                similarity_strength = gr.Slider(minimum=0.2, maximum=1.0, step=0.1, value=0.7, label="Similarity Strength")
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            
            gr.Button("Generate").click(image_variation, inputs=[images, prompt, negative_text, similarity_strength, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Image Conditioning"):
        with gr.Column():
            gr.Markdown("""
                Generate an image conditioned by an input image.  You need to add a text prompt to direct the model (required).
                You have two modes to control the conditioning,"CANNY" and "SEGMENTATION".  CANNY will follow the edges of the conditioning image closely.
                SEGMENTATION will follow the layout or shapes of the conditioning image. 
            """, elem_classes="center-markdown")
            condition_image = gr.Image(type='pil', label="Condition Image")
            prompt = gr.Textbox(label="Prompt", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
            gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                control_mode = gr.Radio(choices=["CANNY_EDGE", "SEGMENTATION"], value="CANNY_EDGE", label="Control Mode")
                control_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.7, label="Control Strength")
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            gr.Button("Generate").click(image_conditioning, inputs=[condition_image, prompt, negative_text, control_mode, control_strength, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Color Guided"):
        with gr.Column():
            gr.Markdown("""
                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. 
                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.
            """, elem_classes="center-markdown")
            with gr.Row():
                with gr.Column(scale=70): 
                    colors = gr.Textbox(label="Colors", placeholder="Enter up to 10 colors as hex values, e.g., #00FF00,#FCF2AB", max_lines=1)
                with gr.Column(scale=30):                    
                            color_picker = gr.ColorPicker(label="Color Picker", show_label=False, value='#473c80', interactive=True)
                            #add_color_button = gr.Button("Add Color")  
                            #add_color_button.click(fn=add_color_to_list, inputs=[colors, color_picker], outputs=colors)
            prompt = gr.Textbox(label="Text", placeholder="Enter a text prompt (1-1024 characters)", max_lines=4)
            gr.Button("Generate Prompt").click(generate_nova_prompt, outputs=prompt)
            with gr.Accordion("Optional Reference Image", open=False):
                reference_image = gr.Image(type='pil', label="Reference Image")   
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            with gr.Accordion("Advanced Options", open=False):
                negative_text, width, height, quality, cfg_scale, seed = create_advanced_options()
            gr.Button("Generate").click(color_guided_content, inputs=[prompt, reference_image, negative_text, colors, height, width, quality, cfg_scale, seed], outputs=[output, error_box])

    with gr.Tab("Background Removal"):
        with gr.Column():
            gr.Markdown("""
                Remove the background from an image.
            """, elem_classes="center-markdown")
            image = gr.Image(type='pil', label="Input Image")
            error_box = gr.Markdown(visible=False, label="Error", elem_classes="center-markdown")
            output = gr.Image()
            gr.Button("Generate").click(background_removal, inputs=image, outputs=[output, error_box])

    with gr.Accordion("Tips", open=False):
        gr.Markdown("On Inference Speed: Resolution (width/height), and quality all have an impact on Inference Speed.")
        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\".")
        gr.Markdown("On Prompt Length: When diffusion models were first introduced, they could process only 77 tokens. While new techniques have extended this limit, they remain bound by their training data. AWS Nova Canvas limits input by character length instead, ensuring no characters beyond the set limit are considered in the generated model.")

    gr.Markdown("""<h1>Sample Prompts and Results</h1>""", elem_classes="center-markdown")
    
    # Example 1
    with gr.Row():
        with gr.Column():
            gr.Image("examples/sample2.png", width=200, show_label=False, show_download_button=False, show_share_button=False, container=False)
        with gr.Column():
            gr.Markdown("""A whimsical outdoor scene where vibrant flowers and sprawling vines, crafted from an array of colorful fruit leathers and intricately designed candies, flutter with delicate, lifelike butterflies made from translucent, shimmering sweets. Each petal and leaf glistens with a soft, sugary sheen, casting playful reflections. The butterflies, with their candy wings adorned in fruity patterns, flit about, creating a magical, edible landscape that delights the senses.""")
    
    # Example 2
    with gr.Row():
        with gr.Column():
            gr.Image("examples/sample3.png", width=200, show_label=False, show_download_button=False, show_share_button=False, container=False)
        with gr.Column():
            gr.Markdown("""A Kansas Jayhawk with a basketball photorealistic""")

    # Example 3
    with gr.Row():
        with gr.Column():
            gr.Image("examples/sample4.png", width=200, show_label=False, show_download_button=False, show_share_button=False, container=False)
        with gr.Column():
            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.""")



if __name__ == "__main__":
    demo.launch()