Download scripts/label_loghub.py from Kolgrim/hades: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/Kolgrim/hades/resolve/main/scripts/label_loghub.py
- Command line
-
hf download hf://Kolgrim/hades/scripts/label_loghub.py
-
curl -L -o label_loghub.py https://huggingface.co/Kolgrim/hades/resolve/main/scripts/label_loghub.py
2.19 kB
| """Review rule-suggested labels for Loghub windows. Saves after every answer; rerun to resume. | |
| Usage: python scripts/label_loghub.py [--n 300] | |
| Keys: Enter = accept suggestion, 1-7 = choose label, s = skip, q = quit. | |
| Guide: docs/eval/authlog-labelling.md | |
| """ | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from hades.data.synth.authlog import LABELS | |
| from hades.evals.baselines import rule_label | |
| from hades.evals.loghub import LABELS_PATH, RAW_DIR, pending_windows | |
| from hades.tokenizer.bpe import BPETokenizer | |
| TOKENIZER = Path("hades/tokenizer/hades-16k.json") | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--n", type=int, default=300) | |
| ap.add_argument("--labels", type=Path, default=LABELS_PATH) | |
| args = ap.parse_args() | |
| if not TOKENIZER.exists(): | |
| raise SystemExit(f"{TOKENIZER} not found: train the tokenizer (Task 17) before labelling") | |
| tok = BPETokenizer.load(TOKENIZER) | |
| queue = pending_windows(RAW_DIR, args.labels, lambda s: len(tok.encode(s)), args.n) | |
| done = sum(1 for l in args.labels.read_text().splitlines() if l.strip()) if args.labels.exists() else 0 | |
| menu = " ".join(f"{i + 1}={l}" for i, l in enumerate(LABELS)) | |
| for k, w in enumerate(queue): | |
| suggestion = rule_label(w["text"]) | |
| print("\n" + "=" * 100) | |
| print(f"[{done + k + 1}/{done + len(queue)}] {w['file']} lines {w['start']}-{w['end']}\n") | |
| print(w["text"]) | |
| print(f"\nSuggested: {suggestion}\n{menu}") | |
| while True: | |
| answer = input("label> ").strip().lower() | |
| if answer == "q": | |
| return | |
| if answer == "s": | |
| break | |
| if answer == "" or (answer.isdigit() and 1 <= int(answer) <= len(LABELS)): | |
| label = suggestion if answer == "" else LABELS[int(answer) - 1] | |
| row = {"file": w["file"], "start": w["start"], "end": w["end"], "label": label, "prelabel": suggestion} | |
| with open(args.labels, "a") as f: | |
| f.write(json.dumps(row) + "\n") | |
| break | |
| print("Enter, 1-7, s or q") | |
| print("\nAll windows labelled.") | |
| if __name__ == "__main__": | |
| main() | |