
"""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  _snake_case	:






										@staticmethod
										def 				__lowercase	(       *snake_case__    :      int    ,			**snake_case__    :      Tuple     ):






															"""simple docstring"""



															pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class  _snake_case	(    unittest.TestCase							):
							SCREAMING_SNAKE_CASE		  =    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							SCREAMING_SNAKE_CASE		  =    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






							def 				__lowercase	(       self    :      Optional[Any]    ,			snake_case__    :      int    ,			snake_case__    :      List[str]    ,			snake_case__    :      Optional[Any]     ):






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      Any    ,			snake_case__    :      Tuple    ,			snake_case__    :      str     ):






												"""simple docstring"""



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

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

												# Accepts URL + PIL.Image + lists
												__a:    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 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



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

												__a:    List[str]		     =       image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""     )
												self.assertEqual(
												    nested_simplify(snake_case__    ,			decimals=4     )    ,			[{"""label""": """LABEL_1""", """score""": 0.574}, {"""label""": """LABEL_0""", """score""": 0.426}]    ,			)

												__a:    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.574}, {"""label""": """LABEL_0""", """score""": 0.426}],
												        [{"""label""": """LABEL_1""", """score""": 0.574}, {"""label""": """LABEL_0""", """score""": 0.426}],
												    ]    ,			)






							@require_tf
							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



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

												__a:    Any		     =       image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""     )
												self.assertEqual(
												    nested_simplify(snake_case__    ,			decimals=4     )    ,			[{"""label""": """LABEL_1""", """score""": 0.574}, {"""label""": """LABEL_0""", """score""": 0.426}]    ,			)

												__a:    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.574}, {"""label""": """LABEL_0""", """score""": 0.426}],
												        [{"""label""": """LABEL_1""", """score""": 0.574}, {"""label""": """LABEL_0""", """score""": 0.426}],
												    ]    ,			)






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    Any		     =       PreTrainedTokenizer()

												# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
												__a:    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 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    List[Any]		     =       pipeline("""image-classification"""    ,			model="""deepmind/vision-perceiver-conv"""     )
												__a:    Union[str, Any]		     =       image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""     )
												self.assertEqual(
												    nested_simplify(snake_case__    ,			decimals=4     )    ,			[
												        {"""score""": 0.4_385, """label""": """tabby, tabby cat"""},
												        {"""score""": 0.321, """label""": """tiger cat"""},
												        {"""score""": 0.0_502, """label""": """Egyptian cat"""},
												        {"""score""": 0.0_137, """label""": """crib, cot"""},
												        {"""score""": 0.007, """label""": """radiator"""},
												    ]    ,			)

												__a:    Tuple		     =       pipeline("""image-classification"""    ,			model="""deepmind/vision-perceiver-fourier"""     )
												__a:    List[Any]		     =       image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""     )
												self.assertEqual(
												    nested_simplify(snake_case__    ,			decimals=4     )    ,			[
												        {"""score""": 0.5_658, """label""": """tabby, tabby cat"""},
												        {"""score""": 0.1_309, """label""": """tiger cat"""},
												        {"""score""": 0.0_722, """label""": """Egyptian cat"""},
												        {"""score""": 0.0_707, """label""": """remote control, remote"""},
												        {"""score""": 0.0_082, """label""": """computer keyboard, keypad"""},
												    ]    ,			)

												__a:    Optional[Any]		     =       pipeline("""image-classification"""    ,			model="""deepmind/vision-perceiver-learned"""     )
												__a:    Dict		     =       image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""     )
												self.assertEqual(
												    nested_simplify(snake_case__    ,			decimals=4     )    ,			[
												        {"""score""": 0.3_022, """label""": """tabby, tabby cat"""},
												        {"""score""": 0.2_362, """label""": """Egyptian cat"""},
												        {"""score""": 0.1_856, """label""": """tiger cat"""},
												        {"""score""": 0.0_324, """label""": """remote control, remote"""},
												        {"""score""": 0.0_096, """label""": """quilt, comforter, comfort, puff"""},
												    ]    ,			)


"""simple docstring"""
from transformers import ProcessorMixin




class  _snake_case	(    _lowercase							):
							SCREAMING_SNAKE_CASE		  =    '''AutoFeatureExtractor'''
							SCREAMING_SNAKE_CASE		  =    '''AutoTokenizer'''
