






'''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							="hf-internal-testing/tiny-random-bert"
UpperCamelCase							=os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
UpperCamelCase							="9b8c223d42b2188cb49d29af482996f9d0f3e5a6"




class     A    (      unittest.TestCase						):






 """simple docstring"""





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   int					    =							cached_file(__lowerCAmelCase			,     __lowerCAmelCase )
       # Should have downloaded the file in here
       self.assertTrue(os.path.isdir(__lowerCAmelCase ) )
       # Cache should contain at least those three subfolders:
       for subfolder in ["blobs", "refs", "snapshots"]:
             self.assertTrue(os.path.isdir(os.path.join(__lowerCAmelCase			,     __lowerCAmelCase ) ) )
       with open(os.path.join(__lowerCAmelCase			,     """refs"""			,     """main""" ) ) as f:
             UpperCamelCase_				:   Union[str, Any]					    =							f.read()
       self.assertEqual(__lowerCAmelCase			,     os.path.join(__lowerCAmelCase			,     """snapshots"""			,     __lowerCAmelCase			,     __lowerCAmelCase ) )
       self.assertTrue(os.path.isfile(__lowerCAmelCase ) )

       # File is cached at the same place the second time.
       UpperCamelCase_				:   Optional[int]					    =							cached_file(__lowerCAmelCase			,     __lowerCAmelCase )
       self.assertEqual(__lowerCAmelCase			,     __lowerCAmelCase )

       # Using a specific revision to test the full commit hash.
       UpperCamelCase_				:   Optional[int]					    =							cached_file(__lowerCAmelCase			,     __lowerCAmelCase			,     revision="""9b8c223""" )
       self.assertEqual(__lowerCAmelCase			,     os.path.join(__lowerCAmelCase			,     """snapshots"""			,     __lowerCAmelCase			,     __lowerCAmelCase ) )
 def   _UpperCAmelCase       (							self ):
       with self.assertRaisesRegex(__lowerCAmelCase			,     """is not a valid model identifier""" ):
             UpperCamelCase_				:   List[Any]					    =							cached_file("""tiny-random-bert"""			,     __lowerCAmelCase )

       with self.assertRaisesRegex(__lowerCAmelCase			,     """is not a valid git identifier""" ):
             UpperCamelCase_				:   Tuple					    =							cached_file(__lowerCAmelCase			,     __lowerCAmelCase			,     revision="""aaaa""" )

       with self.assertRaisesRegex(__lowerCAmelCase			,     """does not appear to have a file named""" ):
             UpperCamelCase_				:   int					    =							cached_file(__lowerCAmelCase			,     """conf""" )
 def   _UpperCAmelCase       (							self ):
       with self.assertRaisesRegex(__lowerCAmelCase			,     """does not appear to have a file named""" ):
             UpperCamelCase_				:   Any					    =							cached_file(__lowerCAmelCase			,     """conf""" )

       with open(os.path.join(__lowerCAmelCase			,     """refs"""			,     """main""" ) ) as f:
             UpperCamelCase_				:   List[Any]					    =							f.read()
       self.assertTrue(os.path.isfile(os.path.join(__lowerCAmelCase			,     """.no_exist"""			,     __lowerCAmelCase			,     """conf""" ) ) )

       UpperCamelCase_				:   Optional[int]					    =							cached_file(__lowerCAmelCase			,     """conf"""			,     _raise_exceptions_for_missing_entries=__lowerCAmelCase )
       self.assertIsNone(__lowerCAmelCase )

       UpperCamelCase_				:   Union[str, Any]					    =							cached_file(__lowerCAmelCase			,     """conf"""			,     local_files_only=__lowerCAmelCase			,     _raise_exceptions_for_missing_entries=__lowerCAmelCase )
       self.assertIsNone(__lowerCAmelCase )

       UpperCamelCase_				:   Any					    =							mock.Mock()
       UpperCamelCase_				:   int					    =							5_00
       UpperCamelCase_				:   List[str]					    =							{}
       UpperCamelCase_				:   Union[str, Any]					    =							HTTPError
       UpperCamelCase_				:   Union[str, Any]					    =							{}

       # Under the mock environment we get a 500 error when trying to reach the tokenizer.
       with mock.patch("""requests.Session.request"""			,     return_value=__lowerCAmelCase ) as mock_head:
             UpperCamelCase_				:   Tuple					    =							cached_file(__lowerCAmelCase			,     """conf"""			,     _raise_exceptions_for_connection_errors=__lowerCAmelCase )
             self.assertIsNone(__lowerCAmelCase )
             # This check we did call the fake head request
             mock_head.assert_called()
 def   _UpperCAmelCase       (							self ):
       self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only"""			,     __lowerCAmelCase ) )
       self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""			,     __lowerCAmelCase ) )
       self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""			,     __lowerCAmelCase ) )
 def   _UpperCAmelCase       (							self ):
       # `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(__lowerCAmelCase			,     """is not a valid model identifier""" ):
             get_file_from_repo("""bert-base-case"""			,     __lowerCAmelCase )

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

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





 def   _UpperCAmelCase       (							self ):
       with tempfile.TemporaryDirectory() as tmp_dir:
             UpperCamelCase_				:   List[Any]					    =							Path(__lowerCAmelCase ) / """a.txt"""
             filename.touch()
             self.assertEqual(get_file_from_repo(__lowerCAmelCase			,     """a.txt""" )			,     str(__lowerCAmelCase ) )

             self.assertIsNone(get_file_from_repo(__lowerCAmelCase			,     """b.txt""" ) )














'''simple docstring'''

import warnings

from .generation import TFGenerationMixin




class     A    (      SCREAMING_SNAKE_CASE__						):






 """simple docstring"""





 warnings.warn(
     '''Importing `TFGenerationMixin` from `src/transformers/generation_tf_utils.py` is deprecated and will '''
     '''be removed in Transformers v5. Import as `from transformers import TFGenerationMixin` instead.''',   SCREAMING_SNAKE_CASE__,   )






