a
    bg                     @   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                
   @   sv   e Zd ZdZdeeeeeeeeef  f eedddZ	ee
 dddZeeeeeef eeef d	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   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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Y n0 || j}|| j}g }| jD ].}| j|||d\}}t	||d}	|
|	 qR|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.load)r   r   r   r   c                 C   s,  zddl m} W n ty*   tdY n0 |}zpt|trR|t|d}nLt|tr|	dsp|	dr|t
j|ddjd}n||d}W n4 ty   t|trd	}n
d
| }t|Y n0 ||ddd}|jf i |}||d }	t|trddi}
n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_path)ZPILr$   r   r   r   openr   convertr   
startswithrequestsgetraw	Exception
ValueErrorgeneratedecode)r   r   r   r   r$   r(   msginputsoutputr!   r   r   r   r   r   @   s:    





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   