mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-04 20:46:45 +00:00
community: better support of pathlib paths in document loaders (#18396)
So this arose from the https://github.com/langchain-ai/langchain/pull/18397 problem of document loaders not supporting `pathlib.Path`. This pull request provides more uniform support for Path as an argument. The core ideas for this upgrade: - if there is a local file path used as an argument, it should be supported as `pathlib.Path` - if there are some external calls that may or may not support Pathlib, the argument is immidiately converted to `str` - if there `self.file_path` is used in a way that it allows for it to stay pathlib without conversion, is is only converted for the metadata. Twitter handle: https://twitter.com/mwmajewsk
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import Any, List, Tuple, Union
|
||||
|
||||
import requests
|
||||
@@ -17,7 +18,7 @@ class ImageCaptionLoader(BaseLoader):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
images: Union[str, bytes, List[Union[str, bytes]]],
|
||||
images: Union[str, Path, bytes, List[Union[str, bytes, Path]]],
|
||||
blip_processor: str = "Salesforce/blip-image-captioning-base",
|
||||
blip_model: str = "Salesforce/blip-image-captioning-base",
|
||||
):
|
||||
@@ -29,7 +30,7 @@ class ImageCaptionLoader(BaseLoader):
|
||||
blip_processor: The name of the pre-trained BLIP processor.
|
||||
blip_model: The name of the pre-trained BLIP model.
|
||||
"""
|
||||
if isinstance(images, (str, bytes)):
|
||||
if isinstance(images, (str, Path, bytes)):
|
||||
self.images = [images]
|
||||
else:
|
||||
self.images = images
|
||||
@@ -61,7 +62,7 @@ class ImageCaptionLoader(BaseLoader):
|
||||
return results
|
||||
|
||||
def _get_captions_and_metadata(
|
||||
self, model: Any, processor: Any, image: Union[str, bytes]
|
||||
self, model: Any, processor: Any, image: Union[str, Path, bytes]
|
||||
) -> Tuple[str, dict]:
|
||||
"""Helper function for getting the captions and metadata of an image."""
|
||||
try:
|
||||
@@ -76,7 +77,9 @@ class ImageCaptionLoader(BaseLoader):
|
||||
try:
|
||||
if isinstance(image, bytes):
|
||||
image = Image.open(BytesIO(image)).convert("RGB")
|
||||
elif image.startswith("http://") or image.startswith("https://"):
|
||||
elif isinstance(image, str) and (
|
||||
image.startswith("http://") or image.startswith("https://")
|
||||
):
|
||||
image = Image.open(requests.get(image, stream=True).raw).convert("RGB")
|
||||
else:
|
||||
image = Image.open(image).convert("RGB")
|
||||
@@ -94,6 +97,6 @@ class ImageCaptionLoader(BaseLoader):
|
||||
if isinstance(image_source, bytes):
|
||||
metadata: dict = {"image_source": "Image bytes provided"}
|
||||
else:
|
||||
metadata = {"image_path": image_source}
|
||||
metadata = {"image_path": str(image_source)}
|
||||
|
||||
return caption, metadata
|
||||
|
Reference in New Issue
Block a user