File size: 4,986 Bytes
3d1d388
0d02a82
6c64dd2
3d1d388
 
7c7b7af
 
 
 
 
 
 
 
 
 
 
9bd4611
fd41354
b5e972d
9bd4611
fd41354
 
6c64dd2
9bd4611
6c64dd2
9bd4611
 
6c64dd2
9bd4611
 
 
 
 
 
 
 
 
 
 
 
 
6c64dd2
7c7b7af
 
 
 
 
 
470f9a1
fd41354
3d1d388
 
470f9a1
 
 
 
f157c01
470f9a1
 
 
3d1d388
7c7b7af
 
fd41354
7c7b7af
b5e972d
 
 
 
27fedfc
b5e972d
fd41354
 
 
7c7b7af
 
 
fd41354
 
 
 
7c7b7af
9bd4611
6c64dd2
7c7b7af
 
 
6c64dd2
7c7b7af
 
 
 
 
6c64dd2
fd41354
 
 
 
 
470f9a1
16dfbed
fd41354
5543001
f157c01
16dfbed
5543001
fd41354
 
 
 
 
9bd4611
fd41354
a72db6b
fd41354
a72db6b
 
 
7c7b7af
 
 
 
 
 
 
 
 
 
b5e972d
 
f157c01
b5e972d
7c7b7af
 
 
 
 
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
import re
import math
import itertools
import pandas as pd
import gradio as gr

from link import UrlProcessor
# from main import TextProcessor


description = f'''
You must set the BITLY_ACCESS_TOKEN environment variable.
For more information, see
- https://app.bitly.com/settings/api
'''

def calculate_layout(item_size=(40, 40), pos=(280, 20), pos_p=(30, 67)):
    layout = []
    pos_x, pos_y = pos
    pos_px, pos_py = pos_p
    size_x, size_y = item_size
    size_px, size_py = item_size

    return layout, pos_x, pos_y, pos_px, pos_py, size_x, size_y, size_px, size_py

def update_df(*items):
    items = [item for item in items if item is not None]

    if len(items) >= 2 and all(items):
        image_count = len(items[0]) * len(items[1])
    else:
        image_count = 1

    if image_count:
        layout, pos_x, pos_y, pos_px, pos_py, size_x, size_y, size_px, size_py = calculate_layout()
        positions = [(pos_x, pos_y, pos_px, pos_py, size_x, size_y, size_px, size_py) for _ in range(image_count)]

        df = pd.DataFrame(positions, columns=headers)
        df.insert(0, 'ImageId', range(1, image_count + 1))

        return df

with gr.Blocks() as demo:
    gr.Markdown(description)
    with gr.Row():
        with gr.Column():
            access_token = gr.Textbox(label="Access Token", placeholder="***")
            api_url = gr.Textbox(label="API base URL", placeholder="https://")
            txt_input = gr.MultimodalTextbox(label="Enter Text with URLs")
            count_url = gr.Dropdown(visible=False, allow_custom_value=True)
            @gr.render(inputs=[txt_input])
            def update_cnt(txt):
                text = ''
                if txt['files']:
                    for f in txt['files']:
                        with open(f) as f:
                            text += f.read() + '\n\n'
                if txt['text']:
                    text += txt['text']
                urls = re.findall(r'http[s]?://\S+', text)
                return urls

            with gr.Row():
                shorten_checkbox = gr.Checkbox(label="Shorten URL", value=False)
                qr_checkbox = gr.Checkbox(label="Generate QR Code", value=True)

            with gr.Row():
                sx = gr.Number(label="Card Width", minimum=1, maximum=210, value=91, step=1)
                sy = gr.Number(label="Card Height", minimum=1, maximum=297, value=55, step=1)
                scale = gr.Number(label="Scale", minimum=1, maximum=10, value=2, step=0.1)

            with gr.Row():
                clear_button = gr.ClearButton(txt_input)
                submit_button = gr.Button("Process URLs", variant='primary')

            with gr.Accordion(open=False, label="Insert"):
                with gr.Row():
                    img_formats = ['png',
                    'jpeg', 'jpg', 'webp', 'svg', 'gif', 'tiff']
                    cm_img_input = gr.Files(file_types=img_formats, label='Insert common images')
                    idv_img_input = gr.Files(file_types=img_formats, label='Insert individual images')

                headers = ['PosX', 'PosY', 'PosPx', 'PosPy', 'SizeX', 'SizeY', 'SizePx', 'SizePy']

        with gr.Column():
            file_output = gr.File(label="Download ZIP")
            with gr.Accordion(open=True, label="Gallery"):
                gallery_output = gr.Gallery(object_fit='contain', label="PNG Gallery")
            with gr.Accordion(open=False, label="Summary"):
                json_output = gr.JSON(label="JSON Summary")
            with gr.Accordion(open=False, label="Preview"):
                preview_output = gr.TextArea(label="Raw Text")


    with gr.Row():
        inputs = [
            txt_input, cm_img_input, idv_img_input
        ]
        examples =     [
            [{'files': ['demo/demo.md'], 'text': 'https://example.com'},
            ['demo/white.png'],
            ['demo/logo.png']
            ],
            [{'files': ['demo/demo.md', 'demo/demo2.md']},
            ['demo/dark.png'],
            ['demo/img/number-1.png', 'demo/img/number-2.png', 'demo/img/number-3.png', 'demo/img/number-4.png', 'demo/img/number-5.png', ]
            ]
        ]
        gr.Examples(examples, inputs)

    with gr.Row():
        df = gr.Dataframe(headers=['ImageId'] + headers, label="Positions")

    count_items = [count_url, idv_img_input]
    txt_input.change(update_cnt, txt_input, count_url)
    count_url.change(update_df, count_items, df)
    cm_img_input.change(update_df, count_items, df)
    idv_img_input.change(update_df, count_items, df)
    submit_button.click(
        UrlProcessor.shorten_and_generate_qr,
        inputs=[
            access_token,
            api_url,
            txt_input,
            shorten_checkbox,
            qr_checkbox,
            cm_img_input,
            idv_img_input,
            sx,
            sy,
            scale,
            df
            ],
        outputs=[preview_output, gallery_output, file_output, json_output]
    )

demo.launch()