Ë
    T^(h!%  ã                   ó  — d dl mZmZ ddlmZmZmZmZmZm	Z	 ddl
mZmZ  e«       rd dlmZ ddlmZ  e«       rddlmZ  e«       r
d d	lZdd
lmZ  ej,                  e«      Z e edd¬«      «       G d„ de«      «       Zy	)é    )ÚListÚUnioné   )Úadd_end_docstringsÚis_tf_availableÚis_torch_availableÚis_vision_availableÚloggingÚrequires_backendsé   )ÚPipelineÚbuild_pipeline_init_args)ÚImage)Ú
load_image)Ú'TF_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMESN)Ú$MODEL_FOR_VISION_2_SEQ_MAPPING_NAMEST)Úhas_tokenizerÚhas_image_processorc                   ój   ‡ — e Zd ZdZˆ fd„Zd
d„Zddeeee   ded   f   fˆ fd„Z	dd„Z
d„ Zd	„ Zˆ xZS )ÚImageToTextPipelinea  
    Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> captioner = pipeline(model="ydshieh/vit-gpt2-coco-en")
    >>> captioner("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
    [{'generated_text': 'two birds are standing next to each other '}]
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    This image to text pipeline can currently be loaded from pipeline() using the following task identifier:
    "image-to-text".

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?pipeline_tag=image-to-text).
    c                 óš   •— t        ‰| �  |i |¤Ž t        | d«       | j                  | j                  dk(  rt
        «       y t        «       y )NÚvisionÚtf)ÚsuperÚ__init__r   Úcheck_model_typeÚ	frameworkr   r   )ÚselfÚargsÚkwargsÚ	__class__s      €úb/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/image_to_text.pyr   zImageToTextPipeline.__init__E   sD   ø€ Ü‰Ñ˜$Ð) &Ò)Ü˜$ Ô)Ø×ÑØ7;·~±~ÈÒ7MÔ3õ	
ÜSwõ	
ó    c                 ó  — i }i }|�||d<   |�||d<   |�||d<   |�"|�d|v rt        d«      ‚|j                  |«       | j                  �| j                  |d<   | j                  �| j                  |d<   | j                  |d<   ||i fS )NÚpromptÚtimeoutÚmax_new_tokenszp`max_new_tokens` is defined both as an argument and inside `generate_kwargs` argument, please use only 1 versionÚassistant_modelÚ	tokenizerÚassistant_tokenizer)Ú
ValueErrorÚupdater(   r*   r)   )r   r'   Úgenerate_kwargsr%   r&   Úforward_paramsÚpreprocess_paramss          r"   Ú_sanitize_parametersz(ImageToTextPipeline._sanitize_parametersL   sË   € ØˆØÐàÐØ*0Ð˜hÑ'ØÐØ+2Ð˜iÑ(àÐ%Ø/=ˆNÐ+Ñ,ØÐ&ØÐ)Ð.>À/Ñ.QÜ ð&óð ð ×!Ñ! /Ô2à×ÑÐ+Ø04×0DÑ0DˆNÐ,Ñ-Ø×#Ñ#Ð/Ø*.¯.©.ˆN˜;Ñ'Ø48×4LÑ4LˆNÐ0Ñ1à  .°"Ð4Ð4r#   ÚinputszImage.Imagec                 óh   •— d|v r|j                  d«      }|€t        d«      ‚t        ‰| �  |fi |¤ŽS )a¿  
        Assign labels to the image(s) passed as inputs.

        Args:
            inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`):
                The pipeline handles three types of images:

                - A string containing a HTTP(s) link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

                The pipeline accepts either a single image or a batch of images.

            max_new_tokens (`int`, *optional*):
                The amount of maximum tokens to generate. By default it will use `generate` default.

            generate_kwargs (`Dict`, *optional*):
                Pass it to send all of these arguments directly to `generate` allowing full control of this function.

            timeout (`float`, *optional*, defaults to None):
                The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
                the call may block forever.

        Return:
            A list or a list of list of `dict`: Each result comes as a dictionary with the following key:

            - **generated_text** (`str`) -- The generated text.
        ÚimageszBCannot call the image-to-text pipeline without an inputs argument!)Úpopr+   r   Ú__call__)r   r1   r    r!   s      €r"   r5   zImageToTextPipeline.__call__g   sA   ø€ ð< �vÑØ—Z‘Z Ó)ˆFØˆ>ÜÐaÓbÐbÜ‰wÑ Ñ1¨&Ñ1Ð1r#   c                 óê  — t        ||¬«      }|��ñt        j                  d«       t        |t        «      st        dt        |«      › d�«      ‚| j                  j                  j                  }|dk(  r·| j                  || j                  ¬«      }| j                  dk(  r|j                  | j                  «      }| j                  |d¬	«      j                  }| j                  j                   g|z   }t#        j$                  |«      j'                  d
«      }|j)                  d|i«       �n|dk(  rI| j                  ||| j                  ¬«      }| j                  dk(  rí|j                  | j                  «      }nÑ|dk7  rv| j                  || j                  ¬«      }| j                  dk(  r|j                  | j                  «      }| j                  || j                  ¬«      }|j)                  |«       nVt        d|› d�«      ‚| j                  || j                  ¬«      }| j                  dk(  r|j                  | j                  «      }| j                  j                  j                  dk(  r|€d |d<   |S )N)r&   u¦   Passing `prompt` to the `image-to-text` pipeline is deprecated and will be removed in version 4.48 of ðŸ¤— Transformers. Use the `image-text-to-text` pipeline insteadz&Received an invalid text input, got - zy - but expected a single string. Note also that one single text can be provided for conditional image to text generation.Úgit)r3   Úreturn_tensorsÚptF)ÚtextÚadd_special_tokensr   Ú	input_idsÚ
pix2struct)r3   Úheader_textr8   zvision-encoder-decoder)r8   zModel type z- does not support conditional text generation)r   ÚloggerÚwarning_onceÚ
isinstanceÚstrr+   ÚtypeÚmodelÚconfigÚ
model_typeÚimage_processorr   ÚtoÚtorch_dtyper)   r<   Úcls_token_idÚtorchÚtensorÚ	unsqueezer,   )r   Úimager%   r&   rF   Úmodel_inputsr<   Útext_inputss           r"   Ú
preprocesszImageToTextPipeline.preprocess‹   s<  € Ü˜5¨'Ô2ˆàÑÜ×ÑðWôô ˜f¤cÔ*Ü Ø<¼TÀ&»\¸Nð Koð oóð ð
 Ÿ™×*Ñ*×5Ñ5ˆJà˜UÒ"Ø#×3Ñ3¸5ÐQU×Q_ÑQ_Ð3Ó`�Ø—>‘> TÒ)Ø#/§?¡?°4×3CÑ3CÓ#D�LØ ŸN™N°È5˜NÓQ×[Ñ[�	Ø!Ÿ^™^×8Ñ8Ð9¸IÑE�	Ü!ŸL™L¨Ó3×=Ñ=¸aÓ@�	Ø×#Ñ# [°)Ð$<Ö=à˜|Ò+Ø#×3Ñ3¸5ÈfÐei×esÑesÐ3Ót�Ø—>‘> TÒ)Ø#/§?¡?°4×3CÑ3CÓ#D‘LàÐ7Ò7à#×3Ñ3¸5ÐQU×Q_ÑQ_Ð3Ó`�Ø—>‘> TÒ)Ø#/§?¡?°4×3CÑ3CÓ#D�LØ"Ÿn™n¨VÀDÇNÁN˜nÓS�Ø×#Ñ# KÕ0ô ! ;¨z¨lÐ:gÐ!hÓiÐið  ×/Ñ/°uÈTÏ^É^Ð/Ó\ˆLØ�~‰~ Ò%Ø+Ÿ™¨t×/?Ñ/?Ó@�à�:‰:×Ñ×'Ñ'¨5Ò0°V°^Ø(,ˆL˜Ñ%àÐr#   c                 ó  — d|v r-t        |d   t        «      rt        d„ |d   D «       «      rd |d<   d|vr| j                  |d<   |j	                  | j
                  j                  «      } | j
                  j                  |fi |¤|¤Ž}|S )Nr<   c              3   ó$   K  — | ]  }|d u –— Œ
 y ­w©N© )Ú.0Úxs     r"   ú	<genexpr>z/ImageToTextPipeline._forward.<locals>.<genexpr>Ä   s   è ø€ ÒA !�A˜”IÑAùs   ‚Úgeneration_config)rA   ÚlistÚallrY   r4   rD   Úmain_input_nameÚgenerate)r   rO   r-   r1   Úmodel_outputss        r"   Ú_forwardzImageToTextPipeline._forward¾   s•   € ð ˜<Ñ'Ü˜<¨Ñ4´dÔ;ÜÑA |°KÑ'@ÔAÔAà(,ˆL˜Ñ%ð  oÑ5Ø37×3IÑ3IˆOÐ/Ñ0ð ×!Ñ! $§*¡*×"<Ñ"<Ó=ˆØ+˜Ÿ
™
×+Ñ+¨FÑV°lÐVÀoÑVˆØÐr#   c                 óx   — g }|D ]2  }d| j                   j                  |d¬«      i}|j                  |«       Œ4 |S )NÚgenerated_textT)Úskip_special_tokens)r)   ÚdecodeÚappend)r   r^   ÚrecordsÚ
output_idsÚrecords        r"   ÚpostprocesszImageToTextPipeline.postprocessÔ   sQ   € ØˆØ'ò 	#ˆJà  $§.¡.×"7Ñ"7ØØ(,ð #8ó #ðˆFð �N‰N˜6Õ"ð	#ð ˆr#   )NNNNrT   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r0   r   rB   r   r5   rQ   r_   rh   Ú__classcell__)r!   s   @r"   r   r   -   sF   ø„ ñô,
ó5ñ6"2˜u S¨$¨s©)°]ÀDÈÑDWÐ%WÑXõ "2óH1òfö,
r#   r   )Útypingr   r   Úutilsr   r   r   r	   r
   r   Úbaser   r   ÚPILr   Úimage_utilsr   Úmodels.auto.modeling_tf_autor   rK   Úmodels.auto.modeling_autor   Ú
get_loggerri   r?   r   rU   r#   r"   ú<module>rv      s{   ð÷  ÷÷ ÷ 5ñ ÔÝå(áÔÝVáÔÛåPà	ˆ×	Ñ	˜HÓ	%€ñ Ñ,¸4ÐUYÔZÓ[ôp˜(ó pó \ñpr#   