Skip to content
Snippets Groups Projects
Unverified Commit 19644078 authored by Alex Yang's avatar Alex Yang Committed by GitHub
Browse files

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

parent d85faf17
No related branches found
No related tags found
No related merge requests found
import base64
import os
import filetype
import logging
from typing import List, Optional, Sequence
......@@ -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.
......
......@@ -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)
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment