






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


_UpperCamelCase    :   int   			=						'hf-internal-testing/tiny-random-bert'
_UpperCamelCase    :   List[str]   			=						os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
_UpperCamelCase    :   str   			=						'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'






class 						snake_case  (  unittest.TestCase	):





				def     lowerCamelCase__				(							self     : Optional[Any]      ):


						'''simple docstring'''




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

						# File is cached at the same place the second time.
						a					:      Union[str, Any]    =       cached_file(snake_case__    ,		snake_case__      )
						self.assertEqual(snake_case__    ,		snake_case__      )

						# Using a specific revision to test the full commit hash.
						a					:      Dict    =       cached_file(snake_case__    ,		snake_case__    ,		revision='9b8c223'      )
						self.assertEqual(snake_case__    ,		os.path.join(snake_case__    ,		'snapshots'    ,		snake_case__    ,		snake_case__      )      )





				def     lowerCamelCase__				(							self     : List[Any]      ):


						'''simple docstring'''




						with self.assertRaisesRegex(snake_case__    ,		'is not a valid model identifier'      ):
								a					:      Tuple    =       cached_file('tiny-random-bert'    ,		snake_case__      )

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

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





				def     lowerCamelCase__				(							self     : str      ):


						'''simple docstring'''




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

						with open(os.path.join(snake_case__    ,		'refs'    ,		'main'      )      ) as f:
								a					:      str    =       f.read()
						self.assertTrue(os.path.isfile(os.path.join(snake_case__    ,		'.no_exist'    ,		snake_case__    ,		'conf'      )      )      )

						a					:      Any    =       cached_file(snake_case__    ,		'conf'    ,		_raise_exceptions_for_missing_entries=snake_case__      )
						self.assertIsNone(snake_case__      )

						a					:      int    =       cached_file(snake_case__    ,		'conf'    ,		local_files_only=snake_case__    ,		_raise_exceptions_for_missing_entries=snake_case__      )
						self.assertIsNone(snake_case__      )

						a					:      Optional[Any]    =       mock.Mock()
						a					:      Tuple    =       5_0_0
						a					:      List[str]    =       {}
						a					:      Dict    =       HTTPError
						a					:      Any    =       {}

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





				def     lowerCamelCase__				(							self     : Dict      ):


						'''simple docstring'''




						self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'    ,		snake_case__      )      )
						self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'    ,		snake_case__      )      )
						self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'    ,		snake_case__      )      )





				def     lowerCamelCase__				(							self     : Dict      ):


						'''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(snake_case__    ,		'is not a valid model identifier'      ):
								get_file_from_repo('bert-base-case'    ,		snake_case__      )

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

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





				def     lowerCamelCase__				(							self     : List[Any]      ):


						'''simple docstring'''




						with tempfile.TemporaryDirectory() as tmp_dir:
								a					:      Dict    =       Path(snake_case__      ) / "a.txt"
								filename.touch()
								self.assertEqual(get_file_from_repo(snake_case__    ,		'a.txt'      )    ,		str(snake_case__      )      )

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












"""simple docstring"""


from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels
from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features
from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor
from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels




