Download evaluate.py from Urdatorn/Makron-code: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/Urdatorn/Makron-code/resolve/main/evaluate.py
- Command line
-
hf download hf://Urdatorn/Makron-code/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/Urdatorn/Makron-code/resolve/main/evaluate.py
2.36 kB
| ''' | |
| Score one or more checkpoints (local dirs or HF repo ids) on Norma. | |
| python evaluate.py --models Ericu950/oga-macronizer-char runs/final/seed1/best \ | |
| --splits test validation --out results/eval | |
| For each model and split it prints the Table 1 metrics and the per-source | |
| scores, and writes: | |
| - <out>/<name>.<split>.metrics.json | |
| - <out>/<name>.<split>.positions.jsonl: one row per scored position | |
| (source, line, char, word, gold, pred), for the error analysis. | |
| ''' | |
| import argparse | |
| import json | |
| import os | |
| import time | |
| from norma import format_table, load_split, metrics, score | |
| from predict import MacronPredictor | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--models", nargs="+", required=True) | |
| ap.add_argument("--splits", nargs="+", default=["test"]) | |
| ap.add_argument("--norma_repo", default="Urdatorn/norma") | |
| ap.add_argument("--norma_revision", default=None) | |
| ap.add_argument("--out", default=None) | |
| ap.add_argument("--device", default=None) | |
| ap.add_argument("--batch_size", type=int, default=256, | |
| help="1 = one line at a time without padding, as the original predict.py") | |
| a = ap.parse_args() | |
| data = {s: load_split(s, a.norma_repo, a.norma_revision) for s in a.splits} | |
| all_metrics = {} | |
| for m in a.models: | |
| t = time.time() | |
| pred = MacronPredictor(m, device=a.device) | |
| name = m.strip("/").replace("/", "__") + ("" if a.batch_size != 1 else ".unbatched") | |
| for s, recs in data.items(): | |
| outputs = pred.macronize_lines([r.plain for r in recs], batch_size=a.batch_size) | |
| pos = score(recs, outputs) | |
| met = metrics(pos) | |
| all_metrics[(m, s)] = met | |
| print(f"\n=== {m} on Norma {s} ({len(recs)} lines, {met['n']} positions, " | |
| f"{time.time()-t:.1f}s) ===", flush=True) | |
| print(format_table(met), flush=True) | |
| if a.out: | |
| os.makedirs(a.out, exist_ok=True) | |
| with open(os.path.join(a.out, f"{name}.{s}.metrics.json"), "w") as f: | |
| json.dump(met, f, indent=2) | |
| with open(os.path.join(a.out, f"{name}.{s}.positions.jsonl"), "w", encoding="utf-8") as f: | |
| for p in pos: | |
| f.write(json.dumps(p, ensure_ascii=False) + "\n") | |
| if __name__ == "__main__": | |
| main() | |