



from __future__ import annotations

from string import ascii_letters



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		List[Any]			,    SCREAMING_SNAKE_CASE__    :		List[Any]			,    SCREAMING_SNAKE_CASE__    :		Dict = None			)					->       str:
 _snake_case      :					str					=   alphabet or ascii_letters

 # The final result string
 _snake_case      :					Dict					=   ''''''

 for character in input_string:
  if character not in alpha:
   # Append without encryption if character is not in the alphabet
   result += character
  else:
   # Get the index of the new key and make sure it isn't too large
   _snake_case      :					Dict					=   (alpha.index(SCREAMING_SNAKE_CASE__			) + key) % len(SCREAMING_SNAKE_CASE__			)

   # Append the encoded character to the alphabet
   result += alpha[new_key]

 return result



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		int			,    SCREAMING_SNAKE_CASE__    :		Union[str, Any]			,    SCREAMING_SNAKE_CASE__    :		List[str] = None			)					->       Dict:
 key *= -1

 return encrypt(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			)



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		str			,    SCREAMING_SNAKE_CASE__    :		Tuple = None			)					->       Tuple:
 _snake_case      :					Optional[int]					=   alphabet or ascii_letters

 # To store data on all the combinations
 _snake_case      :					Tuple					=   {}

 # Cycle through each combination
 for key in range(1			,    len(SCREAMING_SNAKE_CASE__			) + 1			):
  # Decrypt the message and store the result in the data
  _snake_case      :					str					=   decrypt(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			)

 return brute_force_data


if __name__ == "__main__":
     while True:
          print(F'''\n{'-' * 10}\n Menu\n{'-' * 10}''')
          print(*["""1.Encrypt""", """2.Decrypt""", """3.BruteForce""", """4.Quit"""], sep="""\n""")

          # get user input
          a__      =		input("""\nWhat would you like to do?: """).strip() or """4"""

          # run functions based on what the user chose
          if choice not in ("1", "2", "3", "4"):
               print("""Invalid choice, please enter a valid choice""")
          elif choice == "1":
               a__      =		input("""Please enter the string to be encrypted: """)
               a__      =		int(input("""Please enter off-set: """).strip())

               print(encrypt(input_string, key))
          elif choice == "2":
               a__      =		input("""Please enter the string to be decrypted: """)
               a__      =		int(input("""Please enter off-set: """).strip())

               print(decrypt(input_string, key))
          elif choice == "3":
               a__      =		input("""Please enter the string to be decrypted: """)
               a__      =		brute_force(input_string)

               for key, value in brute_force_data.items():
                    print(F'''Key: {key} | Message: {value}''')

          elif choice == "4":
               print("""Goodbye.""")
               break





import argparse
import json
from pathlib import Path

import requests
import torch
from datasets import load_dataset
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    BeitConfig,
    BeitForImageClassification,
    BeitForMaskedImageModeling,
    BeitForSemanticSegmentation,
    BeitImageProcessor,
)
from transformers.image_utils import PILImageResampling
from transformers.utils import logging


logging.set_verbosity_info()
a__      =		logging.get_logger(__name__)



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		Optional[int]			,    SCREAMING_SNAKE_CASE__    :		Dict=False			,    SCREAMING_SNAKE_CASE__    :		Optional[Any]=False			)					->       Any:
 _snake_case      :					int					=   """backbone.""" if is_semantic else """"""

 _snake_case      :					Optional[Any]					=   []
 for i in range(config.num_hidden_layers			):
  # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
  rename_keys.append((F'''{prefix}blocks.{i}.norm1.weight''', F'''beit.encoder.layer.{i}.layernorm_before.weight''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.norm1.bias''', F'''beit.encoder.layer.{i}.layernorm_before.bias''')			)
  rename_keys.append(
      (F'''{prefix}blocks.{i}.attn.proj.weight''', F'''beit.encoder.layer.{i}.attention.output.dense.weight''')			)
  rename_keys.append(
      (F'''{prefix}blocks.{i}.attn.proj.bias''', F'''beit.encoder.layer.{i}.attention.output.dense.bias''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.norm2.weight''', F'''beit.encoder.layer.{i}.layernorm_after.weight''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.norm2.bias''', F'''beit.encoder.layer.{i}.layernorm_after.bias''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.weight''', F'''beit.encoder.layer.{i}.intermediate.dense.weight''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.bias''', F'''beit.encoder.layer.{i}.intermediate.dense.bias''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.weight''', F'''beit.encoder.layer.{i}.output.dense.weight''')			)
  rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.bias''', F'''beit.encoder.layer.{i}.output.dense.bias''')			)

 # projection layer + position embeddings
 rename_keys.extend(
     [
         (F'''{prefix}cls_token''', """beit.embeddings.cls_token"""),
         (F'''{prefix}patch_embed.proj.weight''', """beit.embeddings.patch_embeddings.projection.weight"""),
         (F'''{prefix}patch_embed.proj.bias''', """beit.embeddings.patch_embeddings.projection.bias"""),
     ]			)

 if has_lm_head:
  # mask token + shared relative position bias + layernorm
  rename_keys.extend(
      [
          ("""mask_token""", """beit.embeddings.mask_token"""),
          (
              """rel_pos_bias.relative_position_bias_table""",
              """beit.encoder.relative_position_bias.relative_position_bias_table""",
          ),
          (
              """rel_pos_bias.relative_position_index""",
              """beit.encoder.relative_position_bias.relative_position_index""",
          ),
          ("""norm.weight""", """layernorm.weight"""),
          ("""norm.bias""", """layernorm.bias"""),
      ]			)
 elif is_semantic:
  # semantic segmentation classification heads
  rename_keys.extend(
      [
          ("""decode_head.conv_seg.weight""", """decode_head.classifier.weight"""),
          ("""decode_head.conv_seg.bias""", """decode_head.classifier.bias"""),
          ("""auxiliary_head.conv_seg.weight""", """auxiliary_head.classifier.weight"""),
          ("""auxiliary_head.conv_seg.bias""", """auxiliary_head.classifier.bias"""),
      ]			)
 else:
  # layernorm + classification head
  rename_keys.extend(
      [
          ("""fc_norm.weight""", """beit.pooler.layernorm.weight"""),
          ("""fc_norm.bias""", """beit.pooler.layernorm.bias"""),
          ("""head.weight""", """classifier.weight"""),
          ("""head.bias""", """classifier.bias"""),
      ]			)

 return rename_keys



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		Tuple			,    SCREAMING_SNAKE_CASE__    :		Union[str, Any]			,    SCREAMING_SNAKE_CASE__    :		List[Any]=False			,    SCREAMING_SNAKE_CASE__    :		List[str]=False			)					->       List[Any]:
 for i in range(config.num_hidden_layers			):
  _snake_case      :					int					=   """backbone.""" if is_semantic else """"""
  # queries, keys and values
  _snake_case      :					List[Any]					=   state_dict.pop(F'''{prefix}blocks.{i}.attn.qkv.weight'''			)
  _snake_case      :					List[str]					=   state_dict.pop(F'''{prefix}blocks.{i}.attn.q_bias'''			)
  _snake_case      :					Any					=   state_dict.pop(F'''{prefix}blocks.{i}.attn.v_bias'''			)

  _snake_case      :					Optional[Any]					=   in_proj_weight[
      : config.hidden_size, :
  ]
  _snake_case      :					Union[str, Any]					=   q_bias
  _snake_case      :					Dict					=   in_proj_weight[
      config.hidden_size : config.hidden_size * 2, :
  ]
  _snake_case      :					str					=   in_proj_weight[
      -config.hidden_size :, :
  ]
  _snake_case      :					List[Any]					=   v_bias

  # gamma_1 and gamma_2
  # we call them lambda because otherwise they are renamed when using .from_pretrained
  _snake_case      :					Tuple					=   state_dict.pop(F'''{prefix}blocks.{i}.gamma_1'''			)
  _snake_case      :					List[str]					=   state_dict.pop(F'''{prefix}blocks.{i}.gamma_2'''			)

  _snake_case      :					Dict					=   gamma_a
  _snake_case      :					Tuple					=   gamma_a

  # relative_position bias table + index
  if not has_lm_head:
   # each layer has its own relative position bias
   _snake_case      :					Tuple					=   state_dict.pop(F'''{prefix}blocks.{i}.attn.relative_position_bias_table'''			)
   _snake_case      :					Any					=   state_dict.pop(F'''{prefix}blocks.{i}.attn.relative_position_index'''			)

   _snake_case      :					Optional[int]					=   table
   _snake_case      :					Dict					=   index



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		List[str]			,    SCREAMING_SNAKE_CASE__    :		List[str]			,    SCREAMING_SNAKE_CASE__    :		Optional[Any]			)					->       Optional[int]:
 _snake_case      :					str					=   dct.pop(SCREAMING_SNAKE_CASE__			)
 _snake_case      :					Tuple					=   val



def  lowercase    (				)					->       Any:
 _snake_case      :					List[Any]					=   """http://images.cocodataset.org/val2017/000000039769.jpg"""
 _snake_case      :					List[str]					=   Image.open(requests.get(SCREAMING_SNAKE_CASE__			,    stream=SCREAMING_SNAKE_CASE__			).raw			)
 return im



@torch.no_grad()
def  lowercase    (				SCREAMING_SNAKE_CASE__    :		Union[str, Any]			,    SCREAMING_SNAKE_CASE__    :		str			)					->       Dict:
 _snake_case      :					Any					=   BeitConfig()
 _snake_case      :					Union[str, Any]					=   False
 _snake_case      :					str					=   False
 _snake_case      :					Tuple					=   """huggingface/label-files"""
 # set config parameters based on URL
 if checkpoint_url[-9:-4] == "pt22k":
  # masked image modeling
  _snake_case      :					Optional[int]					=   True
  _snake_case      :					Union[str, Any]					=   True
  _snake_case      :					Dict					=   True
 elif checkpoint_url[-9:-4] == "ft22k":
  # intermediate fine-tuning on ImageNet-22k
  _snake_case      :					List[str]					=   True
  _snake_case      :					List[Any]					=   21_841
  _snake_case      :					Optional[Any]					=   """imagenet-22k-id2label.json"""
  _snake_case      :					Optional[int]					=   json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    repo_type="""dataset"""			)			,    """r"""			)			)
  _snake_case      :					str					=   {int(SCREAMING_SNAKE_CASE__			): v for k, v in idalabel.items()}
  # this dataset contains 21843 labels but the model only has 21841
  # we delete the classes as mentioned in https://github.com/google-research/big_transfer/issues/18
  del idalabel[9_205]
  del idalabel[15_027]
  _snake_case      :					str					=   idalabel
  _snake_case      :					int					=   {v: k for k, v in idalabel.items()}
 elif checkpoint_url[-8:-4] == "to1k":
  # fine-tuning on ImageNet-1k
  _snake_case      :					Any					=   True
  _snake_case      :					List[Any]					=   1_000
  _snake_case      :					int					=   """imagenet-1k-id2label.json"""
  _snake_case      :					Dict					=   json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    repo_type="""dataset"""			)			,    """r"""			)			)
  _snake_case      :					List[str]					=   {int(SCREAMING_SNAKE_CASE__			): v for k, v in idalabel.items()}
  _snake_case      :					Any					=   idalabel
  _snake_case      :					Dict					=   {v: k for k, v in idalabel.items()}
  if "384" in checkpoint_url:
   _snake_case      :					Any					=   384
  if "512" in checkpoint_url:
   _snake_case      :					List[str]					=   512
 elif "ade20k" in checkpoint_url:
  # fine-tuning
  _snake_case      :					Optional[Any]					=   True
  _snake_case      :					List[str]					=   150
  _snake_case      :					Any					=   """ade20k-id2label.json"""
  _snake_case      :					int					=   json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    repo_type="""dataset"""			)			,    """r"""			)			)
  _snake_case      :					Optional[Any]					=   {int(SCREAMING_SNAKE_CASE__			): v for k, v in idalabel.items()}
  _snake_case      :					Any					=   idalabel
  _snake_case      :					Optional[Any]					=   {v: k for k, v in idalabel.items()}
  _snake_case      :					int					=   640
  _snake_case      :					List[Any]					=   True
 else:
  raise ValueError("""Checkpoint not supported, URL should either end with 'pt22k', 'ft22k', 'to1k' or 'ade20k'"""			)

 # size of the architecture
 if "base" in checkpoint_url:
  pass
 elif "large" in checkpoint_url:
  _snake_case      :					List[Any]					=   1_024
  _snake_case      :					int					=   4_096
  _snake_case      :					Union[str, Any]					=   24
  _snake_case      :					Optional[int]					=   16
  if "ade20k" in checkpoint_url:
   _snake_case      :					Any					=   640
   _snake_case      :					Tuple					=   [7, 11, 15, 23]
 else:
  raise ValueError("""Should either find 'base' or 'large' in checkpoint URL"""			)

 # load state_dict of original model, remove and rename some keys
 _snake_case      :					Any					=   torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__			,    map_location="""cpu"""			,    check_hash=SCREAMING_SNAKE_CASE__			)
 _snake_case      :					Optional[int]					=   state_dict["""model"""] if """ade20k""" not in checkpoint_url else state_dict["""state_dict"""]

 _snake_case      :					Union[str, Any]					=   create_rename_keys(SCREAMING_SNAKE_CASE__			,    has_lm_head=SCREAMING_SNAKE_CASE__			,    is_semantic=SCREAMING_SNAKE_CASE__			)
 for src, dest in rename_keys:
  rename_key(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			)
 read_in_q_k_v(SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    has_lm_head=SCREAMING_SNAKE_CASE__			,    is_semantic=SCREAMING_SNAKE_CASE__			)
 if is_semantic:
  # add prefix to decoder keys
  for key, val in state_dict.copy().items():
   _snake_case      :					int					=   state_dict.pop(SCREAMING_SNAKE_CASE__			)
   if key.startswith("""backbone.fpn"""			):
    _snake_case      :					Union[str, Any]					=   key.replace("""backbone.fpn"""			,    """fpn"""			)
   _snake_case      :					int					=   val

    # load HuggingFace model
 if checkpoint_url[-9:-4] == "pt22k":
  _snake_case      :					List[str]					=   BeitForMaskedImageModeling(SCREAMING_SNAKE_CASE__			)
 elif "ade20k" in checkpoint_url:
  _snake_case      :					Tuple					=   BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE__			)
 else:
  _snake_case      :					str					=   BeitForImageClassification(SCREAMING_SNAKE_CASE__			)
 model.eval()
 model.load_state_dict(SCREAMING_SNAKE_CASE__			)

 # Check outputs on an image
 if is_semantic:
  _snake_case      :					int					=   BeitImageProcessor(size=config.image_size			,    do_center_crop=SCREAMING_SNAKE_CASE__			)
  _snake_case      :					Any					=   load_dataset("""hf-internal-testing/fixtures_ade20k"""			,    split="""test"""			)
  _snake_case      :					Any					=   Image.open(ds[0]["""file"""]			)
 else:
  _snake_case      :					Union[str, Any]					=   BeitImageProcessor(
      size=config.image_size			,    resample=PILImageResampling.BILINEAR			,    do_center_crop=SCREAMING_SNAKE_CASE__			)
  _snake_case      :					List[str]					=   prepare_img()

 _snake_case      :					Tuple					=   image_processor(images=SCREAMING_SNAKE_CASE__			,    return_tensors="""pt"""			)
 _snake_case      :					int					=   encoding["""pixel_values"""]

 _snake_case      :					str					=   model(SCREAMING_SNAKE_CASE__			)
 _snake_case      :					Tuple					=   outputs.logits

 # verify logits
 _snake_case      :					int					=   torch.Size([1, 1_000]			)
 if checkpoint_url[:-4].endswith("""beit_base_patch16_224_pt22k"""			):
  _snake_case      :					int					=   torch.Size([1, 196, 8_192]			)
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_224_pt22k"""			):
  _snake_case      :					Any					=   torch.Size([1, 196, 8_192]			)
 elif checkpoint_url[:-4].endswith("""beit_base_patch16_224_pt22k_ft22k"""			):
  _snake_case      :					Optional[int]					=   torch.Size([1, 21_841]			)
  _snake_case      :					Union[str, Any]					=   torch.tensor([2.2_2_8_8, 2.4_6_7_1, 0.7_3_9_5]			)
  _snake_case      :					List[str]					=   2_397
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_224_pt22k_ft22k"""			):
  _snake_case      :					Union[str, Any]					=   torch.Size([1, 21_841]			)
  _snake_case      :					List[Any]					=   torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]			)
  _snake_case      :					Optional[int]					=   2_396
 elif checkpoint_url[:-4].endswith("""beit_base_patch16_224_pt22k_ft1k"""			):
  _snake_case      :					Optional[int]					=   torch.tensor([0.1_2_4_1, 0.0_7_9_8, -0.6_5_6_9]			)
  _snake_case      :					Any					=   285
 elif checkpoint_url[:-4].endswith("""beit_base_patch16_224_pt22k_ft22kto1k"""			):
  _snake_case      :					Dict					=   torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]			)
  _snake_case      :					int					=   281
 elif checkpoint_url[:-4].endswith("""beit_base_patch16_384_pt22k_ft22kto1k"""			):
  _snake_case      :					Optional[Any]					=   torch.tensor([-1.5_3_0_3, -0.9_4_8_4, -0.3_1_4_7]			)
  _snake_case      :					int					=   761
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_224_pt22k_ft1k"""			):
  _snake_case      :					Union[str, Any]					=   torch.tensor([0.4_6_1_0, -0.0_9_2_8, 0.2_0_8_6]			)
  _snake_case      :					Optional[int]					=   761
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_224_pt22k_ft22kto1k"""			):
  _snake_case      :					Dict					=   torch.tensor([-0.4_8_0_4, 0.6_2_5_7, -0.1_8_3_7]			)
  _snake_case      :					Union[str, Any]					=   761
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_384_pt22k_ft22kto1k"""			):
  _snake_case      :					List[Any]					=   torch.tensor([[-0.5_1_2_2, 0.5_1_1_7, -0.2_1_1_3]]			)
  _snake_case      :					int					=   761
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_512_pt22k_ft22kto1k"""			):
  _snake_case      :					List[str]					=   torch.tensor([-0.3_0_6_2, 0.7_2_6_1, 0.4_8_5_2]			)
  _snake_case      :					Tuple					=   761
 elif checkpoint_url[:-4].endswith("""beit_base_patch16_640_pt22k_ft22ktoade20k"""			):
  _snake_case      :					Dict					=   (1, 150, 160, 160)
  _snake_case      :					Dict					=   torch.tensor(
      [
          [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]],
          [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]],
          [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]],
      ]			)
 elif checkpoint_url[:-4].endswith("""beit_large_patch16_640_pt22k_ft22ktoade20k"""			):
  _snake_case      :					Tuple					=   (1, 150, 160, 160)
  _snake_case      :					str					=   torch.tensor(
      [
          [[-4.3_3_0_5, -2.3_0_4_9, -3.0_1_6_1], [-2.9_5_9_1, -1.5_3_0_5, -2.2_2_5_1], [-3.4_1_9_8, -1.8_0_0_4, -2.9_0_6_2]],
          [[-5.8_9_2_2, -3.7_4_3_5, -4.3_9_7_8], [-4.2_0_6_3, -2.7_8_7_2, -3.4_7_5_5], [-4.2_7_9_1, -3.1_8_7_4, -4.1_6_8_1]],
          [[0.9_8_9_5, 4.3_4_6_7, 4.7_6_6_3], [4.2_4_7_6, 5.6_8_3_0, 6.1_5_1_8], [4.5_5_5_0, 6.2_4_9_5, 6.5_1_5_4]],
      ]			)
 else:
  raise ValueError("""Can't verify logits as model is not supported"""			)

 assert logits.shape == expected_shape, "Shape of logits not as expected"
 if not has_lm_head:
  if is_semantic:
   assert torch.allclose(
       logits[0, :3, :3, :3]			,    SCREAMING_SNAKE_CASE__			,    atol=1e-3			), "First elements of logits not as expected"
  else:
   print("""Predicted class idx:"""			,    logits.argmax(-1			).item()			)
   assert torch.allclose(
       logits[0, :3]			,    SCREAMING_SNAKE_CASE__			,    atol=1e-3			), "First elements of logits not as expected"
   assert logits.argmax(-1			).item() == expected_class_idx, "Predicted class index not as expected"

 Path(SCREAMING_SNAKE_CASE__			).mkdir(exist_ok=SCREAMING_SNAKE_CASE__			)
 print(F'''Saving model to {pytorch_dump_folder_path}'''			)
 model.save_pretrained(SCREAMING_SNAKE_CASE__			)
 print(F'''Saving image processor to {pytorch_dump_folder_path}'''			)
 image_processor.save_pretrained(SCREAMING_SNAKE_CASE__			)


if __name__ == "__main__":
  a__      =		argparse.ArgumentParser()

  parser.add_argument(
      """--checkpoint_url""",
      default="""https://conversationhub.blob.core.windows.net/beit-share-public/beit/beit_base_patch16_224_pt22k_ft22kto1k.pth""",
      type=str,
      help="""URL to the original PyTorch checkpoint (.pth file).""",
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the folder to output PyTorch model."""
  )
  a__      =		parser.parse_args()
  convert_beit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
