





import numpy as np
import torch
from imwatermark import WatermarkEncoder


# Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66
_lowerCAmelCase 				=     0B101_100_111_110_110_010_010_000_011_110_111_011_000_110_011_110
# bin(x)[2:] gives bits of x as str, use int to convert them to 0/1
_lowerCAmelCase 				=     [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]

class    _UpperCAmelCase							:
      def __init__(		self       ):
         A_     :				List[Any]			      =	WATERMARK_BITS
         A_     :				Dict			      =	WatermarkEncoder()

         self.encoder.set_watermark("""bits"""      ,       self.watermark       )






      def 		_lowerCamelCase      (		self      ,       a__       ):
         # can't encode images that are smaller than 256
         if images.shape[-1] < 256:
            return images

         A_     :				Optional[int]			      =	(255 * (images / 2 + 0.5)).cpu().permute(0      ,       2      ,       3      ,       1       ).float().numpy()

         A_     :				Union[str, Any]			      =	[self.encoder.encode(a__      ,       """dwtDct"""       ) for image in images]

         A_     :				List[Any]			      =	torch.from_numpy(np.array(a__       )       ).permute(0      ,       3      ,       1      ,       2       )

         A_     :				List[str]			      =	torch.clamp(2 * (images / 255 - 0.5)      ,       min=-1.0      ,       max=1.0       )
         return images













_lowerCAmelCase 				=     """ABCDEFGHIJKLMNOPQRSTUVWXYZ"""



def     _lowerCAmelCase		(  ):
   '''simple docstring'''


   A_     :				Any			      =	input("""Enter message: """     )
   A_     :				Dict			      =	input("""Enter key [alphanumeric]: """     )
   A_     :				Dict			      =	input("""Encrypt/Decrypt [e/d]: """     )

   if mode.lower().startswith("""e"""     ):
      A_     :				Union[str, Any]			      =	"""encrypt"""
      A_     :				Optional[int]			      =	encrypt_message(_lowerCAmelCase  ,_lowerCAmelCase     )
   elif mode.lower().startswith("""d"""     ):
      A_     :				str			      =	"""decrypt"""
      A_     :				Dict			      =	decrypt_message(_lowerCAmelCase  ,_lowerCAmelCase     )

   print(f"""\n{mode.title()}ed message:"""     )
   print(_lowerCAmelCase     )



def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase     ):
   '''simple docstring'''


   return translate_message(_lowerCAmelCase  ,_lowerCAmelCase  ,"""encrypt"""     )



def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase     ):
   '''simple docstring'''


   return translate_message(_lowerCAmelCase  ,_lowerCAmelCase  ,"""decrypt"""     )



def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase     ):
   '''simple docstring'''


   A_     :				Dict			      =	[]
   A_     :				List[Any]			      =	0
   A_     :				int			      =	key.upper()

   for symbol in message:
      A_     :				Optional[int]			      =	LETTERS.find(symbol.upper()     )
      if num != -1:
         if mode == "encrypt":
            num += LETTERS.find(key[key_index]     )
         elif mode == "decrypt":
            num -= LETTERS.find(key[key_index]     )

         num %= len(_lowerCAmelCase     )

         if symbol.isupper():
            translated.append(LETTERS[num]     )
         elif symbol.islower():
            translated.append(LETTERS[num].lower()     )

         key_index += 1
         if key_index == len(_lowerCAmelCase     ):
            A_     :				Dict			      =	0
      else:
         translated.append(_lowerCAmelCase     )
   return "".join(_lowerCAmelCase     )


if __name__ == "__main__":
      main()






