






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


SCREAMING_SNAKE_CASE						=	'hf-internal-testing/tiny-random-bert'
SCREAMING_SNAKE_CASE						=	os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
SCREAMING_SNAKE_CASE						=	'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      def 				A__      ( self			:		List[str]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   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:
        lowercase					:   List[str]				  =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.
       lowercase					:   Optional[int]				  =cached_file(UpperCAmelCase     ,      UpperCAmelCase		)
       self.assertEqual(UpperCAmelCase     ,      UpperCAmelCase		)

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






      def 				A__      ( self			:		List[str]		)  ->  Optional[int]:


       '''simple docstring'''

       with self.assertRaisesRegex(UpperCAmelCase     ,      '''is not a valid model identifier'''		):
        lowercase					:   List[str]				  =cached_file('''tiny-random-bert'''     ,      UpperCAmelCase		)

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

       with self.assertRaisesRegex(UpperCAmelCase     ,      '''does not appear to have a file named'''		):
        lowercase					:   Optional[Any]				  =cached_file(UpperCAmelCase     ,      '''conf'''		)






      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

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

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

       lowercase					:   Dict				  =cached_file(UpperCAmelCase     ,      '''conf'''     ,      _raise_exceptions_for_missing_entries=UpperCAmelCase		)
       self.assertIsNone(UpperCAmelCase		)

       lowercase					:   str				  =cached_file(UpperCAmelCase     ,      '''conf'''     ,      local_files_only=UpperCAmelCase     ,      _raise_exceptions_for_missing_entries=UpperCAmelCase		)
       self.assertIsNone(UpperCAmelCase		)

       lowercase					:   Tuple				  =mock.Mock()
       lowercase					:   Dict				  =500
       lowercase					:   Union[str, Any]				  ={}
       lowercase					:   Dict				  =HTTPError
       lowercase					:   List[str]				  ={}

       # 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:
        lowercase					:   List[Any]				  =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			:		List[Any]		)  ->  int:


       '''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			:		Dict		)  ->  Any:


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

       lowercase					:   Dict				  =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.
       lowercase					:   Any				  =json.loads(open(UpperCAmelCase     ,      '''r'''		).read()		)
       self.assertEqual(config['''hidden_size''']     ,      768		)






      def 				A__      ( self			:		List[str]		)  ->  Optional[int]:


       '''simple docstring'''

       with tempfile.TemporaryDirectory() as tmp_dir:
        lowercase					:   Dict				  =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'''		)		)











'''simple docstring'''



from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE						=	{
    'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig']
}

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

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

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'RemBertForCausalLM',
          'RemBertForMaskedLM',
          'RemBertForMultipleChoice',
          'RemBertForQuestionAnswering',
          'RemBertForSequenceClassification',
          'RemBertForTokenClassification',
          'RemBertLayer',
          'RemBertModel',
          'RemBertPreTrainedModel',
          'load_tf_weights_in_rembert',
      ]


try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'TFRemBertForCausalLM',
          'TFRemBertForMaskedLM',
          'TFRemBertForMultipleChoice',
          'TFRemBertForQuestionAnswering',
          'TFRemBertForSequenceClassification',
          'TFRemBertForTokenClassification',
          'TFRemBertLayer',
          'TFRemBertModel',
          'TFRemBertPreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert import RemBertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert_fast import RemBertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_rembert import (
                REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                RemBertForCausalLM,
                RemBertForMaskedLM,
                RemBertForMultipleChoice,
                RemBertForQuestionAnswering,
                RemBertForSequenceClassification,
                RemBertForTokenClassification,
                RemBertLayer,
                RemBertModel,
                RemBertPreTrainedModel,
                load_tf_weights_in_rembert,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_rembert import (
                TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFRemBertForCausalLM,
                TFRemBertForMaskedLM,
                TFRemBertForMultipleChoice,
                TFRemBertForQuestionAnswering,
                TFRemBertForSequenceClassification,
                TFRemBertForTokenClassification,
                TFRemBertLayer,
                TFRemBertModel,
                TFRemBertPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



