File size: 3,461 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
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
import argparse
import json
from pathlib import Path

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

PREDICATE_TEXT = {
    "left_of": "to the left of",
    "right_of": "to the right of",
    "above": "above",
    "below": "below",
    "in_front_of": "in front of",
    "behind": "behind",
    "on": "on",
    "not_on": "not on",
}

STATUS = {"Tpos": "ASSERTED", "Tneg": "NEGATED", "Tabs": "ABSENT", "Tunk": "UNKNOWN"}


def subject_phrase(category):
    return category.replace("_", " ")


def relation_for(family, question):
    if family == "left_right":
        return "left_of"
    if family == "above_below":
        return "above"
    if family == "front_behind":
        return "in_front_of"
    return "on"


def evidence_block(family, subject, obj, condition):
    predicate = PREDICATE_TEXT[relation_for(family, None)]
    entities = [
        {"id": "e1", "name": subject_phrase(subject), "category": subject},
        {"id": "e2", "name": subject_phrase(obj), "category": obj},
    ]
    if condition == "Tabs":
        relation = {
            "subject_id": "e1",
            "object_id": "e2",
            "predicate": None,
            "claim_status": STATUS[condition],
        }
    else:
        relation = {
            "subject_id": "e1",
            "object_id": "e2",
            "predicate": f"not {predicate}" if condition == "Tneg" else predicate,
            "claim_status": STATUS[condition],
        }
    payload = {"entities": entities, "relation_evidence": relation}
    return json.dumps(payload, ensure_ascii=False, indent=2)


def build_prompt(question, condition, family, subject, obj):
    if condition == "T0":
        return f"Look at the image.\n{question}\nRespond with exactly one word: yes or no."
    evidence = evidence_block(family, subject, obj, condition)
    return (
        f"Look at the image.\n{question}\n"
        f"A previous step produced this relation evidence:\n{evidence}\n"
        "Respond with exactly one word: yes or no."
    )


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--truth", required=True)
    parser.add_argument("--out", required=True)
    args = parser.parse_args()
    rows = [json.loads(line) for line in Path(args.truth).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 row in rows:
            spec_subject = row["family"]
            subject = row.get("subject_category") or ""
            obj = row.get("object_category") or ""
            for condition in CONDITIONS:
                prompt = build_prompt(row["question"], condition, spec_subject, subject, obj)
                record = {
                    "scene_id": row["scene_id"],
                    "state": row["state"],
                    "image": row["image"],
                    "question": row["question"],
                    "truth": row["computed"],
                    "declared_truth": row["declared"],
                    "condition": condition,
                    "prompt": prompt,
                    "prompt_sha256": __import__("hashlib").sha256(prompt.encode("utf-8")).hexdigest(),
                }
                fh.write(json.dumps(record, ensure_ascii=False) + "\n")
                written += 1
    print(json.dumps({"rows": len(rows), "prompts": written, "conditions": list(CONDITIONS)}))


if __name__ == "__main__":
    main()