




"""simple docstring"""


import argparse
import hashlib  # hashlib is only used inside the Test class
import struct


class _lowerCamelCase    :






   def __init__(		self       :		Tuple   ,     UpperCamelCase       :		Dict   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	data
         lowerCAmelCase__      :			Tuple   =	[0X67_45_23_01, 0Xef_cd_ab_89, 0X98_ba_dc_fe, 0X10_32_54_76, 0Xc3_d2_e1_f0]






   @staticmethod
   def 	_lowerCAmelCase   (		UpperCamelCase       :		Dict   ,     UpperCamelCase       :		Union[str, Any]   )			->						List[str]:

         """simple docstring"""
         return ((n << b) | (n >> (32 - b))) & 0Xff_ff_ff_ff






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	b"""\x80""" + b"""\x00""" * (63 - (len(self.data   ) + 8) % 64)
         lowerCAmelCase__      :			str   =	self.data + padding + struct.pack(""">Q"""   ,     8 * len(self.data   )   )
         return padded_data






   def 	_lowerCAmelCase   (		self       :		Any   )			->						int:

         """simple docstring"""
         return [
             self.padded_data[i : i + 64] for i in range(0   ,     len(self.padded_data   )   ,     64   )
         ]






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   ,     UpperCamelCase       :		List[Any]   )			->						Any:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	list(struct.unpack(""">16L"""   ,     UpperCamelCase   )   ) + [0] * 64
         for i in range(16   ,     80   ):
               lowerCAmelCase__      :			Union[str, Any]   =	self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16])   ,     1   )
         return w






   def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	self.padding()
         lowerCAmelCase__      :			Optional[Any]   =	self.split_blocks()
         for block in self.blocks:
               lowerCAmelCase__      :			int   =	self.expand_block(UpperCamelCase   )
               lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__      :			int   =	self.h
               for i in range(0   ,     80   ):
                     if 0 <= i < 20:
                           lowerCAmelCase__      :			List[str]   =	(b & c) | ((~b) & d)
                           lowerCAmelCase__      :			Union[str, Any]   =	0X5a_82_79_99
                     elif 20 <= i < 40:
                           lowerCAmelCase__      :			List[str]   =	b ^ c ^ d
                           lowerCAmelCase__      :			Dict   =	0X6e_d9_eb_a1
                     elif 40 <= i < 60:
                           lowerCAmelCase__      :			List[str]   =	(b & c) | (b & d) | (c & d)
                           lowerCAmelCase__      :			int   =	0X8f_1b_bc_dc
                     elif 60 <= i < 80:
                           lowerCAmelCase__      :			Union[str, Any]   =	b ^ c ^ d
                           lowerCAmelCase__      :			Optional[Any]   =	0Xca_62_c1_d6
                     lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__      :			Optional[int]   =	(
                         self.rotate(UpperCamelCase   ,     5   ) + f + e + k + expanded_block[i] & 0Xff_ff_ff_ff,
                         a,
                         self.rotate(UpperCamelCase   ,     30   ),
                         c,
                         d,
                     )
               lowerCAmelCase__      :			Union[str, Any]   =	(
                   self.h[0] + a & 0Xff_ff_ff_ff,
                   self.h[1] + b & 0Xff_ff_ff_ff,
                   self.h[2] + c & 0Xff_ff_ff_ff,
                   self.h[3] + d & 0Xff_ff_ff_ff,
                   self.h[4] + e & 0Xff_ff_ff_ff,
               )
         return ("{:08x}" * 5).format(*self.h   )






def 	lowercase_				(			)						->    Optional[Any]:
      lowerCAmelCase__      :			Dict   =	B"""Test String"""
      assert SHAaHash(__UpperCAmelCase				).final_hash() == hashlib.shaa(__UpperCAmelCase				).hexdigest()  # noqa: S324



def 	lowercase_				(			)						->    List[str]:
      lowerCAmelCase__      :			List[Any]   =	argparse.ArgumentParser(description="""Process some strings or files"""				)
      parser.add_argument(
          """--string"""    ,		dest="""input_string"""    ,		default="""Hello World!! Welcome to Cryptography"""    ,		help="""Hash the string"""    ,		)
      parser.add_argument("""--file"""    ,		dest="""input_file"""    ,		help="""Hash contents of a file"""				)
      lowerCAmelCase__      :			Optional[int]   =	parser.parse_args()
      lowerCAmelCase__      :			List[str]   =	args.input_string
      # In any case hash input should be a bytestring
      if args.input_file:
            with open(args.input_file    ,		"""rb"""				) as f:
                  lowerCAmelCase__      :			Dict   =	f.read()
      else:
            lowerCAmelCase__      :			int   =	bytes(__UpperCAmelCase    ,		"""utf-8"""				)
      print(SHAaHash(__UpperCAmelCase				).final_hash()				)


if __name__ == "__main__":
  main()
  import doctest

  doctest.testmod()











"""simple docstring"""


from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase    ,		__UpperCAmelCase=1E-12				)						->    Dict:
      lowerCAmelCase__      :			Optional[int]   =	jnp.divide(emb_a.T    ,		jnp.clip(jnp.linalg.norm(__UpperCAmelCase    ,		axis=1				)    ,		a_min=__UpperCAmelCase				)				).T
      lowerCAmelCase__      :			Any   =	jnp.divide(emb_a.T    ,		jnp.clip(jnp.linalg.norm(__UpperCAmelCase    ,		axis=1				)    ,		a_min=__UpperCAmelCase				)				).T
      return jnp.matmul(__UpperCAmelCase    ,		norm_emb_a.T				)


class _lowerCamelCase    (					nn.Module						):
   _lowerCamelCase    :CLIPConfig
   _lowerCamelCase    :jnp.dtype     				=     jnp.floataa






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	FlaxCLIPVisionModule(self.config.vision_config   )
         lowerCAmelCase__      :			Dict   =	nn.Dense(self.config.projection_dim   ,     use_bias=UpperCamelCase   ,     dtype=self.dtype   )

         lowerCAmelCase__      :			Optional[int]   =	self.param("""concept_embeds"""   ,     jax.nn.initializers.ones   ,     (17, self.config.projection_dim)   )
         lowerCAmelCase__      :			Optional[int]   =	self.param(
             """special_care_embeds"""   ,     jax.nn.initializers.ones   ,     (3, self.config.projection_dim)   )

         lowerCAmelCase__      :			List[Any]   =	self.param("""concept_embeds_weights"""   ,     jax.nn.initializers.ones   ,     (17,)   )
         lowerCAmelCase__      :			List[Any]   =	self.param("""special_care_embeds_weights"""   ,     jax.nn.initializers.ones   ,     (3,)   )






   def __call__(		self       :		str   ,     UpperCamelCase       :		List[Any]   )			->						Union[str, Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	self.vision_model(UpperCamelCase   )[1]
         lowerCAmelCase__      :			List[Any]   =	self.visual_projection(UpperCamelCase   )

         lowerCAmelCase__      :			str   =	jax_cosine_distance(UpperCamelCase   ,     self.special_care_embeds   )
         lowerCAmelCase__      :			Optional[int]   =	jax_cosine_distance(UpperCamelCase   ,     self.concept_embeds   )

         # increase this value to create a stronger `nfsw` filter
         # at the cost of increasing the possibility of filtering benign image inputs
         lowerCAmelCase__      :			Any   =	0.0

         lowerCAmelCase__      :			List[Any]   =	special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
         lowerCAmelCase__      :			Any   =	jnp.round(UpperCamelCase   ,     3   )
         lowerCAmelCase__      :			List[str]   =	jnp.any(special_scores > 0   ,     axis=1   ,     keepdims=UpperCamelCase   )
         # Use a lower threshold if an image has any special care concept
         lowerCAmelCase__      :			Tuple   =	is_special_care * 0.01

         lowerCAmelCase__      :			List[Any]   =	cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
         lowerCAmelCase__      :			Dict   =	jnp.round(UpperCamelCase   ,     3   )
         lowerCAmelCase__      :			List[str]   =	jnp.any(concept_scores > 0   ,     axis=1   )

         return has_nsfw_concepts






class _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Optional[Any]     				=     CLIPConfig
   _lowerCamelCase    :Dict     				=     "clip_input"
   _lowerCamelCase    :int     				=     FlaxStableDiffusionSafetyCheckerModule






   def __init__(		self       :		int   ,     UpperCamelCase       :		CLIPConfig   ,     UpperCamelCase       :		Optional[Tuple] = None   ,     UpperCamelCase       :		int = 0   ,     UpperCamelCase       :		jnp.dtype = jnp.floataa   ,     UpperCamelCase       :		bool = True   ,     **UpperCamelCase       :		int   ,     )			->						str:

         """simple docstring"""
         if input_shape is None:
               lowerCAmelCase__      :			str   =	(1, 2_24, 2_24, 3)
         lowerCAmelCase__      :			Union[str, Any]   =	self.module_class(config=UpperCamelCase   ,     dtype=UpperCamelCase   ,     **UpperCamelCase   )
         super().__init__(UpperCamelCase   ,     UpperCamelCase   ,     input_shape=UpperCamelCase   ,     seed=UpperCamelCase   ,     dtype=UpperCamelCase   ,     _do_init=_do_init   )






   def 	_lowerCAmelCase   (		self       :		Dict   ,     UpperCamelCase       :		jax.random.KeyArray   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		FrozenDict = None   )			->						FrozenDict:

         """simple docstring"""
         # init input tensor
         lowerCAmelCase__      :			Union[str, Any]   =	jax.random.normal(UpperCamelCase   ,     UpperCamelCase   )

         lowerCAmelCase__ ,   lowerCAmelCase__      :			Union[str, Any]   =	jax.random.split(UpperCamelCase   )
         lowerCAmelCase__      :			List[Any]   =	{"""params""": params_rng, """dropout""": dropout_rng}

         lowerCAmelCase__      :			str   =	self.module.init(UpperCamelCase   ,     UpperCamelCase   )["""params"""]

         return random_params






   def __call__(		self       :		Dict   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		dict = None   ,     )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	jnp.transpose(UpperCamelCase   ,     (0, 2, 3, 1)   )

         return self.module.apply(
             {"""params""": params or self.params}   ,     jnp.array(UpperCamelCase   ,     dtype=jnp.floataa   )   ,     rngs={}   ,     )





