


'''simple docstring'''



import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
      import tensorflow as tf

if is_tensorflow_text_available():
      from transformers.models.bert import TFBertTokenizer


A__						:   str				=    ["""bert-base-uncased""", """bert-base-cased"""]
A__						:   Union[str, Any]				=    """hf-internal-testing/tiny-bert-tf-only"""

if is_tf_available():





      class   UpperCAmelCase_    (tf.keras.Model    ):





          """simple docstring"""






          def __init__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	Optional[int]:
               super().__init__()
               __lowerCamelCase  :      Union[str, Any]														=						tokenizer
               __lowerCamelCase  :      int														=						AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_      )
               __lowerCamelCase  :      Union[str, Any]														=						TFAutoModel.from_config(SCREAMING_SNAKE_CASE_      )







          def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_      )							->	int:
               __lowerCamelCase  :      str														=						self.tokenizer(SCREAMING_SNAKE_CASE_      )
               __lowerCamelCase  :      Any														=						self.bert(**SCREAMING_SNAKE_CASE_      )
               return out["pooler_output"]





@require_tf
@require_tensorflow_text
class   UpperCAmelCase_    (unittest.TestCase    ):





    """simple docstring"""






    def  lowercase_							(				self      )							->	Any:
         super().setUp()

         __lowerCamelCase  :      List[str]														=						[
             BertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_      ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
         ]  # repeat for when fast_bert_tokenizer=false
         __lowerCamelCase  :      Union[str, Any]														=						[TFBertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_      ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
             TFBertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_     ,	use_fast_bert_tokenizer=SCREAMING_SNAKE_CASE_      )
             for checkpoint in TOKENIZER_CHECKPOINTS
         ]
         assert len(self.tokenizers      ) == len(self.tf_tokenizers      )

         __lowerCamelCase  :      Optional[Any]														=						[
             'This is a straightforward English test sentence.',
             'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
             'Now we\'re going to add some Chinese: 一 二 三 一二三',
             'And some much more rare Chinese: 齉 堃 齉堃',
             'Je vais aussi écrire en français pour tester les accents',
             'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
         ]
         __lowerCamelCase  :      str														=						list(zip(self.test_sentences     ,	self.test_sentences[::-1]      )      )







    def  lowercase_							(				self      )							->	Optional[int]:
         for tokenizer, tf_tokenizer in zip(self.tokenizers     ,	self.tf_tokenizers      ):
              for test_inputs in (self.test_sentences, self.paired_sentences):
                   __lowerCamelCase  :      Union[str, Any]														=						tokenizer(SCREAMING_SNAKE_CASE_     ,	return_tensors='tf'     ,	padding='longest'      )
                   __lowerCamelCase  :      Any														=						tf_tokenizer(SCREAMING_SNAKE_CASE_      )

                   for key in python_outputs.keys():
                        self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape      )      )
                        self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]     ,	tf.intaa      ) == tf_outputs[key]      )      )







    @slow
    def  lowercase_							(				self      )							->	List[str]:
         for tf_tokenizer in self.tf_tokenizers:
              __lowerCamelCase  :      Any														=						tf_tokenizer(self.paired_sentences      )
              __lowerCamelCase  :      int														=						tf_tokenizer(
                  text=[sentence[0] for sentence in self.paired_sentences]     ,	text_pair=[sentence[1] for sentence in self.paired_sentences]     ,	)
              for key in merged_outputs.keys():
                   self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]     ,	tf.intaa      ) == separated_outputs[key]      )      )







    @slow
    def  lowercase_							(				self      )							->	int:
         for tf_tokenizer in self.tf_tokenizers:
              __lowerCamelCase  :      Optional[int]														=						tf.function(SCREAMING_SNAKE_CASE_      )
              for test_inputs in (self.test_sentences, self.paired_sentences):
                   __lowerCamelCase  :      Dict														=						tf.constant(SCREAMING_SNAKE_CASE_      )
                   __lowerCamelCase  :      Optional[int]														=						compiled_tokenizer(SCREAMING_SNAKE_CASE_      )
                   __lowerCamelCase  :      Optional[int]														=						tf_tokenizer(SCREAMING_SNAKE_CASE_      )

                   for key in eager_outputs.keys():
                        self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]      )      )







    @slow
    def  lowercase_							(				self      )							->	Any:
         for tf_tokenizer in self.tf_tokenizers:
              __lowerCamelCase  :      Union[str, Any]														=						ModelToSave(tokenizer=SCREAMING_SNAKE_CASE_      )
              __lowerCamelCase  :      List[str]														=						tf.convert_to_tensor(self.test_sentences      )
              __lowerCamelCase  :      Optional[Any]														=						model(SCREAMING_SNAKE_CASE_      )  # Build model with some sample inputs
              with TemporaryDirectory() as tempdir:
                   __lowerCamelCase  :      List[Any]														=						Path(SCREAMING_SNAKE_CASE_      ) / 'saved.model'
                   model.save(SCREAMING_SNAKE_CASE_      )
                   __lowerCamelCase  :      Any														=						tf.keras.models.load_model(SCREAMING_SNAKE_CASE_      )
              __lowerCamelCase  :      List[Any]														=						loaded_model(SCREAMING_SNAKE_CASE_      )
              # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
              self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output      )      )     ,	1E-5      )










'''simple docstring'''



import argparse


A__						:   Optional[Any]				=    """docs/source/_static/js/custom.js"""



def      UpperCAmelCase__   (      UpperCAmelCase_       :							Optional[int]				)	->						int:
     with open(UpperCAmelCase_ ,       encoding='utf-8' ,       newline='\n'				) as f:
          __lowerCamelCase  :      Dict														=						f.readlines()
     __lowerCamelCase  :      Tuple														=						0

     # First let's put the right version
     while not lines[index].startswith('const stableVersion ='				):
          index += 1
     __lowerCamelCase  :      Dict														=						F'const stableVersion = "v{version}"\n'

     # Then update the dictionary
     while not lines[index].startswith('const versionMapping = {'				):
          index += 1

     # We go until the end
     while not lines[index].startswith('}'				):
          index += 1
     # We add the new version at the end
     lines[index - 1] += F'    "v{version}": "v{version}",\n'

     with open(UpperCAmelCase_ ,       'w' ,       encoding='utf-8' ,       newline='\n'				) as f:
          f.writelines(UpperCAmelCase_				)


if __name__ == "__main__":
      A__						:   str				=    argparse.ArgumentParser()
      parser.add_argument("""--version""", help="""Release version.""")
      A__						:   Any				=    parser.parse_args()
      update_custom_js(args.version)






