


import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
						from transformers.pipelines.conversational import Conversation

snake_case							:       int     			=      logging.get_logger(__name__)


snake_case							:       Optional[int]     			=      {
    '''vocab_file''': '''vocab.json''',
    '''merges_file''': '''merges.txt''',
    '''tokenizer_config_file''': '''tokenizer_config.json''',
}

snake_case							:       str     			=      {
    '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''},
    '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''},
    '''tokenizer_config_file''': {
        '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json'''
    },
}

snake_case							:       List[str]     			=      {'''facebook/blenderbot-3B''': 1_28}






@lru_cache()
# Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode
def 							__lowercase   ( ):
							a__             =     (
							    list(range(ord('!'   )					,    ord('~'   ) + 1   )   ) + list(range(ord('¡'   )					,    ord('¬'   ) + 1   )   ) + list(range(ord('®'   )					,    ord('ÿ'   ) + 1   )   )
							)
							a__             =     bs[:]
							a__             =     0
							for b in range(2**8   ):
														if b not in bs:
																					bs.append(__lowerCAmelCase   )
																					cs.append(2**8 + n   )
																					n += 1
							a__             =     [chr(__lowerCAmelCase   ) for n in cs]
							return dict(zip(__lowerCAmelCase					,    __lowerCAmelCase   )   )






def 							__lowercase   ( __lowerCAmelCase							:				str   ):
							a__             =     set()
							a__             =     word[0]
							for char in word[1:]:
														pairs.add((prev_char, char)   )
														a__             =     char
							return pairs







class        snake_case_   (lowerCamelCase_      ):
					UpperCAmelCase__					:    Tuple          =			VOCAB_FILES_NAMES
					UpperCAmelCase__					:    int          =			PRETRAINED_VOCAB_FILES_MAP
					UpperCAmelCase__					:    Union[str, Any]          =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					UpperCAmelCase__					:    int          =			['''input_ids''', '''attention_mask''']
					def __init__(      self   :Optional[Any]			,__snake_case   :List[Any]			,__snake_case   :List[Any]			,__snake_case   :Optional[Any]="replace"			,__snake_case   :Tuple="<s>"			,__snake_case   :List[str]="</s>"			,__snake_case   :List[Any]="</s>"			,__snake_case   :Optional[int]="<s>"			,__snake_case   :str="<unk>"			,__snake_case   :Tuple="<pad>"			,__snake_case   :str="<mask>"			,__snake_case   :List[str]=False			,**__snake_case   :Tuple			,)					->    str:
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else bos_token
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else eos_token
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else sep_token
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else cls_token
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else unk_token
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else pad_token

												# Mask token behave like a normal word, i.e. include the space before it
												a__             =     AddedToken(__snake_case			,lstrip=__snake_case			,rstrip=__snake_case      ) if isinstance(__snake_case			,__snake_case      ) else mask_token

												super().__init__(
												    errors=__snake_case			,bos_token=__snake_case			,eos_token=__snake_case			,unk_token=__snake_case			,sep_token=__snake_case			,cls_token=__snake_case			,pad_token=__snake_case			,mask_token=__snake_case			,add_prefix_space=__snake_case			,**__snake_case			,)

												with open(__snake_case			,encoding='utf-8'      ) as vocab_handle:
																			a__             =     json.load(__snake_case      )
												a__             =     {v: k for k, v in self.encoder.items()}
												a__             =     errors  # how to handle errors in decoding
												a__             =     bytes_to_unicode()
												a__             =     {v: k for k, v in self.byte_encoder.items()}
												with open(__snake_case			,encoding='utf-8'      ) as merges_handle:
																			a__             =     merges_handle.read().split('\n'      )[1:-1]
												a__             =     [tuple(merge.split()      ) for merge in bpe_merges]
												a__             =     dict(zip(__snake_case			,range(len(__snake_case      )      )      )      )
												a__             =     {}
												a__             =     add_prefix_space

												# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
												a__             =     re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'      )
					@property
					# Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot
					def  lowerCamelCase__(      self   :Optional[Any]      )					->    Optional[Any]:
												return len(self.encoder      )
					def  lowerCamelCase__(      self   :Any      )					->    int:
												return dict(self.encoder			,**self.added_tokens_encoder      )
					def  lowerCamelCase__(      self   :Dict			,__snake_case   :Optional[Any]      )					->    Optional[int]:
												if token in self.cache:
																			return self.cache[token]
												a__             =     tuple(__snake_case      )
												a__             =     get_pairs(__snake_case      )

												if not pairs:
																			return token

												while True:
																			a__             =     min(__snake_case			,key=lambda __snake_case      : self.bpe_ranks.get(__snake_case			,float('inf'      )      )      )
																			if bigram not in self.bpe_ranks:
																										break
																			a__      ,					a__             =     bigram
																			a__             =     []
																			a__             =     0
																			while i < len(__snake_case      ):
																										try:
																																	a__             =     word.index(__snake_case			,__snake_case      )
																										except ValueError:
																																	new_word.extend(word[i:]      )
																																	break
																										else:
																																	new_word.extend(word[i:j]      )
																																	a__             =     j

																										if word[i] == first and i < len(__snake_case      ) - 1 and word[i + 1] == second:
																																	new_word.append(first + second      )
																																	i += 2
																										else:
																																	new_word.append(word[i]      )
																																	i += 1
																			a__             =     tuple(__snake_case      )
																			a__             =     new_word
																			if len(__snake_case      ) == 1:
																										break
																			else:
																										a__             =     get_pairs(__snake_case      )
												a__             =     ' '.join(__snake_case      )
												a__             =     word
												return word
					def  lowerCamelCase__(      self   :Any			,__snake_case   :Any      )					->    Tuple:
												a__             =     []
												for token in re.findall(self.pat			,__snake_case      ):
																			a__             =     ''.join(
																			    self.byte_encoder[b] for b in token.encode('utf-8'      )      )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
																			bpe_tokens.extend(bpe_token for bpe_token in self.bpe(__snake_case      ).split(' '      )      )
												return bpe_tokens
					def  lowerCamelCase__(      self   :Optional[Any]			,__snake_case   :List[str]      )					->    Any:
												return self.encoder.get(__snake_case			,self.encoder.get(self.unk_token      )      )
					def  lowerCamelCase__(      self   :List[str]			,__snake_case   :Optional[int]      )					->    Optional[int]:
												return self.decoder.get(__snake_case      )
					def  lowerCamelCase__(      self   :List[str]			,__snake_case   :Dict      )					->    List[Any]:
												a__             =     ''.join(__snake_case      )
												a__             =     bytearray([self.byte_decoder[c] for c in text]      ).decode('utf-8'			,errors=self.errors      )
												return text
					def  lowerCamelCase__(      self   :int			,__snake_case   :str			,__snake_case   :Optional[str] = None      )					->    Tuple[str]:
												if not os.path.isdir(__snake_case      ):
																			logger.error(F'Vocabulary path ({save_directory}) should be a directory'      )
																			return
												a__             =     os.path.join(
												    __snake_case			,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']      )
												a__             =     os.path.join(
												    __snake_case			,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']      )

												with open(__snake_case			,'w'			,encoding='utf-8'      ) as f:
																			f.write(json.dumps(self.encoder			,indent=2			,sort_keys=__snake_case			,ensure_ascii=__snake_case      ) + '\n'      )

												a__             =     0
												with open(__snake_case			,'w'			,encoding='utf-8'      ) as writer:
																			writer.write('#version: 0.2\n'      )
																			for bpe_tokens, token_index in sorted(self.bpe_ranks.items()			,key=lambda __snake_case      : kv[1]      ):
																										if index != token_index:
																																	logger.warning(
																																	    F'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'
																																	    ' Please check that the tokenizer is not corrupted!'      )
																																	a__             =     token_index
																										writer.write(' '.join(__snake_case      ) + '\n'      )
																										index += 1

												return vocab_file, merge_file
					def  lowerCamelCase__(      self   :Optional[int]			,__snake_case   :List[int]			,__snake_case   :Optional[List[int]] = None			,__snake_case   :bool = False      )					->    List[int]:
												if already_has_special_tokens:
																			return super().get_special_tokens_mask(
																			    token_ids_a=__snake_case			,token_ids_a=__snake_case			,already_has_special_tokens=__snake_case      )

												if token_ids_a is None:
																			return [1] + ([0] * len(__snake_case      )) + [1]
												return [1] + ([0] * len(__snake_case      )) + [1, 1] + ([0] * len(__snake_case      )) + [1]
					def  lowerCamelCase__(      self   :Any			,__snake_case   :List[int]			,__snake_case   :Optional[List[int]] = None      )					->    List[int]:
												a__             =     [self.sep_token_id]
												a__             =     [self.cls_token_id]

												if token_ids_a is None:
																			return len(cls + token_ids_a + sep      ) * [0]
												return len(cls + token_ids_a + sep + sep + token_ids_a + sep      ) * [0]
					def  lowerCamelCase__(      self   :str			,__snake_case   :List[Any]			,__snake_case   :Dict=False			,**__snake_case   :Optional[int]      )					->    List[str]:
												a__             =     kwargs.pop('add_prefix_space'			,self.add_prefix_space      )
												if (is_split_into_words or add_prefix_space) and (len(__snake_case      ) > 0 and not text[0].isspace()):
																			a__             =     ' ' + text
												return (text, kwargs)
					def  lowerCamelCase__(      self   :str			,__snake_case   :List[int]			,__snake_case   :Optional[List[int]] = None      )					->    str:
												return token_ids_a + [self.eos_token_id]



					def  lowerCamelCase__(      self   :Dict			,__snake_case   :"Conversation"      )					->    List[int]:
												a__             =     []
												for is_user, text in conversation.iter_texts():
																			if is_user:
																										# We need to space prefix as it's being done within blenderbot
																										inputs.append(' ' + text      )
																			else:
																										# Generated responses should contain them already.
																										inputs.append(__snake_case      )

												a__             =     '  '.join(__snake_case      )
												a__             =     self.encode(__snake_case      )
												if len(__snake_case      ) > self.model_max_length:
																			a__             =     input_ids[-self.model_max_length :]
																			logger.warning(F'Trimmed input from conversation as it was longer than {self.model_max_length} tokens.'      )
												return input_ids








import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812






def 							__lowercase   ( ):
							print('Making key files...'   )
							make_key_files('rsa'					,    1_0_2_4   )
							print('Key files generation successful.'   )






def 							__lowercase   ( __lowerCAmelCase							:				int   ):
							print('Generating prime p...'   )
							a__             =     rabinMiller.generate_large_prime(__lowerCAmelCase   )
							print('Generating prime q...'   )
							a__             =     rabinMiller.generate_large_prime(__lowerCAmelCase   )
							a__             =     p * q

							print('Generating e that is relatively prime to (p - 1) * (q - 1)...'   )
							while True:
														a__             =     random.randrange(2 ** (key_size - 1)					,    2 ** (key_size)   )
														if cryptoMath.gcd(__lowerCAmelCase					,    (p - 1) * (q - 1)   ) == 1:
																					break

							print('Calculating d that is mod inverse of e...'   )
							a__             =     cryptoMath.find_mod_inverse(__lowerCAmelCase					,    (p - 1) * (q - 1)   )

							a__             =     (n, e)
							a__             =     (n, d)
							return (public_key, private_key)






def 							__lowercase   ( __lowerCAmelCase							:				str					,    __lowerCAmelCase							:				int   ):
							if os.path.exists(F'{name}_pubkey.txt'   ) or os.path.exists(F'{name}_privkey.txt'   ):
														print('\nWARNING:'   )
														print(
														    F'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n'
														    'Use a different name or delete these files and re-run this program.'   )
														sys.exit()

							a__      ,					a__             =     generate_key(__lowerCAmelCase   )
							print(F'\nWriting public key to file {name}_pubkey.txt...'   )
							with open(F'{name}_pubkey.txt'					,    'w'   ) as out_file:
														out_file.write(F'{key_size},{public_key[0]},{public_key[1]}'   )

							print(F'Writing private key to file {name}_privkey.txt...'   )
							with open(F'{name}_privkey.txt'					,    'w'   ) as out_file:
														out_file.write(F'{key_size},{private_key[0]},{private_key[1]}'   )


if __name__ == "__main__":
						main()




