
    h{                     j    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y)	    )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y)ImageCaptionLoaderzLoad 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                 v    t        |t        t        t        f      r	|g| _        n|| _        || _        || _        y)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       q/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/document_loaders/image_captions.py__init__zImageCaptionLoader.__init__   s4     fsD%01!(DK DK,$    returnc                 D   	 ddl m}m} |j	                  | j
                        }|j	                  | j                        }g }| j                  D ]7  }| j                  |||      \  }}t        ||      }	|j                  |	       9 |S # t        $ r t        d      w xY w)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 "11$2E2EF	,<<T__M[[ 	 E $ ? ?y !@ !GX (CCNN3	  #  	. 	s   B
 
Br   r   r   c                    	 ddl m} |}	 t        |t              r*|j                  t        |            j                  d      }nt        |t              rb|j                  d      s|j                  d      r@|j                  t        j                  |d      j                        j                  d      }n |j                  |      j                  d      } ||dd      } |j                  di |}|j!                  |d         }	t        |t              rddi}
|	|
fS dt        |      i}
|	|
fS # t        $ r t        d      w xY w# t        $ r. t        |t              rd	}t        |      d
| }t        |      w xY w)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+   r0   msginputsoutputr'   r    s              r   r$   z-ImageCaptionLoader._get_captions_and_metadata@   s   	! 	"%'

75>2::5AE3'  +u/?/?
/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 	   	",.; S/! 6l^DS/!	"s   D1 CE	 1E	7F N)%Salesforce/blip-image-captioning-baserA   )__name__
__module____qualname____doc__r   r   r   r   r   r   r	   r)   r   r   dictr$   r2   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   r7   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r2   r   r   <module>rL      s(      * *  - @[! [!r   