import json, math, torchfrom PIL import Imagefrom peft import PeftModelfrom transformers import AutoModelForImageTextToText, AutoProcessor, BitsAndBytesConfig BASE_MODEL = "Qwen/Qwen3.5-4B"ADAPTER_ID = "OpenAdapt/openadapt-veto-4b-r6"LABELS = ("PASS", "VETO", "ABSTAIN") SYSTEM_PROMPT = ( "You are OpenAdapt-Veto, a strict visual verification guard. " "Inspect the post-action screenshot and intended state summary. " "Reply with exactly one token: PASS, VETO, or ABSTAIN. " "If any validation error, wrong field value, unexpected state, or session reset is visible, reply VETO. " "If the target region is occluded or ambiguous, reply ABSTAIN. " "Never guess PASS when uncertain.") bnb = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True,)processor = AutoProcessor.from_pretrained(ADAPTER_ID)base = AutoModelForImageTextToText.from_pretrained( BASE_MODEL, quantization_config=bnb, device_map="auto", torch_dtype=torch.bfloat16)model = PeftModel.from_pretrained(base, ADAPTER_ID).eval() image = Image.open("post_action.png").convert("RGB")dom_summary = {"intended_fields": {"record_id": "REC-0104", "amount": "$420.00"}, "aria_errors": []}user_text = ( "Verify whether the post-action screenshot matches the intended state with zero visual faults.\n" f"Intended DOM summary: {json.dumps(dom_summary, sort_keys=True)}\n" "Answer (PASS, VETO, or ABSTAIN):")messages = [ {"role": "system", "content": [{"type": "text", "text": SYSTEM_PROMPT}]}, {"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": user_text}]},]prompt = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)inputs = processor(text=[prompt], images=[image], return_tensors="pt").to(model.device) label_ids = [processor.tokenizer.encode(lbl, add_special_tokens=False)[0] for lbl in LABELS]with torch.no_grad(): logits = model(**inputs).logits[0, -1, :].float() vals = [float(logits[tid].item()) for tid in label_ids] m = max(vals) exps = [math.exp(x - m) for x in vals] probs = [e / sum(exps) for e in exps] verdict = LABELS[max(range(3), key=lambda i: probs[i])]print({"verdict": verdict, "probs": dict(zip(LABELS, [round(p, 4) for p in probs]))})