





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


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

class 		SCREAMING_SNAKE_CASE__				(       unittest.TestCase  ):


      def    a (self       :      Any      ):





         """simple docstring"""



         __snake_case							       =   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:
            __snake_case							       =   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.
         __snake_case							       =   cached_file(_UpperCamelCase				,			_UpperCamelCase      )
         self.assertEqual(_UpperCamelCase				,			_UpperCamelCase      )

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


      def    a (self       :      Tuple      ):





         """simple docstring"""



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

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

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


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



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

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

         __snake_case							       =   cached_file(_UpperCamelCase				,			'''conf'''				,			_raise_exceptions_for_missing_entries=_UpperCamelCase      )
         self.assertIsNone(_UpperCamelCase      )

         __snake_case							       =   cached_file(_UpperCamelCase				,			'''conf'''				,			local_files_only=_UpperCamelCase				,			_raise_exceptions_for_missing_entries=_UpperCamelCase      )
         self.assertIsNone(_UpperCamelCase      )

         __snake_case							       =   mock.Mock()
         __snake_case							       =   500
         __snake_case							       =   {}
         __snake_case							       =   HTTPError
         __snake_case							       =   {}

         # 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:
            __snake_case							       =   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    a (self       :      Dict      ):





         """simple docstring"""



         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    a (self       :      Optional[int]      ):





         """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(_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'''      )

         __snake_case							       =   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.
         __snake_case							       =   json.loads(open(_UpperCamelCase				,			'''r'''      ).read()      )
         self.assertEqual(config['''hidden_size''']				,			768      )


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



         with tempfile.TemporaryDirectory() as tmp_dir:
            __snake_case							       =   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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase_       	=	{'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']}

try:
					if not is_sentencepiece_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	['''XGLMTokenizer''']

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	['''XGLMTokenizerFast''']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''XGLMForCausalLM''',
					    '''XGLMModel''',
					    '''XGLMPreTrainedModel''',
					]


try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''FlaxXGLMForCausalLM''',
					    '''FlaxXGLMModel''',
					    '''FlaxXGLMPreTrainedModel''',
					]


try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFXGLMForCausalLM''',
					    '''TFXGLMModel''',
					    '''TFXGLMPreTrainedModel''',
					]


if TYPE_CHECKING:
					from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_xglm import XGLMTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_xglm_fast import XGLMTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_xglm import (
										    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFXGLMForCausalLM,
										    TFXGLMModel,
										    TFXGLMPreTrainedModel,
										)


else:
					import sys

					lowerCAmelCase_       	=	_LazyModule(__name__, globals()['''__file__'''], _import_structure)