MCPcopy Create free account
hub / github.com/ChenWu98/agent-attack / load

Function load

scripts/eval_step_attack.py:202–251  ·  view source on GitHub ↗
(example: dict, attack: str, use_caption: bool, args)

Source from the content-addressed store, hash-verified

200
201
202def load(example: dict, attack: str, use_caption: bool, args) -> tuple:
203 intent = example["intent"]
204 images = []
205 if example["query_image"]:
206 images.append(example["query_image"].convert("RGB"))
207 obs_text = example["obs_text"] if use_caption else example["no_cap_obs_text"]
208
209 if attack == "clip_attack":
210 suffixes = {
211 "gpt-4-vision-preview": "",
212 "gemini-1.5-pro-preview-0409": "_gemini-1.5-pro-latest",
213 "claude-3-opus-20240229": "_claude-3-opus-20240229",
214 "gpt-4o-2024-05-13": "_gpt-4o-2024-05-13",
215 }
216 attack += suffixes[args.model]
217
218 # Modify the caption
219 if use_caption:
220 caption_file = os.path.join("exp_data", "agent_adv", example["id"], f"{attack}_attack_caption.txt")
221 with open(caption_file, "r") as f:
222 caption = f.read().strip()
223 modified_obs_text = replace_target_caption(obs_text, example["victim_som_id"], caption)
224 print(f"Target caption: {example['target_caption']}")
225 else:
226 modified_obs_text = obs_text
227 print(f"Modified observation text: {modified_obs_text}")
228
229 # Modify the screenshot
230 modified_screenshot = example["obs_screenshot"]
231 if attack != "none":
232 adv_image_file = os.path.join("exp_data", "agent_adv", example["id"], f"{attack}_attack_image.png")
233 adv_image = Image.open(adv_image_file)
234 # Resize the adversarial image and paste it on the screenshot
235 x, y = example["position"]["position"]
236 w, h = example["position"]["size"]
237 adv_image = adv_image.resize((w, h))
238 modified_screenshot.paste(adv_image, (x, y))
239 # Visualize the modified screenshot
240 # plt.imshow(modified_screenshot)
241 # plt.show()
242
243 trajectory = [
244 {
245 "observation": {"text": modified_obs_text, "image": np.array(modified_screenshot)},
246 "info": {"page": DetachedPage(example["obs_url"], "")},
247 }
248 ]
249 meta_data = {"action_history": [example["previous_action"]]}
250
251 return intent, images, trajectory, meta_data
252
253
254@beartype

Callers 1

runFunction · 0.70

Calls 1

replace_target_captionFunction · 0.85

Tested by

no test coverage detected