





'''simple docstring'''

import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor







@require_tokenizers
@require_vision
class __a (   unittest.TestCase			):





   def      UpperCAmelCase__     (					self				:			str    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       tempfile.mkdtemp()

      # fmt: off
      __SCREAMING_SNAKE_CASE      							=       ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
      # fmt: on
      __SCREAMING_SNAKE_CASE      							=       os.path.join(self.tmpdirname				,VOCAB_FILES_NAMES["""vocab_file"""]    )
      with open(self.vocab_file				,"""w"""				,encoding="""utf-8"""    ) as vocab_writer:
         vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]    )    )

      __SCREAMING_SNAKE_CASE      							=       {
          """do_resize""": True,
          """size""": {"""height""": 18, """width""": 18},
          """do_normalize""": True,
          """image_mean""": [0.5, 0.5, 0.5],
          """image_std""": [0.5, 0.5, 0.5],
      }
      __SCREAMING_SNAKE_CASE      							=       os.path.join(self.tmpdirname				,lowerCamelCase    )
      with open(self.image_processor_file				,"""w"""				,encoding="""utf-8"""    ) as fp:
         json.dump(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			str				,**lowerCamelCase				:			Any    ):

      '''simple docstring'''

      return BertTokenizer.from_pretrained(self.tmpdirname				,**lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			Tuple				,**lowerCamelCase				:			List[Any]    ):

      '''simple docstring'''

      return ViTImageProcessor.from_pretrained(self.tmpdirname				,**lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      shutil.rmtree(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       [np.random.randint(255				,size=(3, 30, 400)				,dtype=np.uinta    )]

      __SCREAMING_SNAKE_CASE      							=       [Image.fromarray(np.moveaxis(lowerCamelCase				,0				,-1    )    ) for x in image_inputs]

      return image_inputs





   def      UpperCAmelCase__     (					self				:			Any    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()
      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      processor.save_pretrained(self.tmpdirname    )
      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname    )

      self.assertEqual(processor.tokenizer.get_vocab()				,tokenizer.get_vocab()    )
      self.assertIsInstance(processor.tokenizer				,(BertTokenizer, BertTokenizerFast)    )

      self.assertEqual(processor.image_processor.to_json_string()				,image_processor.to_json_string()    )
      self.assertIsInstance(processor.image_processor				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			Union[str, Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(
          tokenizer=self.get_tokenizer()				,image_processor=self.get_image_processor()    )
      processor.save_pretrained(self.tmpdirname    )

      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer(bos_token="""(BOS)"""				,eos_token="""(EOS)"""    )
      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor(do_normalize=lowerCamelCase				,padding_value=1.0    )

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor.from_pretrained(
          self.tmpdirname				,bos_token="""(BOS)"""				,eos_token="""(EOS)"""				,do_normalize=lowerCamelCase				,padding_value=1.0    )

      self.assertEqual(processor.tokenizer.get_vocab()				,tokenizer_add_kwargs.get_vocab()    )
      self.assertIsInstance(processor.tokenizer				,(BertTokenizer, BertTokenizerFast)    )

      self.assertEqual(processor.image_processor.to_json_string()				,image_processor_add_kwargs.to_json_string()    )
      self.assertIsInstance(processor.image_processor				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()
      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       image_processor(lowerCamelCase				,return_tensors="""np"""    )
      __SCREAMING_SNAKE_CASE      							=       processor(images=lowerCamelCase				,return_tensors="""np"""    )

      for key in input_feat_extract.keys():
         self.assertAlmostEqual(input_feat_extract[key].sum()				,input_processor[key].sum()				,delta=1E-2    )





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()
      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       """lower newer"""

      __SCREAMING_SNAKE_CASE      							=       processor(text=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       tokenizer(lowerCamelCase    )

      for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key]				,encoded_processor[key]    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()
      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       """lower newer"""
      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       processor(text=lowerCamelCase				,images=lowerCamelCase    )

      self.assertListEqual(list(inputs.keys()    )				,["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]    )

      # test if it raises when no input is passed
      with self.assertRaises(lowerCamelCase    ):
         processor()





   def      UpperCAmelCase__     (					self				:			Tuple    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()
      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      __SCREAMING_SNAKE_CASE      							=       processor.batch_decode(lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       tokenizer.batch_decode(lowerCamelCase    )

      self.assertListEqual(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			str    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()
      __SCREAMING_SNAKE_CASE      							=       self.get_tokenizer()

      __SCREAMING_SNAKE_CASE      							=       VisionTextDualEncoderProcessor(tokenizer=lowerCamelCase				,image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       """lower newer"""
      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       processor(text=lowerCamelCase				,images=lowerCamelCase    )

      self.assertListEqual(list(inputs.keys()    )				,processor.model_input_names    )












'''simple docstring'''

from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

a        =						[
    "python",
    "tqdm",
    "regex",
    "requests",
    "packaging",
    "filelock",
    "numpy",
    "tokenizers",
    "huggingface-hub",
    "safetensors",
    "accelerate",
    "pyyaml",
]

for pkg in pkgs_to_check_at_runtime:
    if pkg in deps:
        if pkg == "tokenizers":
            # must be loaded here, or else tqdm check may fail
            from .utils import is_tokenizers_available

            if not is_tokenizers_available():
                continue  # not required, check version only if installed
        elif pkg == "accelerate":
            # must be loaded here, or else tqdm check may fail
            from .utils import is_accelerate_available

            # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of
            # Transformers with PyTorch
            if not is_accelerate_available():
                continue  # not required, check version only if installed

        require_version_core(deps[pkg])
    else:
        raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''')

def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase=None      )						->					Optional[Any]:

   '''simple docstring'''







   require_version(deps[pkg]							,	__UpperCAmelCase      )





