Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions ms_agent/tools/docling/doc_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def html_handle_figure(self, element: Tag, doc: DoclingDocument) -> None:

if img_url:
if img_url.startswith('data:'):
img_pil, ext = load_image_from_uri_to_pil(img_url)
img_pil = load_image_from_uri_to_pil(img_url)
else:
if not img_url.startswith('http'):
img_url = validate_url(img_url=img_url, backend=self)
Expand Down Expand Up @@ -98,7 +98,7 @@ def html_handle_image(self, element: Tag, doc: DoclingDocument) -> None:

if img_url:
if img_url.startswith('data:'):
img_pil, ext = load_image_from_uri_to_pil(img_url)
img_pil = load_image_from_uri_to_pil(img_url)
else:
if not img_url.startswith('http'):
img_url = validate_url(img_url=img_url, backend=self)
Expand Down
37 changes: 0 additions & 37 deletions ms_agent/utils/download.py

This file was deleted.

8 changes: 3 additions & 5 deletions ms_agent/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def load_image_from_url_to_pil(url: str) -> 'Image.Image':
return None


def load_image_from_uri_to_pil(uri: str) -> tuple:
def load_image_from_uri_to_pil(uri: str) -> 'Image.Image':
"""
Load image from URI as a PIL Image object and extract its format extension.
URI format: data:[<mime>][;base64],<encoded>
Expand All @@ -352,18 +352,16 @@ def load_image_from_uri_to_pil(uri: str) -> tuple:
raw = base64.b64decode(encoded)
else:
raw = encoded.encode('utf-8')
m = re.match(r'data:(image/[^;]+)', header)
ext = m.group(1).split('/')[-1] if m else 'bin'
img = Image.open(BytesIO(raw))
return img, ext
return img
except ValueError as e:
print(f'Error parsing URI format: {e}')
return None
except base64.binascii.Error as e:
print(f'Error decoding base64 data: {e}')
return None
except IOError as e:
print(f'Error opening image: {e}')
print(f'Error opening image with PIL: {e}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the clarification in the error message is helpful, using print() for logging errors is generally discouraged in application code. It's a better practice to use Python's logging module, which offers more flexibility and control (e.g., log levels, different handlers, formatting).

I recommend replacing all print calls within the except blocks of this function with logging.error(). This would require import logging at the module level.

Suggested change
print(f'Error opening image with PIL: {e}')
logging.error(f'Error opening image with PIL: {e}')

return None
except Exception as e:
print(f'Unexpected error loading image from URI: {e}')
Expand Down
Loading