File size: 32,873 Bytes
cbf9ae2
 
 
 
 
 
 
5add6fe
6ef117e
3d65503
cbf9ae2
 
5add6fe
cbf9ae2
5add6fe
cbf9ae2
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
3d65503
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
5add6fe
cbf9ae2
 
5add6fe
cbf9ae2
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
5add6fe
cbf9ae2
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
6ef117e
 
 
 
 
 
 
 
 
 
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
1cb68a6
cbf9ae2
 
1cb68a6
cbf9ae2
 
 
 
 
 
6ef117e
 
 
 
 
 
 
 
 
cbf9ae2
 
 
1cb68a6
 
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cb68a6
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
 
cbf9ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5add6fe
cbf9ae2
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import gradio as gr
from PIL import Image, ImageDraw, ImageChops, ImageColor
from haishoku.haishoku import Haishoku
import os
from tempfile import NamedTemporaryFile
from pathlib import Path
import atexit
import random
import spaces

# Import constants
import utils.constants as constants

IS_SHARED_SPACE = constants.IS_SHARED_SPACE

# Import functions from modules
from utils.file_utils import cleanup_temp_files

from utils.color_utils import (
    rgb_to_hex,
    hex_to_rgb,
    detect_color_format,
    update_color_opacity,
)
from utils.misc import (get_filename, pause, convert_ratio_to_dimensions)
from utils.depth_estimation import estimate_depth, create_3d_model, generate_depth_and_3d, generate_depth_button_click

from utils.image_utils import (
    change_color,
    open_image,
    build_prerendered_images,
    upscale_image,
    lerp_imagemath,
    shrink_and_paste_on_blank,
    show_lut,
    apply_lut_to_image_path,
    multiply_and_blend_images,
    alpha_composite_with_control
)

from utils.hex_grid import (
    generate_hexagon_grid,
    generate_hexagon_grid_interface,
)

from utils.excluded_colors import (
    add_color,
    delete_color,
    build_dataframe,
    on_input,
    excluded_color_list,
    on_color_display_select
)

from utils.ai_generator import (
    generate_ai_image,
)
from utils.version_info import (
    versions_html,
    get_torch_info
)
from utils.lora_details import (
    upd_prompt_notes
)

input_image_palette = []
current_prerendered_image = gr.State("./images/images/Beeuty-1.png")

# Register the cleanup function
atexit.register(cleanup_temp_files)

def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
    global input_image_palette

    try:
        # Load and process the input image
        input_image = Image.open(input_image_path).convert("RGBA")
    except Exception as e:
        print(f"Failed to convert image to RGBA: {e}")
        # Open the original image without conversion
        input_image = Image.open(input_image_path)
        # Ensure the canvas is at least 1344x768 pixels
        min_width, min_height = 1344, 768
        canvas_width = max(min_width, input_image.width)
        canvas_height = max(min_height, input_image.height)

        # Create a transparent canvas with the required dimensions
        new_canvas = Image.new("RGBA", (canvas_width, canvas_height), (0, 0, 0, 0))

        # Calculate position to center the input image on the canvas
        paste_x = (canvas_width - input_image.width) // 2
        paste_y = (canvas_height - input_image.height) // 2

        # Paste the input image onto the canvas
        new_canvas.paste(input_image, (paste_x, paste_y))

        # Save the 'RGBA' image to a temporary file and update 'input_image_path'
        with NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
            new_canvas.save(tmp_file.name, format="PNG")
            input_image_path = tmp_file.name
            constants.temp_files.append(tmp_file.name)

        # Update 'input_image' with the new image as a file path
        input_image = Image.open(input_image_path)
    
    # Use Haishoku to get the palette from the new image
    input_palette = Haishoku.loadHaishoku(input_image_path)
    input_image_palette = input_palette.palette

    # Update colors with opacity
    background_color = update_color_opacity(
        hex_to_rgb(background_color_hex),
        int(background_opacity * (255 / 100))
    )
    border_color = update_color_opacity(
        hex_to_rgb(border_color_hex),
        int(border_opacity * (255 / 100))
    )

    # Prepare excluded colors list
    excluded_color_list = [tuple(lst) for lst in excluded_colors_var]

    # Generate the hexagon grid images
    grid_image = generate_hexagon_grid_interface(
        hex_size,
        border_size,
        input_image,
        start_x,
        start_y,
        end_x,
        end_y,
        rotation,
        background_color,
        border_color,
        fill_hex,
        excluded_color_list,
        filter_color,
        x_spacing,
        y_spacing,
        add_hex_text_option,
        custom_text_list,
        custom_text_color_list
    )

    return grid_image

def get_model_and_lora(model_textbox):
    """
    Determines the model and LoRA weights based on the model_textbox input.
    wieghts must be in an array ["Borcherding/FLUX.1-dev-LoRA-FractalLand-v0.1"]
    """
    # If the input is in the list of models, return it with None as LoRA weights
    if model_textbox in constants.MODELS:
        return model_textbox, []
    # If the input is in the list of LoRA weights, get the corresponding model
    elif model_textbox in constants.LORA_WEIGHTS:
        model = constants.LORA_TO_MODEL.get(model_textbox)
        return model, model_textbox.split()
    else:
        # Default to a known model if input is unrecognized
        default_model = model_textbox
        return default_model, []

#@spaces.GPU(duration=256)
def generate_input_image_click(map_option, prompt_textbox_value, negative_prompt_textbox_value, model_textbox_value, use_conditioned_image=False, strength=0.5, image_format="16:9", scale_factor=3):
    # Get the model and LoRA weights
    model, lora_weights = get_model_and_lora(model_textbox_value)
    global current_prerendered_image
    conditioned_image=None

    if use_conditioned_image:
        print(f"Conditioned path: {current_prerendered_image.value}.. converting to RGB\n")
        # ensure the conditioned image is an image and not a string, cannot use RGBA
        if isinstance(current_prerendered_image.value, str):            
            conditioned_image = open_image(current_prerendered_image.value).convert("RGB")
            print(f"Conditioned Image: {conditioned_image.size}.. converted to RGB\n")

    # Convert image_format from a string split by ":" into two numbers divided
    width_ratio, height_ratio = map(int, image_format.split(":"))
    aspect_ratio = width_ratio / height_ratio
    
    width, height = convert_ratio_to_dimensions(aspect_ratio, 512)
    
    # Generate the AI image and get the image path
    image_path = generate_ai_image(
        map_option,
        prompt_textbox_value,
        negative_prompt_textbox_value,
        model,
        lora_weights,
        conditioned_image,
        stength=strength,
        height=height,
        width=width
    )
       
    # Open the generated image
    try:
        image = Image.open(image_path).convert("RGBA")
    except Exception as e:
        print(f"Failed to open generated image: {e}")
        return image_path  # Return the original image path if opening fails
       
    # Upscale the image
    upscaled_image = upscale_image(image, scale_factor)
       
    # Save the upscaled image to a temporary file
    with NamedTemporaryFile(delete=False, suffix=".png") as tmp_upscaled:
        upscaled_image.save(tmp_upscaled.name, format="PNG")
        constants.temp_files.append(tmp_upscaled.name)
        print(f"Upscaled image saved to {tmp_upscaled.name}")
       
    # Return the path of the upscaled image
    return tmp_upscaled.name

def update_prompt_visibility(map_option):
      is_visible = (map_option == "Prompt")
      return (
          gr.update(visible=is_visible),
          gr.update(visible=is_visible),
          gr.update(visible=is_visible)
      )

def update_prompt_notes(model_textbox_value):
    return upd_prompt_notes(model_textbox_value)

def on_prerendered_gallery_selection(event_data: gr.SelectData):
    global current_prerendered_image
    selected_index = event_data.index
    selected_image = constants.pre_rendered_maps_paths[selected_index]
    print(f"Gallery Image Selected: {selected_image}\n")
    current_prerendered_image.value = selected_image
    return current_prerendered_image

def combine_images_with_lerp(input_image, output_image, alpha):
    in_image = open_image(input_image)
    out_image = open_image(output_image)
    print(f"Combining images with alpha: {alpha}")
    return lerp_imagemath(in_image, out_image, alpha)

def add_border(image, mask_width, mask_height, blank_color):
    bordered_image_output = Image.open(image).convert("RGBA")
    margin_color = detect_color_format(blank_color)
    print(f"Adding border to image with width: {mask_width}, height: {mask_height}, color: {margin_color}")
    return shrink_and_paste_on_blank(bordered_image_output, mask_width, mask_height, margin_color)

title = "HexaGrid Creator"
description = "Customizable Hexagon Grid Image Generator"
examples = [["assets//examples//hex_map_p1.png", 32, 1, 0, 0, 0, 0, 0, "#ede9ac44","#12165380", True]]

gr.set_static_paths(paths=["images/","images/images","images/prerendered","LUT/","fonts/"])
# Gradio Blocks Interface
with gr.Blocks(css_paths="style_20250128.css", title="HexaGrid Creator", theme='Surn/beeuty') as beeuty:
    with gr.Row():
        gr.Markdown ("""
        # HexaGrid Creator
        ## Transform Your Images into Mesmerizing Hexagon Grid Masterpieces! ⬢
        <details>
        <summary>
        Welcome to HexaGrid Creator, the ultimate tool for transforming your images into stunning hexagon grid artworks. Whether you're a tabletop game enthusiast, a digital artist, or someone who loves unique patterns, HexaGrid Creator has something for you.

        ## Drop an image into the Input Image and get started!

        </summary>

        ## What is HexaGrid Creator?
        HexaGrid Creator is a web-based application that allows you to apply a hexagon grid overlay to any image. You can customize the size, color, and opacity of the hexagons, as well as the background and border colors. The result is a visually striking image that looks like it was made from hexagonal tiles!

        ### What Can You Do?
        - **Generate Hexagon Grids:** Create beautiful hexagon grid overlays on any image with fully customizable parameters.
        - **AI-Powered Image Generation:** Use advanced AI models to generate images based on your prompts and apply hexagon grids to them.
        - **Color Exclusion:** Select and exclude specific colors from your hexagon grid for a cleaner and more refined look.
        - **Interactive Customization:** Adjust hexagon size, border size, rotation, background color, and more in real-time.
        - **Depth and 3D Model Generation:** Generate depth maps and 3D models from your images for enhanced visualization.
        - **Image Filter [Look-Up Table (LUT)] Application:** Apply filters (LUTs) to your images for color grading and enhancement.
        - **Pre-rendered Maps:** Access a library of pre-rendered hexagon maps for quick and easy customization.
        - **Add Margins:** Add customizable margins around your images for a polished finish.

        ### Why You'll Love It
        - **Fun and Easy to Use:** With an intuitive interface and real-time previews, creating hexagon grids has never been this fun!
        - **Endless Creativity:** Unleash your creativity with endless customization options and see your images transform in unique ways.
        - **Hexagon-Inspired Theme:** Enjoy a delightful yellow and purple theme inspired by hexagons! ⬢
        - **Advanced AI Models:** Leverage advanced AI models and LoRA weights for high-quality image generation and customization.

        ### Get Started
        1. **Upload or Generate an Image:** Start by uploading your own image or generate one using our AI-powered tool.
        2. **Customize Your Grid:** Play around with the settings to create the perfect hexagon grid overlay.
        3. **Download and Share:** Once you're happy with your creation, download it and share it with the world!

        ### Advanced Features
        - **Generative AI Integration:** Utilize models like `black-forest-labs/FLUX.1-dev` and various LoRA weights for generating unique images.
        - **Pre-rendered Maps:** Access a library of pre-rendered hexagon maps for quick and easy customization.
        - **Image Filter [Look-Up Table (LUT)] Application:** Apply filters (LUTs) to your images for color grading and enhancement.
        - **Depth and 3D Model Generation:** Create depth maps and 3D models from your images for enhanced visualization.
        - **Add Margins:** Customize margins around your images for a polished finish.

        Join the hive and start creating with HexaGrid Creator today!
        </details>
        """, elem_classes="intro")
    with gr.Row():
        with gr.Column(scale=2):
            input_image = gr.Image(
                label="Input Image",
                type="filepath",
                interactive=True,
                elem_classes="centered solid imgcontainer",
                key="imgInput",
                image_mode="RGBA",
                format="PNG"
            )
        with gr.Column():
            with gr.Accordion("Hex Coloring and Exclusion", open = False):
                with gr.Row():
                    with gr.Column():
                        color_picker = gr.ColorPicker(label="Pick a color to exclude",value="#505050")
                    with gr.Column():
                            filter_color = gr.Checkbox(label="Filter Excluded Colors from Sampling", value=False,)
                    exclude_color_button = gr.Button("Exclude Color", elem_id="exlude_color_button", elem_classes="solid")
                    color_display = gr.DataFrame(label="List of Excluded RGBA Colors", headers=["R", "G", "B", "A"], elem_id="excluded_colors", type="array", value=build_dataframe(excluded_color_list), interactive=True, elem_classes="solid centered")
                    selected_row = gr.Number(0, label="Selected Row", visible=False)
                    delete_button = gr.Button("Delete Row", elem_id="delete_exclusion_button", elem_classes="solid")
                    fill_hex = gr.Checkbox(label="Fill Hex with color from Image", value=True)
            with gr.Accordion("Image Filters", open = False):
                with gr.Row():
                    with gr.Column():
                        composite_color = gr.ColorPicker(label="Color", value="#ede9ac44")
                    with gr.Column():
                        composite_opacity = gr.Slider(label="Opacity %", minimum=0, maximum=100, value=50, interactive=True)
                with gr.Row():
                    composite_button = gr.Button("Composite", elem_classes="solid")
                with gr.Row():
                    with gr.Column():
                        lut_filename = gr.Textbox(
                            value="", 
                            label="Look Up Table (LUT) File Name",
                            elem_id="lutFileName")
                    with gr.Column():
                        lut_file = gr.File(
                            value=None,
                            file_count="single",
                            file_types=[".cube"],
                            type="filepath",
                            label="LUT cube File")
                with gr.Row():
                    lut_example_image = gr.Image(type="pil", label="Filter (LUT) Example Image", value=constants.default_lut_example_img)
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("""
                        ### Included Filters (LUTs)
                        There are several included Filters:

                        Try them on the example image before applying to your Input Image.
                        """, elem_id="lut_markdown")
                    with gr.Column():
                        gr.Examples(elem_id="lut_examples",
                            examples=[[f] for f in constants.lut_files],
                            inputs=[lut_filename],
                            outputs=[lut_filename],
                            label="Select a Filter (LUT) file. Populate the LUT File Name field"
                        )
                    
                with gr.Row():
                    apply_lut_button = gr.Button("Apply Filter (LUT)", elem_classes="solid", elem_id="apply_lut_button")
                    
                lut_file.change(get_filename, inputs=[lut_file], outputs=[lut_filename])
                lut_filename.change(show_lut, inputs=[lut_filename, lut_example_image], outputs=[lut_example_image])
                apply_lut_button.click(apply_lut_to_image_path, inputs=[lut_filename, input_image], outputs=[input_image],scroll_to_output=True)

    with gr.Row():
        with gr.Accordion("Generative AI", open = False):
            with gr.Row():
                with gr.Column():
                    model_options = gr.Dropdown(
                        label="Model Options",
                        choices=constants.MODELS + constants.LORA_WEIGHTS + ["Manual Entry"],
                        value="Cossale/Frames2-Flex.1",
                        elem_classes="solid"
                    )
                    model_textbox = gr.Textbox(
                        label="LORA/Model",
                        value="Cossale/Frames2-Flex.1",
                        elem_classes="solid",
                        elem_id="inference_model",
                        visible=False
                    )
                    # Update map_options to a Dropdown with choices from constants.PROMPTS keys
                    with gr.Row():
                        with gr.Column():
                                map_options = gr.Dropdown(
                                label="Map Options",
                                choices=list(constants.PROMPTS.keys()),
                                value="Alien Landscape",
                                elem_classes="solid"
                            )
                        with gr.Column():
                            # Add Dropdown for sizing of Images, height and width based on selection. Options are 16x9, 16x10, 4x5, 1x1
                            # The values of height and width are based on common resolutions for each aspect ratio
                            # Default to 16x9, 912x512
                            image_size_ratio = gr.Dropdown(label="Image Size", choices=["16:9", "16:10", "4:5", "4:3", "2:1","3:2","1:1", "9:16", "10:16", "5:4", "3:4","1:2", "2:3"], value="16:9", elem_classes="solid", type="value",interactive=True)
                    prompt_textbox = gr.Textbox(
                        label="Prompt",
                        visible=False,
                        elem_classes="solid",
                        value="top-down, (tabletop_map built from small hexagon pieces) hexagon map of a Battletech_boardgame forest with lakes, forest, magic fauna, and snow at the top and bottom, (middle is dark, no_reflections, no_shadows) , tall and short hexagon tiles. Viewed from above.",
                        lines=4
                    )
                    negative_prompt_textbox = gr.Textbox(
                        label="Negative Prompt",
                        visible=False,
                        elem_classes="solid",
                        value="low quality, bad anatomy, blurry, cropped, worst quality, shadows, people, humans, reflections, shadows, realistic map of the Earth, isometric, text"
                    )
                    prompt_notes_label = gr.Label(
                        "You should use FRM$ as trigger words. @1.5 minutes",
                        elem_classes="solid centered small",
                        show_label=False,
                        visible=False
                    )
                    # Keep the change event to maintain functionality
                    map_options.change(
                        fn=update_prompt_visibility,
                        inputs=[map_options],
                        outputs=[prompt_textbox, negative_prompt_textbox, prompt_notes_label]
                    )
                    with gr.Row():
                        generate_input_image = gr.Button(
                            "Generate AI Image",
                            elem_id="generate_input_image",
                            elem_classes="solid"
                    )
                with gr.Column(scale=2):
                     with gr.Accordion("Template Image Styles", open = False):
                         with gr.Row():
                            # Gallery from PRE_RENDERED_IMAGES GOES HERE
                            prerendered_image_gallery = gr.Gallery(label="Image Gallery", show_label=True, value=build_prerendered_images(constants.pre_rendered_maps_paths), elem_id="gallery", elem_classes="solid", type="filepath", columns=[3], rows=[3], preview=False ,object_fit="contain", height="auto",file_types=["image"], format="png",allow_preview=False)
                         with gr.Row():
                             image_guidance_stength = gr.Slider(label="Image Guidance Strength", minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=True)
                             with gr.Column():
                                 replace_input_image_button = gr.Button(
                                     "Replace Input Image",
                                     elem_id="prerendered_replace_input_image_button",
                                     elem_classes="solid"
                                 )
                             with gr.Column():
                                 generate_input_image_from_gallery = gr.Button(
                                     "Generate AI Image from Gallery",
                                     elem_id="generate_input_image_from_gallery",
                                     elem_classes="solid"
                                 )

    with gr.Accordion("Advanced Hexagon Settings", open = False):
        with gr.Row():
            start_x = gr.Number(label="Start X", value=0, minimum=-512, maximum= 512, precision=0)
            start_y = gr.Number(label="Start Y", value=0, minimum=-512, maximum= 512, precision=0)
            end_x = gr.Number(label="End X", value=0, minimum=-512, maximum= 512, precision=0)
            end_y = gr.Number(label="End Y", value=0, minimum=-512, maximum= 512, precision=0)
        with gr.Row():
            x_spacing = gr.Number(label="Adjust Horizontal spacing", value=-1, minimum=-200, maximum=200, precision=1)
            y_spacing = gr.Number(label="Adjust Vertical spacing", value=1, minimum=-200, maximum=200, precision=1)
        with gr.Row():
            rotation = gr.Slider(-90, 180, 0.0, 0.1, label="Hexagon Rotation (degree)")
            add_hex_text = gr.Dropdown(label="Add Text to Hexagons", choices=[None, "Row-Column Coordinates", "Sequential Numbers", "Playing Cards Sequential", "Playing Cards Alternate Red and Black", "Custom List"], value=None)
    with gr.Row():
            custom_text_list = gr.TextArea(label="Custom Text List", value=constants.cards_alternating, visible=False,)
            custom_text_color_list = gr.TextArea(label="Custom Text Color List", value=constants.card_colors_alternating, visible=False)
    with gr.Row():
            hex_text_info = gr.Markdown("""
            ### Text Color uses the Border Color and Border Opacity, unless you use a custom list.
            ### The Custom Text List and Custom Text Color List are comma separated lists.
            ### The custom color list is a comma separated list of hex colors.
            #### Example: "A,2,3,4,5,6,7,8,9,10,J,Q,K", "red,#0000FF,#00FF00,red,#FFFF00,#00FFFF,#FF8000,#FF00FF,#FF0080,#FF8000,#FF0080,lightblue"
            """, elem_id="hex_text_info", visible=False)
            add_hex_text.change(
                fn=lambda x: (
                    gr.update(visible=(x == "Custom List")), 
                    gr.update(visible=(x == "Custom List")),
                    gr.update(visible=(x != None))
                ),
                inputs=add_hex_text,
                outputs=[custom_text_list, custom_text_color_list, hex_text_info]
            )
    with gr.Row():
        hex_size = gr.Number(label="Hexagon Size", value=32, minimum=1, maximum=768)
        border_size = gr.Slider(-5,25,value=0,step=1,label="Border Size")
    with gr.Row():
        rotation = gr.Slider(-90, 180, 0.0, 0.1, label="deg. Rotation")
        background_color = gr.ColorPicker(label="Background Color", value="#000000", interactive=True)
        background_opacity = gr.Slider(0,100,0,1,label="Background Opacity %")
        border_color = gr.ColorPicker(label="Border Color", value="#7b7b7b", interactive=True)
        border_opacity = gr.Slider(0,100,0,1,label="Border Opacity %")
    with gr.Row():
        hex_button = gr.Button("Generate Hex Grid!", elem_classes="solid", elem_id="btn-generate")
    with gr.Row():
        output_image = gr.Image(label="Hexagon Grid Image", image_mode = "RGBA", show_download_button=True, show_share_button=True,elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOutput")
        overlay_image = gr.Image(label="Hexagon Overlay Image", image_mode = "RGBA", show_share_button=True, elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOverlay")
    with gr.Row():
        output_overlay_composite = gr.Slider(0,100,50,0.5, label="Interpolate Intensity")
        output_blend_multiply_composite = gr.Slider(0,100,50,0.5, label="Overlay Intensity")
        output_alpha_composite = gr.Slider(0,100,50,0.5, label="Alpha Composite Intensity")
    with gr.Accordion("Add Margins (bleed)", open=False):
        with gr.Row():
            border_image_source = gr.Radio(label="Add Margins around which Image", choices=["Input Image", "Overlay Image"], value="Overlay Image")
        with gr.Row():
            mask_width = gr.Number(label="Margins Width", value=10, minimum=0, maximum=100, precision=0)
            mask_height = gr.Number(label="Margins Height", value=10, minimum=0, maximum=100, precision=0)
        with gr.Row():
                margin_color = gr.ColorPicker(label="Margin Color", value="#333333FF", interactive=True)
                margin_opacity = gr.Slider(0,100,95,0.5,label="Margin Opacity %")
        with gr.Row():
            add_border_button = gr.Button("Add Margins", elem_classes="solid", variant="secondary")
        with gr.Row():
            bordered_image_output = gr.Image(label="Image with Margins", image_mode="RGBA", show_download_button=True, show_share_button=True, elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgBordered")

    with gr.Accordion("Height Maps and 3D", open = False):
        with gr.Row():
            with gr.Column():
                voxel_size_factor = gr.Slider(label="Voxel Size Factor", value=1.00, minimum=0.01, maximum=40.00, step=0.01)
            with gr.Column():
                depth_image_source = gr.Radio(label="Depth Image Source", choices=["Input Image", "Output Image", "Overlay Image","Image with Margins"], value="Input Image")
        with gr.Row():
            generate_depth_button = gr.Button("Generate Depth Map and 3D Model From Selected Image", elem_classes="solid", variant="secondary")
    with gr.Row():
            depth_map_output = gr.Image(label="Depth Map", image_mode="L", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgDepth")
            model_output = gr.Model3D(label="3D Model", clear_color=[1.0, 1.0, 1.0, 0.25], key="Img3D", elem_classes="centered solid imgcontainer")
    with gr.Row():
        gr.Examples(examples=[
            ["assets//examples//hex_map_p1.png", False, True, -32,-31,80,80,-1.8,0,35,0,1,"#FFD0D0", 15],
            ["assets//examples//hex_map_p1_overlayed.png", False, False, -32,-31,80,80,-1.8,0,35,0,1,"#FFD0D0", 75],
            ["assets//examples//hex_flower_logo.png", False, True, -95,-95,100,100,-24,-2,190,30,2,"#FF8951", 50],
            ["assets//examples//hexed_fract_1.png", False, True, 0,0,0,0,0,0,10,0,0,"#000000", 5],
            ["assets//examples//tmpzt3mblvk.png", False, True, -20,10,0,0,-6,-2,35,30,1,"#ffffff", 0],
            ],
            inputs=[input_image, filter_color, fill_hex, start_x, start_y, end_x, end_y, x_spacing, y_spacing, hex_size, rotation, border_size, border_color, border_opacity],
            elem_id="examples")
    with gr.Row():
        gr.HTML(value=versions_html(), visible=True, elem_id="versions")

    color_display.select(on_color_display_select,inputs=[color_display], outputs=[selected_row])
    color_display.input(on_input,inputs=[color_display], outputs=[color_display, gr.State(excluded_color_list)])

    delete_button.click(fn=delete_color, inputs=[selected_row, color_display], outputs=[color_display])
    exclude_color_button.click(fn=add_color, inputs=[color_picker, gr.State(excluded_color_list)], outputs=[color_display, gr.State(excluded_color_list)])
    hex_button.click(hex_create, inputs=[hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list], outputs=[output_image, overlay_image], scroll_to_output=True)
    generate_input_image.click(
        fn=generate_input_image_click,
        inputs=[map_options, prompt_textbox, negative_prompt_textbox, model_textbox, gr.State(False), gr.State(0.5), image_size_ratio],
        outputs=[input_image], scroll_to_output=True
    )
    generate_depth_button.click(
        fn=generate_depth_button_click,
        inputs=[depth_image_source, voxel_size_factor, input_image, output_image, overlay_image, bordered_image_output],
        outputs=[depth_map_output, model_output], scroll_to_output=True
    )
    model_textbox.change(
        fn=update_prompt_notes,
        inputs=model_textbox,
        outputs=prompt_notes_label,preprocess=False
    )
    model_options.change(
        fn=lambda x: (gr.update(visible=(x == "Manual Entry")), gr.update(value=x) if x != "Manual Entry" else gr.update()),
        inputs=model_options,
        outputs=[model_textbox, model_textbox]
    )
    model_options.change(
        fn=update_prompt_notes,
        inputs=model_options,
        outputs=prompt_notes_label
    )
    composite_button.click(
        fn=change_color,
        inputs=[input_image, composite_color, composite_opacity],
        outputs=[input_image]
    )

    #use conditioned_image as the input_image for generate_input_image_click
    generate_input_image_from_gallery.click(
        fn=generate_input_image_click,
        inputs=[map_options, prompt_textbox, negative_prompt_textbox, model_textbox, gr.State(True), image_guidance_stength, image_size_ratio],
        outputs=[input_image], scroll_to_output=True
    )

    # Update the state variable with the prerendered image filepath when an image is selected
    prerendered_image_gallery.select(
        fn=on_prerendered_gallery_selection, 
        inputs=None, 
        outputs=[gr.State(current_prerendered_image)],  # Update the state with the selected image
        show_api=False
    )
    # replace input image with selected gallery image
    replace_input_image_button.click(
        lambda: current_prerendered_image.value,
        inputs=None,
        outputs=[input_image], scroll_to_output=True
    )
    output_overlay_composite.change(
        fn=combine_images_with_lerp,
        inputs=[input_image, output_image, output_overlay_composite],
        outputs=[overlay_image], scroll_to_output=True
    )
    output_blend_multiply_composite.change(
        fn=multiply_and_blend_images,
        inputs=[input_image, output_image, output_blend_multiply_composite],
        outputs=[overlay_image],
        scroll_to_output=True
    )
    output_alpha_composite.change(
        fn=alpha_composite_with_control,
        inputs=[input_image, output_image, output_alpha_composite],
        outputs=[overlay_image],
        scroll_to_output=True
    )
    add_border_button.click(
        fn=lambda image_source, mask_w, mask_h, color, opacity, input_img, overlay_img: add_border(input_img if image_source == "Input Image" else overlay_img, mask_w, mask_h, update_color_opacity(detect_color_format(color), opacity * 2.55)),
        inputs=[border_image_source, mask_width, mask_height, margin_color, margin_opacity, input_image, overlay_image],
        outputs=[bordered_image_output],
        scroll_to_output=True
    )
(())
if __name__ == "__main__":
    beeuty.queue(default_concurrency_limit=1,max_size=12,api_open=False)
    beeuty.launch(allowed_paths=["assets","/","./assets","images","./images", "./images/prerendered"], favicon_path="./assets/favicon.ico", max_file_size="10mb")