File size: 3,790 Bytes
8d18f89
c115883
 
60d8ae5
381bbf4
60d8ae5
 
 
 
 
 
 
8d18f89
 
 
 
0658a37
5c2cbe3
21c87da
e03657f
c115883
 
 
 
 
 
 
 
 
 
 
 
 
f9141c6
8d18f89
f9141c6
 
d30cc33
e03657f
be3c9c0
e03657f
be3c9c0
 
3cb882a
 
be3c9c0
 
 
 
 
 
 
 
 
e03657f
be3c9c0
 
e03657f
 
1e41501
 
 
 
d30cc33
1e41501
bb209bb
1e41501
 
bb209bb
1e41501
21c87da
 
1e41501
3bc2cfb
bb209bb
c115883
5eb89c4
1e41501
60d8ae5
 
6df9119
 
 
e03657f
c115883
 
 
 
 
bb209bb
f9141c6
 
 
c115883
 
 
 
44c7f77
 
60d8ae5
 
 
 
 
21c87da
 
 
 
dbb4f67
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
import logging
import os

import gradio as gr

from app.gradio_config import css, theme
from app.tabs.export import collection as collection_export_state
from app.tabs.export import export
from app.tabs.submit import collection_submit_state, submit
from app.tabs.visualizer import collection as collection_viz_state
from app.tabs.visualizer import visualizer

# Suppress transformers logging
logging.getLogger("transformers").setLevel(logging.ERROR)


TEMPLATE_YAML_FOLDER = "app/assets/templates"
gr.set_static_paths(paths=[TEMPLATE_YAML_FOLDER])


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}"


def activate_tab(collection):
    return gr.update(interactive=collection is not None)


# Please remove this line when running locally
matomo = """
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['setDomains', ['*.riksarkivet.se', 'huggingface.co']]);
  _paq.push(['enableCrossDomainLinking']);
  _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:
    gr.Markdown(load_markdown(None, "main_title"), elem_classes="title-h1")

    with gr.Sidebar(label="Menu"):
        gr.HTML(load_markdown(None, "main_sub_title_hum"))
        gr.Markdown(load_markdown(None, "sidebar"))

    with gr.Tabs(elem_classes="top-navbar") as navbar:
        with gr.Tab(label="1. Upload") as tab_submit:
            submit.render()
        with gr.Tab(label="2. Result", interactive=False, id="result") as tab_visualizer:
            visualizer.render()

        with gr.Tab(label="3. Export", interactive=False) as tab_export:
            export.render()

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

    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()

    collection_submit_state.change(activate_tab, collection_submit_state, tab_visualizer)
    collection_submit_state.change(activate_tab, collection_submit_state, tab_export)
    collection_submit_state.change(lambda: gr.Tabs(selected="result"), outputs=navbar)

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

    tab_export.select(
        inputs=[collection_submit_state, collection_export_state],
        outputs=[collection_export_state],
        fn=sync_gradio_object_state,
    )

demo.queue()

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, enable_monitoring=False, show_api=False)