File size: 5,337 Bytes
b8077d0
9714711
b8077d0
 
 
9714711
4433b8c
b8077d0
 
 
 
 
 
5015c53
b8077d0
 
 
1846d66
b8077d0
 
 
 
 
 
5015c53
ee45a15
 
 
 
b8077d0
 
 
ee45a15
 
 
 
 
 
 
 
b8077d0
 
ee45a15
b8077d0
 
 
 
ee45a15
 
 
 
 
b0bdc40
b8077d0
ee45a15
 
 
b0bdc40
b8077d0
 
 
 
 
 
1846d66
b8077d0
5015c53
 
 
b8077d0
 
 
 
 
 
 
 
 
1846d66
b8077d0
5015c53
 
 
 
 
 
 
 
 
b8077d0
b0bdc40
b8077d0
 
 
 
 
 
 
 
 
5015c53
 
 
 
ee45a15
5015c53
 
9714711
b8077d0
 
ee45a15
 
 
 
 
 
 
 
 
 
 
b8077d0
ee45a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5015c53
b8077d0
 
5015c53
 
 
 
 
 
ee45a15
5015c53
 
9714711
b8077d0
1846d66
 
b8077d0
1846d66
5015c53
 
 
 
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
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,),
    )