
"""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 			__magic_name__       (       unittest.TestCase ):
       def 							lowercase_							(     self       )     ->	int:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     tempfile.mkdtemp()

         # fmt: off
         _lowercase:			Union[str, Any]       =     ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''']
         # fmt: on
         _lowercase:			Tuple       =     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]       )       )

         _lowercase:			Dict       =     {
             '''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],
         }
         _lowercase:			Tuple       =     os.path.join(self.tmpdirname ,				A_       )
         with open(self.image_processor_file ,				'''w''' ,				encoding='''utf-8'''       ) as fp:
           json.dump(A_ ,				A_       )
       def 							lowercase_							(     self ,				**A_       )     ->	Optional[int]:





         """simple docstring"""




         return BertTokenizer.from_pretrained(self.tmpdirname ,				**A_       )
       def 							lowercase_							(     self ,				**A_       )     ->	Union[str, Any]:





         """simple docstring"""




         return ViTImageProcessor.from_pretrained(self.tmpdirname ,				**A_       )
       def 							lowercase_							(     self       )     ->	Dict:





         """simple docstring"""




         shutil.rmtree(self.tmpdirname       )
       def 							lowercase_							(     self       )     ->	Dict:





         """simple docstring"""




         _lowercase:			Optional[int]       =     [np.random.randint(255 ,				size=(3, 30, 400) ,				dtype=np.uinta       )]

         _lowercase:			Dict       =     [Image.fromarray(np.moveaxis(A_ ,				0 ,				-1       )       ) for x in image_inputs]

         return image_inputs
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




         _lowercase:			Any       =     self.get_tokenizer()
         _lowercase:			Dict       =     self.get_image_processor()

         _lowercase:			Dict       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         processor.save_pretrained(self.tmpdirname       )
         _lowercase:			Optional[Any]       =     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 ,				A_       )
       def 							lowercase_							(     self       )     ->	Any:





         """simple docstring"""




         _lowercase:			int       =     VisionTextDualEncoderProcessor(
             tokenizer=self.get_tokenizer() ,				image_processor=self.get_image_processor()       )
         processor.save_pretrained(self.tmpdirname       )

         _lowercase:			int       =     self.get_tokenizer(bos_token='''(BOS)''' ,				eos_token='''(EOS)'''       )
         _lowercase:			List[str]       =     self.get_image_processor(do_normalize=A_ ,				padding_value=1.0       )

         _lowercase:			Union[str, Any]       =     VisionTextDualEncoderProcessor.from_pretrained(
             self.tmpdirname ,				bos_token='''(BOS)''' ,				eos_token='''(EOS)''' ,				do_normalize=A_ ,				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 ,				A_       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			str       =     self.get_image_processor()
         _lowercase:			Tuple       =     self.get_tokenizer()

         _lowercase:			Optional[int]       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         _lowercase:			Optional[int]       =     self.prepare_image_inputs()

         _lowercase:			Dict       =     image_processor(A_ ,				return_tensors='''np'''       )
         _lowercase:			Optional[Any]       =     processor(images=A_ ,				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 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Dict       =     self.get_image_processor()
         _lowercase:			str       =     self.get_tokenizer()

         _lowercase:			Tuple       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         _lowercase:			List[Any]       =     '''lower newer'''

         _lowercase:			Tuple       =     processor(text=A_       )

         _lowercase:			int       =     tokenizer(A_       )

         for key in encoded_tok.keys():
           self.assertListEqual(encoded_tok[key] ,				encoded_processor[key]       )
       def 							lowercase_							(     self       )     ->	Any:





         """simple docstring"""




         _lowercase:			List[str]       =     self.get_image_processor()
         _lowercase:			Dict       =     self.get_tokenizer()

         _lowercase:			Dict       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         _lowercase:			Dict       =     '''lower newer'''
         _lowercase:			Tuple       =     self.prepare_image_inputs()

         _lowercase:			int       =     processor(text=A_ ,				images=A_       )

         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(A_       ):
           processor()
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			List[Any]       =     self.get_image_processor()
         _lowercase:			Union[str, Any]       =     self.get_tokenizer()

         _lowercase:			str       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         _lowercase:			Optional[int]       =     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         _lowercase:			Dict       =     processor.batch_decode(A_       )
         _lowercase:			Optional[Any]       =     tokenizer.batch_decode(A_       )

         self.assertListEqual(A_ ,				A_       )



       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			Tuple       =     self.get_image_processor()
         _lowercase:			int       =     self.get_tokenizer()

         _lowercase:			Any       =     VisionTextDualEncoderProcessor(tokenizer=A_ ,				image_processor=A_       )

         _lowercase:			Optional[Any]       =     '''lower newer'''
         _lowercase:			Union[str, Any]       =     self.prepare_image_inputs()

         _lowercase:			Optional[int]       =     processor(text=A_ ,				images=A_       )

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


"""simple docstring"""




import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


A__   :					Any							=   'hf-internal-testing/tiny-random-bert'
A__   :					int							=   os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
A__   :					Dict							=   '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'


class 			__magic_name__       (       unittest.TestCase ):
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         _lowercase:			Dict       =     cached_file(A_ ,				A_       )
         # Should have downloaded the file in here
         self.assertTrue(os.path.isdir(A_       )       )
         # Cache should contain at least those three subfolders:
         for subfolder in ["blobs", "refs", "snapshots"]:
           self.assertTrue(os.path.isdir(os.path.join(A_ ,				A_       )       )       )
         with open(os.path.join(A_ ,				'''refs''' ,				'''main'''       )       ) as f:
           _lowercase:			Tuple       =     f.read()
         self.assertEqual(A_ ,				os.path.join(A_ ,				'''snapshots''' ,				A_ ,				A_       )       )
         self.assertTrue(os.path.isfile(A_       )       )

         # File is cached at the same place the second time.
         _lowercase:			List[Any]       =     cached_file(A_ ,				A_       )
         self.assertEqual(A_ ,				A_       )

         # Using a specific revision to test the full commit hash.
         _lowercase:			Union[str, Any]       =     cached_file(A_ ,				A_ ,				revision='''9b8c223'''       )
         self.assertEqual(A_ ,				os.path.join(A_ ,				'''snapshots''' ,				A_ ,				A_       )       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         with self.assertRaisesRegex(A_ ,				'''is not a valid model identifier'''       ):
           _lowercase:			Union[str, Any]       =     cached_file('''tiny-random-bert''' ,				A_       )

         with self.assertRaisesRegex(A_ ,				'''is not a valid git identifier'''       ):
           _lowercase:			List[Any]       =     cached_file(A_ ,				A_ ,				revision='''aaaa'''       )

         with self.assertRaisesRegex(A_ ,				'''does not appear to have a file named'''       ):
           _lowercase:			int       =     cached_file(A_ ,				'''conf'''       )
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         with self.assertRaisesRegex(A_ ,				'''does not appear to have a file named'''       ):
           _lowercase:			Dict       =     cached_file(A_ ,				'''conf'''       )

         with open(os.path.join(A_ ,				'''refs''' ,				'''main'''       )       ) as f:
           _lowercase:			int       =     f.read()
         self.assertTrue(os.path.isfile(os.path.join(A_ ,				'''.no_exist''' ,				A_ ,				'''conf'''       )       )       )

         _lowercase:			Optional[int]       =     cached_file(A_ ,				'''conf''' ,				_raise_exceptions_for_missing_entries=A_       )
         self.assertIsNone(A_       )

         _lowercase:			Optional[int]       =     cached_file(A_ ,				'''conf''' ,				local_files_only=A_ ,				_raise_exceptions_for_missing_entries=A_       )
         self.assertIsNone(A_       )

         _lowercase:			Optional[Any]       =     mock.Mock()
         _lowercase:			Optional[Any]       =     500
         _lowercase:			Optional[Any]       =     {}
         _lowercase:			Dict       =     HTTPError
         _lowercase:			Optional[Any]       =     {}

         # Under the mock environment we get a 500 error when trying to reach the tokenizer.
         with mock.patch('''requests.Session.request''' ,				return_value=A_       ) as mock_head:
           _lowercase:			Tuple       =     cached_file(A_ ,				'''conf''' ,				_raise_exceptions_for_connection_errors=A_       )
           self.assertIsNone(A_       )
           # This check we did call the fake head request
           mock_head.assert_called()
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
         self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
         self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         self.assertIsNone(get_file_from_repo('''bert-base-cased''' ,				'''ahah.txt'''       )       )

         # The function raises if the repository does not exist.
         with self.assertRaisesRegex(A_ ,				'''is not a valid model identifier'''       ):
           get_file_from_repo('''bert-base-case''' ,				A_       )

         # The function raises if the revision does not exist.
         with self.assertRaisesRegex(A_ ,				'''is not a valid git identifier'''       ):
           get_file_from_repo('''bert-base-cased''' ,				A_ ,				revision='''ahaha'''       )

         _lowercase:			Optional[int]       =     get_file_from_repo('''bert-base-cased''' ,				A_       )
         # The name is the cached name which is not very easy to test, so instead we load the content.
         _lowercase:			Union[str, Any]       =     json.loads(open(A_ ,				'''r'''       ).read()       )
         self.assertEqual(config['''hidden_size'''] ,				768       )



       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         with tempfile.TemporaryDirectory() as tmp_dir:
           _lowercase:			List[Any]       =     Path(A_       ) / '''a.txt'''
           filename.touch()
           self.assertEqual(get_file_from_repo(A_ ,				'''a.txt'''       ) ,				str(A_       )       )

           self.assertIsNone(get_file_from_repo(A_ ,				'''b.txt'''       )       )
