Spaces:
Sleeping
Sleeping
| 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) | |
| 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() | |