





import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class       __lowerCAmelCase (    _a,  unittest.TestCase  ):
			lowerCamelCase_  :  Union[str, Any]     =   KandinskyPriorPipeline
			lowerCamelCase_  :  Tuple     =   ['''prompt''']
			lowerCamelCase_  :  List[str]     =   ['''prompt''', '''negative_prompt''']
			lowerCamelCase_  :  int     =   [
			    '''num_images_per_prompt''',
			    '''generator''',
			    '''num_inference_steps''',
			    '''latents''',
			    '''negative_prompt''',
			    '''guidance_scale''',
			    '''output_type''',
			    '''return_dict''',
			]
			lowerCamelCase_  :  Optional[Any]     =   False






			@property
			def     lowerCamelCase						(self					)  ->     List[str]:


							'''simple docstring'''



							return 32






			@property
			def     lowerCamelCase						(self					)  ->     List[Any]:


							'''simple docstring'''



							return 32






			@property
			def     lowerCamelCase						(self					)  ->     Any:


							'''simple docstring'''



							return self.time_input_dim






			@property
			def     lowerCamelCase						(self					)  ->     List[str]:


							'''simple docstring'''



							return self.time_input_dim * 4






			@property
			def     lowerCamelCase						(self					)  ->     List[Any]:


							'''simple docstring'''



							return 100






			@property
			def     lowerCamelCase						(self					)  ->     Union[str, Any]:


							'''simple docstring'''



							snake_case_		:					int			    =						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)
							return tokenizer






			@property
			def     lowerCamelCase						(self					)  ->     Tuple:


							'''simple docstring'''



							torch.manual_seed(0					)
							snake_case_		:					Tuple			    =						CLIPTextConfig(
							    bos_token_id=0      ,							eos_token_id=2      ,							hidden_size=self.text_embedder_hidden_size      ,							projection_dim=self.text_embedder_hidden_size      ,							intermediate_size=37      ,							layer_norm_eps=1e-05      ,							num_attention_heads=4      ,							num_hidden_layers=5      ,							pad_token_id=1      ,							vocab_size=1000      ,							)
							return CLIPTextModelWithProjection(__magic_name__					)






			@property
			def     lowerCamelCase						(self					)  ->     Any:


							'''simple docstring'''



							torch.manual_seed(0					)

							snake_case_		:					Dict			    =						{
							    '''num_attention_heads''': 2,
							    '''attention_head_dim''': 12,
							    '''embedding_dim''': self.text_embedder_hidden_size,
							    '''num_layers''': 1,
							}

							snake_case_		:					Union[str, Any]			    =						PriorTransformer(**__magic_name__					)
							# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
							snake_case_		:					Tuple			    =						nn.Parameter(torch.ones(model.clip_std.shape					)					)
							return model






			@property
			def     lowerCamelCase						(self					)  ->     List[str]:


							'''simple docstring'''



							torch.manual_seed(0					)
							snake_case_		:					Tuple			    =						CLIPVisionConfig(
							    hidden_size=self.text_embedder_hidden_size      ,							image_size=224      ,							projection_dim=self.text_embedder_hidden_size      ,							intermediate_size=37      ,							num_attention_heads=4      ,							num_channels=3      ,							num_hidden_layers=5      ,							patch_size=14      ,							)

							snake_case_		:					Union[str, Any]			    =						CLIPVisionModelWithProjection(__magic_name__					)
							return model






			@property
			def     lowerCamelCase						(self					)  ->     Optional[int]:


							'''simple docstring'''



							snake_case_		:					Optional[Any]			    =						CLIPImageProcessor(
							    crop_size=224      ,							do_center_crop=__magic_name__      ,							do_normalize=__magic_name__      ,							do_resize=__magic_name__      ,							image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073]      ,							image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711]      ,							resample=3      ,							size=224      ,							)

							return image_processor






			def     lowerCamelCase						(self					)  ->     List[str]:


							'''simple docstring'''



							snake_case_		:					Union[str, Any]			    =						self.dummy_prior
							snake_case_		:					List[str]			    =						self.dummy_image_encoder
							snake_case_		:					str			    =						self.dummy_text_encoder
							snake_case_		:					Union[str, Any]			    =						self.dummy_tokenizer
							snake_case_		:					Tuple			    =						self.dummy_image_processor

							snake_case_		:					int			    =						UnCLIPScheduler(
							    variance_type='''fixed_small_log'''      ,							prediction_type='''sample'''      ,							num_train_timesteps=1000      ,							clip_sample=__magic_name__      ,							clip_sample_range=10.0      ,							)

							snake_case_		:					str			    =						{
							    '''prior''': prior,
							    '''image_encoder''': image_encoder,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''scheduler''': scheduler,
							    '''image_processor''': image_processor,
							}

							return components






			def     lowerCamelCase						(self      ,							__magic_name__      ,							__magic_name__=0					)  ->     List[Any]:


							'''simple docstring'''



							if str(__magic_name__					).startswith('''mps'''					):
											snake_case_		:					Tuple			    =						torch.manual_seed(__magic_name__					)
							else:
											snake_case_		:					Optional[int]			    =						torch.Generator(device=__magic_name__					).manual_seed(__magic_name__					)
							snake_case_		:					List[str]			    =						{
							    '''prompt''': '''horse''',
							    '''generator''': generator,
							    '''guidance_scale''': 4.0,
							    '''num_inference_steps''': 2,
							    '''output_type''': '''np''',
							}
							return inputs






			def     lowerCamelCase						(self					)  ->     Optional[Any]:


							'''simple docstring'''



							snake_case_		:					Optional[Any]			    =						'''cpu'''

							snake_case_		:					List[Any]			    =						self.get_dummy_components()

							snake_case_		:					Optional[int]			    =						self.pipeline_class(**__magic_name__					)
							snake_case_		:					Optional[int]			    =						pipe.to(__magic_name__					)

							pipe.set_progress_bar_config(disable=__magic_name__					)

							snake_case_		:					Tuple			    =						pipe(**self.get_dummy_inputs(__magic_name__					)					)
							snake_case_		:					int			    =						output.image_embeds

							snake_case_		:					Tuple			    =						pipe(
							    **self.get_dummy_inputs(__magic_name__					)      ,							return_dict=__magic_name__      ,							)[0]

							snake_case_		:					List[Any]			    =						image[0, -10:]
							snake_case_		:					str			    =						image_from_tuple[0, -10:]

							assert image.shape == (1, 32)

							snake_case_		:					Optional[Any]			    =						np.array(
							    [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() < 1e-2






			@skip_mps
			def     lowerCamelCase						(self					)  ->     List[Any]:


							'''simple docstring'''



							snake_case_		:					Optional[Any]			    =						torch_device == '''cpu'''
							snake_case_		:					Tuple			    =						True
							snake_case_		:					str			    =						False

							self._test_inference_batch_single_identical(
							    test_max_difference=__magic_name__      ,							relax_max_difference=__magic_name__      ,							test_mean_pixel_difference=__magic_name__      ,							)






			@skip_mps
			def     lowerCamelCase						(self					)  ->     Tuple:


							'''simple docstring'''



							snake_case_		:					Tuple			    =						torch_device == '''cpu'''
							snake_case_		:					str			    =						False

							self._test_attention_slicing_forward_pass(
							    test_max_difference=__magic_name__      ,							test_mean_pixel_difference=__magic_name__      ,							)







def   lowerCamelCase_		(       _UpperCamelCase				)	->		int:






				"""simple docstring"""


				barcode //= 10  # exclude the last digit
				snake_case_		:					str			    =						False
				snake_case_		:					str			    =						0

				# extract and check each digit
				while barcode != 0:
								snake_case_		:					Any			    =						1 if checker else 3
								s += mult * (barcode % 10)
								barcode //= 10
								snake_case_		:					Tuple			    =						not checker

				return (10 - (s % 10)) % 10






def   lowerCamelCase_		(       _UpperCamelCase				)	->		bool:






				"""simple docstring"""


				return len(str(_UpperCamelCase				)				) == 13 and get_check_digit(_UpperCamelCase				) == barcode % 10






def   lowerCamelCase_		(       _UpperCamelCase				)	->		int:






				"""simple docstring"""


				if str(_UpperCamelCase				).isalpha():
								snake_case_		:					Optional[Any]			    =						f'''Barcode \'{barcode}\' has alphabetic characters.'''
								raise ValueError(_UpperCamelCase				)
				elif int(_UpperCamelCase				) < 0:
								raise ValueError('''The entered barcode has a negative value. Try again.'''				)
				else:
								return int(_UpperCamelCase				)


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	lowerCAmelCase_												=       get_barcode(input('''Barcode: ''').strip())

	if is_valid(barcode):
		print(F'''\'{barcode}\' is a valid barcode.''')
	else:
		print(F'''\'{barcode}\' is NOT a valid barcode.''')
