§
    9²¸jÜ  ã                   ó  — d dl mZ d dlmZmZmZ ddl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Zdd
lmZ  e
j        e¦  «        Z e ed¬¦  «        ¦  «         G d„ de¦  «        ¦   «         Zd	S )é    )ÚUserDict)ÚAnyÚUnionÚoverloadé   )Úadd_end_docstringsÚis_torch_availableÚis_vision_availableÚloggingÚrequires_backendsé   )ÚPipelineÚbuild_pipeline_init_args)ÚImage)Ú
load_imageN)Ú6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMEST)Úhas_image_processorc                   óÎ  ‡ — e Zd ZdZdZdZdZdZˆ fd„Ze	de
edf         dee         ded	eeeef                  fd
„¦   «         Ze	dee         ed         z  dee         ded	eeeeef                           fd„¦   «         Zde
eee         ded         f         dee         ded	eeeef                  eeeeef                           z  fˆ fd„Zdd„Z	 	 	 	 dd„Zd„ Zd„ Zˆ xZS )Ú#ZeroShotImageClassificationPipelineaL  
    Zero shot image classification pipeline using `CLIPModel`. This pipeline predicts the class of an image when you
    provide an image and a set of `candidate_labels`.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> classifier = pipeline(model="google/siglip-so400m-patch14-384")
    >>> classifier(
    ...     "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png",
    ...     candidate_labels=["animals", "humans", "landscape"],
    ... )
    [{'score': 0.965, 'label': 'animals'}, {'score': 0.03, 'label': 'humans'}, {'score': 0.005, 'label': 'landscape'}]

    >>> classifier(
    ...     "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png",
    ...     candidate_labels=["black and white", "photorealist", "painting"],
    ... )
    [{'score': 0.996, 'label': 'black and white'}, {'score': 0.003, 'label': 'photorealist'}, {'score': 0.0, 'label': 'painting'}]
    ```

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

    This image classification pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"zero-shot-image-classification"`.

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=zero-shot-image-classification).
    FTc                 óŽ   •—  t          ¦   «         j        di |¤Ž t          | d¦  «         |                      t          ¦  «         d S )NÚvision© )ÚsuperÚ__init__r   Úcheck_model_typer   )ÚselfÚkwargsÚ	__class__s     €ú…/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/pipelines/zero_shot_image_classification.pyr   z,ZeroShotImageClassificationPipeline.__init__C   sK   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"å˜$ Ñ)Ô)Ð)Ø×ÒÕTÑUÔUÐUÐUÐUó    ÚimagezImage.ImageÚcandidate_labelsr   Úreturnc                 ó   — d S ©Nr   ©r   r!   r"   r   s       r   Ú__call__z,ZeroShotImageClassificationPipeline.__call__I   s	   € ð  #˜sr    c                 ó   — d S r%   r   r&   s       r   r'   z,ZeroShotImageClassificationPipeline.__call__N   s	   € ð &) Sr    c                 ó�   •— d|v r|                      d¦  «        }|€t          d¦  «        ‚ t          ¦   «         j        |fd|i|¤ŽS )aó  
        Assign labels to the image(s) passed as inputs.

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

                - A string containing a http link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

            candidate_labels (`list[str]`):
                The candidate labels for this image. They will be formatted using *hypothesis_template*.

            hypothesis_template (`str`, *optional*, defaults to `"This is a photo of {}"`):
                The format used in conjunction with *candidate_labels* to attempt the image classification by
                replacing the placeholder with the candidate_labels. Pass "{}" if *candidate_labels* are
                already formatted.

            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 of dictionaries containing one entry per proposed label. Each dictionary contains the
            following keys:
            - **label** (`str`) -- One of the suggested *candidate_labels*.
            - **score** (`float`) -- The score attributed by the model to that label. It is a value between
                0 and 1, computed as the `softmax` of `logits_per_image`.
        ÚimagesNzSCannot call the zero-shot-image-classification pipeline without an images argument!r"   )ÚpopÚ
ValueErrorr   r'   )r   r!   r"   r   r   s       €r   r'   z,ZeroShotImageClassificationPipeline.__call__S   s[   ø€ ðJ �vÐÐØ—J’J˜xÑ(Ô(ˆEØˆ=ÝÐrÑsÔsÐsØ�u‰wŒwÔ ÐSÐSÐ8HÐSÈFÐSÐSÐSr    Nc                 ój   — i }d|v r|d         |d<   d|v r|d         |d<   d|v r|d         |d<   |i i fS )Nr"   ÚtimeoutÚhypothesis_templater   )r   Útokenizer_kwargsr   Úpreprocess_paramss       r   Ú_sanitize_parametersz8ZeroShotImageClassificationPipeline._sanitize_parameters~   sj   € ØÐØ Ð'Ð'Ø4:Ð;MÔ4NÐÐ0Ñ1Ø˜ÐÐØ+1°)Ô+<Ð˜iÑ(Ø  FÐ*Ð*Ø7=Ð>SÔ7TÐÐ3Ñ4à  " bÐ(Ð(r    úThis is a photo of {}.c                 óp  ‡— |€i }t          ||¬¦  «        }|                      |gd¬¦  «        }|                     | j        ¦  «        }||d<   ˆfd„|D ¦   «         }ddi}d| j        j        j        v r|                     d	d
d¬¦  «         |                     |¦  «          | j        |fddi|¤Ž}	|	g|d<   |S )N)r.   Úpt)r*   Úreturn_tensorsr"   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r   )Úformat)Ú.0Úxr/   s     €r   ú
<listcomp>zBZeroShotImageClassificationPipeline.preprocess.<locals>.<listcomp>—   s(   ø€ ÐMÐMÐM°qÐ(×/Ò/°Ñ2Ô2ÐMÐMÐMr    ÚpaddingTÚsiglipÚ
max_lengthé@   )r<   r>   Ú
truncationr6   Útext_inputs)	r   Úimage_processorÚtoÚdtypeÚmodelÚconfigÚ
model_typeÚupdateÚ	tokenizer)
r   r!   r"   r/   r.   r0   ÚinputsÚ	sequencesÚtokenizer_default_kwargsrA   s
      `      r   Ú
preprocessz.ZeroShotImageClassificationPipeline.preprocess‰   sô   ø€ ð Ð#Ø!ÐÝ˜5¨'Ð2Ñ2Ô2ˆØ×%Ò%¨e¨WÀTÐ%ÑJÔJˆØ—’˜4œ:Ñ&Ô&ˆØ%5ˆÐ!Ñ"ØMÐMÐMÐMÐ<LÐMÑMÔMˆ	Ø$-¨tÐ#4Ð Ø�t”zÔ(Ô3Ð3Ð3Ø$×+Ò+°LÈRÐ\`Ð+ÑaÔaÐaØ ×'Ò'Ð(8Ñ9Ô9Ð9Ø$�d”n YÐ`Ð`¸tÐ`ÐG_Ð`Ð`ˆØ!, ˆˆ}ÑØˆr    c                 óð   — |                      d¦  «        }|                      d¦  «        }t          |d         t          ¦  «        r	|d         }n|d         d         } | j        di |¤|¤Ž}||j        dœ}|S )Nr"   rA   r   )r"   Úlogitsr   )r+   Ú
isinstancer   rE   Úlogits_per_image)r   Úmodel_inputsr"   rA   ÚoutputsÚmodel_outputss         r   Ú_forwardz,ZeroShotImageClassificationPipeline._forward    s–   € Ø'×+Ò+Ð,>Ñ?Ô?ÐØ"×&Ò& }Ñ5Ô5ˆÝ�k !”n¥hÑ/Ô/ð 	,Ø% aœ.ˆKˆKð & aœ.¨Ô+ˆKà�$”*Ð;Ð;˜{Ð;¨lÐ;Ð;ˆð !1ØÔ.ð
ð 
ˆð Ðr    c                 ó  — |                      d¦  «        }|d         d         }d| j        j        j        v rTt	          j        |¦  «                             d¦  «        }|                     ¦   «         }t          |t          ¦  «        s|g}nU| 
                    d¬¦  «                             d¦  «        }|                     ¦   «         }t          |t          ¦  «        s|g}d„ t          t          ||¦  «        d„ ¬	¦  «        D ¦   «         }|S )
Nr"   rO   r   r=   éÿÿÿÿ)Údimc                 ó   — g | ]
\  }}||d œ‘ŒS ))ÚscoreÚlabelr   )r9   rZ   Úcandidate_labels      r   r;   zCZeroShotImageClassificationPipeline.postprocess.<locals>.<listcomp>¿   s4   € ð 
ð 
ð 
á&��ð  oÐ6Ð6ð
ð 
ð 
r    c                 ó   — | d          S )Nr   r   )r:   s    r   ú<lambda>zAZeroShotImageClassificationPipeline.postprocess.<locals>.<lambda>Á   s   € Ð_`ÐabÔ_cÐ^c€ r    )Úkey)r+   rE   rF   rG   ÚtorchÚsigmoidÚsqueezeÚtolistrP   ÚlistÚsoftmaxÚsortedÚzip)r   rT   r"   rO   ÚprobsÚscoresÚresults          r   Úpostprocessz/ZeroShotImageClassificationPipeline.postprocess±   s  € Ø(×,Ò,Ð-?Ñ@Ô@ÐØ˜xÔ(¨Ô+ˆØ�t”zÔ(Ô3Ð3Ð3Ý”M &Ñ)Ô)×1Ò1°"Ñ5Ô5ˆEØ—\’\‘^”^ˆFÝ˜f¥dÑ+Ô+ð "Ø ˜�øà—N’N r�NÑ*Ô*×2Ò2°2Ñ6Ô6ˆEØ—\’\‘^”^ˆFÝ˜f¥dÑ+Ô+ð "Ø ˜�ð
ð 
å*0µ°VÐ=MÑ1NÔ1NÐTcÐTcÐ*dÑ*dÔ*dð
ñ 
ô 
ˆð ˆr    r%   )Nr3   NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r   r   Ústrrd   r   Údictr'   r2   rM   rU   rk   Ú__classcell__)r   s   @r   r   r      s  ø€ € € € € ðð ð@ €OØ ÐØ#ÐØ€OðVð Vð Vð Vð Vð ð#Ø˜3 Ð-Ô.ð#ØBFÀsÄ)ð#ØWZð#à	ˆd�3˜�8ŒnÔ	ð#ð #ð #ñ „Xð#ð ð)Ø˜#”Y  mÔ!4Ñ4ð)ØHLÈSÌ	ð)Ø]`ð)à	ˆd�4˜˜S˜”>Ô"Ô	#ð)ð )ð )ñ „Xð)ð)Tà�S˜$˜sœ) ]°D¸Ô4GÐGÔHð)Tð ˜sœ)ð)Tð ð	)Tð
 
ˆd�3˜�8ŒnÔ	  T¨$¨s°C¨x¬.Ô%9Ô :Ñ	:ð)Tð )Tð )Tð )Tð )Tð )TðV	)ð 	)ð 	)ð 	)ð Ø4ØØðð ð ð ð.ð ð ð"ð ð ð ð ð ð r    r   )Úcollectionsr   Útypingr   r   r   Úutilsr   r	   r
   r   r   Úbaser   r   ÚPILr   Úimage_utilsr   r`   Úmodels.auto.modeling_autor   Ú
get_loggerrl   Úloggerr   r   r    r   ú<module>r€      ss  ðØ  Ð  Ð  Ð  Ð  Ð  Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'ðð ð ð ð ð ð ð ð ð ð ð ð ð ð 5Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4ð ÐÑÔð )ØÐÐÐÐÐà(Ð(Ð(Ð(Ð(Ð(àÐÑÔð cØ€L€L€LàbÐbÐbÐbÐbÐbð 
ˆÔ	˜HÑ	%Ô	%€ð ÐÐ,Ð,ÀÐFÑFÔFÑGÔGðfð fð fð fð f¨(ñ fô fñ HÔGðfð fð fr    