dawsonia-demo / app /main.py
Ashwin V. Mohanan
Add first rough version
b8077d0
raw
history blame
3.04 kB
import logging
import os
import gradio as gr
# from htrflow.models.huggingface.trocr import TrOCR
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)
html_header = ""
with gr.Blocks(title="Dawsonia Demo", theme=theme, css=css, head=html_header) as demo:
with gr.Row():
with gr.Column(scale=1):
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="Upload") as tab_submit:
submit.render()
# with gr.Tab(label="Result", interactive=False, id="result") as tab_visualizer:
# visualizer.render()
#
# with gr.Tab(label="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=True, show_api=False
)