






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


SCREAMING_SNAKE_CASE							=    {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

SCREAMING_SNAKE_CASE							=    {
    'vocab_file': {
        'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt',
    },
    'tokenizer_file': {
        'unc-nlp/lxmert-base-uncased': (
            'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json'
        ),
    },
}

SCREAMING_SNAKE_CASE							=    {
    'unc-nlp/lxmert-base-uncased': 512,
}

SCREAMING_SNAKE_CASE							=    {
    'unc-nlp/lxmert-base-uncased': {'do_lower_case': True},
}
class 				A_     (    SCREAMING_SNAKE_CASE__      ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE				:      Optional[Any]			=       VOCAB_FILES_NAMES
 _SCREAMING_SNAKE_CASE				:      Any			=       PRETRAINED_VOCAB_FILES_MAP
 _SCREAMING_SNAKE_CASE				:      Optional[int]			=       PRETRAINED_INIT_CONFIGURATION
 _SCREAMING_SNAKE_CASE				:      Optional[int]			=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _SCREAMING_SNAKE_CASE				:      Optional[Any]			=       LxmertTokenizer


 def __init__(						self				,	_A=None				,	_A=None				,	_A=True				,	_A="[UNK]"				,	_A="[SEP]"				,	_A="[PAD]"				,	_A="[CLS]"				,	_A="[MASK]"				,	_A=True				,	_A=None				,	**_A				,	)			->		str:

     """simple docstring"""



     super().__init__(
         A_				,	tokenizer_file=A_				,	do_lower_case=A_				,	unk_token=A_				,	sep_token=A_				,	pad_token=A_				,	cls_token=A_				,	mask_token=A_				,	tokenize_chinese_chars=A_				,	strip_accents=A_				,	**A_				,	)

     _UpperCAmelCase					:   Union[str, Any]      	=					json.loads(self.backend_tokenizer.normalizer.__getstate__())
     if (
         normalizer_state.get('''lowercase'''				,	A_) != do_lower_case
         or normalizer_state.get('''strip_accents'''				,	A_) != strip_accents
         or normalizer_state.get('''handle_chinese_chars'''				,	A_) != tokenize_chinese_chars
     ):
         _UpperCAmelCase					:   Optional[Any]      	=					getattr(A_				,	normalizer_state.pop('''type'''))
         _UpperCAmelCase					:   List[str]      	=					do_lower_case
         _UpperCAmelCase					:   Any      	=					strip_accents
         _UpperCAmelCase					:   List[Any]      	=					tokenize_chinese_chars
         _UpperCAmelCase					:   Optional[int]      	=					normalizer_class(**A_)

     _UpperCAmelCase					:   List[str]      	=					do_lower_case


 def 			snake_case__					(						self				,	_A				,	_A=None)			->		int:

     """simple docstring"""



     _UpperCAmelCase					:   Any      	=					[self.cls_token_id] + token_ids_a + [self.sep_token_id]

     if token_ids_a:
         output += token_ids_a + [self.sep_token_id]

     return output


 def 			snake_case__					(						self				,	_A				,	_A = None)			->		List[int]:

     """simple docstring"""



     _UpperCAmelCase					:   List[Any]      	=					[self.sep_token_id]
     _UpperCAmelCase					:   Any      	=					[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 			snake_case__					(						self				,	_A				,	_A = None)			->		Tuple[str]:

     """simple docstring"""



     _UpperCAmelCase					:   str      	=					self._tokenizer.model.save(A_				,	name=A_)
     return tuple(A_)






"""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       __magic_name__							:
         @staticmethod
         def 				lowercase_				(  *A_			,					**A_							)  ->						Optional[int]:





           """simple docstring"""




           pass


@is_pipeline_test
@require_vision
class       __magic_name__							(							unittest.TestCase			):
       @require_torch
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




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

         _lowercase:      Any   			=  image_classifier([image] * 5			,					candidate_labels=['''A''', '''B''', '''C''']			,					batch_size=2							)
         self.assertEqual(
             nested_simplify(A_							)			,					[
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
             ]			,					)
       @require_tf
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




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

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

         _lowercase:      List[str]   			=  image_classifier([image] * 5			,					candidate_labels=['''A''', '''B''', '''C''']			,					batch_size=2							)
         self.assertEqual(
             nested_simplify(A_							)			,					[
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                     {'''score''': 0.3_33, '''label''': ANY(A_							)},
                 ],
             ]			,					)
       @slow
       @require_torch
       def 				lowercase_				(  self							)  ->						Any:





         """simple docstring"""




         _lowercase:      Tuple   			=  pipeline(
             task='''zero-shot-image-classification'''			,					model='''openai/clip-vit-base-patch32'''			,					)
         # This is an image of 2 cats with remotes and no planes
         _lowercase:      str   			=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
         _lowercase:      Optional[Any]   			=  image_classifier(A_			,					candidate_labels=['''cat''', '''plane''', '''remote''']							)

         self.assertEqual(
             nested_simplify(A_							)			,					[
                 {'''score''': 0.5_11, '''label''': '''remote'''},
                 {'''score''': 0.4_85, '''label''': '''cat'''},
                 {'''score''': 0.0_04, '''label''': '''plane'''},
             ]			,					)

         _lowercase:      Optional[Any]   			=  image_classifier([image] * 5			,					candidate_labels=['''cat''', '''plane''', '''remote''']			,					batch_size=2							)
         self.assertEqual(
             nested_simplify(A_							)			,					[
                 [
                     {'''score''': 0.5_11, '''label''': '''remote'''},
                     {'''score''': 0.4_85, '''label''': '''cat'''},
                     {'''score''': 0.0_04, '''label''': '''plane'''},
                 ],
             ]
             * 5			,					)



       @slow
       @require_tf
       def 				lowercase_				(  self							)  ->						Optional[Any]:





         """simple docstring"""




         _lowercase:      Tuple   			=  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
         _lowercase:      str   			=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
         _lowercase:      Optional[int]   			=  image_classifier(A_			,					candidate_labels=['''cat''', '''plane''', '''remote''']							)
         self.assertEqual(
             nested_simplify(A_							)			,					[
                 {'''score''': 0.5_11, '''label''': '''remote'''},
                 {'''score''': 0.4_85, '''label''': '''cat'''},
                 {'''score''': 0.0_04, '''label''': '''plane'''},
             ]			,					)

         _lowercase:      Optional[Any]   			=  image_classifier([image] * 5			,					candidate_labels=['''cat''', '''plane''', '''remote''']			,					batch_size=2							)
         self.assertEqual(
             nested_simplify(A_							)			,					[
                 [
                     {'''score''': 0.5_11, '''label''': '''remote'''},
                     {'''score''': 0.4_85, '''label''': '''cat'''},
                     {'''score''': 0.0_04, '''label''': '''plane'''},
                 ],
             ]
             * 5			,					)
