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






						'''simple docstring'''







						@staticmethod
						def    _UpperCamelCase							(  *__magic_name__ :Any       ,    **__magic_name__ :List[Any]			)			->   Dict:

								'''simple docstring'''






								pass
@is_pipeline_test
@require_torch_or_tf
@require_vision
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	snake_case__							:		int         =		MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
	snake_case__							:		str         =		TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING


	def    _UpperCamelCase							(  self :Dict       ,    __magic_name__ :Optional[int]       ,    __magic_name__ :Union[str, Any]       ,    __magic_name__ :Optional[int]			)			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     ImageClassificationPipeline(model=lowerCAmelCase__       ,    image_processor=lowerCAmelCase__       ,    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    _UpperCamelCase							(  self :List[Any]       ,    __magic_name__ :Union[str, Any]       ,    __magic_name__ :str			)			->   int:

			'''simple docstring'''






			a__   		=     image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)

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

			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(
			    lowerCAmelCase__       ,    [
			        [
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			        ],
			        [
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			        ],
			        [
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			        ],
			        [
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			        ],
			        [
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			            {'''score''': ANY(lowerCAmelCase__			), '''label''': ANY(lowerCAmelCase__			)},
			        ],
			    ]       ,    )


	@require_torch
	def    _UpperCamelCase							(  self :str			)			->   int:

			'''simple docstring'''






			a__   		=     'hf-internal-testing/tiny-random-vit'
			a__   		=     pipeline('''image-classification'''       ,    model=lowerCAmelCase__			)

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

			'''simple docstring'''






			a__   		=     'hf-internal-testing/tiny-random-vit'
			a__   		=     pipeline('''image-classification'''       ,    model=lowerCAmelCase__       ,    framework='''tf'''			)

			a__   		=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
			self.assertEqual(
			    nested_simplify(lowerCAmelCase__       ,    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(lowerCAmelCase__       ,    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 :Tuple			)			->   str:

			'''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=lowerCAmelCase__			)

			self.assertIs(image_classifier.tokenizer       ,    lowerCAmelCase__			)






	@slow
	@require_torch
	def    _UpperCamelCase							(  self :Optional[Any]			)			->   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(lowerCAmelCase__       ,    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(lowerCAmelCase__       ,    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(lowerCAmelCase__       ,    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'''},
			    ]       ,    )








from typing import TYPE_CHECKING

from ...utils import _LazyModule


__lowerCAmelCase									=				{'''tokenization_phobert''': ['''PhobertTokenizer''']}


if TYPE_CHECKING:
 from .tokenization_phobert import PhobertTokenizer

else:
 import sys

 __lowerCAmelCase									=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


