Datasets:
Download code/scripts_seqroute/score_aux.py from divy123/sequential-router-v2: direct link, hf CLI and curl.
- Browser
- Download file 4.88 kB
-
https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/score_aux.py
- Command line
-
hf download hf://datasets/divy123/sequential-router-v2/code/scripts_seqroute/score_aux.py
-
curl -L -o score_aux.py https://huggingface.co/datasets/divy123/sequential-router-v2/resolve/main/code/scripts_seqroute/score_aux.py
4.88 kB
| """Auxiliary-metric scoring: COMET (translation) and SARI + BERTScore (simplification). Resumable per (model, query). | |
| python score_aux.py --what comet (run with /mnt/localssd/venvs/comet/bin/python) | |
| python score_aux.py --what simp (run with /mnt/localssd/venvs/main/bin/python) | |
| Q definitions (fixed in advance): | |
| translation: Q = 10 * clip((COMET-22 - 0.6) / 0.3, 0, 1) | |
| simplification: Q = 10 * (0.5 * clip(SARI / 60, 0, 1) + 0.5 * clip(BERTScore-F1(source, output; baseline-rescaled), 0, 1)) | |
| Empty final answers score 0. | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import pandas as pd | |
| os.environ.setdefault("HF_HOME", "/mnt/localssd/hf_cache") | |
| os.environ.setdefault("TMPDIR", "/mnt/localssd/tmp") | |
| sys.path.insert(0, "src") | |
| ROOT = Path("/mnt/localssd/seqroute1m") | |
| def final_of(r): | |
| t = r["text"] or "" | |
| if "</think>" in t: | |
| return t.rsplit("</think>", 1)[1].strip() | |
| if r.get("reasoning"): | |
| return t.strip() | |
| if "<think>" in t: | |
| return "" | |
| return t.strip() | |
| def clip(x, lo=0.0, hi=1.0): | |
| return max(lo, min(hi, x)) | |
| def collect(q, models, family, out_path): | |
| done = set() | |
| if out_path.exists(): | |
| done = {json.loads(l)["id"] for l in open(out_path)} | |
| qq = q[q.task_family == family].set_index("query_id") | |
| items = [] | |
| for m in models: | |
| f = ROOT / "responses" / f"{m}.jsonl" | |
| if not f.exists(): | |
| continue | |
| for l in open(f): | |
| r = json.loads(l) | |
| rid = f"{m}|{r['id']}" | |
| if r["id"] in qq.index and rid not in done: | |
| items.append((rid, r["id"], final_of(r))) | |
| return qq, items | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--what", required=True, choices=["comet", "simp"]) | |
| ap.add_argument("--models", default="") | |
| ap.add_argument("--queries", default=str(ROOT / "queries/master_queries.parquet")) | |
| a = ap.parse_args() | |
| q = pd.read_parquet(a.queries) | |
| models = [m["id"] for m in json.load(open(ROOT / "manifests/models.json"))] | |
| if a.models: | |
| models = [m for m in models if m in a.models.split(",")] | |
| out_dir = ROOT / "scores/aux" | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| if a.what == "comet": | |
| from comet import download_model, load_from_checkpoint | |
| out_path = out_dir / "comet.jsonl" | |
| qq, items = collect(q, models, "translation", out_path) | |
| if not items: | |
| print("nothing to score"); return | |
| model = load_from_checkpoint(download_model("Unbabel/wmt22-comet-da")) | |
| data, idx = [], [] | |
| with open(out_path, "a") as f: | |
| for rid, qid, fin in items: | |
| if not fin.strip(): | |
| f.write(json.dumps(dict(id=rid, Q=0.0, raw=None, raw_name="comet22", empty_final=True)) + "\n") | |
| continue | |
| pl = json.loads(qq.loc[qid].payload) | |
| data.append(dict(src=pl["source"], mt=fin.split("\n")[0] if len(fin.split("\n")) > 1 and len(fin.split("\n")[0]) > 0.6 * len(fin) else fin, ref=pl["references"][0])) | |
| idx.append(rid) | |
| t0 = time.time() | |
| out = model.predict(data, batch_size=128, gpus=1, num_workers=8, progress_bar=False) | |
| for rid, s in zip(idx, out.scores): | |
| f.write(json.dumps(dict(id=rid, Q=10.0 * clip((s - 0.6) / 0.3), raw=float(s), raw_name="comet22", empty_final=False)) + "\n") | |
| print(f"comet: scored {len(data)} in {time.time() - t0:.0f}s") | |
| else: | |
| from bert_score import BERTScorer | |
| from llmroute.seqroute.sari import sari_sentence | |
| out_path = out_dir / "simplification.jsonl" | |
| qq, items = collect(q, models, "simplification", out_path) | |
| if not items: | |
| print("nothing to score"); return | |
| scorer = BERTScorer(model_type="roberta-large", lang="en", rescale_with_baseline=True, device="cuda:0", batch_size=64) | |
| live = [(rid, qid, fin) for rid, qid, fin in items if fin.strip()] | |
| _, _, f1 = scorer.score([fin for _, _, fin in live], [json.loads(qq.loc[qid].payload)["source"] for _, qid, _ in live]) | |
| with open(out_path, "a") as f: | |
| for rid, qid, fin in items: | |
| if not fin.strip(): | |
| f.write(json.dumps(dict(id=rid, Q=0.0, raw=None, raw_name="sari+bertscore", empty_final=True)) + "\n") | |
| for (rid, qid, fin), b in zip(live, f1.tolist()): | |
| pl = json.loads(qq.loc[qid].payload) | |
| s = sari_sentence(pl["source"], fin.strip(), pl["references"]) | |
| Q = 10.0 * (0.5 * clip(s / 60.0) + 0.5 * clip(b)) | |
| f.write(json.dumps(dict(id=rid, Q=Q, raw=s, raw_name="sari+bertscore", bertscore_f1=float(b), sari=s, empty_final=False)) + "\n") | |
| print(f"simplification: scored {len(items)}") | |
| if __name__ == "__main__": | |
| main() | |