

import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
  from tokenizers import Tokenizer
  from tokenizers.models import WordPiece







class  UpperCamelCase_      (	unittest.TestCase      ):


  """simple docstring"""



  def   _UpperCamelCase     (						self     :					List[str]  ,	A_     :					List[str]						)       ->			Optional[Any]:
      SCREAMING_SNAKE_CASE__         =    list(tokenizer_class.max_model_input_sizes.keys()						)
      for model_name in sa_models[:1]:
          SCREAMING_SNAKE_CASE__         =    tokenizer_class.from_pretrained(UpperCAmelCase_						)
          self.assertIsNotNone(UpperCAmelCase_						)
          self.assertIsInstance(UpperCAmelCase_  ,	UpperCAmelCase_						)
          self.assertIsInstance(UpperCAmelCase_  ,	UpperCAmelCase_						)

          for special_tok in tokenizer.all_special_tokens:
              self.assertIsInstance(UpperCAmelCase_  ,	UpperCAmelCase_						)
              SCREAMING_SNAKE_CASE__         =    tokenizer.convert_tokens_to_ids(UpperCAmelCase_						)
              self.assertIsInstance(UpperCAmelCase_  ,	UpperCAmelCase_						)



  def   _UpperCamelCase     (						self     :					int  ,	A_     :					Union[str, Any]  ,	A_     :					int = None						)       ->			Any:
      SCREAMING_SNAKE_CASE__         =    pickle.dumps(UpperCAmelCase_						)
      self.assertIsNotNone(UpperCAmelCase_						)

      SCREAMING_SNAKE_CASE__         =    pickle.loads(UpperCAmelCase_						)

      # Ensure is_fast is correctly restored
      self.assertEqual(be_restored.is_fast  ,	be_original.is_fast						)

      # Ensure encodings are potentially correctly restored
      if be_original.is_fast:
          self.assertIsNotNone(be_restored.encodings						)
      else:
          self.assertIsNone(be_restored.encodings						)

      # Ensure the keys are the same
      for original_v, restored_v in zip(be_original.values()  ,	be_restored.values()						):
          if equal_op:
              self.assertTrue(equal_op(UpperCAmelCase_  ,	UpperCAmelCase_						)						)
          else:
              self.assertEqual(UpperCAmelCase_  ,	UpperCAmelCase_						)



  @slow
  def   _UpperCamelCase     (						self     :					Union[str, Any]						)       ->			Optional[Any]:
      self.check_tokenizer_from_pretrained(UpperCAmelCase_						)



  def   _UpperCamelCase     (						self     :					List[Any]						)       ->			Dict:
      self.assertEqual(TensorType("tf"						)  ,	TensorType.TENSORFLOW						)
      self.assertEqual(TensorType("pt"						)  ,	TensorType.PYTORCH						)
      self.assertEqual(TensorType("np"						)  ,	TensorType.NUMPY						)



  @require_tokenizers
  def   _UpperCamelCase     (						self     :					Optional[Any]						)       ->			Dict:
      import numpy as np

      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)
      SCREAMING_SNAKE_CASE__         =    BertTokenizerFast.from_pretrained("bert-base-cased"						)

      # Python no tensor
      with self.subTest("BatchEncoding (Python, return_tensors=None)"						):
          self.assert_dump_and_restore(tokenizer_p("Small example to encode"						)						)

      with self.subTest("BatchEncoding (Python, return_tensors=NUMPY)"						):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"  ,	return_tensors=TensorType.NUMPY						)  ,	np.array_equal						)

      with self.subTest("BatchEncoding (Rust, return_tensors=None)"						):
          self.assert_dump_and_restore(tokenizer_r("Small example to encode"						)						)

      with self.subTest("BatchEncoding (Rust, return_tensors=NUMPY)"						):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"  ,	return_tensors=TensorType.NUMPY						)  ,	np.array_equal						)



  @require_tf
  @require_tokenizers
  def   _UpperCamelCase     (						self     :					str						)       ->			Tuple:
      import tensorflow as tf

      def tf_array_equals(A_     :					List[Any]  ,	A_     :					Optional[int]						):
          return tf.reduce_all(tf.equal(UpperCAmelCase_  ,	UpperCAmelCase_						)						)

      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)
      SCREAMING_SNAKE_CASE__         =    BertTokenizerFast.from_pretrained("bert-base-cased"						)

      with self.subTest("BatchEncoding (Python, return_tensors=TENSORFLOW)"						):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"  ,	return_tensors=TensorType.TENSORFLOW						)  ,	UpperCAmelCase_						)

      with self.subTest("BatchEncoding (Rust, return_tensors=TENSORFLOW)"						):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"  ,	return_tensors=TensorType.TENSORFLOW						)  ,	UpperCAmelCase_						)



  @require_torch
  @require_tokenizers
  def   _UpperCamelCase     (						self     :					List[str]						)       ->			Optional[Any]:
      import torch

      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)
      SCREAMING_SNAKE_CASE__         =    BertTokenizerFast.from_pretrained("bert-base-cased"						)

      with self.subTest("BatchEncoding (Python, return_tensors=PYTORCH)"						):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"  ,	return_tensors=TensorType.PYTORCH						)  ,	torch.equal						)

      with self.subTest("BatchEncoding (Rust, return_tensors=PYTORCH)"						):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"  ,	return_tensors=TensorType.PYTORCH						)  ,	torch.equal						)



  @require_tokenizers
  def   _UpperCamelCase     (						self     :					Any						)       ->			str:
      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)
      SCREAMING_SNAKE_CASE__         =    BertTokenizerFast.from_pretrained("bert-base-cased"						)

      with self.subTest("Python Tokenizer"						):
          self.assertFalse(tokenizer_p("Small example to_encode"						).is_fast						)

      with self.subTest("Rust Tokenizer"						):
          self.assertTrue(tokenizer_r("Small example to_encode"						).is_fast						)



  @require_tokenizers
  def   _UpperCamelCase     (						self     :					Dict						)       ->			List[str]:
      SCREAMING_SNAKE_CASE__         =    BertTokenizerFast.from_pretrained("bert-base-cased"						)
      SCREAMING_SNAKE_CASE__         =    tokenizer_r(["Test", "\xad", "test"]  ,	is_split_into_words=UpperCAmelCase_						)

      self.assertEqual(encoded.word_to_tokens(0						)  ,	TokenSpan(start=1  ,	end=2						)						)
      self.assertEqual(encoded.word_to_tokens(1						)  ,	UpperCAmelCase_						)
      self.assertEqual(encoded.word_to_tokens(2						)  ,	TokenSpan(start=2  ,	end=3						)						)



  def   _UpperCamelCase     (						self     :					Tuple						)       ->			List[Any]:
      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="np"						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(2, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(2,)						)
      # test converting the converted
      with CaptureStderr() as cs:
          SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="np"						)
      self.assertFalse(len(cs.err						)  ,	msg=f"should have no warning, but got {cs.err}"						)

      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="np"  ,	prepend_batch_axis=UpperCAmelCase_						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(1, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(1,)						)



  @require_torch
  def   _UpperCamelCase     (						self     :					Tuple						)       ->			List[Any]:
      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="pt"						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(2, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(2,)						)
      # test converting the converted
      with CaptureStderr() as cs:
          SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="pt"						)
      self.assertFalse(len(cs.err						)  ,	msg=f"should have no warning, but got {cs.err}"						)

      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="pt"  ,	prepend_batch_axis=UpperCAmelCase_						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(1, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(1,)						)



  @require_tf
  def   _UpperCamelCase     (						self     :					List[str]						)       ->			List[str]:
      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="tf"						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(2, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(2,)						)
      # test converting the converted
      with CaptureStderr() as cs:
          SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="tf"						)
      self.assertFalse(len(cs.err						)  ,	msg=f"should have no warning, but got {cs.err}"						)

      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="tf"  ,	prepend_batch_axis=UpperCAmelCase_						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(1, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(1,)						)



  @require_flax
  def   _UpperCamelCase     (						self     :					str						)       ->			Optional[Any]:
      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="jax"						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(2, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(2,)						)
      # test converting the converted
      with CaptureStderr() as cs:
          SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="jax"						)
      self.assertFalse(len(cs.err						)  ,	msg=f"should have no warning, but got {cs.err}"						)

      SCREAMING_SNAKE_CASE__         =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}						)
      SCREAMING_SNAKE_CASE__         =    batch.convert_to_tensors(tensor_type="jax"  ,	prepend_batch_axis=UpperCAmelCase_						)
      self.assertEqual(tensor_batch["inputs"].shape  ,	(1, 3)						)
      self.assertEqual(tensor_batch["labels"].shape  ,	(1,)						)



  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			Optional[Any]:
      SCREAMING_SNAKE_CASE__         =    [{"input_ids": np.array([0, 1, 2]						)}, {"input_ids": np.array([0, 1, 2, 3]						)}]
      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)

      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	np.ndarray						)						)
      self.assertEqual(batch["input_ids"].tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)
      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_  ,	return_tensors="np"						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	np.ndarray						)						)
      self.assertEqual(batch["input_ids"].tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)



  @require_torch
  def   _UpperCamelCase     (						self     :					Optional[Any]						)       ->			Tuple:
      import torch

      SCREAMING_SNAKE_CASE__         =    [{"input_ids": torch.tensor([0, 1, 2]						)}, {"input_ids": torch.tensor([0, 1, 2, 3]						)}]
      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)

      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	torch.Tensor						)						)
      self.assertEqual(batch["input_ids"].tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)
      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_  ,	return_tensors="pt"						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	torch.Tensor						)						)
      self.assertEqual(batch["input_ids"].tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)



  @require_tf
  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			int:
      import tensorflow as tf

      SCREAMING_SNAKE_CASE__         =    [{"input_ids": tf.constant([0, 1, 2]						)}, {"input_ids": tf.constant([0, 1, 2, 3]						)}]
      SCREAMING_SNAKE_CASE__         =    BertTokenizer.from_pretrained("bert-base-cased"						)

      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	tf.Tensor						)						)
      self.assertEqual(batch["input_ids"].numpy().tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)
      SCREAMING_SNAKE_CASE__         =    tokenizer.pad(UpperCAmelCase_  ,	padding=UpperCAmelCase_  ,	return_tensors="tf"						)
      self.assertTrue(isinstance(batch["input_ids"]  ,	tf.Tensor						)						)
      self.assertEqual(batch["input_ids"].numpy().tolist()  ,	[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]						)



  @require_tokenizers
  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			str:
      SCREAMING_SNAKE_CASE__         =    Tokenizer(WordPiece(unk_token="[UNK]"						)						)
      PreTrainedTokenizerFast(tokenizer_object=UpperCAmelCase_						)







  @require_tokenizers
  def   _UpperCamelCase     (						self     :					Any						)       ->			int:
      SCREAMING_SNAKE_CASE__         =    Tokenizer(WordPiece(unk_token="[UNK]"						)						)
      with tempfile.TemporaryDirectory() as tmpdirname:
          bert_tokenizer.save(os.path.join(UpperCAmelCase_  ,	"tokenizer.json"						)						)
          PreTrainedTokenizerFast(tokenizer_file=os.path.join(UpperCAmelCase_  ,	"tokenizer.json"						)						)








import os
import sys

from . import rsa_key_generator as rkg

UpperCAmelCase_    =128
UpperCAmelCase_    =256



def    UpperCAmelCase							(  _snake_case    ,			_snake_case = DEFAULT_BLOCK_SIZE				):
							lowerCAmelCase =		message.encode('''ascii'''				)

							lowerCAmelCase =		[]
							for block_start in range(0    ,			len(_snake_case				)    ,			_snake_case				):
														lowerCAmelCase =		0
														for i in range(_snake_case    ,			min(block_start + block_size    ,			len(_snake_case				)				)				):
																					block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
														block_ints.append(_snake_case				)
							return block_ints



def    UpperCAmelCase							(  _snake_case    ,			_snake_case    ,			_snake_case = DEFAULT_BLOCK_SIZE				):
							lowerCAmelCase =		[]
							for block_int in block_ints:
														lowerCAmelCase =		[]
														for i in range(block_size - 1    ,			-1    ,			-1				):
																					if len(_snake_case				) + i < message_length:
																												lowerCAmelCase =		block_int // (BYTE_SIZE**i)
																												lowerCAmelCase =		block_int % (BYTE_SIZE**i)
																												block_message.insert(0    ,			chr(_snake_case				)				)
														message.extend(_snake_case				)
							return "".join(_snake_case				)



def    UpperCAmelCase							(  _snake_case    ,			_snake_case    ,			_snake_case = DEFAULT_BLOCK_SIZE				):
							lowerCAmelCase =		[]
							lowerCAmelCase ,  lowerCAmelCase =		key

							for block in get_blocks_from_text(_snake_case    ,			_snake_case				):
														encrypted_blocks.append(pow(_snake_case    ,			_snake_case    ,			_snake_case				)				)
							return encrypted_blocks



def    UpperCAmelCase							(  _snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case = DEFAULT_BLOCK_SIZE    ,			):
							lowerCAmelCase =		[]
							lowerCAmelCase ,  lowerCAmelCase =		key
							for block in encrypted_blocks:
														decrypted_blocks.append(pow(_snake_case    ,			_snake_case    ,			_snake_case				)				)
							return get_text_from_blocks(_snake_case    ,			_snake_case    ,			_snake_case				)



def    UpperCAmelCase							(  _snake_case				):
							with open(_snake_case				) as fo:
														lowerCAmelCase =		fo.read()
							lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		content.split(''','''				)
							return (int(_snake_case				), int(_snake_case				), int(_snake_case				))



def    UpperCAmelCase							(  _snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case = DEFAULT_BLOCK_SIZE    ,			):
							lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		read_key_file(_snake_case				)
							if key_size < block_size * 8:
														sys.exit(
														    '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
														    '''requires the block size to be equal to or greater than the key size. '''
														    '''Either decrease the block size or use different keys.'''.format(
														        block_size * 8    ,			_snake_case				)				)

							lowerCAmelCase =		[str(_snake_case				) for i in encrypt_message(_snake_case    ,			(n, e)    ,			_snake_case				)]

							lowerCAmelCase =		''','''.join(_snake_case				)
							lowerCAmelCase =		F"""{len(_snake_case				)}_{block_size}_{encrypted_content}"""
							with open(_snake_case    ,			'''w'''				) as fo:
														fo.write(_snake_case				)
							return encrypted_content



def    UpperCAmelCase							(  _snake_case    ,			_snake_case				):
							lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		read_key_file(_snake_case				)
							with open(_snake_case				) as fo:
														lowerCAmelCase =		fo.read()
							lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		content.split('''_'''				)
							lowerCAmelCase =		int(_snake_case				)
							lowerCAmelCase =		int(_snake_case				)

							if key_size < block_size * 8:
														sys.exit(
														    '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
														    '''requires the block size to be equal to or greater than the key size. '''
														    '''Did you specify the correct key file and encrypted file?'''.format(
														        block_size * 8    ,			_snake_case				)				)

							lowerCAmelCase =		[]
							for block in encrypted_message.split(''','''				):
														encrypted_blocks.append(int(_snake_case				)				)

							return decrypt_message(_snake_case    ,			_snake_case    ,			(n, d)    ,			_snake_case				)



def    UpperCAmelCase							(  ):
							lowerCAmelCase =		'''encrypted_file.txt'''
							lowerCAmelCase =		input(R'''Encrypt\Decrypt [e\d]: '''				)

							if response.lower().startswith('''e'''				):
														lowerCAmelCase =		'''encrypt'''
							elif response.lower().startswith('''d'''				):
														lowerCAmelCase =		'''decrypt'''

							if mode == "encrypt":
														if not os.path.exists('''rsa_pubkey.txt'''				):
																					rkg.make_key_files('''rsa'''    ,			1024				)

														lowerCAmelCase =		input('''\nEnter message: '''				)
														lowerCAmelCase =		'''rsa_pubkey.txt'''
														print(F"""Encrypting and writing to {filename}..."""				)
														lowerCAmelCase =		encrypt_and_write_to_file(_snake_case    ,			_snake_case    ,			_snake_case				)

														print('''\nEncrypted text:'''				)
														print(_snake_case				)

							elif mode == "decrypt":
														lowerCAmelCase =		'''rsa_privkey.txt'''
														print(F"""Reading from {filename} and decrypting..."""				)
														lowerCAmelCase =		read_from_file_and_decrypt(_snake_case    ,			_snake_case				)
														print('''writing decryption to rsa_decryption.txt...'''				)
														with open('''rsa_decryption.txt'''    ,			'''w'''				) as dec:
																					dec.write(_snake_case				)

														print('''\nDecryption:'''				)
														print(_snake_case				)


if __name__ == "__main__":
		main()



