hmgill commited on
Commit
4e17a1a
Β·
verified Β·
1 Parent(s): 534b2bf

Update agents/agent.py

Browse files
Files changed (1) hide show
  1. agents/agent.py +52 -27
agents/agent.py CHANGED
@@ -23,7 +23,7 @@ class CellposeAgent:
23
  def attach_images_callback(step_log: ActionStep, agent: ToolCallingAgent) -> None:
24
  """
25
  Callback to attach actual PIL images for VLM inspection.
26
- Images are automatically resized to reduce token consumption.
27
  """
28
  if not isinstance(step_log, ActionStep):
29
  return
@@ -31,16 +31,42 @@ class CellposeAgent:
31
  if not step_log.observations:
32
  return
33
 
34
- def resize_image(img: Image.Image, max_size: int = 1024) -> Image.Image:
35
- """Resize image maintaining aspect ratio, max dimension = max_size."""
36
- if max(img.size) <= max_size:
37
- return img
38
-
39
- ratio = max_size / max(img.size)
40
- new_size = tuple(int(dim * ratio) for dim in img.size)
41
- resized = img.resize(new_size, Image.Resampling.LANCZOS)
42
- print(f" Resized {img.size} β†’ {resized.size}")
43
- return resized
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  try:
46
  obs_data = json.loads(step_log.observations)
@@ -52,25 +78,24 @@ class CellposeAgent:
52
 
53
  try:
54
  img = Image.open(image_path)
55
- resized_img = resize_image(img)
56
 
57
- # Attach resized PIL Image
58
- step_log.observations_images = [resized_img]
59
 
60
  # Keep metadata for context
61
  obs_data["image_info"] = {
62
  "original_dimensions": f"{img.size[0]}x{img.size[1]} pixels",
63
- "resized_dimensions": f"{resized_img.size[0]}x{resized_img.size[1]} pixels",
64
- "mode": resized_img.mode,
65
- "note": "Image attached for visual inspection (resized for efficiency)"
66
  }
67
  step_log.observations = json.dumps(obs_data, indent=2)
68
- print(f"[Callback] βœ“ Attached resized image for VLM inspection")
69
  except Exception as e:
70
  print(f"[Callback] Error attaching image: {e}")
71
 
72
  # Pattern 2: Multiple images from refine_segmentation
73
-
74
  elif obs_data.get("status") == "ready_for_visual_analysis":
75
  paths = obs_data.get("image_paths", {})
76
  original = paths.get("original")
@@ -82,21 +107,21 @@ class CellposeAgent:
82
  orig_img = Image.open(original)
83
  seg_img = Image.open(segmented)
84
 
85
- # Resize both images
86
- resized_orig = resize_image(orig_img)
87
- resized_seg = resize_image(seg_img)
88
 
89
- # Attach both resized images as list
90
- step_log.observations_images = [resized_orig, resized_seg]
91
 
92
  obs_data["images_info"] = {
93
  "image_order": ["original", "segmented"],
94
  "original_size": f"{orig_img.size[0]}x{orig_img.size[1]}",
95
- "resized_size": f"{resized_orig.size[0]}x{resized_orig.size[1]}",
96
- "note": "Both images attached for visual comparison (resized for efficiency)"
97
  }
98
  step_log.observations = json.dumps(obs_data, indent=2)
99
- print(f"[Callback] βœ“ Attached both resized images for VLM inspection")
100
  except Exception as e:
101
  print(f"[Callback] Error attaching images: {e}")
102
 
 
23
  def attach_images_callback(step_log: ActionStep, agent: ToolCallingAgent) -> None:
24
  """
25
  Callback to attach actual PIL images for VLM inspection.
26
+ Images are automatically resized and compressed to reduce token consumption.
27
  """
28
  if not isinstance(step_log, ActionStep):
29
  return
 
31
  if not step_log.observations:
32
  return
33
 
34
+ def resize_and_compress_image(img: Image.Image, max_size: int = 512, quality: int = 75) -> Image.Image:
35
+ """
36
+ Resize and compress image to reduce payload size.
37
+
38
+ Args:
39
+ img: Input PIL Image
40
+ max_size: Maximum dimension (width or height)
41
+ quality: JPEG quality (1-95, lower = smaller file)
42
+
43
+ Returns:
44
+ Compressed PIL Image
45
+ """
46
+ # Convert to RGB if needed (JPEG doesn't support RGBA)
47
+ if img.mode in ('RGBA', 'LA', 'P'):
48
+ background = Image.new('RGB', img.size, (255, 255, 255))
49
+ if img.mode == 'P':
50
+ img = img.convert('RGBA')
51
+ background.paste(img, mask=img.split()[-1] if img.mode in ('RGBA', 'LA') else None)
52
+ img = background
53
+ elif img.mode != 'RGB':
54
+ img = img.convert('RGB')
55
+
56
+ # Resize maintaining aspect ratio
57
+ if max(img.size) > max_size:
58
+ ratio = max_size / max(img.size)
59
+ new_size = tuple(int(dim * ratio) for dim in img.size)
60
+ img = img.resize(new_size, Image.Resampling.LANCZOS)
61
+
62
+ # Compress using JPEG encoding
63
+ buffer = BytesIO()
64
+ img.save(buffer, format='JPEG', quality=quality, optimize=True)
65
+ buffer.seek(0)
66
+ compressed_img = Image.open(buffer)
67
+
68
+ print(f" Resized and compressed to {compressed_img.size}, quality={quality}")
69
+ return compressed_img
70
 
71
  try:
72
  obs_data = json.loads(step_log.observations)
 
78
 
79
  try:
80
  img = Image.open(image_path)
81
+ compressed_img = resize_and_compress_image(img, max_size=512, quality=75)
82
 
83
+ # Attach compressed PIL Image
84
+ step_log.observations_images = [compressed_img]
85
 
86
  # Keep metadata for context
87
  obs_data["image_info"] = {
88
  "original_dimensions": f"{img.size[0]}x{img.size[1]} pixels",
89
+ "processed_dimensions": f"{compressed_img.size[0]}x{compressed_img.size[1]} pixels",
90
+ "mode": compressed_img.mode,
91
+ "note": "Image compressed for API efficiency (JPEG quality=75)"
92
  }
93
  step_log.observations = json.dumps(obs_data, indent=2)
94
+ print(f"[Callback] βœ“ Attached compressed image for VLM inspection")
95
  except Exception as e:
96
  print(f"[Callback] Error attaching image: {e}")
97
 
98
  # Pattern 2: Multiple images from refine_segmentation
 
99
  elif obs_data.get("status") == "ready_for_visual_analysis":
100
  paths = obs_data.get("image_paths", {})
101
  original = paths.get("original")
 
107
  orig_img = Image.open(original)
108
  seg_img = Image.open(segmented)
109
 
110
+ # Compress both images
111
+ compressed_orig = resize_and_compress_image(orig_img, max_size=512, quality=75)
112
+ compressed_seg = resize_and_compress_image(seg_img, max_size=512, quality=75)
113
 
114
+ # Attach both compressed images as list
115
+ step_log.observations_images = [compressed_orig, compressed_seg]
116
 
117
  obs_data["images_info"] = {
118
  "image_order": ["original", "segmented"],
119
  "original_size": f"{orig_img.size[0]}x{orig_img.size[1]}",
120
+ "processed_size": f"{compressed_orig.size[0]}x{compressed_orig.size[1]}",
121
+ "note": "Both images compressed for API efficiency (JPEG quality=75)"
122
  }
123
  step_log.observations = json.dumps(obs_data, indent=2)
124
+ print(f"[Callback] βœ“ Attached both compressed images for VLM inspection")
125
  except Exception as e:
126
  print(f"[Callback] Error attaching images: {e}")
127