File size: 5,099 Bytes
aa7e82c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Diff HuggingFace published queries vs local scene *_queries.json."""
from __future__ import annotations

import json
import sys
import urllib.request
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from scripts.predictions_ground_truth import SCENES_CONFIG

REPO = "LiYacheng/r4d-bench-qa"
BASE = f"https://huggingface.co/datasets/{REPO}/raw/main"


def fetch(path: str):
    url = f"{BASE}/{path}"
    with urllib.request.urlopen(url, timeout=60) as r:
        return r.read().decode("utf-8")


def main():
    diffs = []
    for cfg in SCENES_CONFIG:
        folder = cfg["folder"]
        local_path = ROOT / "data" / "scenes" / folder / cfg["query_file"]
        rel = f"data/scenes/{folder}/{cfg['query_file']}"
        try:
            remote_raw = fetch(rel)
        except Exception as e:
            diffs.append({"file": rel, "error": str(e)})
            print(f"FAIL fetch {rel}: {e}")
            continue
        local = json.loads(local_path.read_text(encoding="utf-8"))
        remote = json.loads(remote_raw)
        if isinstance(local, dict):
            local = local["queries"]
        if isinstance(remote, dict):
            remote = remote["queries"]
        loc = {q["query_id"]: q for q in local}
        rem = {q["query_id"]: q for q in remote}
        for qid in sorted(set(loc) | set(rem)):
            if qid not in loc:
                diffs.append({"query_id": qid, "issue": "only_on_HF"})
                continue
            if qid not in rem:
                diffs.append({"query_id": qid, "issue": "only_local"})
                continue
            L, R = loc[qid], rem[qid]
            for field in ("text_en", "text_zh", "query_type"):
                if (L.get(field) or "") != (R.get(field) or ""):
                    diffs.append(
                        {
                            "query_id": qid,
                            "issue": f"{field}_diff",
                            "local": L.get(field),
                            "hf": R.get(field),
                        }
                    )
            if sorted(L.get("target_track_ids") or []) != sorted(
                R.get("target_track_ids") or []
            ):
                diffs.append(
                    {
                        "query_id": qid,
                        "issue": "target_track_ids_diff",
                        "local": L.get("target_track_ids"),
                        "hf": R.get("target_track_ids"),
                    }
                )

    # also eval files
    for rel in [
        "evaluation/R4D-Bench_queries.json",
        "evaluation/R4D-Bench_predictions.json",
    ]:
        try:
            remote = json.loads(fetch(rel))
            local = json.loads((ROOT / rel).read_text(encoding="utf-8"))
        except Exception as e:
            diffs.append({"file": rel, "error": str(e)})
            continue
        if rel.endswith("predictions.json"):
            for qid in sorted(set(local) | set(remote)):
                if sorted(local.get(qid) or []) != sorted(remote.get(qid) or []):
                    diffs.append(
                        {
                            "query_id": qid,
                            "issue": "eval_predictions_diff",
                            "local": local.get(qid),
                            "hf": remote.get(qid),
                        }
                    )
        else:
            loc = {q["query_id"]: q for q in local}
            rem = {q["query_id"]: q for q in remote}
            for qid in sorted(set(loc) | set(rem)):
                if qid not in loc or qid not in rem:
                    diffs.append({"query_id": qid, "issue": "eval_queries_presence"})
                    continue
                if (loc[qid].get("text_en") or "") != (rem[qid].get("text_en") or ""):
                    diffs.append(
                        {
                            "query_id": qid,
                            "issue": "eval_text_en_diff",
                            "local": loc[qid].get("text_en"),
                            "hf": rem[qid].get("text_en"),
                        }
                    )
                if sorted(loc[qid].get("target_track_ids") or []) != sorted(
                    rem[qid].get("target_track_ids") or []
                ):
                    diffs.append(
                        {
                            "query_id": qid,
                            "issue": "eval_ids_diff",
                            "local": loc[qid].get("target_track_ids"),
                            "hf": rem[qid].get("target_track_ids"),
                        }
                    )

    out = ROOT / "assets" / "gt_audit" / "hf_vs_local_diff.json"
    out.write_text(json.dumps(diffs, ensure_ascii=False, indent=2), encoding="utf-8")
    print(f"DIFFS={len(diffs)}")
    for d in diffs:
        print(d)
    print(f"Wrote {out}")


if __name__ == "__main__":
    main()