Ë
    ÜÓ^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y	)é    )Ú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                   óP  ‡ — 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                 óf   •— t        ‰| �  di |¤Ž t        | d«       | j                  t        «       y )NÚvision© )ÚsuperÚ__init__r   Úcheck_model_typer   )ÚselfÚkwargsÚ	__class__s     €ú€/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/pipelines/zero_shot_image_classification.pyr   z,ZeroShotImageClassificationPipeline.__init__C   s,   ø€ Ü‰ÑÑ"˜6Ò"ä˜$ Ô)Ø×ÑÔTÕUó    ÚimagezImage.ImageÚcandidate_labelsr   Úreturnc                  ó   — y ©Nr   ©r   r!   r"   r   s       r   Ú__call__z,ZeroShotImageClassificationPipeline.__call__I   s   € ð  #r    c                  ó   — y r%   r   r&   s       r   r'   z,ZeroShotImageClassificationPipeline.__call__N   s   € ð &)r    c                 ól   •— d|v r|j                  d«      }|€t        d«      ‚t        ‰| �  |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`.
        ÚimageszSCannot 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   sH   ø€ ðJ �vÑØ—J‘J˜xÓ(ˆEØˆ=ÜÐrÓsÐsÜ‰wÑ ÑSÐ8HÐSÈFÑSÐSr    c                 óX   — 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~   sa   € ØÐØ Ñ'Ø4:Ð;MÑ4NÐÐ0Ñ1Ø˜ÑØ+1°)Ñ+<Ð˜iÑ(Ø  FÑ*Ø7=Ð>SÑ7TÐÐ3Ñ4à  " bÐ(Ð(r    c                 ó¢  — |€i }t        ||¬«      }| j                  |gd¬«      }|j                  | j                  «      }||d<   |D �cg c]  }|j	                  |«      ‘Œ }}ddi}	d| j
                  j                  j                  v r|	j                  dd	d¬
«       |	j                  |«        | j                  |fddi|	¤Ž}
|
g|d<   |S c c}w )N)r.   Úpt)r*   Úreturn_tensorsr"   ÚpaddingTÚsiglipÚ
max_lengthé@   )r6   r8   Ú
truncationr5   Útext_inputs)
r   Úimage_processorÚtoÚdtypeÚformatÚmodelÚconfigÚ
model_typeÚupdateÚ	tokenizer)r   r!   r"   r/   r.   r0   ÚinputsÚxÚ	sequencesÚtokenizer_default_kwargsr;   s              r   Ú
preprocessz.ZeroShotImageClassificationPipeline.preprocess‰   sé   € ð Ð#Ø!ÐÜ˜5¨'Ô2ˆØ×%Ñ%¨e¨WÀTÐ%ÓJˆØ—‘˜4Ÿ:™:Ó&ˆØ%5ˆÐ!Ñ"Ø<LÖM°qÐ(×/Ñ/°Õ2ÐMˆ	ÐMØ$-¨tÐ#4Ð Ø�t—z‘z×(Ñ(×3Ñ3Ñ3Ø$×+Ñ+°LÈRÐ\`Ð+ÔaØ ×'Ñ'Ð(8Ô9Ø$�d—n‘n YÑ`¸tÐ`ÐG_Ñ`ˆØ!, ˆˆ}ÑØˆùò Ns   Á
Cc                 óÒ   — |j                  d«      }|j                  d«      }t        |d   t        «      r|d   }n|d   d   } | j                  di |¤|¤Ž}||j                  dœ}|S )Nr"   r;   r   )r"   Úlogitsr   )r+   Ú
isinstancer   r@   Úlogits_per_image)r   Úmodel_inputsr"   r;   ÚoutputsÚmodel_outputss         r   Ú_forwardz,ZeroShotImageClassificationPipeline._forward    s€   € Ø'×+Ñ+Ð,>Ó?ÐØ"×&Ñ& }Ó5ˆÜ�k !‘n¤hÔ/Ø% a™.‰Kð & a™.¨Ñ+ˆKà�$—*‘*Ñ;˜{Ð;¨lÑ;ˆð !1Ø×.Ñ.ñ
ˆð Ðr    c                 óú  — |j                  d«      }|d   d   }d| j                  j                  j                  v rHt	        j
                  |«      j                  d«      }|j                  «       }t        |t        «      sH|g}nD|j                  d¬«      j                  d«      }|j                  «       }t        |t        «      s|g}t        t        ||«      d„ ¬«      D ��cg c]
  \  }}||d	œ‘Œ }}}|S c c}}w )
Nr"   rK   r   r7   éÿÿÿÿ)Údimc                 ó   — | d    S )Nr   r   )rF   s    r   ú<lambda>zAZeroShotImageClassificationPipeline.postprocess.<locals>.<lambda>Á   s   € Ð_`ÐabÑ_cÐ^c€ r    )Úkey)ÚscoreÚlabel)r+   r@   rA   rB   ÚtorchÚsigmoidÚsqueezeÚtolistrL   ÚlistÚsoftmaxÚsortedÚzip)	r   rP   r"   rK   ÚprobsÚscoresrX   Úcandidate_labelÚresults	            r   Úpostprocessz/ZeroShotImageClassificationPipeline.postprocess±   së   € Ø(×,Ñ,Ð-?Ó@ÐØ˜xÑ(¨Ñ+ˆØ�t—z‘z×(Ñ(×3Ñ3Ñ3Ü—M‘M &Ó)×1Ñ1°"Ó5ˆEØ—\‘\“^ˆFÜ˜f¤dÔ+Ø ˜‘à—N‘N r�NÓ*×2Ñ2°2Ó6ˆEØ—\‘\“^ˆFÜ˜f¤dÔ+Ø ˜�ô +1´°VÐ=MÓ1NÑTcÔ*d÷
á&��ð  oÓ6ð
ˆñ 
ð ˆùó	
s   Ã#C7r%   )NzThis is a photo of {}.NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r   r   Ústrr^   r   Údictr'   r2   rI   rQ   rf   Ú__classcell__)r   s   @r   r   r      s`  ø„ ñð@ €OØ ÐØ#ÐØ€OôVð ð#Ø˜3 Ð-Ñ.ð#ØBFÀsÁ)ð#ØWZð#à	ˆd�3˜�8‰nÑ	ò#ó ð#ð ð)Ø˜#‘Y  mÑ!4Ñ4ð)ØHLÈSÉ	ð)Ø]`ð)à	ˆd�4˜˜S˜‘>Ñ"Ñ	#ò)ó ð)ð)Tà�S˜$˜s™) ]°D¸Ñ4GÐGÑHð)Tð ˜s™)ð)Tð ð	)Tð
 
ˆd�3˜�8‰nÑ	  T¨$¨s°C¨x©.Ñ%9Ñ :Ñ	:õ)TóV	)ð Ø4ØØóò.ö"r    r   )Úcollectionsr   Útypingr   r   r   Úutilsr   r	   r
   r   r   Úbaser   r   ÚPILr   Úimage_utilsr   rZ   Úmodels.auto.modeling_autor   Ú
get_loggerrg   Úloggerr   r   r    r   ú<module>r{      su   ðÝ  ß 'Ñ '÷õ ÷ 5ñ ÔÝå(áÔÛåbð 
ˆ×	Ñ	˜HÓ	%€ñ Ñ,ÀÔFÓGôf¨(ó fó Hñfr    