

"""simple docstring"""


from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
			from transformers import (
			    TFAutoModel,
			    TFAutoModelForCausalLM,
			    TFAutoModelForMaskedLM,
			    TFAutoModelForPreTraining,
			    TFAutoModelForQuestionAnswering,
			    TFAutoModelForSeqaSeqLM,
			    TFAutoModelForSequenceClassification,
			    TFAutoModelForTableQuestionAnswering,
			    TFAutoModelForTokenClassification,
			    TFAutoModelWithLMHead,
			    TFBertForMaskedLM,
			    TFBertForPreTraining,
			    TFBertForQuestionAnswering,
			    TFBertForSequenceClassification,
			    TFBertModel,
			    TFFunnelBaseModel,
			    TFFunnelModel,
			    TFGPTaLMHeadModel,
			    TFRobertaForMaskedLM,
			    TFTaForConditionalGeneration,
			    TFTapasForQuestionAnswering,
			)
			from transformers.models.auto.modeling_tf_auto import (
			    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
			    TF_MODEL_FOR_MASKED_LM_MAPPING,
			    TF_MODEL_FOR_PRETRAINING_MAPPING,
			    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
			    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
			    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
			    TF_MODEL_MAPPING,
			)
			from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
			from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST





class     snake_case	(				__snake_case  ):
				SCREAMING_SNAKE_CASE_   :      Optional[int]         =  """new-model"""


if is_tf_available():





			class     snake_case	(				__snake_case  ):
							SCREAMING_SNAKE_CASE_   :      Dict         =  NewModelConfig





@require_tf
class     snake_case	(				unittest.TestCase  ):

				@slow
				def 							lowercase_     (	self							:				str)->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			"bert-base-cased"
						__lowerCAmelCase:       Dict          =			AutoConfig.from_pretrained(UpperCamelCase__)
						self.assertIsNotNone(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

						__lowerCAmelCase:       List[Any]          =			TFAutoModel.from_pretrained(UpperCamelCase__)
						self.assertIsNotNone(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				Union[str, Any])->   str:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			"bert-base-cased"
						__lowerCAmelCase:       Dict          =			AutoConfig.from_pretrained(UpperCamelCase__)
						self.assertIsNotNone(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

						__lowerCAmelCase:       int          =			TFAutoModelForPreTraining.from_pretrained(UpperCamelCase__)
						self.assertIsNotNone(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				List[Any])->   List[Any]:



						'''simple docstring'''


						for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       str          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       Any          =			TFAutoModelForCausalLM.from_pretrained(UpperCamelCase__)
								__lowerCAmelCase      , __lowerCAmelCase:       Optional[int]          =			TFAutoModelForCausalLM.from_pretrained(UpperCamelCase__     ,  output_loading_info=UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				Any)->   Any:



						'''simple docstring'''


						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       Dict          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       str          =			TFAutoModelWithLMHead.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				int)->   int:



						'''simple docstring'''


						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       Optional[Any]          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       List[str]          =			TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase__)
								__lowerCAmelCase      , __lowerCAmelCase:       int          =			TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase__     ,  output_loading_info=UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				Optional[int])->   Any:



						'''simple docstring'''


						for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:       Union[str, Any]          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       List[str]          =			TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__)
								__lowerCAmelCase      , __lowerCAmelCase:       int          =			TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__     ,  output_loading_info=UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				int)->   List[str]:



						'''simple docstring'''


						for model_name in ["bert-base-uncased"]:
								__lowerCAmelCase:       Any          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       Tuple          =			TFAutoModelForSequenceClassification.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				def 							lowercase_     (	self							:				int)->   List[Any]:



						'''simple docstring'''


						for model_name in ["bert-base-uncased"]:
								__lowerCAmelCase:       str          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       str          =			TFAutoModelForQuestionAnswering.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				@slow
				@require_tensorflow_probability
				def 							lowercase_     (	self							:				Dict)->   int:



						'''simple docstring'''


						for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
								__lowerCAmelCase:       Optional[int]          =			AutoConfig.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

								__lowerCAmelCase:       Any          =			TFAutoModelForTableQuestionAnswering.from_pretrained(UpperCamelCase__)
								__lowerCAmelCase      , __lowerCAmelCase:       int          =			TFAutoModelForTableQuestionAnswering.from_pretrained(
								    UpperCamelCase__     ,  output_loading_info=UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				Dict)->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       Optional[Any]          =			TFAutoModelWithLMHead.from_pretrained(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)
						self.assertEqual(model.num_parameters()     ,  1_4_4_1_0)
						self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase__)     ,  1_4_4_1_0)

				def 							lowercase_     (	self							:				Any)->   str:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			TFAutoModelWithLMHead.from_pretrained(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)
						self.assertEqual(model.num_parameters()     ,  1_4_4_1_0)
						self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase__)     ,  1_4_4_1_0)

				def 							lowercase_     (	self							:				Any)->   str:



						'''simple docstring'''


						__lowerCAmelCase:       List[Any]          =			TFAutoModel.from_pretrained("sgugger/funnel-random-tiny")
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

						__lowerCAmelCase:       Optional[Any]          =			copy.deepcopy(model.config)
						__lowerCAmelCase:       List[str]          =			["FunnelBaseModel"]
						__lowerCAmelCase:       List[str]          =			TFAutoModel.from_config(UpperCamelCase__)
						self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

						with tempfile.TemporaryDirectory() as tmp_dir:
								model.save_pretrained(UpperCamelCase__)
								__lowerCAmelCase:       Union[str, Any]          =			TFAutoModel.from_pretrained(UpperCamelCase__)
								self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				int)->   Optional[Any]:



						'''simple docstring'''


						try:
								AutoConfig.register("new-model"     ,  UpperCamelCase__)

								__lowerCAmelCase:       Optional[int]          =			[
								    TFAutoModel,
								    TFAutoModelForCausalLM,
								    TFAutoModelForMaskedLM,
								    TFAutoModelForPreTraining,
								    TFAutoModelForQuestionAnswering,
								    TFAutoModelForSequenceClassification,
								    TFAutoModelForTokenClassification,
								]

								for auto_class in auto_classes:
										with self.subTest(auto_class.__name__):
												# Wrong config class will raise an error
												with self.assertRaises(UpperCamelCase__):
														auto_class.register(UpperCamelCase__     ,  UpperCamelCase__)
												auto_class.register(UpperCamelCase__     ,  UpperCamelCase__)
												# Trying to register something existing in the Transformers library will raise an error
												with self.assertRaises(UpperCamelCase__):
														auto_class.register(UpperCamelCase__     ,  UpperCamelCase__)

												# Now that the config is registered, it can be used as any other config with the auto-API
												__lowerCAmelCase:       str          =			BertModelTester(self).get_config()
												__lowerCAmelCase:       Dict          =			NewModelConfig(**tiny_config.to_dict())
												__lowerCAmelCase:       Dict          =			auto_class.from_config(UpperCamelCase__)
												self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

												with tempfile.TemporaryDirectory() as tmp_dir:
														model.save_pretrained(UpperCamelCase__)
														__lowerCAmelCase:       Tuple          =			auto_class.from_pretrained(UpperCamelCase__)
														self.assertIsInstance(UpperCamelCase__     ,  UpperCamelCase__)

						finally:
								if "new-model" in CONFIG_MAPPING._extra_content:
										del CONFIG_MAPPING._extra_content["new-model"]
								for mapping in (
								    TF_MODEL_MAPPING,
								    TF_MODEL_FOR_PRETRAINING_MAPPING,
								    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
								    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
								    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
								    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
								    TF_MODEL_FOR_MASKED_LM_MAPPING,
								):
										if NewModelConfig in mapping._extra_content:
												del mapping._extra_content[NewModelConfig]

				def 							lowercase_     (	self							:				str)->   Tuple:



						'''simple docstring'''


						with self.assertRaisesRegex(
						    UpperCamelCase__     ,  "bert-base is not a local folder and is not a valid model identifier"):
								__lowerCAmelCase:       List[str]          =			TFAutoModel.from_pretrained("bert-base")

				def 							lowercase_     (	self							:				Any)->   Optional[Any]:



						'''simple docstring'''


						with self.assertRaisesRegex(
						    UpperCamelCase__     ,  R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"):
								__lowerCAmelCase:       Union[str, Any]          =			TFAutoModel.from_pretrained(UpperCamelCase__     ,  revision="aaaaaa")

				def 							lowercase_     (	self							:				Optional[int])->   Dict:



						'''simple docstring'''


						with self.assertRaisesRegex(
						    UpperCamelCase__     ,  "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"     ,  ):
								__lowerCAmelCase:       Any          =			TFAutoModel.from_pretrained("hf-internal-testing/config-no-model")

				def 							lowercase_     (	self							:				Optional[Any])->   List[str]:



						'''simple docstring'''


						with self.assertRaisesRegex(UpperCamelCase__     ,  "Use `from_pt=True` to load this model"):
								__lowerCAmelCase:       int          =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only")




				def 							lowercase_     (	self							:				List[str])->   Any:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert")
						with RequestCounter() as counter:
								__lowerCAmelCase:       Tuple          =			TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert")
								self.assertEqual(counter.get_request_count     ,  0)
								self.assertEqual(counter.head_request_count     ,  1)
								self.assertEqual(counter.other_request_count     ,  0)

						# With a sharded checkpoint
						__lowerCAmelCase:       Union[str, Any]          =			TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded")
						with RequestCounter() as counter:
								__lowerCAmelCase:       Dict          =			TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded")
								self.assertEqual(counter.get_request_count     ,  0)
								self.assertEqual(counter.head_request_count     ,  1)
								self.assertEqual(counter.other_request_count     ,  0)





"""simple docstring"""


from urllib.parse import quote

import pytest

from datasets.utils.hub import hf_hub_url






@pytest.mark.parametrize("repo_id"					, ["canonical_dataset_name", "org-name/dataset-name"] )
@pytest.mark.parametrize("path"					, ["filename.csv", "filename with blanks.csv"] )
@pytest.mark.parametrize("revision"					, [None, "v2"] )
def     a__    (    __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE )	->	List[str]:
		__lowerCAmelCase:       Optional[Any]          =			hf_hub_url(repo_id=__SCREAMING_SNAKE_CASE					, path=__SCREAMING_SNAKE_CASE					, revision=__SCREAMING_SNAKE_CASE )
		assert url == F"https://huggingface.co/datasets/{repo_id}/resolve/{revision or 'main'}/{quote(__SCREAMING_SNAKE_CASE )}"


