


'''simple docstring'''






import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

lowercase				=3





def lowerCamelCase__     (		__lowerCamelCase			:				int				):







   '''simple docstring'''




   print('Generating primitive root of p'				)
   while True:
      _UpperCAmelCase  :  List[Any]				       =random.randrange(3			,       __lowerCamelCase				)
      if pow(__lowerCamelCase			,       2			,       __lowerCamelCase				) == 1:
         continue
      if pow(__lowerCamelCase			,       __lowerCamelCase			,       __lowerCamelCase				) == 1:
         continue
      return g





def lowerCamelCase__     (		__lowerCamelCase			:				int				):







   '''simple docstring'''




   print('Generating prime p...'				)
   _UpperCAmelCase  :  Tuple				       =rabin_miller.generate_large_prime(__lowerCamelCase				)  # select large prime number.
   _UpperCAmelCase  :  List[str]				       =primitive_root(__lowerCamelCase				)  # one primitive root on modulo p.
   _UpperCAmelCase  :  Union[str, Any]				       =random.randrange(3			,       __lowerCamelCase				)  # private_key -> have to be greater than 2 for safety.
   _UpperCAmelCase  :  Any				       =cryptomath.find_mod_inverse(pow(__lowerCamelCase			,       __lowerCamelCase			,       __lowerCamelCase				)			,       __lowerCamelCase				)

   _UpperCAmelCase  :  Tuple				       =(key_size, e_a, e_a, p)
   _UpperCAmelCase  :  Tuple				       =(key_size, d)

   return public_key, private_key





def lowerCamelCase__     (		__lowerCamelCase			:				str			,       __lowerCamelCase			:				int				):







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

   _UpperCAmelCase       ,			_UpperCAmelCase  :  Optional[Any]				       =generate_key(__lowerCamelCase				)
   print(f"\nWriting public key to file {name}_pubkey.txt..."				)
   with open(f"{name}_pubkey.txt"			,       'w'				) as fo:
      fo.write(f"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"				)

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





def lowerCamelCase__     (		):







   '''simple docstring'''




   print('Making key files...'				)
   make_key_files('elgamal'			,       2_0_4_8				)
   print('Key files generation successful'				)


if __name__ == "__main__":
 main()





'''simple docstring'''






from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
 from PIL import Image

 from ..image_utils import load_image

if is_tf_available():
 import tensorflow as tf

 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

if is_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

lowercase				=logging.get_logger(__name__)






@add_end_docstrings(lowerCAmelCase							)
class    __magic_name__				(						lowerCAmelCase							):






       def __init__(     self    ,				*snake_case    ,				**snake_case)		->    Dict:




          '''simple docstring'''
          super().__init__(*snake_case    ,				**snake_case)
          requires_backends(self    ,				'vision')
          self.check_model_type(
              TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
              if self.framework == 'tf'
              else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING)






       def      lowerCAmelCase						(     self    ,				snake_case=None)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       ={}
          if top_k is not None:
             _UpperCAmelCase  :  Optional[int]				       =top_k
          return {}, {}, postprocess_params






       def __call__(     self    ,				snake_case    ,				**snake_case)		->    List[str]:




          '''simple docstring'''
          return super().__call__(snake_case    ,				**snake_case)






       def      lowerCAmelCase						(     self    ,				snake_case)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =load_image(snake_case)
          _UpperCAmelCase  :  Tuple				       =self.image_processor(images=snake_case    ,				return_tensors=self.framework)
          return model_inputs






       def      lowerCAmelCase						(     self    ,				snake_case)		->    Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =self.model(**snake_case)
          return model_outputs






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case=5)		->    Any:




          '''simple docstring'''
          if top_k > self.model.config.num_labels:
             _UpperCAmelCase  :  Optional[Any]				       =self.model.config.num_labels

          if self.framework == "pt":
             _UpperCAmelCase  :  List[Any]				       =model_outputs.logits.softmax(-1)[0]
             _UpperCAmelCase       ,			_UpperCAmelCase  :  Optional[Any]				       =probs.topk(snake_case)
          elif self.framework == "tf":
             _UpperCAmelCase  :  int				       =stable_softmax(model_outputs.logits    ,				axis=-1)[0]
             _UpperCAmelCase  :  List[Any]				       =tf.math.top_k(snake_case    ,				k=snake_case)
             _UpperCAmelCase       ,			_UpperCAmelCase  :  List[str]				       =topk.values.numpy(), topk.indices.numpy()
          else:
             raise ValueError(f"Unsupported framework: {self.framework}")

          _UpperCAmelCase  :  int				       =scores.tolist()
          _UpperCAmelCase  :  Tuple				       =ids.tolist()
          return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case    ,				snake_case)]

