"""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 						__UpperCAmelCase    (__A     ):







			'''simple docstring'''
			_UpperCamelCase  :						List[Any]					     =				'new-model'


if is_tf_available():
				class 						__UpperCAmelCase    (__A     ):







							'''simple docstring'''
							_UpperCamelCase  :						Optional[int]					     =				NewModelConfig
@require_tf
class 						__UpperCAmelCase    (unittest.TestCase     ):







			'''simple docstring'''





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			"""bert-base-cased"""
										A__       :     int								=			AutoConfig.from_pretrained(snake_case_							)
										self.assertIsNotNone(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)

										A__       :     Union[str, Any]								=			TFAutoModel.from_pretrained(snake_case_							)
										self.assertIsNotNone(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			"""bert-base-cased"""
										A__       :     Union[str, Any]								=			AutoConfig.from_pretrained(snake_case_							)
										self.assertIsNotNone(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)

										A__       :     List[Any]								=			TFAutoModelForPreTraining.from_pretrained(snake_case_							)
										self.assertIsNotNone(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Optional[Any]								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     Tuple								=			TFAutoModelForCausalLM.from_pretrained(snake_case_							)
																	A__				,							A__       :     int								=			TFAutoModelForCausalLM.from_pretrained(snake_case_   ,      output_loading_info=snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Tuple								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     Optional[Any]								=			TFAutoModelWithLMHead.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Union[str, Any]								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     str								=			TFAutoModelForMaskedLM.from_pretrained(snake_case_							)
																	A__				,							A__       :     Union[str, Any]								=			TFAutoModelForMaskedLM.from_pretrained(snake_case_   ,      output_loading_info=snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Tuple								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     Tuple								=			TFAutoModelForSeqaSeqLM.from_pretrained(snake_case_							)
																	A__				,							A__       :     Any								=			TFAutoModelForSeqaSeqLM.from_pretrained(snake_case_   ,      output_loading_info=snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

																	A__       :     Union[str, Any]								=			TFAutoModelForSequenceClassification.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in ["bert-base-uncased"]:
																	A__       :     Tuple								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     Any								=			TFAutoModelForQuestionAnswering.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			@slow
			@require_tensorflow_probability
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
																	A__       :     Dict								=			AutoConfig.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)

																	A__       :     List[Any]								=			TFAutoModelForTableQuestionAnswering.from_pretrained(snake_case_							)
																	A__				,							A__       :     Optional[Any]								=			TFAutoModelForTableQuestionAnswering.from_pretrained(
																	    snake_case_   ,      output_loading_info=snake_case_							)
																	self.assertIsNotNone(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[int]								=			TFAutoModelWithLMHead.from_pretrained(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)
										self.assertEqual(model.num_parameters()   ,      14_410							)
										self.assertEqual(model.num_parameters(only_trainable=snake_case_							)   ,      14_410							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			TFAutoModelWithLMHead.from_pretrained(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)
										self.assertEqual(model.num_parameters()   ,      14_410							)
										self.assertEqual(model.num_parameters(only_trainable=snake_case_							)   ,      14_410							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			TFAutoModel.from_pretrained("""sgugger/funnel-random-tiny"""							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)

										A__       :     Any								=			copy.deepcopy(model.config							)
										A__       :     Dict								=			["""FunnelBaseModel"""]
										A__       :     Optional[int]								=			TFAutoModel.from_config(snake_case_							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)

										with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(snake_case_							)
																	A__       :     Any								=			TFAutoModel.from_pretrained(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

																	A__       :     List[Any]								=			[
																	    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(snake_case_							):
																																						auto_class.register(snake_case_   ,      snake_case_							)
																															auto_class.register(snake_case_   ,      snake_case_							)
																															# Trying to register something existing in the Transformers library will raise an error
																															with self.assertRaises(snake_case_							):
																																						auto_class.register(snake_case_   ,      snake_case_							)

																															# Now that the config is registered, it can be used as any other config with the auto-API
																															A__       :     str								=			BertModelTester(self							).get_config()
																															A__       :     Union[str, Any]								=			NewModelConfig(**tiny_config.to_dict()							)
																															A__       :     str								=			auto_class.from_config(snake_case_							)
																															self.assertIsInstance(snake_case_   ,      snake_case_							)

																															with tempfile.TemporaryDirectory() as tmp_dir:
																																						model.save_pretrained(snake_case_							)
																																						A__       :     Any								=			auto_class.from_pretrained(snake_case_							)
																																						self.assertIsInstance(snake_case_   ,      snake_case_							)

										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 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    snake_case_   ,      """bert-base is not a local folder and is not a valid model identifier"""							):
																	A__       :     Optional[int]								=			TFAutoModel.from_pretrained("""bert-base"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    snake_case_   ,      r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""							):
																	A__       :     str								=			TFAutoModel.from_pretrained(snake_case_   ,      revision="""aaaaaa"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(snake_case_   ,      """Use `from_pt=True` to load this model"""							):
																	A__       :     Optional[Any]								=			TFAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			TFAutoModel.from_pretrained("""hf-internal-testing/tiny-random-bert"""							)
										with RequestCounter() as counter:
																	A__       :     Optional[int]								=			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
										A__       :     Dict								=			TFAutoModel.from_pretrained("""ArthurZ/tiny-random-bert-sharded"""							)
										with RequestCounter() as counter:
																	A__       :     List[str]								=			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"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin
class 						__UpperCAmelCase    (unittest.TestCase							,   __A     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			load_tool("""text-classification"""							)
										self.tool.setup()
										A__       :     Any								=			load_tool("""text-classification"""   ,      remote=snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			self.remote_tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.remote_tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





