| from dataclasses import dataclass |
| from typing import Dict, Any, List |
|
|
| @dataclass |
| class ScoreResult: |
| score: float |
| details: Dict[str, Any] |
|
|
| def score(sample: Dict[str, Any], prediction: str) -> ScoreResult: |
| |
| try: |
| pred = float((prediction or "").strip()) |
| except Exception: |
| pred = -1.0 |
|
|
| true_raw = sample.get("coherence_score", "") |
| try: |
| true = float(true_raw) if true_raw not in ("", None) else None |
| except Exception: |
| true = None |
|
|
| if true is None: |
| ok = 0.0 <= pred <= 1.0 |
| return ScoreResult(1.0 if ok else 0.0, {"mode": "format_only", "id": sample.get("id"), "pred": pred}) |
|
|
| err = abs(true - pred) |
| return ScoreResult(max(0.0, 1.0 - err), {"id": sample.get("id"), "pred": pred, "true": true, "abs_error": err}) |
|
|
| 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)} |
|
|