Download scripts/monitor_math500_jsonl.py from JulianHJR/8b: direct link, hf CLI and curl.
- Browser
- Download file 5.08 kB
-
https://huggingface.co/datasets/JulianHJR/8b/resolve/main/scripts/monitor_math500_jsonl.py
- Command line
-
hf download hf://datasets/JulianHJR/8b/scripts/monitor_math500_jsonl.py
-
curl -L -o monitor_math500_jsonl.py https://huggingface.co/datasets/JulianHJR/8b/resolve/main/scripts/monitor_math500_jsonl.py
5.08 kB
| #!/usr/bin/env python3 | |
| import argparse | |
| import json | |
| import os | |
| import re | |
| import sys | |
| import time | |
| from tqdm import tqdm | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from configs import get_config | |
| from src.detectors import BehaviorDetector | |
| def think_segment(text): | |
| if not isinstance(text, str): | |
| return "" | |
| # Qwen style / common think tags | |
| m = re.search(r"<think>(.*?)</think>", text, flags=re.S | re.I) | |
| if m: | |
| return m.group(1) | |
| m = re.search(r"<\|channel\|>analysis<\|message\|>(.*?)(<\|channel\|>final|$)", text, flags=re.S) | |
| if m: | |
| return m.group(1) | |
| return text | |
| def repetition_score(text, tail_chars=400, ngram=30): | |
| if not isinstance(text, str): | |
| return 0.0 | |
| tail = text[-tail_chars:] if len(text) > tail_chars else text | |
| if len(tail) < ngram * 2: | |
| return 0.0 | |
| seen = set() | |
| repeated = 0 | |
| total = 0 | |
| for i in range(len(tail) - ngram): | |
| chunk = tail[i:i + ngram] | |
| total += 1 | |
| if chunk in seen: | |
| repeated += 1 | |
| else: | |
| seen.add(chunk) | |
| return repeated / total if total else 0.0 | |
| def pick_text(rec): | |
| for k in [ | |
| "think_text", | |
| "think", | |
| "reasoning", | |
| "cot", | |
| "generated_text", | |
| "full_text", | |
| "text", | |
| "output", | |
| "completion", | |
| "response", | |
| "raw_output", | |
| "answer_text", | |
| ]: | |
| v = rec.get(k) | |
| if isinstance(v, str) and v.strip(): | |
| return v | |
| return "" | |
| def get_idx(rec): | |
| for k in ["problem_idx", "idx", "i", "pid", "problem_id"]: | |
| if k in rec: | |
| return rec[k] | |
| key = rec.get("_key") or rec.get("key") or "" | |
| m = re.search(r"P(\d+)", str(key)) | |
| return m.group(1) if m else key | |
| def get_alpha(rec): | |
| a = rec.get("alpha") | |
| if a is not None: | |
| try: | |
| return float(a) | |
| except Exception: | |
| return a | |
| key = rec.get("_key") or rec.get("key") or "" | |
| m = re.search(r"_A([0-9.]+)", str(key)) | |
| if m: | |
| try: | |
| return float(m.group(1)) | |
| except Exception: | |
| return m.group(1) | |
| return "NA" | |
| def get_ok(rec): | |
| for k in ["correct", "ok", "is_correct"]: | |
| if k in rec: | |
| return rec[k] | |
| return "NA" | |
| def get_ttok(rec): | |
| for k in ["think_tok", "ttok", "think_tokens", "thinking_tokens", "full_gen_tokens"]: | |
| if k in rec: | |
| return rec[k] | |
| return "NA" | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--path", required=True) | |
| ap.add_argument("--total", type=int, default=2000) | |
| ap.add_argument("--dimension", default="monitoring") | |
| ap.add_argument("--poll", type=float, default=3.0) | |
| args = ap.parse_args() | |
| cfg = get_config(args.dimension) | |
| detector = BehaviorDetector(cfg) | |
| print(f"[monitor] watching {args.path}", file=sys.stderr, flush=True) | |
| print(f"[monitor] total={args.total} dimension={args.dimension}", file=sys.stderr, flush=True) | |
| while not os.path.exists(args.path): | |
| print("[monitor] waiting jsonl...", file=sys.stderr, flush=True) | |
| time.sleep(args.poll) | |
| seen = 0 | |
| pbar = tqdm( | |
| total=args.total, | |
| initial=0, | |
| desc="MATH500 mon/tqdm", | |
| file=sys.stderr, | |
| dynamic_ncols=True, | |
| mininterval=1.0, | |
| ) | |
| with open(args.path, "r", encoding="utf-8") as f: | |
| while True: | |
| line = f.readline() | |
| if not line: | |
| if seen >= args.total: | |
| break | |
| time.sleep(args.poll) | |
| continue | |
| line = line.strip() | |
| if not line: | |
| continue | |
| try: | |
| rec = json.loads(line) | |
| except Exception as e: | |
| tqdm.write(f"[monitor] bad json line={seen+1}: {e}", file=sys.stderr) | |
| continue | |
| seen += 1 | |
| # DIRECT_READ_MON_PATCH | |
| if "mon_total" in rec: | |
| mon = rec.get("mon_total", -1) | |
| rep = rec.get("repetition_score", -1.0) | |
| else: | |
| text = pick_text(rec) | |
| if text: | |
| th = text if rec.get("think_text") or rec.get("think") else think_segment(text) | |
| det = detector.detect(th) | |
| mon = det.get("total", 0) if isinstance(det, dict) else 0 | |
| rep = repetition_score(th) | |
| else: | |
| mon = -1 | |
| rep = -1.0 | |
| idx = get_idx(rec) | |
| alpha = get_alpha(rec) | |
| ok = get_ok(rec) | |
| ttok = get_ttok(rec) | |
| tqdm.write( | |
| f"[mon] line={seen}/{args.total} P={idx} A={alpha} " | |
| f"ok={ok} ttok={ttok} mon={mon} rep={rep:.2f}", | |
| file=sys.stderr, | |
| ) | |
| pbar.update(1) | |
| if seen >= args.total: | |
| break | |
| pbar.close() | |
| print(f"[monitor] done seen={seen}", file=sys.stderr, flush=True) | |
| if __name__ == "__main__": | |
| main() | |