File size: 4,530 Bytes
2a0d582
21c87da
c115883
21c87da
c115883
 
 
 
 
dae80b9
c369c5b
e6c2b26
 
5c2cbe3
 
44c7f77
e6c2b26
e03657f
c115883
 
381bbf4
5c2cbe3
 
21c87da
eb09877
 
 
5eb89c4
e03657f
c115883
 
 
 
 
 
 
 
 
 
 
 
 
e03657f
be3c9c0
e03657f
be3c9c0
 
 
 
 
 
 
 
 
 
 
e03657f
be3c9c0
 
e03657f
 
 
21c87da
 
e03657f
 
5a98ee7
e03657f
5a98ee7
e03657f
 
 
21c87da
381bbf4
21c87da
381bbf4
21c87da
 
e6c2b26
 
21c87da
3bc2cfb
 
21c87da
c369c5b
c115883
5eb89c4
e03657f
 
 
 
2a0d582
 
 
 
 
 
44c7f77
 
 
 
 
 
 
c115883
44c7f77
 
 
c115883
 
 
 
 
 
 
 
 
 
 
44c7f77
 
 
c115883
44c7f77
 
c115883
 
 
 
44c7f77
 
e03657f
21c87da
 
 
 
44c7f77
 
8d7c1dd
42175c1
c115883
44c7f77
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
import shutil
import gradio as gr
import os
from app.gradio_config import css, theme
from app.tabs.submit import (
    submit,
    custom_template_yaml,
    collection_submit_state,
)
from app.tabs.visualizer import visualizer, collection as collection_viz_state

from app.tabs.templating import (
    templating_block,
    TEMPLATE_IMAGE_FOLDER,
    TEMPLATE_YAML_FOLDER,
    template_output_yaml_code,
)
from gradio_modal import Modal

from htrflow.models.huggingface.trocr import TrOCR

gr.set_static_paths(paths=[TEMPLATE_IMAGE_FOLDER])
gr.set_static_paths(paths=[TEMPLATE_YAML_FOLDER])

# TODO: fix api/ endpoints..
# TODO add colab
# TDOO addd eexmaple for api


def load_markdown(language, section, content_dir="app/content"):
    """Load markdown content from files."""
    if language is None:
        file_path = os.path.join(content_dir, f"{section}.md")
    else:
        file_path = os.path.join(content_dir, language, f"{section}.md")

    if os.path.exists(file_path):
        with open(file_path, "r", encoding="utf-8") as f:
            return f.read()
    return f"## Content missing for {file_path} in {language}"


matomo = """
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://matomo.riksarkivet.se/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '25']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<noscript><p><img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.riksarkivet.se/matomo.php?idsite=25&amp;rec=1" style="border:0;" alt="" /></p></noscript>
<!-- End Matomo Code -->
"""

with gr.Blocks(title="HTRflow", theme=theme, css=css, head=matomo) as demo:
    with gr.Row():
        with gr.Column(scale=1):
            help_button = gr.Button("Help", scale=0)
            with Modal(visible=False) as help_modal:
                # TODO: tutorial material?
                with gr.Tab("How to use App"):
                    gr.Markdown(load_markdown(None, "how_it_works"))
                with gr.Tab("Contact"):
                    pass

        with gr.Column(scale=2):
            gr.Markdown(load_markdown(None, "main_title"))
        with gr.Column(scale=1):
            gr.Markdown(load_markdown(None, "main_sub_title"))

    with gr.Tabs(elem_classes="top-navbar") as navbar:
        with gr.Tab(label="Templating") as tab_templating:
            templating_block.render()

        with gr.Tab(label="Submit Job") as tab_submit:
            submit.render()

        with gr.Tab(label="Result") as tab_visualizer:
            visualizer.render()

    @demo.load()
    def inital_trocr_load():
        TrOCR("Riksarkivet/trocr-base-handwritten-hist-swe-2")

    @demo.load()
    def inital_yaml_code():
        tmp_dir = "tmp/"
        if os.path.exists(tmp_dir) and os.path.isdir(tmp_dir):
            shutil.rmtree(tmp_dir)

    @demo.load(
        inputs=[template_output_yaml_code],
        outputs=[template_output_yaml_code],
    )
    def inital_yaml_code(template_output_yaml_code):
        return template_output_yaml_code

    def sync_gradio_objects(input_value, state_value):
        """Synchronize the YAML state if there is a mismatch."""
        return input_value if input_value != state_value else gr.skip()

    def sync_gradio_object_state(input_value, state_value):
        """Synchronize the Collection."""
        state_value = input_value
        return state_value if state_value is not None else gr.skip()

    tab_templating.select(
        inputs=[custom_template_yaml, template_output_yaml_code],
        outputs=[template_output_yaml_code],
        fn=sync_gradio_objects,
    )

    tab_submit.select(
        inputs=[template_output_yaml_code, custom_template_yaml],
        outputs=[custom_template_yaml],
        fn=sync_gradio_objects,
    )

    tab_visualizer.select(
        inputs=[collection_submit_state, collection_viz_state],
        outputs=[collection_viz_state],
        fn=sync_gradio_object_state,
    )

    help_button.click(lambda: Modal(visible=True), None, help_modal)

demo.queue()

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        enable_monitoring=True,
        # show_error=True,
    )