Create chatcode/evaluate_articles.py
Browse files
chatcode/evaluate_articles.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Cloud-only format/citation evaluation; human factual review is still required."""
|
| 2 |
+
import argparse
|
| 3 |
+
from contextlib import nullcontext
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from .articles import MODES, behavior_examples, draft_article
|
| 8 |
+
from .runtime import CONFIG, load_engine
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def evaluate(args):
|
| 12 |
+
examples = behavior_examples()[args.split]
|
| 13 |
+
tokenizer, model = load_engine(args.adapter)
|
| 14 |
+
results = {"base_model": CONFIG["base_model"], "base_revision": CONFIG["base_revision"],
|
| 15 |
+
"adapter": args.adapter, "split": args.split, "cases_per_variant": len(examples),
|
| 16 |
+
"human_factual_review_required": True,
|
| 17 |
+
"limitations": ["Small fictional behavior suite, not a real-world article-quality benchmark.",
|
| 18 |
+
"Citation ID validity does not establish source reliability or factual entailment."]}
|
| 19 |
+
for variant in (["base", "adapter"] if args.adapter else ["base"]):
|
| 20 |
+
context = model.disable_adapter() if variant == "base" and args.adapter else nullcontext()
|
| 21 |
+
cases = []
|
| 22 |
+
with context:
|
| 23 |
+
for example in examples:
|
| 24 |
+
request = json.loads(example["messages"][1]["content"])
|
| 25 |
+
source_rows = [[s["title"], s["url"], s["excerpt"]] for s in request["source_packet"]]
|
| 26 |
+
expected = json.loads(example["messages"][-1]["content"])
|
| 27 |
+
mode = MODES[0] if "lead" in expected else MODES[1]
|
| 28 |
+
case = {"group_id": example["group_id"], "topic": request["topic"], "mode": mode,
|
| 29 |
+
"source_packet": request["source_packet"], "human_review": "pending"}
|
| 30 |
+
try:
|
| 31 |
+
rendered, raw = draft_article(tokenizer, model, request["topic"], source_rows, mode)
|
| 32 |
+
case.update({"format_and_citation_ids_accepted": True, "output": raw, "rendered": rendered})
|
| 33 |
+
except ValueError as error:
|
| 34 |
+
case.update({"format_and_citation_ids_accepted": False, "error": str(error)})
|
| 35 |
+
cases.append(case)
|
| 36 |
+
results[variant] = {"format_acceptance_rate": sum(c["format_and_citation_ids_accepted"] for c in cases) / len(cases),
|
| 37 |
+
"cases": cases}
|
| 38 |
+
Path(args.output).write_text(json.dumps(results, ensure_ascii=False, indent=2), encoding="utf-8")
|
| 39 |
+
print(f"Wrote {args.output}; source support, tone and usefulness still need human review.")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
parser = argparse.ArgumentParser(description="Evaluate article formatting and citation IDs on a cloud GPU")
|
| 44 |
+
parser.add_argument("--adapter")
|
| 45 |
+
parser.add_argument("--split", choices=["validation", "test"], default="test")
|
| 46 |
+
parser.add_argument("--output", required=True)
|
| 47 |
+
evaluate(parser.parse_args())
|