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()