
    Ng                     r    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e          ZdS )	    )BytesIO)Path)AnyListTupleUnionN)Document)
BaseLoaderc                       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Z	dee
         fdZd	ed
edeeeef         deeef         f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                     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   s       o/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/image_captions.py__init__zImageCaptionLoader.__init__   sB     fsD%011 	!!(DKK DK,$    returnc                 V   	 ddl m}m} n# t          $ r t          d          w xY w|                    | j                  }|                    | j                  }g }| j        D ]C}|                     |||          \  }}t          ||          }	|
                    |	           D|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)page_contentmetadata)transformersr   r   ImportErrorfrom_pretrainedr   r   r   _get_captions_and_metadatar	   append)
r   r   r   r   r   resultsr   captionr!   docs
             r   loadzImageCaptionLoader.load)   s    	PPPPPPPPP 	 	 	.  	 "11$2EFF	,<<T_MM[ 	  	 E $ ? ?y !@ ! !GX (CCCCNN3s    %r   r   r   c                 z   	 ddl m} n# t          $ r t          d          w xY w|}	 t          |t                    r6|                    t          |                                        d          }nt          |t                    rl|	                    d          s|	                    d          rB|                    t          j        |d          j                                      d          }n(|                    |                              d          }n:# t          $ r- t          |t                    rd	}nd
| }t          |          w xY w ||dd          } |j        d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)return_tensorsimage_sourcezImage bytes provided
image_path )PILr,   r#   r   r   openr   convertr   
startswithrequestsgetraw	Exception
ValueErrorgeneratedecode)r   r   r   r   r,   r1   msginputsoutputr(   r!   s              r   r%   z-ImageCaptionLoader._get_captions_and_metadata@   s   	!!!!!!! 	 	 	S  	
 	"%'' 9

75>>22::5AAE3'' 9  ++9/4/?/?
/K/K9 

8<d#C#C#C#GHHPPQVWW

5))11%88 	" 	" 	",.. E;DlDDS//!	" 5-EEE))&)) ''q	22lE** 	9,.DEHH$c,&7&78H  s   	 #C4D 7EN)r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r	   r*   r   r   dictr%   r3   r   r   r   r      s          FA	% %c4U3t3C-D(EEF% % 	% % % %,d8n    .&!&!%(&!16sD%7G1H&!	sDy	&! &! &! &! &! &!r   r   )ior   pathlibr   typingr   r   r   r   r8   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r3   r   r   <module>rL      s                * * * * * * * * * * * *  - - - - - - @ @ @ @ @ @[! [! [! [! [! [! [! [! [! [!r   