

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



       @staticmethod
       def 		UpperCamelCase_	(    *A__   :       int    ,      **A__   :       List[Any]	)					->  Optional[Any]:
         pass



@is_pipeline_test
@require_torch_or_tf
@require_vision
class 						lowercase_					(						unittest.TestCase  ):
      UpperCamelCase_    :	Dict		   =       MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
      UpperCamelCase_    :	Dict		   =       TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



      def 		UpperCamelCase_	(    self   :       Dict    ,      A__   :       List[str]    ,      A__   :       Tuple    ,      A__   :       List[str]	)					->  str:
        _snake_case		=							ImageClassificationPipeline(model=A__    ,      image_processor=A__    ,      top_k=2	)
        _snake_case		=							[
            Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	),
            '''http://images.cocodataset.org/val2017/000000039769.jpg''',
        ]
        return image_classifier, examples



      def 		UpperCamelCase_	(    self   :       Any    ,      A__   :       List[Any]    ,      A__   :       str	)					->  Optional[Any]:
        _snake_case		=							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

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

        # Accepts URL + PIL.Image + lists
        _snake_case		=							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 		UpperCamelCase_	(    self   :       List[str]	)					->  Optional[int]:
        _snake_case		=							'''hf-internal-testing/tiny-random-vit'''
        _snake_case		=							pipeline('''image-classification'''    ,      model=A__	)

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

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



      @require_tf
      def 		UpperCamelCase_	(    self   :       Dict	)					->  Tuple:
        _snake_case		=							'''hf-internal-testing/tiny-random-vit'''
        _snake_case		=							pipeline('''image-classification'''    ,      model=A__    ,      framework='''tf'''	)

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

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



      def 		UpperCamelCase_	(    self   :       str	)					->  List[Any]:
        _snake_case		=							PreTrainedTokenizer()

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

        self.assertIs(image_classifier.tokenizer    ,      A__	)







      @slow
      @require_torch
      def 		UpperCamelCase_	(    self   :       Union[str, Any]	)					->  int:
        # 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
        _snake_case		=							pipeline('''image-classification'''    ,      model='''deepmind/vision-perceiver-conv'''	)
        _snake_case		=							image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
        self.assertEqual(
            nested_simplify(A__    ,      decimals=4	)    ,      [
                {'''score''': 0.4385, '''label''': '''tabby, tabby cat'''},
                {'''score''': 0.321, '''label''': '''tiger cat'''},
                {'''score''': 0.0502, '''label''': '''Egyptian cat'''},
                {'''score''': 0.0137, '''label''': '''crib, cot'''},
                {'''score''': 0.007, '''label''': '''radiator'''},
            ]    ,      )

        _snake_case		=							pipeline('''image-classification'''    ,      model='''deepmind/vision-perceiver-fourier'''	)
        _snake_case		=							image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
        self.assertEqual(
            nested_simplify(A__    ,      decimals=4	)    ,      [
                {'''score''': 0.5658, '''label''': '''tabby, tabby cat'''},
                {'''score''': 0.1309, '''label''': '''tiger cat'''},
                {'''score''': 0.0722, '''label''': '''Egyptian cat'''},
                {'''score''': 0.0707, '''label''': '''remote control, remote'''},
                {'''score''': 0.0082, '''label''': '''computer keyboard, keypad'''},
            ]    ,      )

        _snake_case		=							pipeline('''image-classification'''    ,      model='''deepmind/vision-perceiver-learned'''	)
        _snake_case		=							image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
        self.assertEqual(
            nested_simplify(A__    ,      decimals=4	)    ,      [
                {'''score''': 0.3022, '''label''': '''tabby, tabby cat'''},
                {'''score''': 0.2362, '''label''': '''Egyptian cat'''},
                {'''score''': 0.1856, '''label''': '''tiger cat'''},
                {'''score''': 0.0324, '''label''': '''remote control, remote'''},
                {'''score''': 0.0096, '''label''': '''quilt, comforter, comfort, puff'''},
            ]    ,      )




from __future__ import annotations



def    snake_case_(_UpperCamelCase  )							->			int | float:



  """simple docstring"""






  if len(_UpperCamelCase  ) == 0:
    raise ValueError('''find_max() arg is an empty sequence'''  )
  _snake_case		=							nums[0]
  for x in nums:
    if x > max_num:
      _snake_case		=							x
  return max_num


if __name__ == "__main__":
 import doctest

 doctest.testmod(verbose=True)

