Download scripts/r5_loopguard/lg_seal_scan.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 5.13 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_loopguard/lg_seal_scan.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_loopguard/lg_seal_scan.py
-
curl -L -o lg_seal_scan.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_loopguard/lg_seal_scan.py
5.13 kB
| """LOOPGUARD seal check: which files under DATA name held-out TEST page ids (page level) or crop ids derived from them. | |
| Prints and writes ids, paths and counts only: no label, no prediction text. Page ids of the generated test pages come | |
| from the FILE NAMES under data/processed/r5/synth/pages/test (no file is opened there). School Notebooks test pages are | |
| recognised by token shape: 'school_test_<digits>' not followed by another id character (page-shaped) versus followed by | |
| '_' or more id characters (crop-shaped, for example a word id). | |
| usage: lg_seal_scan.py --root DATA --out JSON [--workers 16] | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import re | |
| import time | |
| from multiprocessing import Pool | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--root", required=True) | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--workers", type=int, default=16) | |
| a = ap.parse_args() | |
| SCAN_DIRS = ["outputs", "data/processed", "logs", "checksums", "release"] | |
| SKIP_PREFIX = ["data/processed/r5/synth/pages/test/"] # the held-out originals themselves (names only, listed below) | |
| EXT = (".json", ".jsonl", ".tsv", ".txt", ".out", ".csv", ".log", ".sha256", ".md", ".py", ".sh", ".sbatch") | |
| TOK = re.compile(rb"(pg_test_[A-Za-z0-9_]+|school_test_[0-9]+[A-Za-z0-9_]*)") | |
| GEN_PAGE = re.compile(r"pg_test_[a-z]+_[a-z_]+_[lmh]_[0-9]{6}$") | |
| SCHOOL_PAGE = re.compile(r"school_test_[0-9]+$") | |
| def gen_ids(root): | |
| d = os.path.join(root, "data/processed/r5/synth/pages/test") | |
| ids = set() | |
| for dp, _, fns in os.walk(d): | |
| for fn in fns: | |
| if fn.endswith(".jpg"): | |
| ids.add(fn[:-4]) | |
| return ids | |
| def scan(path): | |
| res = {"gen_page": 0, "gen_other": 0, "school_page": 0, "school_other": 0, "ex": {}} | |
| try: | |
| with open(path, "rb") as f: | |
| carry = b"" | |
| while True: | |
| chunk = f.read(64 << 20) | |
| buf = carry + chunk | |
| if not chunk: | |
| carry = b"" | |
| else: | |
| # tokens never span a newline: process up to the last newline, carry the rest (cut at 1 MB) | |
| nl = buf.rfind(b"\n") | |
| if nl < 0 or len(buf) - nl > (1 << 20): | |
| carry = buf[-256:] | |
| buf = buf[:-256] | |
| else: | |
| carry = buf[nl + 1:] | |
| buf = buf[:nl + 1] | |
| for m in TOK.finditer(buf): | |
| t = m.group(1).decode("ascii", "replace") | |
| if t.startswith("pg_test_"): | |
| k = "gen_page" if (t in GEN or GEN_PAGE.match(t)) else "gen_other" | |
| else: | |
| k = "school_page" if SCHOOL_PAGE.match(t) else "school_other" | |
| res[k] += 1 | |
| ex = res["ex"].setdefault(k, []) | |
| if len(ex) < 3 and t not in ex: | |
| ex.append(t) | |
| if not chunk: | |
| break | |
| except OSError as e: | |
| res["error"] = str(e) | |
| return path, res | |
| def init(g): | |
| global GEN | |
| GEN = g | |
| if __name__ == "__main__": | |
| t0 = time.time() | |
| GEN = gen_ids(a.root) | |
| files = [] | |
| for sd in SCAN_DIRS: | |
| top = os.path.join(a.root, sd) | |
| for dp, dns, fns in os.walk(top): | |
| rel = os.path.relpath(dp, a.root) + "/" | |
| if any(rel.startswith(p) for p in SKIP_PREFIX): | |
| dns[:] = [] | |
| continue | |
| for fn in fns: | |
| if fn.lower().endswith(EXT): | |
| files.append(os.path.join(dp, fn)) | |
| # file and directory NAMES that carry a test page id or a held-out page-set name | |
| name_hits = [] | |
| NAME = re.compile(r"(pg_test_|school_test|pages_.*test|test.*pages|gen_test|synth_pages_test)", re.I) | |
| for sd in SCAN_DIRS: | |
| top = os.path.join(a.root, sd) | |
| for dp, dns, fns in os.walk(top): | |
| rel = os.path.relpath(dp, a.root) + "/" | |
| if any(rel.startswith(p) for p in SKIP_PREFIX): | |
| dns[:] = [] | |
| continue | |
| for n in dns + fns: | |
| if NAME.search(n): | |
| name_hits.append(os.path.relpath(os.path.join(dp, n), a.root)) | |
| with Pool(a.workers, initializer=init, initargs=(GEN,)) as pool: | |
| out = pool.map(scan, files, chunksize=8) | |
| hits = {} | |
| for p, r in out: | |
| if r.get("error") or any(r[k] for k in ("gen_page", "gen_other", "school_page", "school_other")): | |
| hits[os.path.relpath(p, a.root)] = r | |
| rep = {"root": a.root, "scan_dirs": SCAN_DIRS, "skipped": SKIP_PREFIX, "extensions": EXT, | |
| "n_files_scanned": len(files), "n_gen_test_page_ids_from_names": len(GEN), | |
| "n_files_with_tokens": len(hits), "name_hits": sorted(name_hits), "content_hits": hits, | |
| "seconds": round(time.time() - t0, 1)} | |
| with open(a.out, "w", encoding="utf-8") as f: | |
| json.dump(rep, f, ensure_ascii=False, indent=1) | |
| print("files", len(files), "gen ids", len(GEN), "files with tokens", len(hits), "name hits", len(name_hits), | |
| "seconds", rep["seconds"], flush=True) | |