





"""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 						__snake_case   :





        """simple docstring"""


        @staticmethod
        def    UpperCamelCase__(       *__lowerCamelCase   ,		**__lowerCamelCase    ):



            '''simple docstring'''




            pass






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





 """simple docstring"""


 @require_torch
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Union[str, Any]							=	pipeline(
         model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''   ,		)
     __A       :		Tuple							=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     __A       :		int							=	image_classifier(_lowerCamelCase   ,		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(_lowerCamelCase    )   ,		[
             [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}],
             [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}],
         ]   ,		)

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

 @require_tf
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		str							=	pipeline(
         model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''   ,		framework='''tf'''    )
     __A       :		Tuple							=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     __A       :		List[Any]							=	image_classifier(_lowerCamelCase   ,		candidate_labels=['''a''', '''b''', '''c''']    )

     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}]   ,		)

     __A       :		Optional[int]							=	image_classifier([image] * 5   ,		candidate_labels=['''A''', '''B''', '''C''']   ,		batch_size=2    )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[
             [
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
             ],
             [
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
             ],
             [
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
             ],
             [
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
             ],
             [
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
                 {'''score''': 0.3_3_3, '''label''': ANY(_lowerCamelCase    )},
             ],
         ]   ,		)

 @slow
 @require_torch
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Optional[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
     __A       :		List[str]							=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     __A       :		int							=	image_classifier(_lowerCamelCase   ,		candidate_labels=['''cat''', '''plane''', '''remote''']    )

     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[
             {'''score''': 0.5_1_1, '''label''': '''remote'''},
             {'''score''': 0.4_8_5, '''label''': '''cat'''},
             {'''score''': 0.0_0_4, '''label''': '''plane'''},
         ]   ,		)

     __A       :		List[Any]							=	image_classifier([image] * 5   ,		candidate_labels=['''cat''', '''plane''', '''remote''']   ,		batch_size=2    )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[
             [
                 {'''score''': 0.5_1_1, '''label''': '''remote'''},
                 {'''score''': 0.4_8_5, '''label''': '''cat'''},
                 {'''score''': 0.0_0_4, '''label''': '''plane'''},
             ],
         ]
         * 5   ,		)







 @slow
 @require_tf
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Union[str, Any]							=	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
     __A       :		Any							=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     __A       :		Optional[Any]							=	image_classifier(_lowerCamelCase   ,		candidate_labels=['''cat''', '''plane''', '''remote''']    )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[
             {'''score''': 0.5_1_1, '''label''': '''remote'''},
             {'''score''': 0.4_8_5, '''label''': '''cat'''},
             {'''score''': 0.0_0_4, '''label''': '''plane'''},
         ]   ,		)

     __A       :		Dict							=	image_classifier([image] * 5   ,		candidate_labels=['''cat''', '''plane''', '''remote''']   ,		batch_size=2    )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    )   ,		[
             [
                 {'''score''': 0.5_1_1, '''label''': '''remote'''},
                 {'''score''': 0.4_8_5, '''label''': '''cat'''},
                 {'''score''': 0.0_0_4, '''label''': '''plane'''},
             ],
         ]
         * 5   ,		)





"""simple docstring"""


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
	from .tokenization_albert import AlbertTokenizer
else:
	a				=None

a				=logging.get_logger(__name__)
a				={'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

a				={
    'vocab_file': {
        'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model',
        'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model',
        'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model',
        'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model',
        'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model',
        'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model',
        'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model',
        'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model',
    },
    'tokenizer_file': {
        'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json',
        'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json',
        'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json',
        'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json',
        'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json',
        'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json',
        'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json',
        'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json',
    },
}

a				={
    'albert-base-v1': 512,
    'albert-large-v1': 512,
    'albert-xlarge-v1': 512,
    'albert-xxlarge-v1': 512,
    'albert-base-v2': 512,
    'albert-large-v2': 512,
    'albert-xlarge-v2': 512,
    'albert-xxlarge-v2': 512,
}

a				='▁'






class   __UpperCAmelCase		(      __lowerCAmelCase      ):
				A__ :  Dict    				=			VOCAB_FILES_NAMES
				A__ :  List[Any]    				=			PRETRAINED_VOCAB_FILES_MAP
				A__ :  List[Any]    				=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				A__ :  Union[str, Any]    				=			AlbertTokenizer

				def __init__( self      ,					_lowerCamelCase=None      ,					_lowerCamelCase=None      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=False      ,					_lowerCamelCase="[CLS]"      ,					_lowerCamelCase="[SEP]"      ,					_lowerCamelCase="<unk>"      ,					_lowerCamelCase="[SEP]"      ,					_lowerCamelCase="<pad>"      ,					_lowerCamelCase="[CLS]"      ,					_lowerCamelCase="[MASK]"      ,					**_lowerCamelCase      ,					):
										# Mask token behave like a normal word, i.e. include the space before it and
										# is included in the raw text, there should be a match in a non-normalized sentence.
										lowerCamelCase__   =(
										    AddedToken(_lowerCamelCase      ,					lstrip=_lowerCamelCase      ,					rstrip=_lowerCamelCase      ,					normalized=_lowerCamelCase )
										    if isinstance(_lowerCamelCase      ,					_lowerCamelCase )
										    else mask_token
										)

										super().__init__(
										    _lowerCamelCase      ,					tokenizer_file=_lowerCamelCase      ,					do_lower_case=_lowerCamelCase      ,					remove_space=_lowerCamelCase      ,					keep_accents=_lowerCamelCase      ,					bos_token=_lowerCamelCase      ,					eos_token=_lowerCamelCase      ,					unk_token=_lowerCamelCase      ,					sep_token=_lowerCamelCase      ,					pad_token=_lowerCamelCase      ,					cls_token=_lowerCamelCase      ,					mask_token=_lowerCamelCase      ,					**_lowerCamelCase      ,					)

										lowerCamelCase__   =do_lower_case
										lowerCamelCase__   =remove_space
										lowerCamelCase__   =keep_accents
										lowerCamelCase__   =vocab_file
										lowerCamelCase__   =False if not self.vocab_file else True

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase = None ):
										lowerCamelCase__   =[self.sep_token_id]
										lowerCamelCase__   =[self.cls_token_id]
										if token_ids_a is None:
																return cls + token_ids_a + sep
										return cls + token_ids_a + sep + token_ids_a + sep

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase = None ):
										lowerCamelCase__   =[self.sep_token_id]
										lowerCamelCase__   =[self.cls_token_id]

										if token_ids_a is None:
																return len(cls + token_ids_a + sep ) * [0]
										return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]






				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase = None ):
										if not self.can_save_slow_tokenizer:
																raise ValueError(
																    "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
																    "tokenizer." )

										if not os.path.isdir(_lowerCamelCase ):
																logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' )
																return
										lowerCamelCase__   =os.path.join(
										    _lowerCamelCase      ,					(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] )

										if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCamelCase ):
																copyfile(self.vocab_file      ,					_lowerCamelCase )

										return (out_vocab_file,)


