Gabriel commited on
Commit
dceb361
·
verified ·
1 Parent(s): 5041a61

Jev in one fold titled with its verdict (picked the reading with fewer errors on K of M lines); each reading's errors and Jev's certainty in percent

Browse files
Files changed (4) hide show
  1. HOW_IT_WORKS.md +7 -5
  2. app.py +21 -17
  3. race_view.css +4 -4
  4. race_view.js +31 -38
HOW_IT_WORKS.md CHANGED
@@ -99,11 +99,13 @@ vLLM only.
99
 
100
  Jev compares a line's image with a candidate reading drawn as an image (`@RENDERER@`) and returns a score from 0 to 1.
101
  Here each line is read on its own by TrOCR and by the MiniCPM reader, and Jev scores both; keeping, line by line, the
102
- reading Jev scores higher is one way to combine two readers. Under the race the page says on how many lines where one
103
- reading was better (fewer wrong characters) Jev picked that one; the bars give the CER over the region of each reader,
104
- of Jev's picks, and of perfect picks (the better reading on every line, the most any chooser could get). The table
105
- has one row per line: the characters that differ from the ground truth are marked, each reading carries Jev's score,
106
- Jev's pick is shaded; click a row to see the line.
 
 
107
 
108
  Jev catches gross mismatches well and near-misses badly. On 2,000 test pairs it tells the correct text from rule-made
109
  wrong candidates (dropped or swapped words, another line's text) with an AUROC of 0.988, but from the reader's own
 
99
 
100
  Jev compares a line's image with a candidate reading drawn as an image (`@RENDERER@`) and returns a score from 0 to 1.
101
  Here each line is read on its own by TrOCR and by the MiniCPM reader, and Jev scores both; keeping, line by line, the
102
+ reading Jev scores higher is one way to combine two readers. A reading's errors are the characters it adds, drops or
103
+ gets wrong against the ground truth. The fold under the race is titled with what Jev did: on how many of the lines
104
+ where one reading has fewer errors it picked that one. Inside, the character error rate over the region of each
105
+ reader, of Jev's picks, and of the best possible picks (the reading with fewer errors on every line, the most any
106
+ chooser could get); then one row per line: each reading with its wrong characters marked, its errors and Jev's score
107
+ in percent, Jev's pick shaded and marked ✓ when it has fewer errors than the other reading, ✗ when it has more. Click
108
+ a row to see the line.
109
 
110
  Jev catches gross mismatches well and near-misses badly. On 2,000 test pairs it tells the correct text from rule-made
111
  wrong candidates (dropped or swapped words, another line's text) with an AUROC of 0.988, but from the reader's own
app.py CHANGED
@@ -145,23 +145,21 @@ class Rate(BaseModel):
145
 
146
 
147
  class JevReading(BaseModel):
148
- """One reader's reading of a line: its text in stretches, each with whether it agrees with the ground truth, its
149
- CER, Jev's score, and whether the whole reading is exactly the ground truth."""
 
150
 
 
151
  marks: list[tuple[str, bool]]
152
- cer: float
153
  score: float
154
  exact: bool
155
 
156
- @property
157
- def text(self) -> str:
158
- return "".join(piece for piece, _ in self.marks)
159
-
160
 
161
  class JevRow(BaseModel):
162
  """One line: its crop, its ground truth, TrOCR's reading and the reader's (each of the line on its own) with Jev's
163
- scores; the reading Jev picks ("" where both read the same) and the better one, with fewer wrong characters (""
164
- where neither is)."""
165
 
166
  index: int
167
  image: str
@@ -173,9 +171,9 @@ class JevRow(BaseModel):
173
 
174
 
175
  class JevTally(BaseModel):
176
- """What Jev did on a region's lines: where the two readings differ, where one of them is better (fewer wrong
177
- characters) and where Jev picked that one; and the CER of each reader, of Jev's picks, and of the better reading
178
- on every line (perfect picks)."""
179
 
180
  lines: int
181
  differ: int
@@ -251,9 +249,13 @@ def _example(example_id: str) -> dict[str, Any]:
251
  return next(e for e in EXAMPLES if e["id"] == example_id)
252
 
253
 
 
 
 
 
 
254
  def _cer(prediction: str, truth: str) -> float:
255
- prediction, truth = " ".join(prediction.split()), " ".join(truth.split())
256
- return Levenshtein.distance(prediction, truth) / max(1, len(truth))
257
 
258
 
259
  def _same(prediction: str, truth: str) -> bool:
@@ -436,21 +438,23 @@ def _marks(reading: str, truth: str) -> list[tuple[str, bool]]:
436
 
437
  def _jev_reading(crop: Image.Image, reading: str, truth: str) -> JevReading:
438
  return JevReading(
 
439
  marks=_marks(reading, truth),
440
- cer=_cer(reading, truth),
441
  score=_jev_score(crop, reading),
442
  exact=_same(reading, truth),
443
  )
444
 
445
 
446
  def _jev_row(index: int, image: str, truth: str, crop: Image.Image, guess: str, own: str) -> JevRow:
447
- """A line's row: Jev's score of TrOCR's reading and of the reader's, the one it picks and the better one."""
 
448
  first = _jev_reading(crop, guess, truth)
449
  if _same(guess, own):
450
  return JevRow(index=index, image=image, truth=truth, trocr=first, minicpm=first, kept="", better="")
451
  second = _jev_reading(crop, own, truth)
452
  kept = "minicpm" if second.score > first.score else "trocr"
453
- better = "" if first.cer == second.cer else ("trocr" if first.cer < second.cer else "minicpm")
454
  return JevRow(index=index, image=image, truth=truth, trocr=first, minicpm=second, kept=kept, better=better)
455
 
456
 
 
145
 
146
 
147
  class JevReading(BaseModel):
148
+ """One reader's reading of a line: its text; the text in stretches, each with whether it agrees with the ground
149
+ truth; its errors (characters added, dropped or changed against the ground truth); Jev's score (how sure Jev is,
150
+ 0 to 1, that the reading is exactly the line); and whether it is exactly the ground truth."""
151
 
152
+ text: str
153
  marks: list[tuple[str, bool]]
154
+ errors: int
155
  score: float
156
  exact: bool
157
 
 
 
 
 
158
 
159
  class JevRow(BaseModel):
160
  """One line: its crop, its ground truth, TrOCR's reading and the reader's (each of the line on its own) with Jev's
161
+ scores; the reading Jev picks ("" where both read the same) and the one with fewer errors ("" where they have as
162
+ many)."""
163
 
164
  index: int
165
  image: str
 
171
 
172
 
173
  class JevTally(BaseModel):
174
+ """What Jev did on a region's lines: where the two readings differ, where one of them has fewer errors and where
175
+ Jev picked that one; and the CER of each reader, of Jev's picks, and of the reading with fewer errors on every
176
+ line (the best possible picks)."""
177
 
178
  lines: int
179
  differ: int
 
249
  return next(e for e in EXAMPLES if e["id"] == example_id)
250
 
251
 
252
+ def _errors(prediction: str, truth: str) -> int:
253
+ """Characters added, dropped or changed against the ground truth, runs of spaces counted as one."""
254
+ return Levenshtein.distance(" ".join(prediction.split()), " ".join(truth.split()))
255
+
256
+
257
  def _cer(prediction: str, truth: str) -> float:
258
+ return _errors(prediction, truth) / max(1, len(" ".join(truth.split())))
 
259
 
260
 
261
  def _same(prediction: str, truth: str) -> bool:
 
438
 
439
  def _jev_reading(crop: Image.Image, reading: str, truth: str) -> JevReading:
440
  return JevReading(
441
+ text=reading,
442
  marks=_marks(reading, truth),
443
+ errors=_errors(reading, truth),
444
  score=_jev_score(crop, reading),
445
  exact=_same(reading, truth),
446
  )
447
 
448
 
449
  def _jev_row(index: int, image: str, truth: str, crop: Image.Image, guess: str, own: str) -> JevRow:
450
+ """A line's row: Jev's score of TrOCR's reading and of the reader's, the one it picks and the one with fewer
451
+ errors."""
452
  first = _jev_reading(crop, guess, truth)
453
  if _same(guess, own):
454
  return JevRow(index=index, image=image, truth=truth, trocr=first, minicpm=first, kept="", better="")
455
  second = _jev_reading(crop, own, truth)
456
  kept = "minicpm" if second.score > first.score else "trocr"
457
+ better = "" if first.errors == second.errors else ("trocr" if first.errors < second.errors else "minicpm")
458
  return JevRow(index=index, image=image, truth=truth, trocr=first, minicpm=second, kept=kept, better=better)
459
 
460
 
race_view.css CHANGED
@@ -137,9 +137,8 @@
137
  .rv-text .rv-draft { animation: rv-flash 0.5s ease-out; }
138
  .rv-own { text-decoration: underline; text-decoration-color: var(--rv-dspark); text-decoration-thickness: 2px; text-underline-offset: 3px; }
139
 
140
- /* Jev: what it did on the region in a sentence and two charts, then the lines, one row each, folded away */
141
- .rv-jnote { margin: 0; max-width: 110ch; font-weight: 600; }
142
- .rv-tallies { max-width: 620px; }
143
  .rv-tally { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
144
  .rv-tallyhead { font-size: 13px; font-weight: 600; }
145
  .rv-trow { display: grid; grid-template-columns: 120px minmax(0, 1fr) 56px; gap: 10px; align-items: center; font-size: 13px; }
@@ -150,7 +149,8 @@
150
  box-shadow: inset 0 0 0 1px var(--rv-lane);
151
  }
152
  .rv-fold { border: 1px solid var(--border-color-primary); border-radius: 6px; padding: 0 12px; }
153
- .rv-foldhead { padding: 8px 0; cursor: pointer; font-weight: 600; }
 
154
  .rv-fold[open] .rv-foldhead { border-bottom: 1px solid var(--border-color-primary); }
155
  .rv-jlist { display: flex; flex-direction: column; }
156
  .rv-jt {
 
137
  .rv-text .rv-draft { animation: rv-flash 0.5s ease-out; }
138
  .rv-own { text-decoration: underline; text-decoration-color: var(--rv-dspark); text-decoration-thickness: 2px; text-underline-offset: 3px; }
139
 
140
+ /* Jev, folded away: its verdict on the region as the fold's title; inside, how to read it, the CER bars, a row a line */
141
+ .rv-tallies { max-width: 660px; margin: 4px 0 6px; }
 
142
  .rv-tally { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
143
  .rv-tallyhead { font-size: 13px; font-weight: 600; }
144
  .rv-trow { display: grid; grid-template-columns: 120px minmax(0, 1fr) 56px; gap: 10px; align-items: center; font-size: 13px; }
 
149
  box-shadow: inset 0 0 0 1px var(--rv-lane);
150
  }
151
  .rv-fold { border: 1px solid var(--border-color-primary); border-radius: 6px; padding: 0 12px; }
152
+ .rv-fold > .rv-note { margin: 10px 0 6px; }
153
+ .rv-foldhead { padding: 9px 0; cursor: pointer; font-size: 15px; font-weight: 600; }
154
  .rv-fold[open] .rv-foldhead { border-bottom: 1px solid var(--border-color-primary); }
155
  .rv-jlist { display: flex; flex-direction: column; }
156
  .rv-jt {
race_view.js CHANGED
@@ -29,16 +29,17 @@ const SPEEDS = [
29
  { label: "¼ speed", speed: 0.25 },
30
  ];
31
  const JEV_HOW =
32
- "Both readers read each line on its own. Jev looks at the line and at a reading drawn as an image, and scores " +
33
- "0 to 1 how sure it is that the reading is exactly the line; where the two readings differ, it picks the one it " +
34
- "scores higher. The better reading is the one with fewer wrong characters. Jev tells a plainly wrong text from " +
35
- "the right one far more reliably than one a letter or two off.";
36
  const TALLY = [
37
  { key: "trocr", lane: "trocr", name: "TrOCR" },
38
  { key: "minicpm", lane: "plain", name: "MiniCPM-OCR" },
39
  { key: "kept", lane: "kept", name: "Jev's picks" },
40
- { key: "best", lane: "best", name: "Perfect picks" },
41
  ];
 
42
  const still = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
43
  const state = { run: null, raced: false, speed: 1, raceStart: 0, frame: 0 };
44
  const derived = new WeakMap(); // per lane payload: what drawing needs, worked out once
@@ -372,29 +373,30 @@ function drawServer(root, race, scale) {
372
  batched.querySelector(".rv-note").textContent = race.batched_note || "";
373
  }
374
 
375
- // A reading in a table cell: its text, the characters that differ from the ground truth marked, and Jev's score.
376
- function cell(data, kept, both) {
377
- const box = make("div", kept ? "rv-jc rv-kept" : "rv-jc");
378
  const text = make("span", "rv-reading");
379
  for (const [piece, agrees] of data.marks) text.append(make("span", agrees ? "" : "rv-bad", piece));
380
  if (!data.marks.length) text.textContent = "(empty)";
381
- const score = make("span", "rv-score", data.score.toFixed(2));
382
- score.title = "Jev's score: how sure it is that this reading is exactly the line";
383
- box.append(text, score);
384
- if (both) box.append(make("span", "rv-k", "both"));
385
  return box;
386
  }
387
 
388
  function outcome(row) {
389
- if (!row.kept) return ["same reading", ""];
390
- if (!row.better) return ["tie", ""];
391
- return row.kept === row.better ? ["✓ better", "good"] : ["✗ worse", "bad"];
392
  }
393
 
394
- // CER bars: each reader, Jev's picks, and perfect picks (the better reading of every line).
395
- function tallyChart(title, tally, scale) {
396
  const box = make("div", "rv-tally");
397
- box.append(make("div", "rv-tallyhead", title));
 
398
  for (const bar of TALLY) {
399
  const row = make("div", "rv-trow");
400
  row.dataset.lane = bar.lane;
@@ -405,13 +407,14 @@ function tallyChart(title, tally, scale) {
405
  row.append(make("span", "", bar.name), track, make("span", "rv-time", `${(100 * tally[bar.key]).toFixed(1)} %`));
406
  box.append(row);
407
  }
 
408
  return box;
409
  }
410
 
411
  function verdict(tally) {
412
- const lines = `the two readings differ on ${tally.differ} of the region's ${tally.lines} lines`;
413
- if (!tally.one_better) return `No pick to judge: ${lines}, and on none is one better than the other.`;
414
- return `Jev picked the better reading on ${tally.kept_better} of the ${tally.one_better} lines where one was better (${lines}).`;
415
  }
416
 
417
  function drawJev(root, race) {
@@ -423,26 +426,17 @@ function drawJev(root, race) {
423
  if (!list) {
424
  list = make("div", "rv-jlist");
425
  const header = make("div", "rv-jt rv-jthead");
426
- for (const name of ["", "Ground truth", "TrOCR", "MiniCPM-OCR", "Jev's pick"]) header.append(make("span", "", name));
427
  const fold = make("details", "rv-fold");
428
- fold.append(make("summary", "rv-foldhead"), header, list);
429
- box.replaceChildren(
430
- make("h4", "rv-title", "Jev, the reader's proof-reader"),
431
- make("p", "rv-jnote"),
432
- make("div", "rv-tallies"),
433
- make("p", "rv-note", JEV_HOW),
434
- fold,
435
- );
436
  }
437
  const tally = race.jev_tally;
438
- box.querySelector(".rv-jnote").textContent = tally
439
- ? verdict(tally)
440
- : `Jev has scored ${rows.length} of ${race.jev_total} lines…`;
441
  const tallies = box.querySelector(".rv-tallies");
442
- if (tally && !tallies.childElementCount) {
443
- const scale = Math.max(0.001, ...TALLY.map((bar) => tally[bar.key]));
444
- tallies.append(tallyChart("CER over the region, each line read on its own", tally, scale));
445
- }
446
  for (let i = list.childElementCount; i < rows.length; i += 1) {
447
  const row = rows[i];
448
  const item = make("div", "rv-jt");
@@ -470,7 +464,6 @@ function drawJev(root, race) {
470
  });
471
  list.append(item);
472
  }
473
- box.querySelector(".rv-foldhead").textContent = `Line by line (${rows.length} of ${race.jev_total} lines)`;
474
  }
475
 
476
  function draw(now) {
 
29
  { label: "¼ speed", speed: 0.25 },
30
  ];
31
  const JEV_HOW =
32
+ "Both readers read each line on its own. A reading's errors are the characters it adds, drops or gets wrong " +
33
+ "against the ground truth (marked in its text). Jev looks at the line and at the reading drawn as an image and " +
34
+ "says how sure it is, from 0 to 100 %, that the reading is exactly what the line says; where the two readings " +
35
+ "differ it picks the one it is surer of. ✓: the reading it picked has fewer errors than the other. ✗: more.";
36
  const TALLY = [
37
  { key: "trocr", lane: "trocr", name: "TrOCR" },
38
  { key: "minicpm", lane: "plain", name: "MiniCPM-OCR" },
39
  { key: "kept", lane: "kept", name: "Jev's picks" },
40
+ { key: "best", lane: "best", name: "Best possible picks" },
41
  ];
42
+ const NAMES = { trocr: "TrOCR", minicpm: "MiniCPM-OCR" };
43
  const still = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
44
  const state = { run: null, raced: false, speed: 1, raceStart: 0, frame: 0 };
45
  const derived = new WeakMap(); // per lane payload: what drawing needs, worked out once
 
373
  batched.querySelector(".rv-note").textContent = race.batched_note || "";
374
  }
375
 
376
+ // A reading in a table cell: its text with the wrong characters marked, its errors and how sure Jev is of it.
377
+ function cell(data, picked, both) {
378
+ const box = make("div", picked ? "rv-jc rv-kept" : "rv-jc");
379
  const text = make("span", "rv-reading");
380
  for (const [piece, agrees] of data.marks) text.append(make("span", agrees ? "" : "rv-bad", piece));
381
  if (!data.marks.length) text.textContent = "(empty)";
382
+ const errors = data.errors === 1 ? "1 error" : `${data.errors} errors`;
383
+ const facts = make("span", "rv-score", `${both ? "both · " : ""}${errors} · Jev ${Math.round(100 * data.score)} %`);
384
+ facts.title = "Jev's score: how sure Jev is that this reading is exactly what the line says";
385
+ box.append(text, facts);
386
  return box;
387
  }
388
 
389
  function outcome(row) {
390
+ if (!row.kept) return ["same text", ""];
391
+ if (!row.better) return ["either: as many errors", ""];
392
+ return row.kept === row.better ? [`${NAMES[row.kept]} ✓`, "good"] : [`${NAMES[row.kept]} ✗`, "bad"];
393
  }
394
 
395
+ // Character error rates over the region: each reader, Jev's picks, and the best possible picks.
396
+ function tallyChart(tally) {
397
  const box = make("div", "rv-tally");
398
+ box.append(make("div", "rv-tallyhead", "Character error rate over the region, each line read on its own (lower is better)"));
399
+ const scale = Math.max(0.001, ...TALLY.map((bar) => tally[bar.key]));
400
  for (const bar of TALLY) {
401
  const row = make("div", "rv-trow");
402
  row.dataset.lane = bar.lane;
 
407
  row.append(make("span", "", bar.name), track, make("span", "rv-time", `${(100 * tally[bar.key]).toFixed(1)} %`));
408
  box.append(row);
409
  }
410
+ box.append(make("div", "rv-k", "Best possible picks: on every line, the reading with fewer errors."));
411
  return box;
412
  }
413
 
414
  function verdict(tally) {
415
+ if (!tally.differ) return "both readers read every line the same: nothing to pick";
416
+ if (!tally.one_better) return `the readings differ on ${tally.differ} lines, always with as many errors: nothing to pick`;
417
+ return `picked the reading with fewer errors on ${tally.kept_better} of ${tally.one_better} lines`;
418
  }
419
 
420
  function drawJev(root, race) {
 
426
  if (!list) {
427
  list = make("div", "rv-jlist");
428
  const header = make("div", "rv-jt rv-jthead");
429
+ for (const name of ["", "Ground truth", "TrOCR", "MiniCPM-OCR", "Jev picked"]) header.append(make("span", "", name));
430
  const fold = make("details", "rv-fold");
431
+ fold.append(make("summary", "rv-foldhead"), make("p", "rv-note", JEV_HOW), make("div", "rv-tallies"), header, list);
432
+ box.replaceChildren(fold);
 
 
 
 
 
 
433
  }
434
  const tally = race.jev_tally;
435
+ box.querySelector(".rv-foldhead").textContent =
436
+ "Jev, the reader's proof-reader: " +
437
+ (tally ? verdict(tally) : `scoring line ${Math.min(rows.length + 1, race.jev_total)} of ${race.jev_total}…`);
438
  const tallies = box.querySelector(".rv-tallies");
439
+ if (tally && !tallies.childElementCount) tallies.append(tallyChart(tally));
 
 
 
440
  for (let i = list.childElementCount; i < rows.length; i += 1) {
441
  const row = rows[i];
442
  const item = make("div", "rv-jt");
 
464
  });
465
  list.append(item);
466
  }
 
467
  }
468
 
469
  function draw(now) {