


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        _a							(			UpperCamelCase__			):
							_lowercase	:			List[str]        = '''new-model'''


if is_tf_available():


			class        _a							(			UpperCamelCase__			):
										_lowercase	:			str        = NewModelConfig


@require_tf
class        _a							(			unittest.TestCase			):

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





									"""simple docstring"""







									lowercase__            =      '''bert-base-cased'''
									lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
									self.assertIsNotNone(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

									lowercase__            =      TFAutoModel.from_pretrained(UpperCamelCase_							)
									self.assertIsNotNone(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







									lowercase__            =      '''bert-base-cased'''
									lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
									self.assertIsNotNone(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

									lowercase__            =      TFAutoModelForPreTraining.from_pretrained(UpperCamelCase_							)
									self.assertIsNotNone(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

							@slow
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Dict:





									"""simple docstring"""







									for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

											lowercase__            =      TFAutoModelForCausalLM.from_pretrained(UpperCamelCase_							)
											lowercase__     ,		lowercase__            =      TFAutoModelForCausalLM.from_pretrained(UpperCamelCase_    ,						output_loading_info=UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







									for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

											lowercase__            =      TFAutoModelWithLMHead.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

							@slow
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Tuple:





									"""simple docstring"""







									for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

											lowercase__            =      TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase_							)
											lowercase__     ,		lowercase__            =      TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase_    ,						output_loading_info=UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

							@slow
							def       lowerCamelCase_ (							self:	List[str]							)			->   Optional[Any]:





									"""simple docstring"""







									for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

											lowercase__            =      TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase_							)
											lowercase__     ,		lowercase__            =      TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase_    ,						output_loading_info=UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







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

											lowercase__            =      TFAutoModelForSequenceClassification.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







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

											lowercase__            =      TFAutoModelForQuestionAnswering.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

							@slow
							@require_tensorflow_probability
							def       lowerCamelCase_ (							self:	Tuple							)			->   Any:





									"""simple docstring"""







									for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
											lowercase__            =      AutoConfig.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

											lowercase__            =      TFAutoModelForTableQuestionAnswering.from_pretrained(UpperCamelCase_							)
											lowercase__     ,		lowercase__            =      TFAutoModelForTableQuestionAnswering.from_pretrained(
											    UpperCamelCase_    ,						output_loading_info=UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







									lowercase__            =      TFAutoModelWithLMHead.from_pretrained(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)
									self.assertEqual(model.num_parameters()    ,						14_410							)
									self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase_							)    ,						14_410							)

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





									"""simple docstring"""







									lowercase__            =      TFAutoModelWithLMHead.from_pretrained(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)
									self.assertEqual(model.num_parameters()    ,						14_410							)
									self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase_							)    ,						14_410							)

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





									"""simple docstring"""







									lowercase__            =      TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

									lowercase__            =      copy.deepcopy(model.config							)
									lowercase__            =      ['''FunnelBaseModel''']
									lowercase__            =      TFAutoModel.from_config(UpperCamelCase_							)
									self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

									with tempfile.TemporaryDirectory() as tmp_dir:
											model.save_pretrained(UpperCamelCase_							)
											lowercase__            =      TFAutoModel.from_pretrained(UpperCamelCase_							)
											self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	Tuple							)			->   Any:





									"""simple docstring"""







									try:
											AutoConfig.register('''new-model'''    ,						UpperCamelCase_							)

											lowercase__            =      [
											    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
															lowercase__            =      BertModelTester(self							).get_config()
															lowercase__            =      NewModelConfig(**tiny_config.to_dict()							)
															lowercase__            =      auto_class.from_config(UpperCamelCase_							)
															self.assertIsInstance(UpperCamelCase_    ,						UpperCamelCase_							)

															with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(UpperCamelCase_							)
																	lowercase__            =      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       lowerCamelCase_ (							self:	Dict							)			->   Optional[Any]:





									"""simple docstring"""







									with self.assertRaisesRegex(
									    UpperCamelCase_    ,						'''bert-base is not a local folder and is not a valid model identifier'''							):
											lowercase__            =      TFAutoModel.from_pretrained('''bert-base'''							)

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





									"""simple docstring"""







									with self.assertRaisesRegex(
									    UpperCamelCase_    ,						r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''							):
											lowercase__            =      TFAutoModel.from_pretrained(UpperCamelCase_    ,						revision='''aaaaaa'''							)

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Dict:





									"""simple docstring"""







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

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





									"""simple docstring"""







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


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





									"""simple docstring"""







									lowercase__            =      TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''							)
									with RequestCounter() as counter:
											lowercase__            =      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
									lowercase__            =      TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''							)
									with RequestCounter() as counter:
											lowercase__            =      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							)






def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')     )
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      credit_card_number
		lowercase__            =      0
		lowercase__            =      len(SCREAMING_SNAKE_CASE     ) - 2
		for i in range(SCREAMING_SNAKE_CASE						,				-1						,				-2     ):
				#  double the value of every second digit
				lowercase__            =      int(cc_number[i]     )
				digit *= 2
				# If doubling of a number results in a two digit number
				# i.e greater than 9(e.g., 6 × 2 = 12),
				# then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6),
				# to get a single digit number.
				if digit > 9:
						digit %= 10
						digit += 1
				lowercase__            =      cc_number[:i] + str(SCREAMING_SNAKE_CASE     ) + cc_number[i + 1 :]
				total += digit

		# Sum up the remaining digits
		for i in range(len(SCREAMING_SNAKE_CASE     ) - 1						,				-1						,				-2     ):
				total += int(cc_number[i]     )

		return total % 10 == 0
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      f'{credit_card_number} is an invalid credit card number because'
		if not credit_card_number.isdigit():
				print(f'{error_message} it has nonnumerical characters.'     )
				return False

		if not 13 <= len(SCREAMING_SNAKE_CASE     ) <= 16:
				print(f'{error_message} of its length.'     )
				return False

		if not validate_initial_digits(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} of its first two digits.'     )
				return False

		if not luhn_validation(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} it fails the Luhn check.'     )
				return False

		print(f'{credit_card_number} is a valid credit card number.'     )
		return True


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			validate_credit_card_number('4111111111111111')
			validate_credit_card_number('32323')


