Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(multimodal): check image path is file before open #18043

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import base64
import os

import filetype
import logging
from typing import List, Optional, Sequence
Expand Down Expand Up @@ -57,13 +59,14 @@ def image_documents_to_base64(
for image_document in image_documents:
if image_document.image: # This field is already base64-encoded
image_encodings.append(image_document.image)
elif (
elif image_document.image_path and os.path.isfile(
image_document.image_path
): # This field is a path to the image, which is then encoded.
image_encodings.append(encode_image(image_document.image_path))
elif (
"file_path" in image_document.metadata
and image_document.metadata["file_path"] != ""
and os.path.isfile(image_document.metadata["file_path"])
): # Alternative path to the image, which is then encoded.
image_encodings.append(encode_image(image_document.metadata["file_path"]))
elif image_document.image_url: # Image can also be pulled from the URL.
Expand Down
11 changes: 7 additions & 4 deletions llama-index-core/tests/multi_modal_llms/test_generic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ def test_image_documents_to_base64_multiple_sources():
]
with patch("requests.get") as mock_get:
mock_get.return_value.content = EXP_BINARY
with patch("builtins.open", mock_open(read_data=EXP_BINARY)):
result = image_documents_to_base64(documents)
with patch("os.path.isfile", return_value=True):
with patch("builtins.open", mock_open(read_data=EXP_BINARY)):
result = image_documents_to_base64(documents)

assert len(result) == 4
assert all(encoding == EXP_BASE64 for encoding in result)
Expand Down Expand Up @@ -144,8 +145,10 @@ def test_set_base64_and_mimetype_for_image_docs():

with patch("requests.get") as mock_get:
mock_get.return_value.content = EXP_BINARY
with patch("builtins.open", mock_open(read_data=EXP_BINARY)):
results = set_base64_and_mimetype_for_image_docs(image_docs)
# patch os.path.isfile
with patch("os.path.isfile", return_value=True):
with patch("builtins.open", mock_open(read_data=EXP_BINARY)):
results = set_base64_and_mimetype_for_image_docs(image_docs)

assert len(results) == 2
assert results[0].image == EXP_BASE64
Expand Down