






"""simple docstring"""
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_       			=   0b101100111110110010010000011110111011000110011110
# 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      :				Optional[int]		)		->    int:
				UpperCAmelCase_  :				Union[str, Any]      =      WATERMARK_BITS
				UpperCAmelCase_  :				List[str]      =      WatermarkEncoder()

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





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any			,			lowerCAmelCase_      :				torch.FloatTensor		)		->    List[Any]:
				# can't encode images that are smaller than 256
				if images.shape[-1] < 256:
							return images

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

				UpperCAmelCase_  :				int      =      [self.encoder.encode(lowerCAmelCase_			,			"dwtDct"		) for image in images]

				UpperCAmelCase_  :				Optional[int]      =      torch.from_numpy(np.array(lowerCAmelCase_		)		).permute(0			,			3			,			1			,			2		)

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











"""simple docstring"""
import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format







class     UpperCamelCase_ :
	__magic_name__			   =       None





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any		)		->    List[Any]:
				UpperCAmelCase_  :				Tuple      =      self.feature_extraction_class(**self.feat_extract_dict		)
				UpperCAmelCase_  :				Union[str, Any]      =      json.loads(feat_extract.to_json_string()		)
				for key, value in self.feat_extract_dict.items():
							self.assertEqual(obj[key]			,			lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				str		)		->    int:
				UpperCAmelCase_  :				Dict      =      self.feature_extraction_class(**self.feat_extract_dict		)

				with tempfile.TemporaryDirectory() as tmpdirname:
							UpperCAmelCase_  :				List[Any]      =      os.path.join(lowerCAmelCase_			,			"feat_extract.json"		)
							feat_extract_first.to_json_file(lowerCAmelCase_		)
							UpperCAmelCase_  :				Any      =      self.feature_extraction_class.from_json_file(lowerCAmelCase_		)

				self.assertEqual(feat_extract_second.to_dict()			,			feat_extract_first.to_dict()		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				int		)		->    Dict:
				UpperCAmelCase_  :				Optional[Any]      =      self.feature_extraction_class(**self.feat_extract_dict		)

				with tempfile.TemporaryDirectory() as tmpdirname:
							UpperCAmelCase_  :				Optional[Any]      =      feat_extract_first.save_pretrained(lowerCAmelCase_		)[0]
							check_json_file_has_correct_format(lowerCAmelCase_		)
							UpperCAmelCase_  :				Any      =      self.feature_extraction_class.from_pretrained(lowerCAmelCase_		)

				self.assertEqual(feat_extract_second.to_dict()			,			feat_extract_first.to_dict()		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any		)		->    Optional[Any]:
				UpperCAmelCase_  :				Optional[int]      =      self.feature_extraction_class()
				self.assertIsNotNone(lowerCAmelCase_		)



