
"""simple docstring"""


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    __lowerCamelCase     :




										@staticmethod
										def 					UpperCAmelCase__	(							*UpperCAmelCase       ,	**UpperCAmelCase      ):
													pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class    __lowerCamelCase     (   unittest.TestCase ):
				a__:			Any									=						MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
				a__:			Any									=						TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     ImageClassificationPipeline(model=UpperCAmelCase       ,	image_processor=UpperCAmelCase       ,	top_k=2      )
							lowerCamelCase_ 		=     [
							    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      ),
							    '''http://images.cocodataset.org/val2017/000000039769.jpg''',
							]
							return image_classifier, examples




				def 					UpperCAmelCase__	(							self       ,	UpperCAmelCase       ,	UpperCAmelCase      ):
							lowerCamelCase_ 		=     image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )

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

							import datasets

							lowerCamelCase_ 		=     datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''       ,	'''image'''       ,	split='''test'''      )

							# Accepts URL + PIL.Image + lists
							lowerCamelCase_ 		=     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(
							    UpperCAmelCase       ,	[
							        [
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							        ],
							        [
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							        ],
							        [
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							        ],
							        [
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							        ],
							        [
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							            {'''score''': ANY(UpperCAmelCase      ), '''label''': ANY(UpperCAmelCase      )},
							        ],
							    ]       ,	)




				@require_torch
				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     '''hf-internal-testing/tiny-random-vit'''
							lowerCamelCase_ 		=     pipeline('''image-classification'''       ,	model=UpperCAmelCase      )

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

							lowerCamelCase_ 		=     image_classifier(
							    [
							        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
							        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
							    ]       ,	top_k=2       ,	)
							self.assertEqual(
							    nested_simplify(UpperCAmelCase       ,	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 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     '''hf-internal-testing/tiny-random-vit'''
							lowerCamelCase_ 		=     pipeline('''image-classification'''       ,	model=UpperCAmelCase       ,	framework='''tf'''      )

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

							lowerCamelCase_ 		=     image_classifier(
							    [
							        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
							        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
							    ]       ,	top_k=2       ,	)
							self.assertEqual(
							    nested_simplify(UpperCAmelCase       ,	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 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     PreTrainedTokenizer()

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

							self.assertIs(image_classifier.tokenizer       ,	UpperCAmelCase      )





				@slow
				@require_torch
				def 					UpperCAmelCase__	(							self      ):
							# Perceiver is not tested by `run_pipeline_test` properly.
							# That is because the type of feature_extractor and model preprocessor need to be kept
							# in sync, which is not the case in the current design
							lowerCamelCase_ 		=     pipeline('''image-classification'''       ,	model='''deepmind/vision-perceiver-conv'''      )
							lowerCamelCase_ 		=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''      )
							self.assertEqual(
							    nested_simplify(UpperCAmelCase       ,	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'''},
							    ]       ,	)

							lowerCamelCase_ 		=     pipeline('''image-classification'''       ,	model='''deepmind/vision-perceiver-fourier'''      )
							lowerCamelCase_ 		=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''      )
							self.assertEqual(
							    nested_simplify(UpperCAmelCase       ,	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'''},
							    ]       ,	)

							lowerCamelCase_ 		=     pipeline('''image-classification'''       ,	model='''deepmind/vision-perceiver-learned'''      )
							lowerCamelCase_ 		=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''      )
							self.assertEqual(
							    nested_simplify(UpperCAmelCase       ,	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'''},
							    ]       ,	)





"""simple docstring"""


import copy

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging


A_						       =				logging.get_logger(__name__)




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			List[str]									=						'hybrid-clip'
				a__:			List[Any]									=						True




				def __init__(							self       ,	UpperCAmelCase=512       ,	**UpperCAmelCase      ):
							super().__init__(**UpperCAmelCase      )

							if "text_config" not in kwargs:
										raise ValueError('''`text_config` can not be `None`.'''      )

							if "vision_config" not in kwargs:
										raise ValueError('''`vision_config` can not be `None`.'''      )

							lowerCamelCase_ 		=     kwargs.pop('''text_config'''      )
							lowerCamelCase_ 		=     kwargs.pop('''vision_config'''      )

							lowerCamelCase_ 		=     text_config.pop('''model_type'''      )
							lowerCamelCase_ 		=     vision_config.pop('''model_type'''      )

							from transformers import AutoConfig

							lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							if vision_model_type == "clip":
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      ).vision_config
							elif vision_model_type == "clip_vision_model":
										from transformers import CLIPVisionConfig

										lowerCamelCase_ 		=     CLIPVisionConfig(**UpperCAmelCase      )
							else:
										lowerCamelCase_ 		=     AutoConfig.for_model(UpperCAmelCase       ,	**UpperCAmelCase      )

							lowerCamelCase_ 		=     projection_dim
							lowerCamelCase_ 		=     1.0




				@classmethod
				def 					UpperCAmelCase__	(							cls       ,	UpperCAmelCase       ,	UpperCAmelCase       ,	**UpperCAmelCase      ):
							return cls(text_config=text_config.to_dict()       ,	vision_config=vision_config.to_dict()       ,	**UpperCAmelCase      )





				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     copy.deepcopy(self.__dict__      )
							lowerCamelCase_ 		=     self.text_config.to_dict()
							lowerCamelCase_ 		=     self.vision_config.to_dict()
							lowerCamelCase_ 		=     self.__class__.model_type
							return output



