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






									@staticmethod
									def 				_lowerCamelCase						(		*a   : int       ,	**a   : Tuple  ):






												'''simple docstring'''







												pass







@is_pipeline_test
@require_torch_or_tf
@require_vision
class 	A__				(	unittest.TestCase      ):
				lowercase            =    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
				lowercase            =    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






				def 				_lowerCamelCase						(		self   : Optional[Any]       ,	a   : int       ,	a   : List[str]       ,	a   : Optional[Any]  ):






							'''simple docstring'''







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






				def 				_lowerCamelCase						(		self   : Any       ,	a   : Tuple       ,	a   : str  ):






							'''simple docstring'''







							lowerCAmelCase__ : int   =						image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png'  )

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

							import datasets

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

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






				@require_torch
				def 				_lowerCamelCase						(		self   : Optional[Any]  ):






							'''simple docstring'''







							lowerCAmelCase__ : int   =						"""hf-internal-testing/tiny-random-vit"""
							lowerCAmelCase__ : int   =						pipeline('image-classification'       ,	model=snake_case__  )

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

							lowerCAmelCase__ : List[str]   =						image_classifier(
							    [
							        'http://images.cocodataset.org/val2017/000000039769.jpg',
							        'http://images.cocodataset.org/val2017/000000039769.jpg',
							    ]       ,	top_k=2       ,	)
							self.assertEqual(
							    nested_simplify(snake_case__       ,	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 				_lowerCamelCase						(		self   : List[Any]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Any   =						"""hf-internal-testing/tiny-random-vit"""
							lowerCAmelCase__ : Optional[int]   =						pipeline('image-classification'       ,	model=snake_case__       ,	framework='tf'  )

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

							lowerCAmelCase__ : Optional[Any]   =						image_classifier(
							    [
							        'http://images.cocodataset.org/val2017/000000039769.jpg',
							        'http://images.cocodataset.org/val2017/000000039769.jpg',
							    ]       ,	top_k=2       ,	)
							self.assertEqual(
							    nested_simplify(snake_case__       ,	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 				_lowerCamelCase						(		self   : Tuple  ):






							'''simple docstring'''







							lowerCAmelCase__ : Any   =						PreTrainedTokenizer()

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

							self.assertIs(image_classifier.tokenizer       ,	snake_case__  )






				@slow
				@require_torch
				def 				_lowerCamelCase						(		self   : Any  ):






							'''simple docstring'''







							lowerCAmelCase__ : List[Any]   =						pipeline('image-classification'       ,	model='deepmind/vision-perceiver-conv'  )
							lowerCAmelCase__ : Union[str, Any]   =						image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'  )
							self.assertEqual(
							    nested_simplify(snake_case__       ,	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__ : Tuple   =						pipeline('image-classification'       ,	model='deepmind/vision-perceiver-fourier'  )
							lowerCAmelCase__ : List[Any]   =						image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'  )
							self.assertEqual(
							    nested_simplify(snake_case__       ,	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__ : Optional[Any]   =						pipeline('image-classification'       ,	model='deepmind/vision-perceiver-learned'  )
							lowerCAmelCase__ : Dict   =						image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'  )
							self.assertEqual(
							    nested_simplify(snake_case__       ,	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 unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput




@dataclass
class  _snake_case	(    _lowercase							):
							SCREAMING_SNAKE_CASE		  =    42
							SCREAMING_SNAKE_CASE		  =    None
							SCREAMING_SNAKE_CASE		  =    None






class  _snake_case	(    unittest.TestCase							):






							def 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    Dict		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertIsNone(x.b     )
												self.assertIsNone(x.c     )
												with self.assertRaises(snake_case__     ):
																	__a:    int		     =       x.d






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )

												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""b"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x["""c"""]

												__a:    str		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""c"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[str]		     =       x["""b"""]






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    str		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a"""]     )

												__a:    Optional[Any]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """b"""]     )

												__a:    Union[str, Any]		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """c"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""c""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """c"""]     )

												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.update({"""d""": 2_0}     )
												with self.assertRaises(snake_case__     ):
																	del x["a"]
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x.pop("""a"""     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.setdefault("""d"""    ,			3_2     )






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    str		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    Any		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    List[Any]		     =       ModelOutputTest({"""a""": 3_0, """b""": 1_0}     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )

												with self.assertRaises(snake_case__     ):
																	__a:    str		     =       ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]     )

												__a:    str		     =       ModelOutputTest(a=(3_0, 3_0)     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(x.a    ,			(3_0, 3_0)     )
