Spaces:
Running
Running
import logging | |
from pathlib import Path | |
import gradio as gr | |
from gradio_modal import Modal | |
from .submit_functions import all_example_images, get_selected_example_image, move_uploaded_file, get_uploaded_image, run_dawsonia, overwrite_table_format_file | |
logger = logging.getLogger(__name__) | |
with gr.Blocks() as submit: | |
gr.Markdown( | |
"π Select or upload the image you want to transcribe. You can upload up to five images at a time." | |
) | |
batch_book_state = gr.State() | |
batch_book_path_state = gr.State() | |
collection_submit_state = gr.State() | |
with gr.Group(): | |
with gr.Row(equal_height=True): | |
with gr.Column(scale=5): | |
batch_image_gallery = gr.Gallery( | |
# file_types=[".pdf", ".zarr.zip"], | |
label="Preview", | |
interactive=False, | |
object_fit="contain", | |
# scale=0.8, | |
) | |
with gr.Column(scale=2): | |
first_page = gr.Number(3, label="First page", precision=0,) | |
last_page = gr.Number(5, label="Last page", precision=0,) | |
table_fmt_filename = gr.Dropdown( | |
[f.name for f in Path("table_formats").iterdir()], | |
interactive=True, | |
label="Select Table Format", | |
) | |
examples = gr.Gallery( | |
all_example_images(), | |
label="1a. Choose from the examples below, or", | |
interactive=False, | |
allow_preview=False, | |
object_fit="scale-down", | |
min_width=250, | |
height=160, | |
) | |
upload_file = gr.File( | |
label="1b. Upload a .pdf or .zarr.zip file", | |
file_types=[".pdf", ".zarr.zip", ".zip"], | |
) | |
# upload_file_true_path = gr.Textbox(visible=False) | |
upload_button = gr.Button(value="1b. Upload", min_width=200) | |
with Modal(visible=False) as edit_table_fmt_modal: | |
with gr.Column(): | |
gr.Markdown( | |
"## Table format configuration\n" | |
"Write a custom table format, overriding the default one. " | |
"Click on the **Save** button when you are done." | |
) | |
save_tf_button = gr.Button( | |
"Save", variant="primary", scale=0, min_width=200 | |
) | |
gr.HTML( | |
( | |
"<a href='https://dawsonia.readthedocs.io/en/latest/user_guide/misc.html#table-formats' target='_blank'>" | |
"Read the docs for the table-formats spec" | |
"</a>. " | |
), | |
padding=False, | |
elem_classes="pipeline-help", | |
) | |
table_fmt_config_override = gr.Code("", language="python") | |
with gr.Row(): | |
prob_thresh = gr.Slider( | |
minimum=0.0, | |
maximum=1.0, | |
value=0.75, | |
step=0.05, | |
label="Prediction probability threshold", | |
) | |
with gr.Row(): | |
run_button = gr.Button("2. Digitize", variant="primary", scale=0, min_width=200) | |
edit_table_fmt_button = gr.Button( | |
"Edit table format", variant="secondary", scale=0, min_width=200 | |
) | |
# All events interactions below | |
examples.select( | |
get_selected_example_image, | |
(first_page, last_page), | |
( | |
batch_image_gallery, | |
batch_book_state, | |
batch_book_path_state, | |
table_fmt_filename, | |
table_fmt_config_override, | |
), | |
trigger_mode="always_last", | |
) | |
upload_file.upload(move_uploaded_file, inputs=[upload_file, table_fmt_filename], outputs=batch_book_path_state) | |
upload_button.click( | |
get_uploaded_image, | |
(first_page, last_page, table_fmt_filename, batch_book_path_state), | |
( | |
batch_image_gallery, | |
batch_book_state, | |
batch_book_path_state, | |
table_fmt_config_override, | |
), | |
) | |
# @batch_image_gallery.upload( | |
# inputs=batch_image_gallery, | |
# outputs=[batch_image_gallery], | |
# ) | |
# def validate_images(images): | |
# print(images) | |
# if len(images) > MAX_IMAGES: | |
# gr.Warning(f"Maximum images you can upload is set to: {MAX_IMAGES}") | |
# return gr.update(value=None) | |
# gr.Warning( | |
# "Digitizing uploaded images is not implemented yet! Work in progress!" | |
# ) | |
# raise NotImplementedError("WIP") | |
# return images | |
run_button.click( | |
fn=run_dawsonia, | |
inputs=( | |
table_fmt_config_override, | |
first_page, | |
last_page, | |
prob_thresh, | |
batch_book_state, | |
batch_book_path_state, | |
batch_image_gallery, | |
), | |
outputs=(collection_submit_state, batch_image_gallery), | |
) | |
## Table formats modal dialog box | |
edit_table_fmt_button.click(lambda: Modal(visible=True), None, edit_table_fmt_modal) | |
save_tf_button.click( | |
overwrite_table_format_file, | |
(batch_book_state, batch_book_path_state, table_fmt_config_override), | |
(batch_book_state,), | |
) | |