



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 			__lowerCAmelCase    (			A						):
			UpperCamelCase   					=     '''new-model'''


if is_tf_available():







							class 			__lowerCAmelCase    (			A						):
										UpperCamelCase   					=     NewModelConfig







@require_tf
class 			__lowerCAmelCase    (			unittest.TestCase						):



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



						"""simple docstring"""






						_UpperCAmelCase		       =   'bert-base-cased'
						_UpperCAmelCase		       =   AutoConfig.from_pretrained(A)
						self.assertIsNotNone(A)
						self.assertIsInstance(A ,		A)

						_UpperCAmelCase		       =   TFAutoModel.from_pretrained(A)
						self.assertIsNotNone(A)
						self.assertIsInstance(A ,		A)



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



						"""simple docstring"""






						_UpperCAmelCase		       =   'bert-base-cased'
						_UpperCAmelCase		       =   AutoConfig.from_pretrained(A)
						self.assertIsNotNone(A)
						self.assertIsInstance(A ,		A)

						_UpperCAmelCase		       =   TFAutoModelForPreTraining.from_pretrained(A)
						self.assertIsNotNone(A)
						self.assertIsInstance(A ,		A)



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



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForCausalLM.from_pretrained(A)
									_UpperCAmelCase		       =   TFAutoModelForCausalLM.from_pretrained(A ,		output_loading_info=A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



			@slow
			def     _lowerCamelCase       (						self   :  Any)   ->    Tuple:



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelWithLMHead.from_pretrained(A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



			@slow
			def     _lowerCamelCase       (						self   :  Dict)   ->    Dict:



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForMaskedLM.from_pretrained(A)
									_UpperCAmelCase		       =   TFAutoModelForMaskedLM.from_pretrained(A ,		output_loading_info=A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



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



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForSeqaSeqLM.from_pretrained(A)
									_UpperCAmelCase		       =   TFAutoModelForSeqaSeqLM.from_pretrained(A ,		output_loading_info=A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



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



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForSequenceClassification.from_pretrained(A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



			@slow
			def     _lowerCamelCase       (						self   :  Any)   ->    str:



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForQuestionAnswering.from_pretrained(A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



			@slow
			@require_tensorflow_probability
			def     _lowerCamelCase       (						self   :  int)   ->    Union[str, Any]:



						"""simple docstring"""






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

									_UpperCAmelCase		       =   TFAutoModelForTableQuestionAnswering.from_pretrained(A)
									_UpperCAmelCase		       =   TFAutoModelForTableQuestionAnswering.from_pretrained(
									    A ,		output_loading_info=A)
									self.assertIsNotNone(A)
									self.assertIsInstance(A ,		A)



			def     _lowerCamelCase       (						self   :  Optional[Any])   ->    Tuple:



						"""simple docstring"""






						_UpperCAmelCase		       =   TFAutoModelWithLMHead.from_pretrained(A)
						self.assertIsInstance(A ,		A)
						self.assertEqual(model.num_parameters() ,		1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=A) ,		1_44_10)



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



						"""simple docstring"""






						_UpperCAmelCase		       =   TFAutoModelWithLMHead.from_pretrained(A)
						self.assertIsInstance(A ,		A)
						self.assertEqual(model.num_parameters() ,		1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=A) ,		1_44_10)



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



						"""simple docstring"""






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

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

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



			def     _lowerCamelCase       (						self   :  str)   ->    Optional[int]:



						"""simple docstring"""






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

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

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

															with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(A)
																		_UpperCAmelCase		       =   auto_class.from_pretrained(A)
																		self.assertIsInstance(A ,		A)

						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   :  int)   ->    Union[str, Any]:



						"""simple docstring"""






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



			def     _lowerCamelCase       (						self   :  Dict)   ->    List[str]:



						"""simple docstring"""






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



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



						"""simple docstring"""






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



			def     _lowerCamelCase       (						self   :  Tuple)   ->    Any:



						"""simple docstring"""






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



			def     _lowerCamelCase       (						self   :  List[Any])   ->    Dict:



						"""simple docstring"""






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







def      _snake_case			(				):
				_lowerCamelCase     :			List[Any]    =    socket.socket(socket.AF_INET							,    socket.SOCK_STREAM		)
				_lowerCamelCase     :			Union[str, Any]    =    socket.gethostname()
				_lowerCamelCase     :			List[Any]    =    12312

				sock.connect((host, port)		)
				sock.send(B'Hello server!'		)

				with open('Received_file'							,    'wb'		) as out_file:
								print('File opened'		)
								print('Receiving data...'		)
								while True:
												_lowerCamelCase     :			int    =    sock.recv(1024		)
												if not data:
																break
												out_file.write(lowercase__		)

				print('Successfully received the file'		)
				sock.close()
				print('Connection closed'		)


if __name__ == "__main__":
			main()