

"""simple docstring"""





import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

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


_a							=							logging.get_logger(__name__)

_a							=							"▁"

_a							=							{
    "vocab_file": "vocab.json",
    "spm_file": "sentencepiece.bpe.model",
    "tokenizer_config_file": "tokenizer_config.json",
}

_a							=							{
    "vocab_file": {
        "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json",
        "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json",
    },
    "spm_file": {
        "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model",
        "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model",
    },
    "tokenizer_config_file": {
        "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json",
        "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json",
    },
}

_a							=							{
    "facebook/m2m100_418M": 10_24,
}

# fmt: off
_a							=							{
    "m2m100": ["af", "am", "ar", "ast", "az", "ba", "be", "bg", "bn", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "ff", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "he", "hi", "hr", "ht", "hu", "hy", "id", "ig", "ilo", "is", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "lb", "lg", "ln", "lo", "lt", "lv", "mg", "mk", "ml", "mn", "mr", "ms", "my", "ne", "nl", "no", "ns", "oc", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sd", "si", "sk", "sl", "so", "sq", "sr", "ss", "su", "sv", "sw", "ta", "th", "tl", "tn", "tr", "uk", "ur", "uz", "vi", "wo", "xh", "yi", "yo", "zh", "zu"],
    "wmt21": ["en", "ha", "is", "ja", "cs", "ru", "zh", "de"]
}




class  _lowerCAmelCase  (     _SCREAMING_SNAKE_CASE       ):







 """simple docstring"""







 __UpperCAmelCase     : Tuple       		=     VOCAB_FILES_NAMES
 __UpperCAmelCase     : Optional[Any]       		=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __UpperCAmelCase     : Any       		=     PRETRAINED_VOCAB_FILES_MAP
 __UpperCAmelCase     : Tuple       		=     ["input_ids", "attention_mask"]

 __UpperCAmelCase     : int       		=     []
 __UpperCAmelCase     : int       		=     []


 def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      Dict, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      List[str]=None, UpperCAmelCase__							:      Dict=None, UpperCAmelCase__							:      Any="<s>", UpperCAmelCase__							:      Optional[int]="</s>", UpperCAmelCase__							:      Dict="</s>", UpperCAmelCase__							:      Optional[Any]="<pad>", UpperCAmelCase__							:      Tuple="<unk>", UpperCAmelCase__							:      int="m2m100", UpperCAmelCase__							:      Optional[Dict[str, Any]] = None, UpperCAmelCase__							:      Tuple=8, **UpperCAmelCase__							:      Any, ):
    __lowercase								=						{} if sp_model_kwargs is None else sp_model_kwargs

    __lowercase								=						language_codes
    __lowercase								=						FAIRSEQ_LANGUAGE_CODES[language_codes]
    __lowercase								=						{lang_code: F"""__{lang_code}__""" for lang_code in fairseq_language_code}

    __lowercase								=						kwargs.get("additional_special_tokens", []     )
    kwargs["additional_special_tokens"] += [
        self.get_lang_token(UpperCAmelCase__     )
        for lang_code in fairseq_language_code
        if self.get_lang_token(UpperCAmelCase__     ) not in kwargs["additional_special_tokens"]
    ]

    super().__init__(
        src_lang=UpperCAmelCase__, tgt_lang=UpperCAmelCase__, bos_token=UpperCAmelCase__, eos_token=UpperCAmelCase__, sep_token=UpperCAmelCase__, unk_token=UpperCAmelCase__, pad_token=UpperCAmelCase__, language_codes=UpperCAmelCase__, sp_model_kwargs=self.sp_model_kwargs, num_madeup_words=UpperCAmelCase__, **UpperCAmelCase__, )

    __lowercase								=						vocab_file
    __lowercase								=						load_json(UpperCAmelCase__     )
    __lowercase								=						{v: k for k, v in self.encoder.items()}
    __lowercase								=						spm_file
    __lowercase								=						load_spm(UpperCAmelCase__, self.sp_model_kwargs     )

    __lowercase								=						len(self.encoder     )

    __lowercase								=						{
        self.get_lang_token(UpperCAmelCase__     ): self.encoder_size + i for i, lang_code in enumerate(UpperCAmelCase__     )
    }
    __lowercase								=						{lang_code: self.encoder_size + i for i, lang_code in enumerate(UpperCAmelCase__     )}
    __lowercase								=						{v: k for k, v in self.lang_token_to_id.items()}

    __lowercase								=						src_lang if src_lang is not None else """en"""
    __lowercase								=						tgt_lang
    __lowercase								=						self.get_lang_id(self._src_lang     )
    self.set_src_lang_special_tokens(self._src_lang     )

    __lowercase								=						num_madeup_words


 @property
 def  _lowercase			(      self							:      Dict     ):
    return len(self.encoder     ) + len(self.lang_token_to_id     )


 @property
 def  _lowercase			(      self							:      Any     ):
    return self._src_lang


 @src_lang.setter
 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      str     ):
    __lowercase								=						new_src_lang
    self.set_src_lang_special_tokens(self._src_lang     )


 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      str     ):
    return self.sp_model.encode(UpperCAmelCase__, out_type=UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      Union[str, Any]     ):
    if token in self.lang_token_to_id:
       return self.lang_token_to_id[token]
    return self.encoder.get(UpperCAmelCase__, self.encoder[self.unk_token]     )


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      int     ):
    if index in self.id_to_lang_token:
       return self.id_to_lang_token[index]
    return self.decoder.get(UpperCAmelCase__, self.unk_token     )


 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      int     ):
    __lowercase								=						[]
    __lowercase								=						""""""
    for token in tokens:
       # make sure that special tokens are not decoded using sentencepiece model
       if token in self.all_special_tokens:
          out_string += self.sp_model.decode(UpperCAmelCase__     ) + token
          __lowercase								=						[]
       else:
          current_sub_tokens.append(UpperCAmelCase__     )
    out_string += self.sp_model.decode(UpperCAmelCase__     )
    return out_string.strip()


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      List[int], UpperCAmelCase__							:      Optional[List[int]] = None, UpperCAmelCase__							:      bool = False     ):

    if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=UpperCAmelCase__, token_ids_a=UpperCAmelCase__, already_has_special_tokens=UpperCAmelCase__     )

    __lowercase								=						[1] * len(self.prefix_tokens     )
    __lowercase								=						[1] * len(self.suffix_tokens     )
    if token_ids_a is None:
       return prefix_ones + ([0] * len(UpperCAmelCase__     )) + suffix_ones
    return prefix_ones + ([0] * len(UpperCAmelCase__     )) + ([0] * len(UpperCAmelCase__     )) + suffix_ones


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      List[int], UpperCAmelCase__							:      Optional[List[int]] = None     ):
    if token_ids_a is None:
       return self.prefix_tokens + token_ids_a + self.suffix_tokens
    # We don't expect to process pairs, but leave the pair logic for API consistency
    return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens


 def  _lowercase			(      self							:      List[Any]     ):
    __lowercase								=						{self.convert_ids_to_tokens(UpperCAmelCase__     ): i for i in range(self.vocab_size     )}
    vocab.update(self.added_tokens_encoder     )
    return vocab


 def __getstate__(      self							:      Optional[int]     ):
    __lowercase								=						self.__dict__.copy()
    __lowercase								=						None
    return state


 def __setstate__(      self							:      int, UpperCAmelCase__							:      Dict     ):
    __lowercase								=						d

    # for backward compatibility
    if not hasattr(self, "sp_model_kwargs"     ):
       __lowercase								=						{}

    __lowercase								=						load_spm(self.spm_file, self.sp_model_kwargs     )


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Optional[str] = None     ):
    __lowercase								=						Path(UpperCAmelCase__     )
    if not save_dir.is_dir():
       raise OSError(F"""{save_directory} should be a directory"""     )
    __lowercase								=						save_dir / (
        (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""]
    )
    __lowercase								=						save_dir / (
        (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""]
    )

    save_json(self.encoder, UpperCAmelCase__     )

    if os.path.abspath(self.spm_file     ) != os.path.abspath(UpperCAmelCase__     ) and os.path.isfile(self.spm_file     ):
       copyfile(self.spm_file, UpperCAmelCase__     )
    elif not os.path.isfile(self.spm_file     ):
       with open(UpperCAmelCase__, "wb"     ) as fi:
          __lowercase								=						self.sp_model.serialized_model_proto()
          fi.write(UpperCAmelCase__     )

    return (str(UpperCAmelCase__     ), str(UpperCAmelCase__     ))


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      str = "en", UpperCAmelCase__							:      Optional[List[str]] = None, UpperCAmelCase__							:      str = "ro", **UpperCAmelCase__							:      List[Any], ):
    __lowercase								=						src_lang
    __lowercase								=						tgt_lang
    self.set_src_lang_special_tokens(self.src_lang     )
    return super().prepare_seqaseq_batch(UpperCAmelCase__, UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      Optional[str], UpperCAmelCase__							:      Optional[str], **UpperCAmelCase__							:      int     ):
    if src_lang is None or tgt_lang is None:
       raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model"     )
    __lowercase								=						src_lang
    __lowercase								=						self(UpperCAmelCase__, add_special_tokens=UpperCAmelCase__, **UpperCAmelCase__     )
    __lowercase								=						self.get_lang_id(UpperCAmelCase__     )
    __lowercase								=						tgt_lang_id
    return inputs


 def  _lowercase			(      self							:      Tuple     ):
    self.set_src_lang_special_tokens(self.src_lang     )


 def  _lowercase			(      self							:      Optional[Any]     ):
    self.set_tgt_lang_special_tokens(self.tgt_lang     )


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      str     ):
    __lowercase								=						self.get_lang_token(UpperCAmelCase__     )
    __lowercase								=						self.lang_token_to_id[lang_token]
    __lowercase								=						[self.cur_lang_id]
    __lowercase								=						[self.eos_token_id]


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      str     ):
    __lowercase								=						self.get_lang_token(UpperCAmelCase__     )
    __lowercase								=						self.lang_token_to_id[lang_token]
    __lowercase								=						[self.cur_lang_id]
    __lowercase								=						[self.eos_token_id]


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      str     ):
    return self.lang_code_to_token[lang]



 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      str     ):
    __lowercase								=						self.get_lang_token(UpperCAmelCase__     )
    return self.lang_token_to_id[lang_token]







def   _A  (  UpperCamelCase_					:	str,      UpperCamelCase_					:	Dict[str, Any])     ->						Any:
   '''simple docstring'''


   __lowercase								=						sentencepiece.SentencePieceProcessor(**A__)
   spm.Load(str(A__))
   return spm




def   _A  (  UpperCamelCase_					:	str)     ->						List[str]:
   '''simple docstring'''


   with open(A__,      "r") as f:
      return json.load(A__)




def   _A  (  UpperCamelCase_					:	Any,      UpperCamelCase_					:	str)     ->						Dict:
   '''simple docstring'''


   with open(A__,      "w") as f:
      json.dump(A__,      A__,      indent=2)


"""simple docstring"""





import os
import random
import sys

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




def   _A  (  )     ->						None:
   '''simple docstring'''


   print("Making key files...")
   make_key_files("rsa",      1024)
   print("Key files generation successful.")




def   _A  (  UpperCamelCase_					:	int)     ->						tuple[tuple[int, int], tuple[int, int]]:
   '''simple docstring'''


   print("Generating prime p...")
   __lowercase								=						rabinMiller.generate_large_prime(UpperCamelCase_)
   print("Generating prime q...")
   __lowercase								=						rabinMiller.generate_large_prime(UpperCamelCase_)
   __lowercase								=						p * q

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

   print("Calculating d that is mod inverse of e...")
   __lowercase								=						cryptoMath.find_mod_inverse(UpperCamelCase_,      (p - 1) * (q - 1))

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




def   _A  (  UpperCamelCase_					:	str,      UpperCamelCase_					:	int)     ->						None:
   '''simple docstring'''


   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()

   __lowercase		,__lowercase								=						generate_key(UpperCamelCase_)
   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()




