



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


__A             =False

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




			'''simple docstring'''






			def 			snake_case__					(       self							:			Tuple			):
					'''simple docstring'''





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




			def 			snake_case__					(       self							:			Any			):
					'''simple docstring'''





					__UpperCAmelCase   :					Any													=     VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''			)
					# remove text_unet
					pipe.remove_unused_weights()
					pipe.to(a_			)
					pipe.set_progress_bar_config(disable=a_			)

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

					__UpperCAmelCase   :					str													=     torch.manual_seed(0			)
					__UpperCAmelCase   :					Dict													=     pipe(
					    prompt='''first prompt'''	,	image=a_	,	text_to_image_strength=0.7_5	,	generator=a_	,	guidance_scale=7.5	,	num_inference_steps=2	,	output_type='''numpy'''	,	).images

					with tempfile.TemporaryDirectory() as tmpdirname:
							pipe.save_pretrained(a_			)
							__UpperCAmelCase   :					str													=     VersatileDiffusionDualGuidedPipeline.from_pretrained(a_			)

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

					__UpperCAmelCase   :					str													=     generator.manual_seed(0			)
					__UpperCAmelCase   :					int													=     pipe(
					    prompt='''first prompt'''	,	image=a_	,	text_to_image_strength=0.7_5	,	generator=a_	,	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 			snake_case__					(       self							:			List[str]			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''			)
					pipe.remove_unused_weights()
					pipe.to(a_			)
					pipe.set_progress_bar_config(disable=a_			)

					__UpperCAmelCase   :					Optional[Any]													=     '''cyberpunk 2077'''
					__UpperCAmelCase   :					Tuple													=     load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''			)
					__UpperCAmelCase   :					Union[str, Any]													=     torch.manual_seed(0			)
					__UpperCAmelCase   :					int													=     pipe(
					    prompt=a_	,	image=a_	,	text_to_image_strength=0.7_5	,	generator=a_	,	guidance_scale=7.5	,	num_inference_steps=50	,	output_type='''numpy'''	,	).images

					__UpperCAmelCase   :					str													=     image[0, 2_53:2_56, 2_53:2_56, -1]

					assert image.shape == (1, 5_12, 5_12, 3)
					__UpperCAmelCase   :					Dict													=     np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]			)

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










import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
				from PIL import Image
else:

				class 		UpperCAmelCase__      :




							'''simple docstring'''






							@staticmethod
							def 			snake_case__					(       *a_							:			Union[str, Any]	,	**a_							:			Any			):
									'''simple docstring'''





									pass

@is_pipeline_test
@require_torch_or_tf
@require_vision
class 		UpperCAmelCase__      (      unittest.TestCase       ):




			'''simple docstring'''






			UpperCamelCase      						=	MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
			UpperCamelCase      						=	TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING




			def 			snake_case__					(       self							:			Any	,	a_							:			str	,	a_							:			Dict	,	a_							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase   :					Any													=     ImageClassificationPipeline(model=a_	,	image_processor=a_	,	top_k=2			)
					__UpperCAmelCase   :					int													=     [
					    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			),
					    '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					]
					return image_classifier, examples




			def 			snake_case__					(       self							:			str	,	a_							:			Optional[int]	,	a_							:			List[str]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)

					self.assertEqual(
					    a_	,	[
					        {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					    ]	,	)

					import datasets

					__UpperCAmelCase   :					Optional[Any]													=     datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''	,	'''image'''	,	split='''test'''			)

					# Accepts URL + PIL.Image + lists
					__UpperCAmelCase   :					Any													=     image_classifier(
					    [
					        Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			),
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        # RGBA
					        dataset[0]['''file'''],
					        # LA
					        dataset[1]['''file'''],
					        # L
					        dataset[2]['''file'''],
					    ]			)
					self.assertEqual(
					    a_	,	[
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					    ]	,	)




			@require_torch
			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					Dict													=     '''hf-internal-testing/tiny-random-vit'''
					__UpperCAmelCase   :					Optional[int]													=     pipeline('''image-classification'''	,	model=a_			)

					__UpperCAmelCase   :					Optional[int]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}]	,	)

					__UpperCAmelCase   :					Dict													=     image_classifier(
					    [
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					    ]	,	top_k=2	,	)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					    ]	,	)




			@require_tf
			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     '''hf-internal-testing/tiny-random-vit'''
					__UpperCAmelCase   :					Optional[Any]													=     pipeline('''image-classification'''	,	model=a_	,	framework='''tf'''			)

					__UpperCAmelCase   :					Optional[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}]	,	)

					__UpperCAmelCase   :					Any													=     image_classifier(
					    [
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					    ]	,	top_k=2	,	)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					    ]	,	)




			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     PreTrainedTokenizer()

					# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
					__UpperCAmelCase   :					Dict													=     pipeline(
					    '''image-classification'''	,	model='''hf-internal-testing/tiny-random-vit'''	,	tokenizer=a_			)

					self.assertIs(image_classifier.tokenizer	,	a_			)




			@slow
			@require_torch
			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-conv'''			)
					__UpperCAmelCase   :					List[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.4_3_8_5, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.3_2_1, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_5_0_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.0_1_3_7, '''label''': '''crib, cot'''},
					        {'''score''': 0.0_0_7, '''label''': '''radiator'''},
					    ]	,	)

					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-fourier'''			)
					__UpperCAmelCase   :					List[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.5_6_5_8, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.1_3_0_9, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_7_2_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.0_7_0_7, '''label''': '''remote control, remote'''},
					        {'''score''': 0.0_0_8_2, '''label''': '''computer keyboard, keypad'''},
					    ]	,	)

					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-learned'''			)
					__UpperCAmelCase   :					Union[str, Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.3_0_2_2, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.2_3_6_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.1_8_5_6, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_3_2_4, '''label''': '''remote control, remote'''},
					        {'''score''': 0.0_0_9_6, '''label''': '''quilt, comforter, comfort, puff'''},
					    ]	,	)





