





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowerCAmelCase_												=       False



@nightly
@require_torch_gpu
class 							__lowerCAmelCase (    unittest.TestCase  ):






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


							'''simple docstring'''



							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()






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


							'''simple docstring'''



							snake_case_		:					List[Any]			    =						VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''					)
							# remove text_unet
							pipe.remove_unused_weights()
							pipe.to(__magic_name__					)
							pipe.set_progress_bar_config(disable=__magic_name__					)

							snake_case_		:					Dict			    =						load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''					)

							snake_case_		:					str			    =						torch.manual_seed(0					)
							snake_case_		:					Any			    =						pipe(
							    prompt='''first prompt'''      ,							image=__magic_name__      ,							text_to_image_strength=0.75      ,							generator=__magic_name__      ,							guidance_scale=7.5      ,							num_inference_steps=2      ,							output_type='''numpy'''      ,							).images

							with tempfile.TemporaryDirectory() as tmpdirname:
											pipe.save_pretrained(__magic_name__					)
											snake_case_		:					Dict			    =						VersatileDiffusionDualGuidedPipeline.from_pretrained(__magic_name__					)

							pipe.to(__magic_name__					)
							pipe.set_progress_bar_config(disable=__magic_name__					)

							snake_case_		:					Any			    =						generator.manual_seed(0					)
							snake_case_		:					List[str]			    =						pipe(
							    prompt='''first prompt'''      ,							image=__magic_name__      ,							text_to_image_strength=0.75      ,							generator=__magic_name__      ,							guidance_scale=7.5      ,							num_inference_steps=2      ,							output_type='''numpy'''      ,							).images

							assert np.abs(image - new_image					).sum() < 1e-5, "Models don't have the same forward pass"






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


							'''simple docstring'''



							snake_case_		:					Any			    =						VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''					)
							pipe.remove_unused_weights()
							pipe.to(__magic_name__					)
							pipe.set_progress_bar_config(disable=__magic_name__					)

							snake_case_		:					int			    =						'''cyberpunk 2077'''
							snake_case_		:					Any			    =						load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''					)
							snake_case_		:					Dict			    =						torch.manual_seed(0					)
							snake_case_		:					List[Any]			    =						pipe(
							    prompt=__magic_name__      ,							image=__magic_name__      ,							text_to_image_strength=0.75      ,							generator=__magic_name__      ,							guidance_scale=7.5      ,							num_inference_steps=50      ,							output_type='''numpy'''      ,							).images

							snake_case_		:					Any			    =						image[0, 253:256, 253:256, -1]

							assert image.shape == (1, 512, 512, 3)
							snake_case_		:					List[str]			    =						np.array([0.0_787, 0.0_849, 0.0_826, 0.0_812, 0.0_807, 0.0_795, 0.0_818, 0.0_798, 0.0_779]					)

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







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