ClarusC64's picture
Create scorer.py
563f412 verified
import re
from dataclasses import dataclass
from typing import Dict, Any, List
LABELS = {
"correct-sequence",
"out-of-order",
"premature-action",
"skipped-step",
"repeated-step",
"delayed-response",
"unsafe-order",
}
@dataclass
class ScoreResult:
score: float
details: Dict[str, Any]
def _has(t: str, pats: List[str]) -> bool:
t = (t or "").lower()
return any(re.search(p, t) for p in pats)
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
pred = (prediction or "").strip()
words_ok = len(pred.split()) <= 240
label_ok = 1 if any(lbl in pred for lbl in LABELS) else 0
seq_ref = 1 if _has(pred, [r"order", r"sequence", r"step", r"before", r"after"]) else 0
action_ref = 1 if _has(pred, [r"grasp", r"align", r"lift", r"release", r"pause", r"reset"]) else 0
safety_ref = 1 if _has(pred, [r"unsafe", r"risk", r"drop", r"human"]) else 0
raw = (
0.25 * int(words_ok) +
0.35 * label_ok +
0.20 * (seq_ref or action_ref) +
0.20 * safety_ref
)
final = max(0.0, min(1.0, raw))
return ScoreResult(
score=final,
details={
"words_ok": words_ok,
"label_ok": label_ok,
"sequence_ref": seq_ref,
"action_ref": action_ref,
"safety_ref": safety_ref,
"temporal_pressure": sample.get("temporal_pressure"),
"scenario": sample.get("scenario"),
}
)
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
if not results:
return {"mean": 0.0, "n": 0}
return {
"mean": sum(r.score for r in results) / len(results),
"n": len(results),
}