Gabriel commited on
Commit
7ca716d
·
verified ·
1 Parent(s): b0dacaa

A compact page: the region box and Read beside a three-column examples table, the live bars beside their summary, our server's timings beside the batched rates, each of Jev's lines beside its readings

Browse files
Files changed (3) hide show
  1. app.py +38 -33
  2. race_view.css +29 -14
  3. race_view.js +22 -12
app.py CHANGED
@@ -605,59 +605,64 @@ def score_line(line: Image.Image | None, text: str) -> tuple[dict[str, float], I
605
  return {"matches": score, "does not match": 1 - score}, render_text(text.strip(), FONT, crop.size)
606
 
607
 
608
- def _label(example: dict[str, Any]) -> str:
609
- collection = example["collection"].removeprefix("GT-").rsplit("_", 1)[0].replace("_", " ")
610
- return f"{len(example['lines'])} lines · {collection}"
 
611
 
612
 
613
- def _region(example: dict[str, Any]) -> str:
614
- return str(HERE / "examples" / example["id"] / "region.png")
615
 
616
 
617
- def _picked(event: gr.SelectData) -> str:
618
- return _region(EXAMPLES[event.index])
619
 
620
 
621
  with gr.Blocks(title="Swedish HTR: TrOCR, MiniCPM-OCR and DSpark") as demo:
622
  gr.Markdown(
623
  "# Reading Swedish handwriting faster\n"
624
- "One text region from an archive volume, read on the same GPU with **one stream each and nothing batched**. "
625
- "Riksarkivet's **Swedish Lion TrOCR** cannot read a region, so it gets the region's text lines, cut out "
626
- "beforehand, and reads them one after the other (its time for all of them as one batch is shown beside). Our "
627
- f"**[MiniCPM-OCR reader](https://huggingface.co/{READER})** reads the region as one image, alone and with "
628
  f"its **[DSpark draft](https://huggingface.co/{DRAFT})**, which guesses 15 tokens at a time for the reader "
629
- f"to check in one pass. Then **[Jev](https://huggingface.co/{JEV})**, the reader's proof-reader, scores both "
630
- "readers' reading of each line. Under the race: the same region on our server, where **vLLM** serves the "
631
- "reader, and what the readers do there when everything is batched."
632
  )
633
  with gr.Tab("Compare"):
634
  with gr.Row():
635
- with gr.Column(scale=3):
636
  region = gr.Image(
637
  value=_region(EXAMPLES[0]),
638
  type="filepath",
639
  image_mode=None,
640
  sources=["upload", "clipboard"],
641
- label="The region to read: an example from below, or your own crop (upload or paste it)",
642
- height=340,
643
  buttons=["fullscreen"],
644
  )
645
- with gr.Column(scale=1, min_width=260):
646
- with_jev = gr.Checkbox(value=True, label="Score each line with Jev (examples only)")
647
- go = gr.Button("Read", variant="primary")
648
- gr.Markdown("Only the reader reads a crop of your own: TrOCR and Jev need the region's lines.")
649
- gallery = gr.Gallery(
650
- [(str(HERE / "examples" / e["id"] / "thumb.jpg"), _label(e)) for e in EXAMPLES],
651
- label="Example regions, from volumes none of the models trained on: click one to put it in the box",
652
- columns=6,
653
- height=230,
654
- object_fit="cover",
655
- allow_preview=False,
656
- interactive=False,
657
- buttons=[],
658
- )
 
 
 
 
 
 
659
  race_view = RaceView()
660
- gallery.select(_picked, None, region, api_visibility="private")
661
  go.click(run, [region, with_jev], race_view, api_name="compare", show_progress="minimal")
662
  with gr.Tab("Try Jev"):
663
  gr.Markdown(
@@ -684,4 +689,4 @@ with gr.Blocks(title="Swedish HTR: TrOCR, MiniCPM-OCR and DSpark") as demo:
684
  (HERE / "HOW_IT_WORKS.md").read_text(encoding="utf-8").replace("@RENDERER@", renderer_version(FONT))
685
  )
686
 
687
- demo.queue(default_concurrency_limit=2).launch(show_error=True, ssr_mode=False)
 
605
  return {"matches": score, "does not match": 1 - score}, render_text(text.strip(), FONT, crop.size)
606
 
607
 
608
+ # the examples table's thumbnails, small enough for its six rows to stand beside the region box
609
+ EXAMPLES_CSS = (
610
+ "#regions .container.table { width: 44px; height: 44px; } #regions td { padding-top: 4px; padding-bottom: 4px; }"
611
+ )
612
 
613
 
614
+ def _collection(example: dict[str, Any]) -> str:
615
+ return example["collection"].removeprefix("GT-").rsplit("_", 1)[0].replace("_", " ")
616
 
617
 
618
+ def _region(example: dict[str, Any]) -> str:
619
+ return str(HERE / "examples" / example["id"] / "region.png")
620
 
621
 
622
  with gr.Blocks(title="Swedish HTR: TrOCR, MiniCPM-OCR and DSpark") as demo:
623
  gr.Markdown(
624
  "# Reading Swedish handwriting faster\n"
625
+ "One text region from an archive volume, read on the same GPU with **one stream each and nothing batched**: "
626
+ "Riksarkivet's **Swedish Lion TrOCR** on the region's text lines, cut out beforehand, one after the other; "
627
+ f"our **[MiniCPM-OCR reader](https://huggingface.co/{READER})** on the region as one image, alone and with "
 
628
  f"its **[DSpark draft](https://huggingface.co/{DRAFT})**, which guesses 15 tokens at a time for the reader "
629
+ f"to check in one pass. Then **[Jev](https://huggingface.co/{JEV})**, the reader's proof-reader, scores "
630
+ "both readings of each line. Under the race: the same region on our server, where **vLLM** serves the "
631
+ "reader, and both readers when everything is batched."
632
  )
633
  with gr.Tab("Compare"):
634
  with gr.Row():
635
+ with gr.Column(scale=5, min_width=320):
636
  region = gr.Image(
637
  value=_region(EXAMPLES[0]),
638
  type="filepath",
639
  image_mode=None,
640
  sources=["upload", "clipboard"],
641
+ label="Region",
642
+ height=300,
643
  buttons=["fullscreen"],
644
  )
645
+ with gr.Row():
646
+ with_jev = gr.Checkbox(
647
+ value=True,
648
+ label="Score each line with Jev",
649
+ info="Examples only: TrOCR and Jev need a region's lines, so only the reader reads your own crop.",
650
+ scale=3,
651
+ )
652
+ go = gr.Button("Read", variant="primary", scale=2, min_width=140)
653
+ with gr.Column(scale=4, min_width=320):
654
+ # an example's own columns: filled by its row, shown nowhere else
655
+ collection = gr.Textbox(label="Collection", visible=False)
656
+ count = gr.Number(label="Lines", visible=False)
657
+ gr.Examples(
658
+ [[_region(e), _collection(e), len(e["lines"])] for e in EXAMPLES],
659
+ [region, collection, count],
660
+ label="Examples, from volumes none of the models trained on (or upload or paste your own crop)",
661
+ examples_per_page=len(EXAMPLES),
662
+ cache_examples=False,
663
+ elem_id="regions",
664
+ )
665
  race_view = RaceView()
 
666
  go.click(run, [region, with_jev], race_view, api_name="compare", show_progress="minimal")
667
  with gr.Tab("Try Jev"):
668
  gr.Markdown(
 
689
  (HERE / "HOW_IT_WORKS.md").read_text(encoding="utf-8").replace("@RENDERER@", renderer_version(FONT))
690
  )
691
 
692
+ demo.queue(default_concurrency_limit=2).launch(show_error=True, ssr_mode=False, css=EXAMPLES_CSS)
race_view.css CHANGED
@@ -28,7 +28,10 @@
28
  max-width: 100ch;
29
  }
30
  .rv-status::before { content: "● "; animation: rv-pulse 1.2s ease-in-out infinite; }
31
- .rv-group { display: flex; flex-direction: column; gap: 10px; }
 
 
 
32
  .rv-title { margin: 0; font-size: 15px; font-weight: 600; }
33
  .rv-summary, .rv-said, .rv-note, .rv-jintro { margin: 0; max-width: 100ch; }
34
  .rv-note, .rv-jintro { font-size: 13px; color: var(--rv-muted); }
@@ -53,18 +56,19 @@
53
  }
54
  .rv-server .rv-swatch { background: color-mix(in srgb, var(--rv-lane) 38%, transparent); box-shadow: inset 0 0 0 1.5px var(--rv-lane); }
55
 
56
- .rv-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
57
  .rv-tile {
58
- display: flex;
59
- flex-direction: column;
60
- gap: 2px;
61
- padding: 10px 12px;
 
62
  border: 1px solid var(--border-color-primary);
63
- border-top: 3px solid var(--rv-lane);
64
  border-radius: 6px;
65
  }
66
  .rv-tilename { font-weight: 600; }
67
- .rv-rate { font-size: 30px; font-weight: 650; line-height: 1.15; font-variant-numeric: tabular-nums; }
68
  .rv-unit { font-size: 13px; font-weight: 400; color: var(--rv-muted); }
69
 
70
  .rv-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
@@ -138,12 +142,14 @@
138
  .rv-fold { border: 1px solid var(--border-color-primary); border-radius: 6px; padding: 0 12px; }
139
  .rv-foldhead { padding: 9px 0; cursor: pointer; font-weight: 600; }
140
  .rv-fold[open] .rv-foldhead { border-bottom: 1px solid var(--border-color-primary); margin-bottom: 10px; }
141
- .rv-jr { display: grid; grid-template-columns: 92px minmax(0, 1fr) 68px 138px 72px; gap: 10px; align-items: center; }
142
- .rv-jhdr { margin-top: 10px; padding-bottom: 6px; font-size: 12px; font-weight: 600; color: var(--rv-muted); }
143
  .rv-jlist { display: flex; flex-direction: column; }
144
- .rv-jrow { display: grid; gap: 5px; padding: 10px 0; border-top: 1px solid var(--border-color-primary); }
145
- .rv-jtop { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
146
- .rv-jimg { max-width: min(100%, 720px); max-height: 64px; border-radius: 3px; background: #fff; }
 
 
 
147
  .rv-verdict {
148
  font-size: 12px;
149
  padding: 1px 9px;
@@ -172,12 +178,21 @@
172
 
173
  @keyframes rv-flash { from { background: color-mix(in srgb, var(--rv-dspark) 70%, transparent); } }
174
  @keyframes rv-pulse { 50% { opacity: 0.3; } }
 
 
 
 
 
 
 
 
 
175
  @media (max-width: 640px) {
176
  .rv-row { grid-template-columns: minmax(0, 1fr) 64px; }
177
  .rv-row .rv-bar { grid-column: 1 / -1; grid-row: 2; }
178
  .rv-jr { grid-template-columns: 76px minmax(0, 1fr) 60px; }
179
  .rv-jr .rv-scorebox, .rv-jr .rv-pick { grid-column: 2 / -1; }
180
- .rv-jhdr span:nth-child(n + 4) { display: none; }
181
  }
182
  @media (prefers-reduced-motion: reduce) {
183
  .rv-text .rv-draft, .rv-status::before { animation: none; }
 
28
  max-width: 100ch;
29
  }
30
  .rv-status::before { content: "● "; animation: rv-pulse 1.2s ease-in-out infinite; }
31
+ .rv-group { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
32
+ /* on a wide page: the bars beside what they amount to, and our server's timings beside the batched rates */
33
+ .rv-split, .rv-pair { display: grid; gap: 12px 32px; align-items: start; }
34
+ .rv-timed { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
35
  .rv-title { margin: 0; font-size: 15px; font-weight: 600; }
36
  .rv-summary, .rv-said, .rv-note, .rv-jintro { margin: 0; max-width: 100ch; }
37
  .rv-note, .rv-jintro { font-size: 13px; color: var(--rv-muted); }
 
56
  }
57
  .rv-server .rv-swatch { background: color-mix(in srgb, var(--rv-lane) 38%, transparent); box-shadow: inset 0 0 0 1.5px var(--rv-lane); }
58
 
59
+ .rv-tiles { display: grid; gap: 8px; }
60
  .rv-tile {
61
+ display: grid;
62
+ grid-template-columns: minmax(0, 1fr) auto;
63
+ gap: 12px;
64
+ align-items: center;
65
+ padding: 7px 12px;
66
  border: 1px solid var(--border-color-primary);
67
+ border-left: 4px solid var(--rv-lane);
68
  border-radius: 6px;
69
  }
70
  .rv-tilename { font-weight: 600; }
71
+ .rv-rate { font-size: 26px; font-weight: 650; line-height: 1.15; font-variant-numeric: tabular-nums; white-space: nowrap; }
72
  .rv-unit { font-size: 13px; font-weight: 400; color: var(--rv-muted); }
73
 
74
  .rv-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
 
142
  .rv-fold { border: 1px solid var(--border-color-primary); border-radius: 6px; padding: 0 12px; }
143
  .rv-foldhead { padding: 9px 0; cursor: pointer; font-weight: 600; }
144
  .rv-fold[open] .rv-foldhead { border-bottom: 1px solid var(--border-color-primary); margin-bottom: 10px; }
145
+ .rv-jr { display: grid; grid-template-columns: 84px minmax(0, 1fr) 62px 128px 68px; gap: 10px; align-items: center; }
 
146
  .rv-jlist { display: flex; flex-direction: column; }
147
+ /* a line: its crop and the verdict, beside its ground truth and the two readings */
148
+ .rv-jrow { display: grid; gap: 6px 24px; padding: 9px 0; border-top: 1px solid var(--border-color-primary); align-items: center; }
149
+ .rv-jhead { margin-top: 8px; padding: 0 0 6px; border-top: 0; font-size: 12px; font-weight: 600; color: var(--rv-muted); }
150
+ .rv-jline { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; min-width: 0; }
151
+ .rv-jreads { display: grid; gap: 4px; min-width: 0; }
152
+ .rv-jimg { max-width: 100%; max-height: 64px; border-radius: 3px; background: #fff; }
153
  .rv-verdict {
154
  font-size: 12px;
155
  padding: 1px 9px;
 
178
 
179
  @keyframes rv-flash { from { background: color-mix(in srgb, var(--rv-dspark) 70%, transparent); } }
180
  @keyframes rv-pulse { 50% { opacity: 0.3; } }
181
+ @media (min-width: 1100px) {
182
+ .rv-split, .rv-pair { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
183
+ .rv-pair.rv-single { grid-template-columns: minmax(0, 1fr); }
184
+ .rv-pair.rv-single .rv-tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
185
+ .rv-jrow { grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); }
186
+ }
187
+ @media (max-width: 1099px) {
188
+ .rv-jlinehead { display: none; }
189
+ }
190
  @media (max-width: 640px) {
191
  .rv-row { grid-template-columns: minmax(0, 1fr) 64px; }
192
  .rv-row .rv-bar { grid-column: 1 / -1; grid-row: 2; }
193
  .rv-jr { grid-template-columns: 76px minmax(0, 1fr) 60px; }
194
  .rv-jr .rv-scorebox, .rv-jr .rv-pick { grid-column: 2 / -1; }
195
+ .rv-jhead span:nth-child(n + 4) { display: none; }
196
  }
197
  @media (prefers-reduced-motion: reduce) {
198
  .rv-text .rv-draft, .rv-status::before { animation: none; }
race_view.js CHANGED
@@ -201,8 +201,11 @@ function ensure(race) {
201
  button.dataset.speed = String(choice.speed);
202
  controls.append(button);
203
  }
204
- const liveTitle = "Here, live: one stream each, in PyTorch on half a GPU";
205
- live.append(make("h4", "rv-title", liveTitle), track, controls, make("p", "rv-summary"));
 
 
 
206
 
207
  const server = make("section", "rv-group rv-server");
208
  const serverTrack = make("div", "rv-track");
@@ -213,13 +216,14 @@ function ensure(race) {
213
  const batchedTitle = "Everything batched, on our server: lines read a second";
214
  batched.append(make("h4", "rv-title", batchedTitle), make("div", "rv-tiles"), make("p", "rv-note"));
215
 
 
 
216
  root.replaceChildren(
217
  make("div", "rv-notice"),
218
  make("div", "rv-status"),
219
  live,
220
  lanesOf(race),
221
- server,
222
- batched,
223
  make("section", "rv-group rv-jev"),
224
  );
225
  return root;
@@ -229,7 +233,7 @@ function ensure(race) {
229
  function elapsed(now) {
230
  if (!state.raced) return 0;
231
  if (still) return Infinity;
232
- return ((now - state.raceStart) / 1000) * state.speed;
233
  }
234
 
235
  function countUntil(times, at, from) {
@@ -328,6 +332,7 @@ function drawServer(root, race, scale) {
328
  const box = root.querySelector(".rv-server");
329
  const server = race.server;
330
  box.hidden = !server;
 
331
  if (server) {
332
  box.querySelector(".rv-title").textContent =
333
  `The same region on our server: a whole ${server.gpu}, the reader served by vLLM`;
@@ -351,9 +356,11 @@ function drawServer(root, race, scale) {
351
  ...rates.map((rate) => {
352
  const tile = make("div", "rv-tile");
353
  tile.dataset.lane = rate.lane;
 
 
354
  const figure = make("div", "rv-rate", rate.per_second.toFixed(rate.per_second < 10 ? 1 : 0));
355
  figure.append(make("span", "rv-unit", " lines a second"));
356
- tile.append(make("div", "rv-tilename", rate.name), figure, make("div", "rv-how", rate.how));
357
  return tile;
358
  }),
359
  );
@@ -391,34 +398,37 @@ function drawJev(root, race) {
391
  let list = box.querySelector(".rv-jlist");
392
  if (!list) {
393
  list = make("div", "rv-jlist");
394
- const header = make("div", "rv-jr rv-jhdr");
395
  for (const name of ["Reader", "Its reading of the line", "Right?", "Jev's score, 0 to 1", ""]) {
396
  header.append(make("span", "", name));
397
  }
 
 
398
  const fold = make("details", "rv-fold");
399
- fold.append(make("summary", "rv-foldhead"), make("p", "rv-jintro", JEV_INTRO), header, list);
400
  box.replaceChildren(make("h4", "rv-title", "Jev, the reader's proof-reader"), make("p", "rv-jnote"), fold);
401
  }
402
  for (let i = list.childElementCount; i < rows.length; i += 1) {
403
  const row = rows[i];
404
  const item = make("div", "rv-jrow");
405
  item.dataset.line = String(row.index);
406
- const top = make("div", "rv-jtop");
407
  const image = make("img", "rv-jimg");
408
  image.src = row.image;
409
  image.alt = `Line ${row.index + 1} of the region`;
410
  image.loading = "lazy";
411
  const verdict = make("span", "rv-verdict", row.verdict);
412
  verdict.dataset.kind = row.verdict.startsWith("Jev picks the right") ? "good" : row.verdict === "both right" ? "" : "bad";
413
- top.append(image, verdict);
414
  const truth = make("div", "rv-jr rv-truth");
415
  truth.append(make("span", "rv-k", "ground truth"), make("span", "rv-reading", row.truth));
416
- item.append(
417
- top,
418
  truth,
419
  reading("TrOCR", "trocr", row.trocr, row.pick === "trocr"),
420
  reading("MiniCPM-OCR", "plain", row.minicpm, row.pick === "minicpm"),
421
  );
 
422
  list.append(item);
423
  }
424
  box.querySelector(".rv-jnote").textContent =
 
201
  button.dataset.speed = String(choice.speed);
202
  controls.append(button);
203
  }
204
+ const timed = make("div", "rv-timed");
205
+ timed.append(track, controls);
206
+ const split = make("div", "rv-split");
207
+ split.append(timed, make("p", "rv-summary"));
208
+ live.append(make("h4", "rv-title", "Here, live: one stream each, in PyTorch on half a GPU"), split);
209
 
210
  const server = make("section", "rv-group rv-server");
211
  const serverTrack = make("div", "rv-track");
 
216
  const batchedTitle = "Everything batched, on our server: lines read a second";
217
  batched.append(make("h4", "rv-title", batchedTitle), make("div", "rv-tiles"), make("p", "rv-note"));
218
 
219
+ const pair = make("div", "rv-pair");
220
+ pair.append(server, batched);
221
  root.replaceChildren(
222
  make("div", "rv-notice"),
223
  make("div", "rv-status"),
224
  live,
225
  lanesOf(race),
226
+ pair,
 
227
  make("section", "rv-group rv-jev"),
228
  );
229
  return root;
 
233
  function elapsed(now) {
234
  if (!state.raced) return 0;
235
  if (still) return Infinity;
236
+ return Math.max(0, ((now - state.raceStart) / 1000) * state.speed); // a frame's clock can precede the start
237
  }
238
 
239
  function countUntil(times, at, from) {
 
332
  const box = root.querySelector(".rv-server");
333
  const server = race.server;
334
  box.hidden = !server;
335
+ root.querySelector(".rv-pair").classList.toggle("rv-single", !server);
336
  if (server) {
337
  box.querySelector(".rv-title").textContent =
338
  `The same region on our server: a whole ${server.gpu}, the reader served by vLLM`;
 
356
  ...rates.map((rate) => {
357
  const tile = make("div", "rv-tile");
358
  tile.dataset.lane = rate.lane;
359
+ const what = make("div", "rv-what");
360
+ what.append(make("div", "rv-tilename", rate.name), make("div", "rv-how", rate.how));
361
  const figure = make("div", "rv-rate", rate.per_second.toFixed(rate.per_second < 10 ? 1 : 0));
362
  figure.append(make("span", "rv-unit", " lines a second"));
363
+ tile.append(what, figure);
364
  return tile;
365
  }),
366
  );
 
398
  let list = box.querySelector(".rv-jlist");
399
  if (!list) {
400
  list = make("div", "rv-jlist");
401
+ const header = make("div", "rv-jr");
402
  for (const name of ["Reader", "Its reading of the line", "Right?", "Jev's score, 0 to 1", ""]) {
403
  header.append(make("span", "", name));
404
  }
405
+ const head = make("div", "rv-jrow rv-jhead");
406
+ head.append(make("div", "rv-jlinehead", "The line, and what Jev's scores amount to"), header);
407
  const fold = make("details", "rv-fold");
408
+ fold.append(make("summary", "rv-foldhead"), make("p", "rv-jintro", JEV_INTRO), head, list);
409
  box.replaceChildren(make("h4", "rv-title", "Jev, the reader's proof-reader"), make("p", "rv-jnote"), fold);
410
  }
411
  for (let i = list.childElementCount; i < rows.length; i += 1) {
412
  const row = rows[i];
413
  const item = make("div", "rv-jrow");
414
  item.dataset.line = String(row.index);
415
+ const line = make("div", "rv-jline");
416
  const image = make("img", "rv-jimg");
417
  image.src = row.image;
418
  image.alt = `Line ${row.index + 1} of the region`;
419
  image.loading = "lazy";
420
  const verdict = make("span", "rv-verdict", row.verdict);
421
  verdict.dataset.kind = row.verdict.startsWith("Jev picks the right") ? "good" : row.verdict === "both right" ? "" : "bad";
422
+ line.append(image, verdict);
423
  const truth = make("div", "rv-jr rv-truth");
424
  truth.append(make("span", "rv-k", "ground truth"), make("span", "rv-reading", row.truth));
425
+ const readings = make("div", "rv-jreads");
426
+ readings.append(
427
  truth,
428
  reading("TrOCR", "trocr", row.trocr, row.pick === "trocr"),
429
  reading("MiniCPM-OCR", "plain", row.minicpm, row.pick === "minicpm"),
430
  );
431
+ item.append(line, readings);
432
  list.append(item);
433
  }
434
  box.querySelector(".rv-jnote").textContent =