File size: 4,175 Bytes
c9a1c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deef83a
c9a1c2d
deef83a
 
 
 
 
 
 
 
 
 
 
 
c9a1c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deef83a
 
 
 
 
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
import gradio as gr

from helper.examples.examples import DemoImages
from src.htr_pipeline.gradio_backend import FastTrack, SingletonModelLoader

model_loader = SingletonModelLoader()

fast_track = FastTrack(model_loader)

images_for_demo = DemoImages()


with gr.Blocks() as htr_tool_tab:
    with gr.Row(equal_height=True):
        with gr.Column(scale=2):
            with gr.Row():
                fast_track_input_region_image = gr.Image(
                    label="Image to run HTR on", type="numpy", tool="editor", elem_id="image_upload", height=395
                )

            with gr.Row():
                # with gr.Group():
                # callback = gr.CSVLogger()
                # # hf_writer = gr.HuggingFaceDatasetSaver(HF_API_TOKEN, "htr_pipelin_flags")
                # flagging_button = gr.Button(
                #     "Flag",
                #     variant="secondary",
                #     visible=True,
                # ).style(full_width=True)
                # radio_file_input = gr.Radio(
                #     value="Text file", choices=["Text file ", "Page XML file "], label="What kind file output?"
                # )

                radio_file_input = gr.CheckboxGroup(
                    choices=["Txt", "XML"],
                    value=["Txt"],
                    label="Output file extension",
                    # info="Only txt and page xml is supported for now!",
                )

                htr_pipeline_button = gr.Button(
                    "Run HTR",
                    variant="primary",
                    visible=True,
                    elem_id="run_pipeline_button",
                ).style(full_width=False)

            with gr.Group():
                with gr.Row():
                    fast_file_downlod = gr.File(label="Download output file", visible=False)
                with gr.Row():
                    with gr.Accordion("Example images to use:", open=False) as fast_example_accord:
                        fast_name_files_placeholder = gr.Markdown(visible=False)

                        gr.Examples(
                            examples=images_for_demo.examples_list,
                            inputs=[fast_name_files_placeholder, fast_track_input_region_image],
                            label="Example images",
                            examples_per_page=5,
                        )
            with gr.Row():
                gr.Markdown(
                    """
                    Image viewer for xml output: 
                    <p align="center">
                        <a href="https://huggingface.co/spaces/Riksarkivet/Viewer_demo">
                            <img src="https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-xl-dark.svg" alt="Badge 1">
                        </a>
                    </p>
                    
                    """
                )

        with gr.Column(scale=4):
            fast_track_output_image = gr.Image(label="HTR results visualizer", type="numpy", tool="editor", height=650)

        with gr.Row(visible=False) as api_placeholder:
            htr_pipeline_button_api = gr.Button(
                "Run pipeline",
                variant="primary",
                visible=False,
            ).style(full_width=False)

    xml_rendered_placeholder_for_api = gr.Textbox(visible=False)
    htr_pipeline_button.click(
        fast_track.segment_to_xml,
        inputs=[fast_track_input_region_image, radio_file_input],
        outputs=[fast_track_output_image, fast_file_downlod, fast_file_downlod],
    )

    htr_pipeline_button_api.click(
        fast_track.segment_to_xml_api,
        inputs=[fast_track_input_region_image],
        outputs=[xml_rendered_placeholder_for_api],
        api_name="predict",
    )

    # callback.setup([fast_track_input_region_image], "flagged_data_points")
    # flagging_button.click(lambda *args: callback.flag(args), [fast_track_input_region_image], None, preprocess=False)
    # flagging_button.click(lambda: (gr.update(value="Flagged")), outputs=flagging_button)
    # fast_track_input_region_image.change(lambda: (gr.update(value="Flag")), outputs=flagging_button)