Download scripts/r5_testpass2/tp2_qwen_az_script.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_testpass2/tp2_qwen_az_script.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_testpass2/tp2_qwen_az_script.py
-
curl -L -o tp2_qwen_az_script.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_testpass2/tp2_qwen_az_script.py
3.37 kB
| """Test pass 2, amendment 5 (26 Nov): script measures of the EXISTING Qwen3-VL-2B az_localdoc_test predictions, the same measures as | |
| error_analysis.json gives for M1 (N0 text, r5eval.align.align_ops): Cyrillic share of output letters, share of rows with any Cyrillic | |
| letter, substitutions, Latin->Cyrillic substitutions and swap pairs, CER, S/D/I per reference character. | |
| usage: tp2_qwen_az_script.py --pred TSV --out JSON | |
| """ | |
| import argparse | |
| import collections | |
| import csv | |
| import json | |
| import sys | |
| import unicodedata | |
| sys.path.insert(0, "/path/to/TURKIC_OCR_CODE/src") | |
| from r5eval import textnorm # noqa: E402 | |
| from r5eval.align import align_ops # noqa: E402 | |
| csv.field_size_limit(1 << 30) | |
| MAN = "/path/to/TURKIC_OCR_DATA/data/processed/r5/manifests/az_localdoc_test.tsv" | |
| def script(ch): | |
| if not unicodedata.category(ch).startswith("L"): | |
| return None | |
| s = textnorm.char_script_set(ch) | |
| if s is None: | |
| return None | |
| if "Latin" in s and "Cyrillic" not in s: | |
| return "Latin" | |
| if "Cyrillic" in s and "Latin" not in s: | |
| return "Cyrillic" | |
| return "other" | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--pred", required=True) | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--model", default="Qwen3-VL-2B R5 leader (test pass 2 CPU predictions)") | |
| ap.add_argument("--ids", default="", help="optional file with one row id per line: restrict to these rows") | |
| a = ap.parse_args() | |
| KEEP = set(x.strip() for x in open(a.ids, encoding="utf-8") if x.strip()) if a.ids else None | |
| with open(a.pred, encoding="utf-8", newline="") as f: | |
| hyp = {r["id"]: r["hyp"] for r in csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)} | |
| with open(MAN, encoding="utf-8", newline="") as f: | |
| rows = list(csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)) | |
| S = D = I = N = 0 | |
| l2c = 0 | |
| pairs = collections.Counter() | |
| letters = cyr = rows_cyr = n = 0 | |
| for r in rows: | |
| if KEEP is not None and r["id"] not in KEEP: | |
| continue | |
| ref = textnorm.norm(r["text"]) | |
| h = textnorm.norm(hyp.get(r["id"], "")) | |
| if not ref: | |
| continue | |
| n += 1 | |
| dist, ops = align_ops(ref, h) | |
| for op, i, j in ops: | |
| if op == "S": | |
| S += 1 | |
| if script(ref[i]) == "Latin" and script(h[j]) == "Cyrillic": | |
| l2c += 1 | |
| pairs[ref[i] + "->" + h[j]] += 1 | |
| elif op == "D": | |
| D += 1 | |
| elif op == "I": | |
| I += 1 | |
| N += len(ref) | |
| c = sum(1 for ch in h if script(ch) == "Cyrillic") | |
| letters += sum(1 for ch in h if script(ch) in ("Latin", "Cyrillic", "other")) | |
| cyr += c | |
| rows_cyr += 1 if c else 0 | |
| res = {"model": a.model, "set": "az_localdoc_test", "pred": a.pred, "row_filter": a.ids or "all rows", "rows": n, "ref_chars": N, | |
| "cer": (S + D + I) / N, "sub_per_ref_char": S / N, "del_per_ref_char": D / N, "ins_per_ref_char": I / N, | |
| "share_of_output_letters_cyrillic": cyr / letters if letters else None, "share_of_rows_with_any_cyrillic_letter": rows_cyr / n, | |
| "subs_total": S, "subs_latin_to_cyrillic": l2c, "share_of_substitutions_latin_to_cyrillic": l2c / S if S else None, | |
| "swap_pairs_latin_to_cyrillic_top30": pairs.most_common(30)} | |
| json.dump(res, open(a.out, "w", encoding="utf-8"), indent=1, ensure_ascii=False) | |
| print(json.dumps({k: v for k, v in res.items() if k != "swap_pairs_latin_to_cyrillic_top30"})) | |