Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions cognee/infrastructure/loaders/external/advanced_pdf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,28 @@ def _format_image_element(self, metadata: Dict[str, Any]) -> str:
"""Format image."""
placeholder = "[Image omitted]"
image_text = placeholder
coordinates = metadata.get("coordinates", {})
points = coordinates.get("points") if isinstance(coordinates, dict) else None
if points and isinstance(points, tuple) and len(points) == 4:
leftup = points[0]
rightdown = points[3]
coordinates = metadata.get("coordinates", None) # Avoid unnecessary empty dict allocation
if (
isinstance(coordinates, dict)
and (points := coordinates.get("points"))
and isinstance(points, tuple)
and len(points) == 4
):
leftup, _, _, rightdown = points
if (
isinstance(leftup, tuple)
and isinstance(rightdown, tuple)
and len(leftup) == 2
and isinstance(rightdown, tuple)
and len(rightdown) == 2
):
image_text = f"{placeholder} (bbox=({leftup[0]}, {leftup[1]}, {rightdown[0]}, {rightdown[1]}))"

layout_width = coordinates.get("layout_width")
layout_height = coordinates.get("layout_height")
system = coordinates.get("system")
if layout_width and layout_height and system:
image_text = (
image_text
+ f", system={system}, layout_width={layout_width}, layout_height={layout_height}))"
)

# Fetch all needed keys in one go to minimize dictionary lookups
layout_width = coordinates.get("layout_width")
layout_height = coordinates.get("layout_height")
system = coordinates.get("system")
if layout_width and layout_height and system:
image_text = f"{image_text}, system={system}, layout_width={layout_width}, layout_height={layout_height}))"
return image_text

def _safe_to_dict(self, element: Any) -> Dict[str, Any]:
Expand Down