



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


a     :    Optional[int]      						=					"""hf-internal-testing/tiny-random-bert"""
a     :    Tuple      						=					os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
a     :    Dict      						=					"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""

class     UpperCamelCase_				(							unittest.TestCase    ):





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

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

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





	def 				_lowercase(						self					)    ->    Union[str, Any]:
								with self.assertRaisesRegex(A	,	"""is not a valid model identifier"""					):
															UpperCAmelCase  :	Optional[Any]							  =	cached_file("""tiny-random-bert"""	,	A					)

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

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





	def 				_lowercase(						self					)    ->    str:
								with self.assertRaisesRegex(A	,	"""does not appear to have a file named"""					):
															UpperCAmelCase  :	List[str]							  =	cached_file(A	,	"""conf"""					)

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

								UpperCAmelCase  :	Tuple							  =	cached_file(A	,	"""conf"""	,	_raise_exceptions_for_missing_entries=A					)
								self.assertIsNone(A					)

								UpperCAmelCase  :	List[Any]							  =	cached_file(A	,	"""conf"""	,	local_files_only=A	,	_raise_exceptions_for_missing_entries=A					)
								self.assertIsNone(A					)

								UpperCAmelCase  :	Tuple							  =	mock.Mock()
								UpperCAmelCase  :	int							  =	500
								UpperCAmelCase  :	Any							  =	{}
								UpperCAmelCase  :	Dict							  =	HTTPError
								UpperCAmelCase  :	List[Any]							  =	{}

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





	def 				_lowercase(						self					)    ->    List[str]:
								self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only"""	,	A					)					)
								self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""	,	A					)					)
								self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""	,	A					)					)





	def 				_lowercase(						self					)    ->    List[str]:
								# `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(A	,	"""is not a valid model identifier"""					):
															get_file_from_repo("""bert-base-case"""	,	A					)

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

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






	def 				_lowercase(						self					)    ->    List[str]:
								with tempfile.TemporaryDirectory() as tmp_dir:
															UpperCAmelCase  :	List[Any]							  =	Path(A					) / """a.txt"""
															filename.touch()
															self.assertEqual(get_file_from_repo(A	,	"""a.txt"""					)	,	str(A					)					)

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






'''simple docstring'''




from typing import TYPE_CHECKING

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


a     :    Optional[Any]      						=					{
    """configuration_bert""": ["""BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BertConfig""", """BertOnnxConfig"""],
    """tokenization_bert""": ["""BasicTokenizer""", """BertTokenizer""", """WordpieceTokenizer"""],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a     :    Dict      						=					["""BertTokenizerFast"""]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a     :    List[Any]      						=					[
							    """BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """BertForMaskedLM""",
							    """BertForMultipleChoice""",
							    """BertForNextSentencePrediction""",
							    """BertForPreTraining""",
							    """BertForQuestionAnswering""",
							    """BertForSequenceClassification""",
							    """BertForTokenClassification""",
							    """BertLayer""",
							    """BertLMHeadModel""",
							    """BertModel""",
							    """BertPreTrainedModel""",
							    """load_tf_weights_in_bert""",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a     :    List[str]      						=					[
							    """TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFBertEmbeddings""",
							    """TFBertForMaskedLM""",
							    """TFBertForMultipleChoice""",
							    """TFBertForNextSentencePrediction""",
							    """TFBertForPreTraining""",
							    """TFBertForQuestionAnswering""",
							    """TFBertForSequenceClassification""",
							    """TFBertForTokenClassification""",
							    """TFBertLMHeadModel""",
							    """TFBertMainLayer""",
							    """TFBertModel""",
							    """TFBertPreTrainedModel""",
							]
try:
							if not is_tensorflow_text_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a     :    int      						=					["""TFBertTokenizer"""]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a     :    Optional[Any]      						=					[
							    """FlaxBertForCausalLM""",
							    """FlaxBertForMaskedLM""",
							    """FlaxBertForMultipleChoice""",
							    """FlaxBertForNextSentencePrediction""",
							    """FlaxBertForPreTraining""",
							    """FlaxBertForQuestionAnswering""",
							    """FlaxBertForSequenceClassification""",
							    """FlaxBertForTokenClassification""",
							    """FlaxBertModel""",
							    """FlaxBertPreTrainedModel""",
							]

if TYPE_CHECKING:
							from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
							from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_bert_fast import BertTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_bert import (
														    BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    BertForMaskedLM,
														    BertForMultipleChoice,
														    BertForNextSentencePrediction,
														    BertForPreTraining,
														    BertForQuestionAnswering,
														    BertForSequenceClassification,
														    BertForTokenClassification,
														    BertLayer,
														    BertLMHeadModel,
														    BertModel,
														    BertPreTrainedModel,
														    load_tf_weights_in_bert,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_bert import (
														    TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFBertEmbeddings,
														    TFBertForMaskedLM,
														    TFBertForMultipleChoice,
														    TFBertForNextSentencePrediction,
														    TFBertForPreTraining,
														    TFBertForQuestionAnswering,
														    TFBertForSequenceClassification,
														    TFBertForTokenClassification,
														    TFBertLMHeadModel,
														    TFBertMainLayer,
														    TFBertModel,
														    TFBertPreTrainedModel,
														)

							try:
														if not is_tensorflow_text_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_bert_tf import TFBertTokenizer

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_bert import (
														    FlaxBertForCausalLM,
														    FlaxBertForMaskedLM,
														    FlaxBertForMultipleChoice,
														    FlaxBertForNextSentencePrediction,
														    FlaxBertForPreTraining,
														    FlaxBertForQuestionAnswering,
														    FlaxBertForSequenceClassification,
														    FlaxBertForTokenClassification,
														    FlaxBertModel,
														    FlaxBertPreTrainedModel,
														)

else:
							import sys

							a     :    Optional[int]      						=					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

