






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






           @staticmethod
           def       lowerCamelCase__     (     *a_  ,  **a_						)						->Union[str, Any]:







             '''simple docstring'''


             pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class  lowercase_     (unittest.TestCase  ):
      A__						:		Union[str, Any]							      =					MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
      A__						:		Dict							      =					TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






      def       lowerCamelCase__     (     self  ,  a_  ,  a_  ,  a_						)						->List[Any]:







        '''simple docstring'''


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






      def       lowerCamelCase__     (     self  ,  a_  ,  a_						)						->Tuple:







        '''simple docstring'''


        _a					      =						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

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

        # Accepts URL + PIL.Image + lists
        _a					      =						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       lowerCamelCase__     (     self						)						->Tuple:







        '''simple docstring'''


        _a					      =						"hf-internal-testing/tiny-random-vit"
        _a					      =						pipeline("image-classification"  ,  model=a_						)

        _a					      =						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}]  ,  )

        _a					      =						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       lowerCamelCase__     (     self						)						->str:







        '''simple docstring'''


        _a					      =						"hf-internal-testing/tiny-random-vit"
        _a					      =						pipeline("image-classification"  ,  model=a_  ,  framework="tf"						)

        _a					      =						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}]  ,  )

        _a					      =						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       lowerCamelCase__     (     self						)						->Any:







        '''simple docstring'''


        _a					      =						PreTrainedTokenizer()

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

        self.assertIs(image_classifier.tokenizer  ,  a_						)






      @slow
      @require_torch
      def       lowerCamelCase__     (     self						)						->Any:







        '''simple docstring'''


        _a					      =						pipeline("image-classification"  ,  model="deepmind/vision-perceiver-conv"						)
        _a					      =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
        self.assertEqual(
            nested_simplify(a_  ,  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					      =						pipeline("image-classification"  ,  model="deepmind/vision-perceiver-fourier"						)
        _a					      =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
        self.assertEqual(
            nested_simplify(a_  ,  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					      =						pipeline("image-classification"  ,  model="deepmind/vision-perceiver-learned"						)
        _a					      =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
        self.assertEqual(
            nested_simplify(a_  ,  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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase											=     {"""configuration_pegasus""": ["""PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusConfig"""]}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizer"""]

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizerFast"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """PegasusForCausalLM""",
         """PegasusForConditionalGeneration""",
         """PegasusModel""",
         """PegasusPreTrainedModel""",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """TFPegasusForConditionalGeneration""",
         """TFPegasusModel""",
         """TFPegasusPreTrainedModel""",
     ]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """FlaxPegasusForConditionalGeneration""",
         """FlaxPegasusModel""",
         """FlaxPegasusPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_pegasus import PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus import PegasusTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus_fast import PegasusTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_pegasus import (
              PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST,
              PegasusForCausalLM,
              PegasusForConditionalGeneration,
              PegasusModel,
              PegasusPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_pegasus import TFPegasusForConditionalGeneration, TFPegasusModel, TFPegasusPreTrainedModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_pegasus import (
              FlaxPegasusForConditionalGeneration,
              FlaxPegasusModel,
              FlaxPegasusPreTrainedModel,
          )

else:
     import sys

     UpperCamelCase											=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


