

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





									@staticmethod
									def 	__magic_name__					(	*__snake_case      ,						**__snake_case      )     ->				List[str]:




												'''simple docstring'''
												pass






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





			def 	__magic_name__					(	self      ,						__snake_case      ,						__snake_case      ,						__snake_case      )     ->				Dict:




						'''simple docstring'''
						__a						   =ImageClassificationPipeline(model=__snake_case      ,						image_processor=__snake_case      ,						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 	__magic_name__					(	self      ,						__snake_case      ,						__snake_case      )     ->				Dict:




						'''simple docstring'''
						__a						   =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						   =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(
						    __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 	__magic_name__					(	self      )     ->				Tuple:




						'''simple docstring'''
						__a						   ="hf-internal-testing/tiny-random-vit"
						__a						   =pipeline('image-classification'      ,						model=__snake_case      )

						__a						   =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						   =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 	__magic_name__					(	self      )     ->				List[str]:




						'''simple docstring'''
						__a						   ="hf-internal-testing/tiny-random-vit"
						__a						   =pipeline('image-classification'      ,						model=__snake_case      ,						framework='tf'      )

						__a						   =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						   =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 	__magic_name__					(	self      )     ->				List[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=__snake_case      )

						self.assertIs(image_classifier.tokenizer      ,						__snake_case      )





			@slow
			@require_torch
			def 	__magic_name__					(	self      )     ->				int:




						'''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(__snake_case      ,						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'},
						    ]      ,						)

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

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

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


_lowerCAmelCase      :    Union[str, Any]						       =    {
    "configuration_ernie_m": ["ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieMConfig"],
}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCAmelCase      :    str						       =    ["ErnieMTokenizer"]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCAmelCase      :    Optional[Any]						       =    [
						    "ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "ErnieMForMultipleChoice",
						    "ErnieMForQuestionAnswering",
						    "ErnieMForSequenceClassification",
						    "ErnieMForTokenClassification",
						    "ErnieMModel",
						    "ErnieMPreTrainedModel",
						    "ErnieMForInformationExtraction",
						]


if TYPE_CHECKING:
						from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_ernie_m import ErnieMTokenizer

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_ernie_m import (
												    ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
												    ErnieMForInformationExtraction,
												    ErnieMForMultipleChoice,
												    ErnieMForQuestionAnswering,
												    ErnieMForSequenceClassification,
												    ErnieMForTokenClassification,
												    ErnieMModel,
												    ErnieMPreTrainedModel,
												)


else:
						import sys

						_lowerCAmelCase      :    Tuple						       =    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






