File size: 1,737 Bytes
6d35426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
import sys
from pathlib import Path
from typing import Any

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from constants import METRIC_COLUMNS


ALLOWED_MODEL_TYPES = {"text-conditioned", "action-conditioned"}
REQUIRED_KEYS = ["model_name", "model_link", "model_type", "total_m_score"]
SUMMARY_SCORE_FIELDS = [
    "total_m_score",
    "entity_score",
    "environment_score",
    "causal_score",
]


def _normalize_key(key: str) -> str:
    return re.sub(r"[^a-z0-9]+", "_", key.lower()).strip("_")


def _is_present(value: Any) -> bool:
    return value is not None and str(value).strip() != ""


def validate_submission_json(data: dict) -> tuple[bool, str]:
    if not isinstance(data, dict):
        return False, "Submission JSON must be an object."

    normalized = {_normalize_key(str(key)): value for key, value in data.items()}

    for key in REQUIRED_KEYS:
        if key not in normalized or not _is_present(normalized[key]):
            return False, f"Missing required field: {key}"

    model_type = str(normalized["model_type"]).strip()
    if model_type not in ALLOWED_MODEL_TYPES:
        return (
            False,
            "model_type must be either text-conditioned or action-conditioned.",
        )

    score_fields = set(SUMMARY_SCORE_FIELDS)
    score_fields.update(_normalize_key(column) for column in METRIC_COLUMNS)

    for key, value in normalized.items():
        if key in score_fields and _is_present(value):
            try:
                float(value)
            except (TypeError, ValueError):
                return False, f"Score field {key} must be convertible to float."

    return True, "Submission JSON is valid."