Spaces:
Runtime error
Runtime error
Update agents/agent.py
Browse files- 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
|
| 35 |
-
"""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 56 |
|
| 57 |
-
# Attach
|
| 58 |
-
step_log.observations_images = [
|
| 59 |
|
| 60 |
# Keep metadata for context
|
| 61 |
obs_data["image_info"] = {
|
| 62 |
"original_dimensions": f"{img.size[0]}x{img.size[1]} pixels",
|
| 63 |
-
"
|
| 64 |
-
"mode":
|
| 65 |
-
"note": "Image
|
| 66 |
}
|
| 67 |
step_log.observations = json.dumps(obs_data, indent=2)
|
| 68 |
-
print(f"[Callback] β Attached
|
| 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 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
# Attach both
|
| 90 |
-
step_log.observations_images = [
|
| 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 |
-
"
|
| 96 |
-
"note": "Both images
|
| 97 |
}
|
| 98 |
step_log.observations = json.dumps(obs_data, indent=2)
|
| 99 |
-
print(f"[Callback] β Attached both
|
| 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 |
|