
    dh{                     j    S SK Jr  S SKJr  S SKJrJrJrJr  S SK	r	S SK
Jr  S SKJr   " S S\5      rg)	    )BytesIO)Path)AnyListTupleUnionN)Document)
BaseLoaderc                       \ rS rSrSr  SS\\\\\	\\\\4      4   S\S\4S jjr
S\	\   4S jrS	\S
\S\\\\4   S\\\4   4S jrSrg)ImageCaptionLoader   zLoad image captions.

By default, the loader utilizes the pre-trained
Salesforce BLIP image captioning model.
https://huggingface.co/Salesforce/blip-image-captioning-base
imagesblip_processor
blip_modelc                 z    [        U[        [        [        45      (       a	  U/U l        OXl        X l        X0l        g)a1  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       k/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/image_captions.py__init__ImageCaptionLoader.__init__   s1     fsD%011!(DK K,$    returnc                 8    SSK JnJn  UR	                  U R
                  5      nUR	                  U R                  5      n/ nU R                   H/  nU R                  XCUS9u  px[        XxS9n	UR                  U	5        M1     U$ ! [         a    [        S5      ef = f)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   loadImageCaptionLoader.load)   s    	P "11$2E2EF	,<<T__M[[E $ ? ? !@ !G CCNN3 ! #  	. 	s   B Br   r   r    c                 &    SSK Jn  Un [        U[        5      (       a*  UR                  [        U5      5      R                  S5      nO[        U[        5      (       aj  UR                  S5      (       d  UR                  S5      (       a>  UR                  [        R                  " USS9R                  5      R                  S5      nO!UR                  U5      R                  S5      n U" USSS9nUR                  " S0 UD6nUR!                  US   5      n	[        U[        5      (       a  SS0n
X4$ S[        U5      0n
X4$ ! [         a    [        S5      ef = f! [         a)    [        U[        5      (       a  S	nOS
U 3n[        U5      ef = f)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.   r3   msginputsoutputr)   r"   s              r   r&   -ImageCaptionLoader._get_captions_and_metadata@   s~   	! 	"%''

75>2::5AE3''  ++u/?/?
/K/K

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

5)11%8 5-E)&) ''q	2lE**,.DEH    %c,&78H  A  	S 	   	",..;5l^DS/!	"s#   E >E 
A>E 	 E E3F)r   r   r   N)%Salesforce/blip-image-captioning-baserE   )__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r	   r+   r   r   dictr&   __static_attributes__r5   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   r:   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r5   r   r   <module>rR      s(      * *  - @[! [!r   