






'''simple docstring'''
from typing import Dict, List, Optional

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


a_					= logging.get_logger(__name__)


a_					= {
    'nielsr/canine-s': 2_0_4_8,
}

# Unicode defines 1,114,112 total “codepoints”
a_					= 1_1_1_4_1_1_2

# Below: Constants defining canonical codepoints for special, pseudo-characters.
# Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py
a_					= 0

a_					= 0xE000
a_					= 0xE001
a_					= 0xE002
a_					= 0xE003
a_					= 0xE004

# Maps special codepoints to human-readable names.
a_					= {
    # Special symbols are represented using codepoints values that are valid,
    # but designated as "Private Use", meaning that they will never be assigned
    # characters by the Unicode Consortium, and are thus safe for use here.
    #
    # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly
    # excluded and should fail with a hard error.
    CLS: "[CLS]",
    SEP: "[SEP]",
    BOS: "[BOS]",
    MASK: "[MASK]",
    PAD: "[PAD]",
    RESERVED: "[RESERVED]",
}

# Maps special codepoint human-readable names to their codepoint values.
a_					= {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()}






class      __SCREAMING_SNAKE_CASE      (			lowerCAmelCase__				):
 snake_case_  							=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES




 def __init__(      self	:							List[str]					,      __lowercase	:							Optional[Any]=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							Any=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							str=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							Optional[int]=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							List[Any]=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							List[Any]=chr(_SCREAMING_SNAKE_CASE      )					,      __lowercase	:							Optional[int]=False					,      __lowercase	:							int=20_48					,      **__lowercase	:							Union[str, Any]					,      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Dict									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else bos_token
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else eos_token
       SCREAMING_SNAKE_CASE__     :				Tuple									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else sep_token
       SCREAMING_SNAKE_CASE__     :				str									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else cls_token
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else pad_token

       # Mask token behave like a normal word, i.e. include the space before it
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=AddedToken(_SCREAMING_SNAKE_CASE					,      lstrip=_SCREAMING_SNAKE_CASE					,      rstrip=_SCREAMING_SNAKE_CASE      ) if isinstance(_SCREAMING_SNAKE_CASE					,      _SCREAMING_SNAKE_CASE      ) else mask_token

       super().__init__(
           bos_token=_SCREAMING_SNAKE_CASE					,      eos_token=_SCREAMING_SNAKE_CASE					,      sep_token=_SCREAMING_SNAKE_CASE					,      cls_token=_SCREAMING_SNAKE_CASE					,      pad_token=_SCREAMING_SNAKE_CASE					,      mask_token=_SCREAMING_SNAKE_CASE					,      add_prefix_space=_SCREAMING_SNAKE_CASE					,      model_max_length=_SCREAMING_SNAKE_CASE					,      **_SCREAMING_SNAKE_CASE					,      )

       # Creates a mapping for looking up the IDs of special symbols.
       SCREAMING_SNAKE_CASE__     :				Dict									={}
       for codepoint, name in SPECIAL_CODEPOINTS.items():
             SCREAMING_SNAKE_CASE__     :				Any									=codepoint

       # Creates a mapping for looking up the string forms of special symbol IDs.
       SCREAMING_SNAKE_CASE__     :				Any									={
           codepoint: name for name, codepoint in self._special_codepoints.items()
       }

       SCREAMING_SNAKE_CASE__     :				Dict									=UNICODE_VOCAB_SIZE
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=len(self._special_codepoints      )




 @property
 def      __magic_name__	(      self	:							Optional[Any]      )	->						int:
       return self._unicode_vocab_size




 def      __magic_name__	(      self	:							Optional[Any]					,      __lowercase	:							Dict      )	->						List[str]:
       return list(_SCREAMING_SNAKE_CASE      )




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							Optional[Any]      )	->						int:
       try:
             return ord(_SCREAMING_SNAKE_CASE      )
       except TypeError:
             raise ValueError(F"invalid token: \'{token}\'"      )




 def      __magic_name__	(      self	:							Dict					,      __lowercase	:							Union[str, Any]      )	->						str:
       try:
             if index in SPECIAL_CODEPOINTS:
                   return SPECIAL_CODEPOINTS[index]
             return chr(_SCREAMING_SNAKE_CASE      )
       except TypeError:
             raise ValueError(F"invalid id: {index}"      )




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							Union[str, Any]      )	->						Tuple:
       return "".join(_SCREAMING_SNAKE_CASE      )




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							Union[str, Any]					,      __lowercase	:							List[Any] = None      )	->						List[int]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=[self.sep_token_id]
       SCREAMING_SNAKE_CASE__     :				Any									=[self.cls_token_id]

       SCREAMING_SNAKE_CASE__     :				Tuple									=cls + token_ids_a + sep
       if token_ids_a is not None:
             result += token_ids_a + sep
       return result




 def      __magic_name__	(      self	:							str					,      __lowercase	:							Tuple					,      __lowercase	:							List[Any] = None					,      __lowercase	:							Optional[int] = False      )	->						List[int]:
       if already_has_special_tokens:
             return super().get_special_tokens_mask(
                 token_ids_a=_SCREAMING_SNAKE_CASE					,      token_ids_a=_SCREAMING_SNAKE_CASE					,      already_has_special_tokens=_SCREAMING_SNAKE_CASE      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=[1] + ([0] * len(_SCREAMING_SNAKE_CASE      )) + [1]
       if token_ids_a is not None:
             result += ([0] * len(_SCREAMING_SNAKE_CASE      )) + [1]
       return result




 def      __magic_name__	(      self	:							int					,      __lowercase	:							Any					,      __lowercase	:							int = None      )	->						List[int]:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[self.sep_token_id]
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[self.cls_token_id]

       SCREAMING_SNAKE_CASE__     :				int									=len(cls + token_ids_a + sep      ) * [0]
       if token_ids_a is not None:
             result += len(token_ids_a + sep      ) * [1]
       return result




 def      __magic_name__	(      self	:							Optional[int]					,      __lowercase	:							str					,      __lowercase	:							Any = None      )	->						Union[str, Any]:
       return ()







'''simple docstring'''
import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:






  class      __SCREAMING_SNAKE_CASE      :




   @staticmethod
   def      __magic_name__	(      *__lowercase	:							int					,      **__lowercase	:							Optional[Any]      )	->						Optional[Any]:
         pass






@is_pipeline_test
@require_vision
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(__lowercase      )					,      [
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      framework='''tf'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @slow
 @require_torch
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Any									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )




 @slow
 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      framework='''tf'''      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )