Ashwin V. Mohanan commited on
Commit
d00dcd7
·
1 Parent(s): 97ba84c

Add nav buttons

Browse files
Files changed (4) hide show
  1. app/gradio_config.py +7 -16
  2. app/tabs/submit.py +1 -1
  3. app/tabs/visualizer.py +70 -6
  4. run.sh +3 -0
app/gradio_config.py CHANGED
@@ -4,12 +4,13 @@ theme = gr.themes.Default(
4
  primary_hue="blue",
5
  secondary_hue="blue",
6
  neutral_hue="slate",
7
- # font=[
8
- # gr.themes.GoogleFont("Open Sans"),
9
- # "ui-sans-serif",
10
- # "system-ui",
11
- # "sans-serif",
12
- # ],
 
13
  )
14
 
15
  css = """
@@ -25,14 +26,6 @@ css = """
25
  top: 20px;
26
  }
27
 
28
- .transcription-column {
29
- height: 100vh;
30
- }
31
-
32
- /* this is needed in order to make the transcription sticky */
33
- .app {
34
- overflow: visible;
35
- }
36
 
37
  /* style of table cell svg elements */
38
  .cellline {
@@ -85,7 +78,6 @@ hr.region-divider {
85
  .pipeline-info {
86
  padding: 0 0 0 2px;
87
  font-weight: var(--block-info-text-weight);
88
- font-size: var(--block-info-text-size);
89
  color: var(--block-info-text-color);
90
  }
91
 
@@ -126,5 +118,4 @@ hr.region-divider {
126
  }
127
  }
128
 
129
-
130
  """
 
4
  primary_hue="blue",
5
  secondary_hue="blue",
6
  neutral_hue="slate",
7
+ font=[
8
+ gr.themes.GoogleFont("Open Sans"),
9
+ "ui-sans-serif",
10
+ "system-ui",
11
+ "sans-serif",
12
+ ],
13
+ # text_size="md",
14
  )
15
 
16
  css = """
 
26
  top: 20px;
27
  }
28
 
 
 
 
 
 
 
 
 
29
 
30
  /* style of table cell svg elements */
31
  .cellline {
 
78
  .pipeline-info {
79
  padding: 0 0 0 2px;
80
  font-weight: var(--block-info-text-weight);
 
81
  color: var(--block-info-text-color);
82
  }
83
 
 
118
  }
119
  }
120
 
 
121
  """
app/tabs/submit.py CHANGED
@@ -186,7 +186,7 @@ with gr.Blocks() as submit:
186
 
187
  with gr.Column(scale=2):
188
  first_page = gr.Number(3, label="First page of the book", precision=0)
189
- last_page = gr.Number(4, label="Last page of the book", precision=0)
190
  examples = gr.Gallery(
191
  all_example_images(),
192
  label="Examples",
 
186
 
187
  with gr.Column(scale=2):
188
  first_page = gr.Number(3, label="First page of the book", precision=0)
189
+ last_page = gr.Number(5, label="Last page of the book", precision=0)
190
  examples = gr.Gallery(
191
  all_example_images(),
192
  label="Examples",
app/tabs/visualizer.py CHANGED
@@ -1,10 +1,12 @@
 
1
  import gradio as gr
2
  from jinja2 import Environment, FileSystemLoader
 
3
 
4
  _ENV = Environment(loader=FileSystemLoader("app/assets/jinja-templates"))
5
  _IMAGE_TEMPLATE = _ENV.get_template("image.j2")
6
 
7
- from typing import NamedTuple
8
  from dawsonia.typing import BBoxTuple
9
 
10
 
@@ -22,11 +24,42 @@ class Page(NamedTuple):
22
  path: str
23
 
24
 
25
- def render_image(collection: list[Page], current_page_index: int) -> str:
 
 
26
  return _IMAGE_TEMPLATE.render(
27
  page=collection[current_page_index],
28
  )
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  with gr.Blocks() as visualizer:
32
  gr.Markdown("# Result")
@@ -36,14 +69,14 @@ with gr.Blocks() as visualizer:
36
 
37
  with gr.Row():
38
  # Annotated image panel
39
- with gr.Column(scale=2):
40
  image = gr.HTML(
41
  label="Annotated image",
42
  padding=False,
43
  elem_classes="svg-image",
44
  container=True,
45
- max_height="65vh",
46
- min_height="65vh",
47
  show_label=True,
48
  )
49
 
@@ -57,6 +90,12 @@ with gr.Blocks() as visualizer:
57
  collection = gr.State()
58
  current_page_index = gr.State(0)
59
 
 
 
 
 
 
 
60
  # Updates on collection change:
61
  # - update the view
62
  # - reset the page index (always start on page 0)
@@ -64,4 +103,29 @@ with gr.Blocks() as visualizer:
64
  # - update the image caption
65
  collection.change(
66
  render_image, inputs=[collection, current_page_index], outputs=image
67
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
  from jinja2 import Environment, FileSystemLoader
4
+ from typing_extensions import TypeAlias
5
 
6
  _ENV = Environment(loader=FileSystemLoader("app/assets/jinja-templates"))
7
  _IMAGE_TEMPLATE = _ENV.get_template("image.j2")
8
 
9
+ from typing import NamedTuple, TypeAlias
10
  from dawsonia.typing import BBoxTuple
11
 
12
 
 
24
  path: str
25
 
26
 
27
+ Collection: TypeAlias = list[Page]
28
+
29
+ def render_image(collection: Collection, current_page_index: int) -> str:
30
  return _IMAGE_TEMPLATE.render(
31
  page=collection[current_page_index],
32
  )
33
 
34
+ def toggle_navigation_button(collection: Collection):
35
+ visible = len(collection) > 1
36
+ return gr.update(visible=visible)
37
+
38
+
39
+ def activate_left_button(current_page_index):
40
+ interactive = current_page_index > 0
41
+ return gr.update(interactive=interactive)
42
+
43
+
44
+ def activate_right_button(collection: Collection, current_page_index):
45
+ interactive = current_page_index + 1 < len(collection)
46
+ return gr.update(interactive=interactive)
47
+
48
+
49
+ def right_button_click(collection: Collection, current_page_index):
50
+ max_index = len(collection) - 1
51
+ return min(max_index, current_page_index + 1)
52
+
53
+
54
+ def left_button_click(current_page_index):
55
+ return max(0, current_page_index - 1)
56
+
57
+
58
+ def update_image_caption(collection: Collection, current_page_index):
59
+ n_pages = len(collection)
60
+ label = os.path.split(collection[current_page_index].path)[-1]
61
+ return f"image {current_page_index + 1} of {n_pages}: `{label}`"
62
+
63
 
64
  with gr.Blocks() as visualizer:
65
  gr.Markdown("# Result")
 
69
 
70
  with gr.Row():
71
  # Annotated image panel
72
+ with gr.Column(scale=1):
73
  image = gr.HTML(
74
  label="Annotated image",
75
  padding=False,
76
  elem_classes="svg-image",
77
  container=True,
78
+ min_height="40vh",
79
+ max_height="50vh",
80
  show_label=True,
81
  )
82
 
 
90
  collection = gr.State()
91
  current_page_index = gr.State(0)
92
 
93
+ # Wiring of navigation buttons
94
+ left.click(left_button_click, current_page_index, current_page_index)
95
+ right.click(
96
+ right_button_click, [collection, current_page_index], current_page_index
97
+ )
98
+
99
  # Updates on collection change:
100
  # - update the view
101
  # - reset the page index (always start on page 0)
 
103
  # - update the image caption
104
  collection.change(
105
  render_image, inputs=[collection, current_page_index], outputs=image
106
+ )
107
+ collection.change(lambda _: 0, current_page_index, current_page_index)
108
+ collection.change(toggle_navigation_button, collection, left)
109
+ collection.change(toggle_navigation_button, collection, right)
110
+ collection.change(
111
+ update_image_caption,
112
+ inputs=[collection, current_page_index],
113
+ outputs=image_caption,
114
+ )
115
+
116
+ # Updates on page change:
117
+ # - update the view
118
+ # - activate/deactivate buttons
119
+ # - update the image caption
120
+ current_page_index.change(
121
+ render_image, inputs=[collection, current_page_index], outputs=image
122
+ )
123
+ current_page_index.change(activate_left_button, current_page_index, left)
124
+ current_page_index.change(
125
+ activate_right_button, [collection, current_page_index], right
126
+ )
127
+ current_page_index.change(
128
+ update_image_caption,
129
+ inputs=[collection, current_page_index],
130
+ outputs=image_caption,
131
+ )
run.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/bin/bash
2
+ source .venv/bin/activate
3
+ GRADIO_CACHE_DIR=.gradio_cache PYTHONPATH=$(pwd) gradio app/main.py