File size: 1,952 Bytes
a863f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import hashlib
import json
import re
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from run_real import MANIFESTS, build_prompt, clean_question

CONDITIONS = ("T0", "Tpos", "Tneg", "Tabs", "Tunk")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", default="/mnt/f70f6709-366e-49a0-861f-497645d98975/lyz/GraphEvolve")
    ap.add_argument("--collection", required=True, choices=sorted(MANIFESTS))
    ap.add_argument("--out", required=True)
    args = ap.parse_args()

    root = Path(args.root)
    rows = [json.loads(line) for line in (root / MANIFESTS[args.collection]).open(encoding="utf-8")]
    out = Path(args.out)
    out.parent.mkdir(parents=True, exist_ok=True)
    written = 0
    with out.open("w", encoding="utf-8") as fh:
        for rec in rows:
            image_path = rec["image_path"]
            if not image_path.startswith("/"):
                image_path = str(root / image_path)
            question = clean_question(rec["question"])
            subject = rec["object_a"]
            obj = rec["object_b"]
            predicate = rec["predicate"]
            for condition in CONDITIONS:
                prompt = build_prompt(question, condition, subject, obj, predicate)
                row = {
                    "scene_id": rec["annotation_id"],
                    "state": "a",
                    "image": image_path,
                    "question": question,
                    "truth": rec["gold"],
                    "declared_truth": rec["gold"],
                    "condition": condition,
                    "prompt": prompt,
                    "prompt_sha256": hashlib.sha256(prompt.encode()).hexdigest(),
                }
                fh.write(json.dumps(row, ensure_ascii=False) + "\n")
                written += 1
    print(json.dumps({"out": str(out), "rows": written}))


if __name__ == "__main__":
    main()