File size: 3,103 Bytes
c9a1c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7263d32
c9a1c2d
 
7263d32
c9a1c2d
 
 
7263d32
 
c9a1c2d
 
 
 
 
 
 
 
 
 
 
deef83a
c9a1c2d
deef83a
 
 
 
 
 
 
 
 
 
 
 
c9a1c2d
 
 
 
 
7263d32
c9a1c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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():
                radio_file_input = gr.CheckboxGroup(
                    choices=["Txt", "XML"],
                    value=["XML"],
                    label="Output file extension",
                    # info="Only txt and page xml is supported for now!",
                    scale=1,
                )

                htr_pipeline_button = gr.Button(
                    "Run HTR", variant="primary", visible=True, elem_id="run_pipeline_button", scale=1
                )
            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, scale=1)

    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",
    )