






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,
)


__UpperCAmelCase      				=		"""hf-internal-testing/tiny-random-bert"""
__UpperCAmelCase      				=		os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
__UpperCAmelCase      				=		"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""



class     lowerCamelCase (unittest.TestCase     ):







		'''simple docstring'''



		def    __UpperCAmelCase     (				self  )      ->				List[Any]:
							UpperCAmelCase_       : List[Any]				=		cached_file(_UpperCamelCase							,						_UpperCamelCase  )
							# Should have downloaded the file in here
							self.assertTrue(os.path.isdir(_UpperCamelCase  )  )
							# Cache should contain at least those three subfolders:
							for subfolder in ["blobs", "refs", "snapshots"]:
												self.assertTrue(os.path.isdir(os.path.join(_UpperCamelCase							,						_UpperCamelCase  )  )  )
							with open(os.path.join(_UpperCamelCase							,						'refs'							,						'main'  )  ) as f:
												UpperCAmelCase_       : Any				=		f.read()
							self.assertEqual(_UpperCamelCase							,						os.path.join(_UpperCamelCase							,						'snapshots'							,						_UpperCamelCase							,						_UpperCamelCase  )  )
							self.assertTrue(os.path.isfile(_UpperCamelCase  )  )

							# File is cached at the same place the second time.
							UpperCAmelCase_       : str				=		cached_file(_UpperCamelCase							,						_UpperCamelCase  )
							self.assertEqual(_UpperCamelCase							,						_UpperCamelCase  )

							# Using a specific revision to test the full commit hash.
							UpperCAmelCase_       : List[str]				=		cached_file(_UpperCamelCase							,						_UpperCamelCase							,						revision='9b8c223'  )
							self.assertEqual(_UpperCamelCase							,						os.path.join(_UpperCamelCase							,						'snapshots'							,						_UpperCamelCase							,						_UpperCamelCase  )  )



		def    __UpperCAmelCase     (				self  )      ->				List[Any]:
							with self.assertRaisesRegex(_UpperCamelCase							,						'is not a valid model identifier'  ):
												UpperCAmelCase_       : Tuple				=		cached_file('tiny-random-bert'							,						_UpperCamelCase  )

							with self.assertRaisesRegex(_UpperCamelCase							,						'is not a valid git identifier'  ):
												UpperCAmelCase_       : Any				=		cached_file(_UpperCamelCase							,						_UpperCamelCase							,						revision='aaaa'  )

							with self.assertRaisesRegex(_UpperCamelCase							,						'does not appear to have a file named'  ):
												UpperCAmelCase_       : Tuple				=		cached_file(_UpperCamelCase							,						'conf'  )



		def    __UpperCAmelCase     (				self  )      ->				Any:
							with self.assertRaisesRegex(_UpperCamelCase							,						'does not appear to have a file named'  ):
												UpperCAmelCase_       : str				=		cached_file(_UpperCamelCase							,						'conf'  )

							with open(os.path.join(_UpperCamelCase							,						'refs'							,						'main'  )  ) as f:
												UpperCAmelCase_       : Optional[Any]				=		f.read()
							self.assertTrue(os.path.isfile(os.path.join(_UpperCamelCase							,						'.no_exist'							,						_UpperCamelCase							,						'conf'  )  )  )

							UpperCAmelCase_       : Any				=		cached_file(_UpperCamelCase							,						'conf'							,						_raise_exceptions_for_missing_entries=_UpperCamelCase  )
							self.assertIsNone(_UpperCamelCase  )

							UpperCAmelCase_       : int				=		cached_file(_UpperCamelCase							,						'conf'							,						local_files_only=_UpperCamelCase							,						_raise_exceptions_for_missing_entries=_UpperCamelCase  )
							self.assertIsNone(_UpperCamelCase  )

							UpperCAmelCase_       : Any				=		mock.Mock()
							UpperCAmelCase_       : List[Any]				=		5_0_0
							UpperCAmelCase_       : Optional[int]				=		{}
							UpperCAmelCase_       : Tuple				=		HTTPError
							UpperCAmelCase_       : Tuple				=		{}

							# Under the mock environment we get a 500 error when trying to reach the tokenizer.
							with mock.patch('requests.Session.request'							,						return_value=_UpperCamelCase  ) as mock_head:
												UpperCAmelCase_       : List[str]				=		cached_file(_UpperCamelCase							,						'conf'							,						_raise_exceptions_for_connection_errors=_UpperCamelCase  )
												self.assertIsNone(_UpperCamelCase  )
												# This check we did call the fake head request
												mock_head.assert_called()



		def    __UpperCAmelCase     (				self  )      ->				str:
							self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'							,						_UpperCamelCase  )  )
							self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'							,						_UpperCamelCase  )  )
							self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'							,						_UpperCamelCase  )  )



		def    __UpperCAmelCase     (				self  )      ->				List[Any]:
							# `get_file_from_repo` returns None if the file does not exist
							self.assertIsNone(get_file_from_repo('bert-base-cased'							,						'ahah.txt'  )  )

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

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

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



		def    __UpperCAmelCase     (				self  )      ->				str:
							with tempfile.TemporaryDirectory() as tmp_dir:
												UpperCAmelCase_       : List[str]				=		Path(_UpperCamelCase  ) / 'a.txt'
												filename.touch()
												self.assertEqual(get_file_from_repo(_UpperCamelCase							,						'a.txt'  )							,						str(_UpperCamelCase  )  )

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









from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 					lowercase							(			lowercase_     ):
				__SCREAMING_SNAKE_CASE   :					Union[str, Any]			     =     ['''image_processor''', '''tokenizer''']
				__SCREAMING_SNAKE_CASE   :					Tuple			     =     '''AutoImageProcessor'''
				__SCREAMING_SNAKE_CASE   :					Dict			     =     '''AutoTokenizer'''




				def __init__(   self ,  snake_case ,  snake_case			):
											super().__init__(snake_case ,  snake_case			)
											snake_case_     							=       self.image_processor




				def __call__(   self ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  **snake_case			):

											if text is None and images is None:
																		raise ValueError('You have to specify either text or images. Both cannot be none.'			)

											if text is not None:
																		snake_case_     							=       self.tokenizer(snake_case ,  return_tensors=snake_case ,  **snake_case			)

											if images is not None:
																		snake_case_     							=       self.image_processor(snake_case ,  return_tensors=snake_case ,  **snake_case			)

											if text is not None and images is not None:
																		snake_case_     							=       image_features.pixel_values
																		return encoding
											elif text is not None:
																		return encoding
											else:
																		return BatchEncoding(data=dict(**snake_case			) ,  tensor_type=snake_case			)




				def        a				(   self ,  *snake_case ,  **snake_case			):
											return self.tokenizer.batch_decode(*snake_case ,  **snake_case			)




				def        a				(   self ,  *snake_case ,  **snake_case			):
											return self.tokenizer.decode(*snake_case ,  **snake_case			)




				@property
				def        a				(   self			):
											return ["input_ids", "attention_mask", "pixel_values"]




