


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






 """simple docstring"""




 __lowerCAmelCase 					=  "new-model"


if is_tf_available():






       class       __A				(       __a			):






        """simple docstring"""




        __lowerCAmelCase 					=  NewModelConfig






@require_tf
class       __A				(       unittest.TestCase			):






 """simple docstring"""




 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Tuple:
     a 		="""bert-base-cased"""
     a 		=AutoConfig.from_pretrained(a_						)
     self.assertIsNotNone(a_						)
     self.assertIsInstance(a_ ,    a_						)

     a 		=TFAutoModel.from_pretrained(a_						)
     self.assertIsNotNone(a_						)
     self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Optional[int]:
     a 		="""bert-base-cased"""
     a 		=AutoConfig.from_pretrained(a_						)
     self.assertIsNotNone(a_						)
     self.assertIsInstance(a_ ,    a_						)

     a 		=TFAutoModelForPreTraining.from_pretrained(a_						)
     self.assertIsNotNone(a_						)
     self.assertIsInstance(a_ ,    a_						)


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



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

         a 		=TFAutoModelForCausalLM.from_pretrained(a_						)
         a 		=TFAutoModelForCausalLM.from_pretrained(a_ ,    output_loading_info=a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Any:



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

         a 		=TFAutoModelWithLMHead.from_pretrained(a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Dict:



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

         a 		=TFAutoModelForMaskedLM.from_pretrained(a_						)
         a 		=TFAutoModelForMaskedLM.from_pretrained(a_ ,    output_loading_info=a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Tuple:



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

         a 		=TFAutoModelForSeqaSeqLM.from_pretrained(a_						)
         a 		=TFAutoModelForSeqaSeqLM.from_pretrained(a_ ,    output_loading_info=a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     int:



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

         a 		=TFAutoModelForSequenceClassification.from_pretrained(a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 def       SCREAMING_SNAKE_CASE							(		self						)					->     Dict:



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

         a 		=TFAutoModelForQuestionAnswering.from_pretrained(a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 @slow
 @require_tensorflow_probability
 def       SCREAMING_SNAKE_CASE							(		self						)					->     str:



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

         a 		=TFAutoModelForTableQuestionAnswering.from_pretrained(a_						)
         a 		=TFAutoModelForTableQuestionAnswering.from_pretrained(
             a_ ,    output_loading_info=a_						)
         self.assertIsNotNone(a_						)
         self.assertIsInstance(a_ ,    a_						)


 def       SCREAMING_SNAKE_CASE							(		self						)					->     Dict:
     a 		=TFAutoModelWithLMHead.from_pretrained(a_						)
     self.assertIsInstance(a_ ,    a_						)
     self.assertEqual(model.num_parameters() ,    1_4410						)
     self.assertEqual(model.num_parameters(only_trainable=a_						) ,    1_4410						)


 def       SCREAMING_SNAKE_CASE							(		self						)					->     List[Any]:
     a 		=TFAutoModelWithLMHead.from_pretrained(a_						)
     self.assertIsInstance(a_ ,    a_						)
     self.assertEqual(model.num_parameters() ,    1_4410						)
     self.assertEqual(model.num_parameters(only_trainable=a_						) ,    1_4410						)


 def       SCREAMING_SNAKE_CASE							(		self						)					->     Tuple:
     a 		=TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''						)
     self.assertIsInstance(a_ ,    a_						)

     a 		=copy.deepcopy(model.config						)
     a 		=["""FunnelBaseModel"""]
     a 		=TFAutoModel.from_config(a_						)
     self.assertIsInstance(a_ ,    a_						)

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


 def       SCREAMING_SNAKE_CASE							(		self						)					->     Tuple:



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

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

                 with tempfile.TemporaryDirectory() as tmp_dir:
                     model.save_pretrained(a_						)
                     a 		=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       SCREAMING_SNAKE_CASE							(		self						)					->     str:



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


 def       SCREAMING_SNAKE_CASE							(		self						)					->     str:



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


 def       SCREAMING_SNAKE_CASE							(		self						)					->     List[Any]:



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


 def       SCREAMING_SNAKE_CASE							(		self						)					->     int:



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





 def       SCREAMING_SNAKE_CASE							(		self						)					->     List[Any]:
     a 		=TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''						)
     with RequestCounter() as counter:
         a 		=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 		=TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''						)
     with RequestCounter() as counter:
         a 		=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 argparse
import hashlib  # hashlib is only used inside the Test class
import struct






class 					lowercase:







 '''simple docstring'''





 def __init__(				self:		List[Any],      a_:		List[str]		):




   '''simple docstring'''



   _snake_case							:		int								=  data
   _snake_case							:		Dict								=  [0X67452301, 0Xefcdab89, 0X98badcfe, 0X10325476, 0Xc3d2e1f0]


 @staticmethod
 def  UpperCamelCase_   (				a_:		Optional[Any],      a_:		Dict		):




   '''simple docstring'''



   return ((n << b) | (n >> (32 - b))) & 0Xffffffff


 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  B"""\x80""" + B"""\x00""" * (63 - (len(self.data		) + 8) % 64)
   _snake_case							:		Optional[int]								=  self.data + padding + struct.pack(""">Q""",      8 * len(self.data		)		)
   return padded_data


 def  UpperCamelCase_   (				self:		Union[str, Any]		):




   '''simple docstring'''



   return [
       self.padded_data[i : i + 64] for i in range(0,      len(self.padded_data		),      64		)
   ]


 def  UpperCamelCase_   (				self:		Optional[Any],      a_:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  list(struct.unpack(""">16L""",      a_		)		) + [0] * 64
   for i in range(16,      80		):
     _snake_case							:		List[Any]								=  self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]),      1		)
   return w


 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  self.padding()
   _snake_case							:		str								=  self.split_blocks()
   for block in self.blocks:
     _snake_case							:		Any								=  self.expand_block(a_		)
     _snake_case		,					_snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Optional[int]								=  self.h
     for i in range(0,      80		):
       if 0 <= i < 20:
         _snake_case							:		int								=  (b & c) | ((~b) & d)
         _snake_case							:		str								=  0X5a827999
       elif 20 <= i < 40:
         _snake_case							:		Optional[int]								=  b ^ c ^ d
         _snake_case							:		str								=  0X6ed9eba1
       elif 40 <= i < 60:
         _snake_case							:		List[Any]								=  (b & c) | (b & d) | (c & d)
         _snake_case							:		List[Any]								=  0X8f1bbcdc
       elif 60 <= i < 80:
         _snake_case							:		List[Any]								=  b ^ c ^ d
         _snake_case							:		int								=  0Xca62c1d6
       _snake_case		,					_snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Optional[int]								=  (
           self.rotate(a_,      5		) + f + e + k + expanded_block[i] & 0Xffffffff,
           a,
           self.rotate(a_,      30		),
           c,
           d,
       )
     _snake_case							:		Union[str, Any]								=  (
         self.h[0] + a & 0Xffffffff,
         self.h[1] + b & 0Xffffffff,
         self.h[2] + c & 0Xffffffff,
         self.h[3] + d & 0Xffffffff,
         self.h[4] + e & 0Xffffffff,
     )
   return ("{:08x}" * 5).format(*self.h		)







def 					UpperCAmelCase__       ():
  """simple docstring"""

  _snake_case							:		Any								=  B"""Test String"""
  assert SHAaHash(snake_case__    ).final_hash() == hashlib.shaa(snake_case__    ).hexdigest()  # noqa: S324


def 					UpperCAmelCase__       ():
  """simple docstring"""

  _snake_case							:		List[Any]								=  argparse.ArgumentParser(description="""Process some strings or files"""    )
  parser.add_argument(
      """--string"""		,   dest="""input_string"""		,   default="""Hello World!! Welcome to Cryptography"""		,   help="""Hash the string"""		,   )
  parser.add_argument("""--file"""		,   dest="""input_file"""		,   help="""Hash contents of a file"""    )
  _snake_case							:		Union[str, Any]								=  parser.parse_args()
  _snake_case							:		List[Any]								=  args.input_string
  # In any case hash input should be a bytestring
  if args.input_file:
    with open(args.input_file		,   """rb"""    ) as f:
      _snake_case							:		str								=  f.read()
  else:
    _snake_case							:		int								=  bytes(snake_case__		,   """utf-8"""    )
  print(SHAaHash(snake_case__    ).final_hash()    )


if __name__ == "__main__":
     main()
     import doctest

     doctest.testmod()


