
    cCi!                     @   S SK r S SKJr  S SKJrJrJr  SSKJrJ	r	J
r
JrJrJr  SSKJrJr  \" 5       (       a  S SKJr  SS	KJr  \
" 5       (       a
  S SKrSS
KJr  \	" 5       (       a  SSKJr  SSKJr  \R8                  " \5      r\" \" SS95       " S S\5      5       rg)    N)UserDict)AnyUnionoverload   )add_end_docstringsis_tf_availableis_torch_availableis_vision_availableloggingrequires_backends   )Pipelinebuild_pipeline_init_args)Image)
load_image)6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES)9TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES)stable_softmaxT)has_image_processorc                   n  ^  \ rS rSrSrSrSrSrSrU 4S jr	\
S\\S4   S\\   S	\S
\\\\4      4S j5       r\
S\\\   \S   4   S\\   S	\S
\\\\\4         4S j5       rS\\\\   S\S   4   S\\   S	\S
\\\\\4      \\\\\4         4   4U 4S jjrSS jr    SS jrS rS rSrU =r$ )#ZeroShotImageClassificationPipeline!   a  
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U ]  " S0 UD6  [        U S5        U R                  U R                  S:X  a  [
        5        g [        5        g )Nvisiontf )super__init__r   check_model_type	frameworkr   r   )selfkwargs	__class__s     o/home/james-whalen/.local/lib/python3.13/site-packages/transformers/pipelines/zero_shot_image_classification.pyr   ,ZeroShotImageClassificationPipeline.__init__H   sH    "6"$)~~% F	
 H	
    imagezImage.Imagecandidate_labelsr#   returnc                     g Nr   r"   r(   r)   r#   s       r%   __call__,ZeroShotImageClassificationPipeline.__call__R   s      #r'   c                     g r,   r   r-   s       r%   r.   r/   W   s     &)r'   c                 t   > SU;   a  UR                  S5      nUc  [        S5      e[        TU ]  " U4SU0UD6$ )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.   r/   \   sH    J vJJx(E=rsswS8HSFSSr'   c                     0 nSU;   a  US   US'   SU;   a  US   US'   SU;   a  US   US'   Ub  [         R                  " S[        5        XS'   U0 0 4$ )Nr)   timeouthypothesis_templatez^The `tokenizer_kwargs` argument is deprecated and will be removed in version 5 of Transformerstokenizer_kwargs)warningswarnFutureWarning)r"   r8   r#   preprocess_paramss       r%   _sanitize_parameters8ZeroShotImageClassificationPipeline._sanitize_parameters   s    '4:;M4N01+1)+<i( F*7=>S7T34'MMp 5E01 "b((r'   c                    Uc  0 n[        XS9nU R                  U/U R                  S9nU R                  S:X  a  UR                  U R                  5      nX&S'   U Vs/ s H  osR                  U5      PM     nnSS0n	SU R                  R                  R                  ;   a  U	R                  SS	SS
9  U	R                  U5        U R                  " U4SU R                  0U	D6n
U
/US'   U$ s  snf )N)r6   )r2   return_tensorsptr)   paddingTsiglip
max_length@   )rB   rD   
truncationr@   text_inputs)r   image_processorr!   todtypeformatmodelconfig
model_typeupdate	tokenizer)r"   r(   r)   r7   r6   r8   inputsx	sequencestokenizer_default_kwargsrG   s              r%   
preprocess.ZeroShotImageClassificationPipeline.preprocess   s     #!52%%eWT^^%T>>T!YYtzz*F%5!"<LM<Lq//2<L	M$-t#4 tzz((333$++LR\`+a ''(89nnYjt~~jQij!,} Ns   C*c                     UR                  S5      nUR                  S5      n[        US   [        5      (       a  US   nOUS   S   nU R                  " S0 UDUD6nUUR                  S.nU$ )Nr)   rG   r   )r)   logitsr   )r3   
isinstancer   rL   logits_per_image)r"   model_inputsr)   rG   outputsmodel_outputss         r%   _forward,ZeroShotImageClassificationPipeline._forward   s    '++,>?"&&}5k!nh//%a.K &a.+K**;{;l; !1..
 r'   c                    UR                  S5      nUS   S   nU R                  S:X  ar  SU R                  R                  R                  ;   aN  [
        R                  " U5      R                  S5      nUR                  5       n[        U[        5      (       d  U/nOU R                  S:X  aG  UR                  SS9R                  S5      nUR                  5       n[        U[        5      (       d  U/nOQU R                  S:X  a)  [        USS	9nUR                  5       R                  5       nO[        S
U R                   35      e[        [!        XR5      S S9 VVs/ s H	  u  pgXgS.PM     nnnU$ s  snnf )Nr)   rX   r   rA   rC   )dimr   )axiszUnsupported framework: c                     U S   * $ )Nr   r   )rR   s    r%   <lambda>AZeroShotImageClassificationPipeline.postprocess.<locals>.<lambda>   s    _`ab_c^cr'   )key)scorelabel)r3   r!   rL   rM   rN   torchsigmoidsqueezetolistrY   listsoftmaxr   numpyr4   sortedzip)	r"   r]   r)   rX   probsscoresrh   candidate_labelresults	            r%   postprocess/ZeroShotImageClassificationPipeline.postprocess   sK   (,,-?@x(+>>T!h$**2C2C2N2N&NMM&)11"5E\\^Ffd++ ^^t#NNrN*2226E\\^Ffd++ ^^t#"63E[[]))+F6t~~6FGHH +1V1NTc*d
*d& 6*d 	 
 	
s   E0r   r,   )NzThis is a photo of {}.NN)__name__
__module____qualname____firstlineno____doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr   r   r   strrn   r   dictr.   r=   rU   r^   rw   __static_attributes____classcell__)r$   s   @r%   r   r   !   so   @ O #O
 #3-.#BFs)#WZ#	d38n	# # )49d=&99:)NRSVi)cf)	d4S>"	#) ))TS$s)]D4GGH)T s))T 	)T
 
tDcN#T$tCH~*>%??	@)TV)( 40" r'   r   ) r9   collectionsr   typingr   r   r   utilsr   r	   r
   r   r   r   baser   r   PILr   image_utilsr   rj   models.auto.modeling_autor   models.auto.modeling_tf_autor   tf_utilsr   
get_loggerry   loggerr   r   r'   r%   <module>r      s       ' '  5 (bh)			H	% ,FGv( v Hvr'   