Skip to content

Commit

Permalink
Revert "fix(multimodal): check image path is file before open"
Browse files Browse the repository at this point in the history
This reverts commit 8f9242e.
  • Loading branch information
himself65 committed Mar 7, 2025
1 parent f7f512d commit 721f533
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ def load_image_urls(image_urls: List[str]) -> List[ImageDocument]:
return [ImageDocument(image_url=url) for url in image_urls]


def is_valid_image_path(image_path: str) -> bool:
"""Check if the image path is valid.
Args:
image_path (str): Path to the image file.
Returns:
bool: True if the image path is valid, False otherwise.
"""
import os

if os.path.isfile(image_path):
return True
return False


def encode_image(image_path: str) -> str:
"""Create base64 representation of an image.
Expand Down Expand Up @@ -73,14 +57,13 @@ 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 image_document.image_path and is_valid_image_path(
elif (
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 is_valid_image_path(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
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ def infer_image_mimetype_from_file_path(image_file_path: str) -> str:
# If the file extension is not recognized


def is_valid_image_path(image_path: str) -> bool:
"""Check if the image path is valid.
Args:
image_path (str): Path to the image file.
Returns:
bool: True if the image path is valid, False otherwise.
"""
import os

if os.path.isfile(image_path):
return True
return False


# Function to encode the image to base64 content
def encode_image(image_path: str) -> str:
with open(image_path, "rb") as image_file:
Expand Down Expand Up @@ -128,7 +112,6 @@ def create_image_content(image_document) -> Optional[Dict[str, Any]]:
elif (
"file_path" in image_document.metadata
and image_document.metadata["file_path"] != ""
and is_valid_image_path(image_document.metadata["file_path"])
):
mimetype = infer_image_mimetype_from_file_path(
image_document.metadata["file_path"]
Expand Down

0 comments on commit 721f533

Please sign in to comment.