asdfasdfqrqwer's picture
Add reproducible integrity checks for evaluation lab
f79ad6f
Raw History Blame Contribute Delete
2.85 kB
#!/usr/bin/env python3
"""Dependency-free integrity checks for the Arabic Evaluation Lab static Space."""
from __future__ import annotations
import json
import re
import shutil
import subprocess
from collections import Counter
from html.parser import HTMLParser
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
HTML = (ROOT / "index.html").read_text(encoding="utf-8")
README = (ROOT / "README.md").read_text(encoding="utf-8")
class Structure(HTMLParser):
def __init__(self):
super().__init__()
self.ids = []
def handle_starttag(self, tag, attrs):
self.ids.extend(v for k, v in attrs if k == "id")
parser = Structure()
parser.feed(HTML)
duplicates = [key for key, count in Counter(parser.ids).items() if count > 1]
assert not duplicates, f"Duplicate HTML IDs: {duplicates}"
match = re.search(r"const C=(\[.*?\]);const \$=", HTML, re.S)
assert match, "Embedded evaluation cases array not found"
cases = json.loads(match.group(1))
assert len(cases) == 36, f"Expected 36 starter cases, found {len(cases)}"
ids = [case["id"] for case in cases]
assert len(ids) == len(set(ids)), "Duplicate case IDs"
required = {"id", "prompt", "category", "expected_criteria", "language", "source"}
for case in cases:
assert required <= case.keys(), f"Missing fields in {case.get('id')}"
assert case["language"] == "ar", f"Unexpected language in {case['id']}"
assert case["source"] == "original_synthetic", f"Unexpected source in {case['id']}"
assert case["prompt"].strip() and case["expected_criteria"].strip()
checks = {
"criteria_not_sent_to_model": "messages:[{role:'user',content:c.prompt}]" in HTML,
"human_scoring_only": "human_review_only" in HTML and 'value="0.5"' in HTML,
"json_export": "arabic-model-evaluation.json" in HTML,
"csv_export": "arabic-model-evaluation.csv" in HTML,
"local_resume": "localStorage.setItem(STORE" in HTML and "localStorage.getItem(STORE" in HTML,
"token_not_persisted": "token:$('token').value" not in HTML and "token: $('token').value" not in HTML,
"failure_ledger": "request_failures:errors" in HTML,
"category_report": "categorySummary" in HTML,
"no_auto_leaderboard": "لا تدّعي الأداة ترتيبًا عالميًا" in README,
}
for name, ok in checks.items():
assert ok, f"FAILED: {name}"
script = HTML[HTML.index("<script>") + 8:HTML.index("</script>")]
if shutil.which("node"):
subprocess.run(["node", "--check", "-"], input=script, text=True, check=True)
print(f"PASS: HTML IDs unique ({len(parser.ids)} IDs)")
print(f"PASS: {len(cases)} Arabic evaluation cases; {len(set(c['category'] for c in cases))} categories")
for name in checks:
print(f"PASS: {name}")
print("PASS: JavaScript syntax" if shutil.which("node") else "SKIP: Node.js unavailable; install Node.js to check JavaScript syntax")