Morgan Svensson commited on
Commit
3bc2cfb
·
1 Parent(s): e6c2b26

added submit tab

Browse files
Files changed (2) hide show
  1. app/main.py +3 -2
  2. app/tabs/submit.py +159 -0
app/main.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
 
3
  from app.gradio_config import css, theme
4
  from app.tabs.adv_htrflow_tab import adv_htrflow_pipeline
 
5
  from app.tabs.examples_tab import examples
6
  from app.tabs.templating import (
7
  templating_block,
@@ -27,8 +28,8 @@ with gr.Blocks(title="HTRflow", theme=theme, css=css) as demo:
27
  with gr.Tab(label="Templating") as tab_templating:
28
  templating_block.render()
29
 
30
- with gr.Tab(label="Submit Job") as tab_submit_job:
31
- adv_htrflow_pipeline.render()
32
 
33
  with gr.Tab(label="Output & Visualize") as tab_examples:
34
  examples.render()
 
2
 
3
  from app.gradio_config import css, theme
4
  from app.tabs.adv_htrflow_tab import adv_htrflow_pipeline
5
+ from app.tabs.submit import submit
6
  from app.tabs.examples_tab import examples
7
  from app.tabs.templating import (
8
  templating_block,
 
28
  with gr.Tab(label="Templating") as tab_templating:
29
  templating_block.render()
30
 
31
+ with gr.Tab(label="Submit Job") as tab_submit:
32
+ submit.render()
33
 
34
  with gr.Tab(label="Output & Visualize") as tab_examples:
35
  examples.render()
app/tabs/submit.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+
4
+ default_template_yaml = """
5
+ steps:
6
+
7
+ # Region segmentation
8
+ - step: Segmentation
9
+ settings:
10
+ model: yolo
11
+ model_settings:
12
+ model: Riksarkivet/yolov9-regions-1
13
+ revision: 7c44178d85926b4a096c55c89bf224855a201fbf
14
+ generation_settings:
15
+ batch_size: 32
16
+ half: true
17
+ conf: 0.5 # confidence threshold - keep all boxes with conf > 0.5
18
+
19
+ # Line segmentation
20
+ - step: Segmentation
21
+ settings:
22
+ model: yolo
23
+ model_settings:
24
+ model: Riksarkivet/yolov9-lines-within-regions-1
25
+ revision: ea2f8987cba316abc62762f3030266ec8875338d
26
+ generation_settings:
27
+ batch_size: 16
28
+ half: true
29
+
30
+ - step: FilterRegionsByShape
31
+ settings:
32
+ min_ratio: 0.5 # keep lines that have at least a 1:2 (=0.5) width-to-height ratio
33
+
34
+ - step: TextRecognition
35
+ settings:
36
+ model: ORTWordLevelTrocr
37
+ model_settings:
38
+ model: ./model
39
+ generation_settings:
40
+ batch_size: 64
41
+ num_beams: 1
42
+ max_new_tokens: 64
43
+
44
+ - step: ReadingOrderMarginalia
45
+ settings:
46
+ two_page: true
47
+
48
+ # Remove garbage lines: anything below 0.7 in confidence
49
+ - step: RemoveLowTextConfidenceLines
50
+ settings:
51
+ threshold: 0.7
52
+
53
+ # Remove garbage regions: any region with mean confidence < 0.75 AFTER the below-0.7 lines have been removed
54
+ - step: RemoveLowTextConfidenceRegions
55
+ settings:
56
+ threshold: 0.75
57
+
58
+ - step: Export
59
+ settings:
60
+ dest: output/job
61
+ format: alto
62
+ template_dir: /app/config
63
+ template_name: alto-with-RA-pageID
64
+
65
+ - step: Export
66
+ settings:
67
+ dest: output/job
68
+ format: json
69
+ indent: null
70
+
71
+ # Sets label format to regionX_lineY_wordZ
72
+ labels:
73
+ level_labels:
74
+ - region
75
+ - line
76
+ - word
77
+ sep: _
78
+ template: "{label}{number}"
79
+ """
80
+
81
+ with gr.Blocks() as submit:
82
+ # Row 1: Please upload the image message
83
+ #with gr.Row():
84
+ # docs_link = gr.HTML(
85
+ # value=''
86
+ # )
87
+ #
88
+ # Row 2: Image Upload and Editor
89
+ with gr.Row(variant="panel"):
90
+ image_editor = gr.ImageEditor(
91
+ label="Upload the image you want to transcribe",
92
+ sources="upload",
93
+ interactive=True,
94
+ layers=False,
95
+ eraser=False,
96
+ brush=False,
97
+ height=400,
98
+ transforms="crop",
99
+ crop_size="16,5",
100
+ visible=False,
101
+ )
102
+ image_mask = gr.Gallery(
103
+ file_types=["image"],
104
+ label="Upload the image you want to transcribe",
105
+ interactive=True,
106
+ height=400,
107
+ object_fit="cover",
108
+ columns=5,
109
+ )
110
+
111
+ # Row 3: Run Template Accordion
112
+ with gr.Row():
113
+ with gr.Accordion(label="Pipeline yaml (expand to customize settings)", open=False):
114
+ # Add the documentation link with an emoji first
115
+ docs_link = gr.HTML(
116
+ value='<p>Bellow is the pipeline yaml that will be used. <a href="https://ai-riksarkivet.github.io/htrflow/latest/getting_started/pipeline.html#example-pipelines" target="_blank">📚 Click here 📚</a> for a detailed description on how to customize the configuration</p>'
117
+ )
118
+ # Then, the code block with the template YAML
119
+ custom_template_yaml = gr.Code(
120
+ value=default_template_yaml, # Set the default template YAML here
121
+ language="yaml",
122
+ label="yaml",
123
+ interactive=True,
124
+ lines=5,
125
+ )
126
+
127
+ # Row 4: Submit and Cancel Buttons
128
+ with gr.Row():
129
+ run_button = gr.Button("Submit", variant="primary", scale=0)
130
+ cancel_button = gr.Button("Stop", variant="stop", scale=0, visible=False)
131
+ d = gr.DownloadButton("Download the file", visible=False, scale=0)
132
+ textbox_ = gr.Textbox(scale=0, visible=False)
133
+
134
+ # Cancel button functionality
135
+ cancel_button.click(
136
+ fn=lambda: gr.update(visible=False),
137
+ inputs=None,
138
+ outputs=cancel_button,
139
+ )
140
+
141
+ # Image Editor Upload handling
142
+ image_editor.upload(
143
+ fn=None,
144
+ inputs=None,
145
+ outputs=None,
146
+ js="""
147
+ () => {
148
+ const button = document.querySelector('button[aria-label="Transform button"][title="Transform button"]');
149
+ if (button) {
150
+ button.click();
151
+ console.log('Transform button clicked.');
152
+ } else {
153
+ console.error('Transform button not found.');
154
+ }
155
+ }
156
+ """,
157
+ ).then(
158
+ fn=lambda: gr.update(crop=None), inputs=None, outputs=image_editor
159
+ )