o
    Zh                     @   sd   d dl mZ d dlmZ d dlmZmZmZmZ d dl	Z	d dl
mZ d dlmZ G dd deZdS )	    )BytesIO)Path)AnyListTupleUnionN)Document)
BaseLoaderc                   @   s   e Zd ZdZ		ddeeeeeeeeef  f dedefddZ	dee
 fd	d
Zdededeeeef deeef fddZdS )ImageCaptionLoaderzLoad image captions.

    By default, the loader utilizes the pre-trained
    Salesforce BLIP image captioning model.
    https://huggingface.co/Salesforce/blip-image-captioning-base
    %Salesforce/blip-image-captioning-baseimagesblip_processor
blip_modelc                 C   s0   t |tttfr|g| _n|| _|| _|| _dS )aa  Initialize with a list of image data (bytes) or file paths

        Args:
            images: Either a single image or a list of images. Accepts
                    image data (bytes) or file paths to images.
            blip_processor: The name of the pre-trained BLIP processor.
            blip_model: The name of the pre-trained BLIP model.
        N)
isinstancestrr   bytesr   r   r   )selfr   r   r    r   j/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/image_captions.py__init__   s
   

zImageCaptionLoader.__init__returnc           
      C   s   z
ddl m}m} W n ty   tdw || j}|| j}g }| jD ]}| j|||d\}}t	||d}	|
|	 q'|S )z,Load from a list of image data or file pathsr   )BlipForConditionalGenerationBlipProcessorzQ`transformers` package not found, please install with `pip install transformers`.)model	processorimage)Zpage_contentmetadata)Ztransformersr   r   ImportErrorZfrom_pretrainedr   r   r   _get_captions_and_metadatar   append)
r   r   r   r   r   resultsr   captionr   docr   r   r   load)   s"   

zImageCaptionLoader.loadr   r   r   c                 C   s.  zddl m} W n ty   tdw |}z8t|tr'|t|d}n&t|trE|	ds6|	drE|t
j|ddjd}n||d}W n tyi   t|tr`d	}t|d
| }t|w ||ddd}|jdi |}||d }	t|trddi}
|	|
fS dt|i}
|	|
fS )zBHelper function for getting the captions and metadata of an image.r   )ImagezA`PIL` package not found, please install with `pip install pillow`RGBzhttp://zhttps://T)streamz#Could not get image data from byteszCould not get image data for zan image ofpt)Zreturn_tensorsimage_sourcezImage bytes providedZ
image_pathNr   )ZPILr$   r   r   r   openr   convertr   
startswithrequestsgetraw	Exception
ValueErrorgeneratedecode)r   r   r   r   r$   r(   msgZinputsoutputr!   r   r   r   r   r   @   sD   




z-ImageCaptionLoader._get_captions_and_metadataN)r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r#   r   r   dictr   r   r   r   r   r
      s*    


r
   )ior   pathlibr   typingr   r   r   r   r,   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser	   r
   r   r   r   r   <module>   s    