Replace test/small_representative with clean v2 shards only
Browse files- README.md +27 -55
- metrics.py +185 -5
- small_representative/small_representative-00000-of-00001.parquet +2 -2
- test/{test-00000-of-00004.parquet → test-00000-of-00002.parquet} +2 -2
- test/{test-00001-of-00004.parquet → test-00001-of-00002.parquet} +2 -2
- test/test-00002-of-00004.parquet +0 -3
- test/test-00003-of-00004.parquet +0 -3
README.md
CHANGED
|
@@ -41,15 +41,11 @@ configs:
|
|
| 41 |
path: test/*
|
| 42 |
- split: small_representative
|
| 43 |
path: small_representative/*
|
| 44 |
-
- config_name: top10-v1.1
|
| 45 |
-
data_files:
|
| 46 |
-
- split: test
|
| 47 |
-
path: top10-v1.1/*
|
| 48 |
---
|
| 49 |
|
| 50 |
# Sarvam Indic OCR Bench
|
| 51 |
|
| 52 |
-
Global benchmarks focus heavily on English document parsing, and to the best of our knowledge there is no Indic OCR benchmark of comparable breadth and rigor. **Sarvam Indic OCR Bench** fills this gap with **
|
| 53 |
|
| 54 |
The benchmark covers **23 languages**: all **22 languages listed in the Eighth Schedule of the Indian Constitution**, plus **English**. Samples are curated at the **semantic block level** so that models are evaluated on coherent units of text rather than full noisy pages. Each block includes a document image and its corresponding ground-truth transcription.
|
| 55 |
|
|
@@ -59,34 +55,10 @@ All ground-truth text has been **reviewed twice by human language experts** to e
|
|
| 59 |
|
| 60 |
| Split | Samples | Description |
|
| 61 |
| --- | ---: | --- |
|
| 62 |
-
| `test` |
|
| 63 |
| `small_representative` | 1,173 | ~51 samples per language; stratified by word length for quick sanity checks |
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
Besides the `default` config above, the dataset provides additional named subsets:
|
| 68 |
-
|
| 69 |
-
| Subset (config) | Split | Samples | Languages | Description |
|
| 70 |
-
| --- | --- | ---: | ---: | --- |
|
| 71 |
-
| `top10-v1.1` | `test` | 8,619 | 11 | Expert-corrected text blocks across 11 languages (LaTeX/math pages excluded) |
|
| 72 |
-
|
| 73 |
-
`top10-v1.1` language distribution: Bengali (790), English (713), Gujarati (693), Hindi (812), Kannada (813), Malayalam (831), Marathi (787), Odia (793), Punjabi (843), Tamil (799), Telugu (745).
|
| 74 |
-
|
| 75 |
-
The `top10-v1.1` subset uses **cleaned, URL-safe `image_name`s** (lowercased, ASCII-only, transliterated from Indic scripts where needed) and adds two extra columns on top of the base schema:
|
| 76 |
-
|
| 77 |
-
| Extra field | Type | Description |
|
| 78 |
-
| --- | --- | --- |
|
| 79 |
-
| `original_image_name` | string | The original (pre-cleanup) identifier, for traceability |
|
| 80 |
-
| `block_type` | string | Block category parsed from the name (e.g. `paragraph`, `unordered-list`, `figure-caption`); `null` when not derivable |
|
| 81 |
-
|
| 82 |
-
```python
|
| 83 |
-
from datasets import load_dataset
|
| 84 |
-
|
| 85 |
-
top10 = load_dataset("sarvam/indic-ocr-bench", "top10-v1.1", split="test")
|
| 86 |
-
print(top10[0]["image_name"], top10[0]["block_type"])
|
| 87 |
-
```
|
| 88 |
-
|
| 89 |
-
Each record in the `default` config contains:
|
| 90 |
|
| 91 |
| Field | Type | Description |
|
| 92 |
| --- | --- | --- |
|
|
@@ -101,30 +73,30 @@ Samples are balanced as evenly as possible across languages, subject to the avai
|
|
| 101 |
|
| 102 |
| Language | ISO 639-3 | Samples |
|
| 103 |
| --- | --- | ---: |
|
| 104 |
-
| Assamese | `asm` |
|
| 105 |
-
| Bengali | `ben` |
|
| 106 |
-
| Bodo | `brx` |
|
| 107 |
-
| Dogri | `doi` |
|
| 108 |
-
| English | `eng` |
|
| 109 |
-
| Gujarati | `guj` |
|
| 110 |
-
| Hindi | `hin` |
|
| 111 |
-
| Kannada | `kan` |
|
| 112 |
-
| Kashmiri | `kas` |
|
| 113 |
-
| Konkani | `kok` |
|
| 114 |
-
| Maithili | `mai` |
|
| 115 |
-
| Malayalam | `mal` |
|
| 116 |
-
| Manipuri | `mni` |
|
| 117 |
-
| Marathi | `mar` |
|
| 118 |
-
| Nepali | `nep` |
|
| 119 |
-
| Odia | `ori` |
|
| 120 |
-
| Punjabi | `pan` |
|
| 121 |
-
| Sanskrit | `san` |
|
| 122 |
-
| Santhali | `sat` |
|
| 123 |
-
| Sindhi | `snd` |
|
| 124 |
-
| Tamil | `tam` |
|
| 125 |
-
| Telugu | `tel` |
|
| 126 |
-
| Urdu | `urd` |
|
| 127 |
-
| **Total** | | **
|
| 128 |
|
| 129 |
## Evaluation
|
| 130 |
|
|
|
|
| 41 |
path: test/*
|
| 42 |
- split: small_representative
|
| 43 |
path: small_representative/*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
---
|
| 45 |
|
| 46 |
# Sarvam Indic OCR Bench
|
| 47 |
|
| 48 |
+
Global benchmarks focus heavily on English document parsing, and to the best of our knowledge there is no Indic OCR benchmark of comparable breadth and rigor. **Sarvam Indic OCR Bench** fills this gap with **6,909** curated text-block samples drawn from document pages spanning the **19th century to the present**, across a wide range of scan quality and content types—including textbooks, newspapers, magazines, and other published material.
|
| 49 |
|
| 50 |
The benchmark covers **23 languages**: all **22 languages listed in the Eighth Schedule of the Indian Constitution**, plus **English**. Samples are curated at the **semantic block level** so that models are evaluated on coherent units of text rather than full noisy pages. Each block includes a document image and its corresponding ground-truth transcription.
|
| 51 |
|
|
|
|
| 55 |
|
| 56 |
| Split | Samples | Description |
|
| 57 |
| --- | ---: | --- |
|
| 58 |
+
| `test` | 6,909 | Full evaluation set |
|
| 59 |
| `small_representative` | 1,173 | ~51 samples per language; stratified by word length for quick sanity checks |
|
| 60 |
|
| 61 |
+
Each record contains:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
| Field | Type | Description |
|
| 64 |
| --- | --- | --- |
|
|
|
|
| 73 |
|
| 74 |
| Language | ISO 639-3 | Samples |
|
| 75 |
| --- | --- | ---: |
|
| 76 |
+
| Assamese | `asm` | 471 |
|
| 77 |
+
| Bengali | `ben` | 271 |
|
| 78 |
+
| Bodo | `brx` | 238 |
|
| 79 |
+
| Dogri | `doi` | 319 |
|
| 80 |
+
| English | `eng` | 300 |
|
| 81 |
+
| Gujarati | `guj` | 300 |
|
| 82 |
+
| Hindi | `hin` | 228 |
|
| 83 |
+
| Kannada | `kan` | 300 |
|
| 84 |
+
| Kashmiri | `kas` | 262 |
|
| 85 |
+
| Konkani | `kok` | 266 |
|
| 86 |
+
| Maithili | `mai` | 279 |
|
| 87 |
+
| Malayalam | `mal` | 300 |
|
| 88 |
+
| Manipuri | `mni` | 207 |
|
| 89 |
+
| Marathi | `mar` | 300 |
|
| 90 |
+
| Nepali | `nep` | 437 |
|
| 91 |
+
| Odia | `ori` | 300 |
|
| 92 |
+
| Punjabi | `pan` | 300 |
|
| 93 |
+
| Sanskrit | `san` | 325 |
|
| 94 |
+
| Santhali | `sat` | 274 |
|
| 95 |
+
| Sindhi | `snd` | 248 |
|
| 96 |
+
| Tamil | `tam` | 299 |
|
| 97 |
+
| Telugu | `tel` | 300 |
|
| 98 |
+
| Urdu | `urd` | 385 |
|
| 99 |
+
| **Total** | | **6,909** |
|
| 100 |
|
| 101 |
## Evaluation
|
| 102 |
|
metrics.py
CHANGED
|
@@ -9,6 +9,9 @@ With ``--normalize`` the pipeline applies the base normalization plus the
|
|
| 9 |
quotes, asterisks, bullets/list markers, ZWJ/ZWNJ, filler rules, and cosmetic
|
| 10 |
spacing are stripped so CER/WER reflect word/character OCR errors).
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
Input format (list of objects):
|
| 13 |
|
| 14 |
[
|
|
@@ -366,7 +369,22 @@ def strip_list_dash_markers(text: str) -> str:
|
|
| 366 |
return re.sub(r"\s+", " ", text).strip()
|
| 367 |
|
| 368 |
|
| 369 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
if normalize:
|
| 371 |
# Existing normalization, then the merged content-normalization folds.
|
| 372 |
gt = normalize_for_metrics(gt)
|
|
@@ -425,6 +443,25 @@ def detect_tail_char_loop(text: str, min_repeats: int = 8) -> bool:
|
|
| 425 |
return False
|
| 426 |
|
| 427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
def detect_tail_loop(pred: str) -> bool:
|
| 429 |
if ERROR_PRED_RE.match(pred or ""):
|
| 430 |
return True
|
|
@@ -432,7 +469,11 @@ def detect_tail_loop(pred: str) -> bool:
|
|
| 432 |
return True
|
| 433 |
if detect_tail_char_loop(pred):
|
| 434 |
return True
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
return True
|
| 437 |
return False
|
| 438 |
|
|
@@ -462,12 +503,21 @@ def is_loop_or_catastrophic(gt: str, pred: str) -> tuple[bool, list[str]]:
|
|
| 462 |
# ---------------------------------------------------------------------------
|
| 463 |
|
| 464 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
def edit_distance(a: list, b: list) -> int:
|
| 466 |
if not a:
|
| 467 |
return len(b)
|
| 468 |
if not b:
|
| 469 |
return len(a)
|
| 470 |
|
|
|
|
|
|
|
|
|
|
| 471 |
prev = list(range(len(b) + 1))
|
| 472 |
for i, ai in enumerate(a, start=1):
|
| 473 |
curr = [i]
|
|
@@ -533,7 +583,16 @@ def load_gt_pred_rows(path: str) -> list[dict]:
|
|
| 533 |
gt = item.get("ground_truth")
|
| 534 |
pred = item.get("pred")
|
| 535 |
if pred is None:
|
| 536 |
-
pred = item.get("prediction"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
language = item.get("language") or item.get("lang")
|
| 538 |
|
| 539 |
if not image_name or gt is None:
|
|
@@ -559,12 +618,24 @@ def is_missing_prediction(pred: str) -> bool:
|
|
| 559 |
return not (pred or "").strip()
|
| 560 |
|
| 561 |
|
| 562 |
-
def default_output_path(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
stem = input_path.replace(".json", "")
|
|
|
|
|
|
|
| 564 |
if normalize:
|
| 565 |
return f"{stem}.metrics.normalized.json"
|
|
|
|
|
|
|
| 566 |
if replace_n:
|
| 567 |
return f"{stem}.metrics.replace_n.json"
|
|
|
|
|
|
|
| 568 |
return f"{stem}.metrics.json"
|
| 569 |
|
| 570 |
|
|
@@ -591,6 +662,7 @@ def compute_metrics(
|
|
| 591 |
*,
|
| 592 |
normalize: bool,
|
| 593 |
replace_n: bool,
|
|
|
|
| 594 |
) -> tuple[dict, list[dict]]:
|
| 595 |
results: list[dict] = []
|
| 596 |
loop_count = 0
|
|
@@ -618,7 +690,13 @@ def compute_metrics(
|
|
| 618 |
)
|
| 619 |
continue
|
| 620 |
|
| 621 |
-
gt, pred = preprocess(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
is_bad, failure_reasons = is_loop_or_catastrophic(gt_raw, pred_raw)
|
| 624 |
if is_bad:
|
|
@@ -675,6 +753,70 @@ def compute_metrics(
|
|
| 675 |
return summary, results
|
| 676 |
|
| 677 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
def main() -> None:
|
| 679 |
parser = argparse.ArgumentParser(
|
| 680 |
description="Compute CER/WER from a single gt+pred JSON file (standalone)",
|
|
@@ -702,17 +844,39 @@ def main() -> None:
|
|
| 702 |
action="store_true",
|
| 703 |
help="Only flatten newlines before scoring",
|
| 704 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
parser.add_argument(
|
| 706 |
"--overwrite",
|
| 707 |
action="store_true",
|
| 708 |
help="Overwrite output if it exists",
|
| 709 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
args = parser.parse_args()
|
| 711 |
|
| 712 |
output_path = args.output or default_output_path(
|
| 713 |
args.input,
|
| 714 |
normalize=args.normalize,
|
| 715 |
replace_n=args.replace_n,
|
|
|
|
| 716 |
)
|
| 717 |
if os.path.exists(output_path) and not args.overwrite:
|
| 718 |
print(f"Output exists: {output_path}. Use --overwrite to replace.")
|
|
@@ -723,6 +887,7 @@ def main() -> None:
|
|
| 723 |
rows,
|
| 724 |
normalize=args.normalize,
|
| 725 |
replace_n=args.replace_n,
|
|
|
|
| 726 |
)
|
| 727 |
|
| 728 |
with open(output_path, "w", encoding="utf-8") as f:
|
|
@@ -750,6 +915,21 @@ def main() -> None:
|
|
| 750 |
|
| 751 |
print(f"\nSaved to {output_path}")
|
| 752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
if __name__ == "__main__":
|
| 755 |
main()
|
|
|
|
| 9 |
quotes, asterisks, bullets/list markers, ZWJ/ZWNJ, filler rules, and cosmetic
|
| 10 |
spacing are stripped so CER/WER reflect word/character OCR errors).
|
| 11 |
|
| 12 |
+
With ``--remove-br`` any ``<br>`` / ``<br/>`` / ``<br />`` tags are replaced
|
| 13 |
+
with a space in both gt and pred before the rest of the pipeline.
|
| 14 |
+
|
| 15 |
Input format (list of objects):
|
| 16 |
|
| 17 |
[
|
|
|
|
| 369 |
return re.sub(r"\s+", " ", text).strip()
|
| 370 |
|
| 371 |
|
| 372 |
+
def remove_br_tags(text: str) -> str:
|
| 373 |
+
"""Replace ``<br>`` / ``<br/>`` / ``<br />`` (any case) with a space."""
|
| 374 |
+
return re.sub(r"<br\s*/?>", " ", text or "", flags=re.IGNORECASE)
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def preprocess(
|
| 378 |
+
gt: str,
|
| 379 |
+
pred: str,
|
| 380 |
+
*,
|
| 381 |
+
normalize: bool,
|
| 382 |
+
replace_n: bool,
|
| 383 |
+
remove_br: bool = False,
|
| 384 |
+
) -> tuple[str, str]:
|
| 385 |
+
if remove_br:
|
| 386 |
+
gt = remove_br_tags(gt)
|
| 387 |
+
pred = remove_br_tags(pred)
|
| 388 |
if normalize:
|
| 389 |
# Existing normalization, then the merged content-normalization folds.
|
| 390 |
gt = normalize_for_metrics(gt)
|
|
|
|
| 443 |
return False
|
| 444 |
|
| 445 |
|
| 446 |
+
def detect_tail_block_loop(text: str, min_block: int = 12, min_repeats: int = 3,
|
| 447 |
+
max_block: int = 500) -> bool:
|
| 448 |
+
"""Detect a block (>= min_block chars) repeated >= min_repeats times at the end.
|
| 449 |
+
|
| 450 |
+
Non-regex replacement for the old ``(.{12,}?)(?:\\s*\\1){2,}$`` pattern, which
|
| 451 |
+
caused catastrophic backtracking on very long repetitive loop predictions.
|
| 452 |
+
"""
|
| 453 |
+
text = text or ""
|
| 454 |
+
n = len(text)
|
| 455 |
+
if n < min_block * min_repeats:
|
| 456 |
+
return False
|
| 457 |
+
upper = min(max_block, n // min_repeats)
|
| 458 |
+
for L in range(min_block, upper + 1):
|
| 459 |
+
block = text[-L:]
|
| 460 |
+
if text[-L * min_repeats:] == block * min_repeats:
|
| 461 |
+
return True
|
| 462 |
+
return False
|
| 463 |
+
|
| 464 |
+
|
| 465 |
def detect_tail_loop(pred: str) -> bool:
|
| 466 |
if ERROR_PRED_RE.match(pred or ""):
|
| 467 |
return True
|
|
|
|
| 469 |
return True
|
| 470 |
if detect_tail_char_loop(pred):
|
| 471 |
return True
|
| 472 |
+
# Old (kept for reference): backreference regex caused catastrophic
|
| 473 |
+
# backtracking / hangs on very long repetitive loop predictions.
|
| 474 |
+
# if re.search(r"(.{12,}?)(?:\s*\1){2,}$", pred or ""):
|
| 475 |
+
# return True
|
| 476 |
+
if detect_tail_block_loop(pred):
|
| 477 |
return True
|
| 478 |
return False
|
| 479 |
|
|
|
|
| 503 |
# ---------------------------------------------------------------------------
|
| 504 |
|
| 505 |
|
| 506 |
+
try: # C-backed Levenshtein (identical result, ~100x faster on long pages)
|
| 507 |
+
import editdistance as _editdistance
|
| 508 |
+
except Exception: # pragma: no cover
|
| 509 |
+
_editdistance = None
|
| 510 |
+
|
| 511 |
+
|
| 512 |
def edit_distance(a: list, b: list) -> int:
|
| 513 |
if not a:
|
| 514 |
return len(b)
|
| 515 |
if not b:
|
| 516 |
return len(a)
|
| 517 |
|
| 518 |
+
if _editdistance is not None:
|
| 519 |
+
return int(_editdistance.eval(a, b))
|
| 520 |
+
|
| 521 |
prev = list(range(len(b) + 1))
|
| 522 |
for i, ai in enumerate(a, start=1):
|
| 523 |
curr = [i]
|
|
|
|
| 583 |
gt = item.get("ground_truth")
|
| 584 |
pred = item.get("pred")
|
| 585 |
if pred is None:
|
| 586 |
+
pred = item.get("prediction")
|
| 587 |
+
# Nested {"data": [{"ground_truth", "prediction"}], ...} shape.
|
| 588 |
+
if (gt is None or pred is None) and isinstance(item.get("data"), list) and item["data"]:
|
| 589 |
+
inner = item["data"][0]
|
| 590 |
+
if gt is None:
|
| 591 |
+
gt = inner.get("ground_truth") or inner.get("gt")
|
| 592 |
+
if pred is None:
|
| 593 |
+
pred = inner.get("prediction") or inner.get("pred")
|
| 594 |
+
if pred is None:
|
| 595 |
+
pred = ""
|
| 596 |
language = item.get("language") or item.get("lang")
|
| 597 |
|
| 598 |
if not image_name or gt is None:
|
|
|
|
| 618 |
return not (pred or "").strip()
|
| 619 |
|
| 620 |
|
| 621 |
+
def default_output_path(
|
| 622 |
+
input_path: str,
|
| 623 |
+
*,
|
| 624 |
+
normalize: bool,
|
| 625 |
+
replace_n: bool,
|
| 626 |
+
remove_br: bool = False,
|
| 627 |
+
) -> str:
|
| 628 |
stem = input_path.replace(".json", "")
|
| 629 |
+
if normalize and remove_br:
|
| 630 |
+
return f"{stem}.metrics.normalized.remove_br.json"
|
| 631 |
if normalize:
|
| 632 |
return f"{stem}.metrics.normalized.json"
|
| 633 |
+
if replace_n and remove_br:
|
| 634 |
+
return f"{stem}.metrics.replace_n.remove_br.json"
|
| 635 |
if replace_n:
|
| 636 |
return f"{stem}.metrics.replace_n.json"
|
| 637 |
+
if remove_br:
|
| 638 |
+
return f"{stem}.metrics.remove_br.json"
|
| 639 |
return f"{stem}.metrics.json"
|
| 640 |
|
| 641 |
|
|
|
|
| 662 |
*,
|
| 663 |
normalize: bool,
|
| 664 |
replace_n: bool,
|
| 665 |
+
remove_br: bool = False,
|
| 666 |
) -> tuple[dict, list[dict]]:
|
| 667 |
results: list[dict] = []
|
| 668 |
loop_count = 0
|
|
|
|
| 690 |
)
|
| 691 |
continue
|
| 692 |
|
| 693 |
+
gt, pred = preprocess(
|
| 694 |
+
gt_raw,
|
| 695 |
+
pred_raw,
|
| 696 |
+
normalize=normalize,
|
| 697 |
+
replace_n=replace_n,
|
| 698 |
+
remove_br=remove_br,
|
| 699 |
+
)
|
| 700 |
|
| 701 |
is_bad, failure_reasons = is_loop_or_catastrophic(gt_raw, pred_raw)
|
| 702 |
if is_bad:
|
|
|
|
| 753 |
return summary, results
|
| 754 |
|
| 755 |
|
| 756 |
+
def default_model_name(input_path: str) -> str:
|
| 757 |
+
"""Derive a model label from the input path (parent dir, else filename stem)."""
|
| 758 |
+
p = os.path.abspath(input_path)
|
| 759 |
+
stem = os.path.basename(p)
|
| 760 |
+
for suffix in (".gt_pred.json", ".gt_pred", ".json"):
|
| 761 |
+
if stem.endswith(suffix):
|
| 762 |
+
stem = stem[: -len(suffix)]
|
| 763 |
+
break
|
| 764 |
+
# Predictions often live at <model>/expert_annotations.json -> use the dir.
|
| 765 |
+
if stem in ("expert_annotations", "annotations", "predictions"):
|
| 766 |
+
return os.path.basename(os.path.dirname(p)) or stem
|
| 767 |
+
return stem
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
# Fixed language column order for the summary TSV.
|
| 771 |
+
LANG_ORDER = [
|
| 772 |
+
"Bengali",
|
| 773 |
+
"Gujarati",
|
| 774 |
+
"Hindi",
|
| 775 |
+
"Kannada",
|
| 776 |
+
"Malayalam",
|
| 777 |
+
"Marathi",
|
| 778 |
+
"Odia",
|
| 779 |
+
"Punjabi",
|
| 780 |
+
"Tamil",
|
| 781 |
+
"Telugu",
|
| 782 |
+
]
|
| 783 |
+
|
| 784 |
+
|
| 785 |
+
def write_summary_tsv(
|
| 786 |
+
summary: dict,
|
| 787 |
+
*,
|
| 788 |
+
model_name: str,
|
| 789 |
+
tsv_path: str,
|
| 790 |
+
lang_order: list[str] | None = None,
|
| 791 |
+
) -> None:
|
| 792 |
+
"""Write a per-model TSV with two rows (WER, CER) across lang columns.
|
| 793 |
+
|
| 794 |
+
Columns: Model, Metric, <lang_order...>, Overall.
|
| 795 |
+
"""
|
| 796 |
+
import csv
|
| 797 |
+
|
| 798 |
+
order = lang_order or LANG_ORDER
|
| 799 |
+
header = ["Model", "Metric", *order, "Overall"]
|
| 800 |
+
lang_scores = summary.get("lang_wise_scores") or {}
|
| 801 |
+
|
| 802 |
+
def make_row(metric_key: str, overall: float) -> list[str]:
|
| 803 |
+
cells = [
|
| 804 |
+
f"{lang_scores[lang][metric_key]:.4f}" if lang in lang_scores else ""
|
| 805 |
+
for lang in order
|
| 806 |
+
]
|
| 807 |
+
return [model_name, metric_key.upper(), *cells, f"{overall:.4f}"]
|
| 808 |
+
|
| 809 |
+
overall_wer = summary.get("valid_samples_wer", summary["avg_metrics"]["wer"])
|
| 810 |
+
overall_cer = summary.get("valid_samples_cer", summary["avg_metrics"]["cer"])
|
| 811 |
+
|
| 812 |
+
os.makedirs(os.path.dirname(os.path.abspath(tsv_path)), exist_ok=True)
|
| 813 |
+
with open(tsv_path, "w", newline="", encoding="utf-8") as f:
|
| 814 |
+
writer = csv.writer(f, delimiter="\t")
|
| 815 |
+
writer.writerow(header)
|
| 816 |
+
writer.writerow(make_row("wer", overall_wer))
|
| 817 |
+
writer.writerow(make_row("cer", overall_cer))
|
| 818 |
+
|
| 819 |
+
|
| 820 |
def main() -> None:
|
| 821 |
parser = argparse.ArgumentParser(
|
| 822 |
description="Compute CER/WER from a single gt+pred JSON file (standalone)",
|
|
|
|
| 844 |
action="store_true",
|
| 845 |
help="Only flatten newlines before scoring",
|
| 846 |
)
|
| 847 |
+
parser.add_argument(
|
| 848 |
+
"--remove-br",
|
| 849 |
+
action="store_true",
|
| 850 |
+
help="Replace <br>/<br/>/<br /> tags with a space in both gt and pred before scoring",
|
| 851 |
+
)
|
| 852 |
parser.add_argument(
|
| 853 |
"--overwrite",
|
| 854 |
action="store_true",
|
| 855 |
help="Overwrite output if it exists",
|
| 856 |
)
|
| 857 |
+
parser.add_argument(
|
| 858 |
+
"--model-name",
|
| 859 |
+
default=None,
|
| 860 |
+
help="Model label for the TSV (default: derived from input path)",
|
| 861 |
+
)
|
| 862 |
+
parser.add_argument(
|
| 863 |
+
"--tsv",
|
| 864 |
+
default=None,
|
| 865 |
+
help="Path for the per-language summary TSV (default: <output>.summary.tsv)",
|
| 866 |
+
)
|
| 867 |
+
parser.add_argument(
|
| 868 |
+
"--lang-order",
|
| 869 |
+
default=None,
|
| 870 |
+
help="Comma-separated language column order for the summary TSV "
|
| 871 |
+
"(default: top-10 Indic LANG_ORDER)",
|
| 872 |
+
)
|
| 873 |
args = parser.parse_args()
|
| 874 |
|
| 875 |
output_path = args.output or default_output_path(
|
| 876 |
args.input,
|
| 877 |
normalize=args.normalize,
|
| 878 |
replace_n=args.replace_n,
|
| 879 |
+
remove_br=args.remove_br,
|
| 880 |
)
|
| 881 |
if os.path.exists(output_path) and not args.overwrite:
|
| 882 |
print(f"Output exists: {output_path}. Use --overwrite to replace.")
|
|
|
|
| 887 |
rows,
|
| 888 |
normalize=args.normalize,
|
| 889 |
replace_n=args.replace_n,
|
| 890 |
+
remove_br=args.remove_br,
|
| 891 |
)
|
| 892 |
|
| 893 |
with open(output_path, "w", encoding="utf-8") as f:
|
|
|
|
| 915 |
|
| 916 |
print(f"\nSaved to {output_path}")
|
| 917 |
|
| 918 |
+
model_name = args.model_name or default_model_name(args.input)
|
| 919 |
+
tsv_path = args.tsv or f"{os.path.splitext(output_path)[0]}.summary.tsv"
|
| 920 |
+
lang_order = (
|
| 921 |
+
[x.strip() for x in args.lang_order.split(",") if x.strip()]
|
| 922 |
+
if args.lang_order
|
| 923 |
+
else None
|
| 924 |
+
)
|
| 925 |
+
write_summary_tsv(
|
| 926 |
+
summary,
|
| 927 |
+
model_name=model_name,
|
| 928 |
+
tsv_path=tsv_path,
|
| 929 |
+
lang_order=lang_order,
|
| 930 |
+
)
|
| 931 |
+
print(f"Saved summary TSV to {tsv_path}")
|
| 932 |
+
|
| 933 |
|
| 934 |
if __name__ == "__main__":
|
| 935 |
main()
|
small_representative/small_representative-00000-of-00001.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:32eaadbd9ccfbe5f5ef9b410d8dcd057d9ae1a37796e5ebab063c6cc502a822d
|
| 3 |
+
size 95507354
|
test/{test-00000-of-00004.parquet → test-00000-of-00002.parquet}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b1d702fd54e8e7b25dcdc62b4a9c636c2b48b2ad73158feb03134f83331bd44e
|
| 3 |
+
size 457492488
|
test/{test-00001-of-00004.parquet → test-00001-of-00002.parquet}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b0403bea515272cd116dc780015eb52ebf20d48c0410b5464daa4d68e43609b
|
| 3 |
+
size 178772669
|
test/test-00002-of-00004.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:5e195c13128f72b3bba74adfa65d4938730ee46aec705fea8717ce1ecd173cb7
|
| 3 |
-
size 378725133
|
|
|
|
|
|
|
|
|
|
|
|
test/test-00003-of-00004.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:dadceb99cd194f00fbef6844cf39fad87688c060004ab06240d2d205fa182b9e
|
| 3 |
-
size 421224220
|
|
|
|
|
|
|
|
|
|
|
|