


'''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__  :





													'''simple docstring'''


													@staticmethod
													def UpperCAmelCase_     (   *__snake_case   , **__snake_case  ):
																	pass





@is_pipeline_test
@require_torch_or_tf
@require_vision
class    lowercase__  (						unittest.TestCase ):





							'''simple docstring'''


							A_							:				Dict				     =  MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							A_							:				List[str]				     =  TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				ImageClassificationPipeline(model=__snake_case   , image_processor=__snake_case   , top_k=2  )
											_SCREAMING_SNAKE_CASE			:	str							=				[
											    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  ),
											    """http://images.cocodataset.org/val2017/000000039769.jpg""",
											]
											return image_classifier, examples






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	str							=				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

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

											# Accepts URL + PIL.Image + lists
											_SCREAMING_SNAKE_CASE			:	Union[str, 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 UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	int							=				"""hf-internal-testing/tiny-random-vit"""
											_SCREAMING_SNAKE_CASE			:	Dict							=				pipeline("""image-classification"""   , model=__snake_case  )

											_SCREAMING_SNAKE_CASE			:	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}]   , )

											_SCREAMING_SNAKE_CASE			:	Dict							=				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 UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Dict							=				"""hf-internal-testing/tiny-random-vit"""
											_SCREAMING_SNAKE_CASE			:	str							=				pipeline("""image-classification"""   , model=__snake_case   , framework="""tf"""  )

											_SCREAMING_SNAKE_CASE			:	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}]   , )

											_SCREAMING_SNAKE_CASE			:	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 UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				PreTrainedTokenizer()

											# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
											_SCREAMING_SNAKE_CASE			:	List[str]							=				pipeline(
											    """image-classification"""   , model="""hf-internal-testing/tiny-random-vit"""   , tokenizer=__snake_case  )

											self.assertIs(image_classifier.tokenizer   , __snake_case  )






							@slow
							@require_torch
							def UpperCAmelCase_     (   self  ):
											# 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
											_SCREAMING_SNAKE_CASE			:	int							=				pipeline("""image-classification"""   , model="""deepmind/vision-perceiver-conv"""  )
											_SCREAMING_SNAKE_CASE			:	Any							=				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"""},
											    ]   , )

											_SCREAMING_SNAKE_CASE			:	str							=				pipeline("""image-classification"""   , model="""deepmind/vision-perceiver-fourier"""  )
											_SCREAMING_SNAKE_CASE			:	str							=				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"""},
											    ]   , )

											_SCREAMING_SNAKE_CASE			:	List[str]							=				pipeline("""image-classification"""   , model="""deepmind/vision-perceiver-learned"""  )
											_SCREAMING_SNAKE_CASE			:	int							=				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"""},
											    ]   , )








'''simple docstring'''


import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase





@require_tokenizers
class    lowercase__  (						unittest.TestCase ):





							'''simple docstring'''


							A_							:				Optional[Any]				     =  EsmTokenizer






							def UpperCAmelCase_     (   self  ):
											super().setUp()
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				tempfile.mkdtemp()
											# fmt: off
											_SCREAMING_SNAKE_CASE			:	List[str]							=				["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
											# fmt: on
											_SCREAMING_SNAKE_CASE			:	Any							=				os.path.join(self.tmpdirname   , VOCAB_FILES_NAMES["""vocab_file"""]  )
											with open(self.vocab_file   , """w"""   , encoding="""utf-8"""  ) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]  )  )






							def UpperCAmelCase_     (   self   , **__snake_case  ):
											return [self.get_tokenizer(**__snake_case  )]






							def UpperCAmelCase_     (   self   , **__snake_case  ):
											return self.tokenizer_class.from_pretrained(self.tmpdirname   , **__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	int							=				tokenizer.tokenize("""LAGVS"""  )
											self.assertListEqual(__snake_case   , ["""L""", """A""", """G""", """V""", """S"""]  )
											self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case  )   , [4, 5, 6, 7, 8]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	int							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				"""LAGVS"""
											self.assertListEqual(tokenizer.encode(__snake_case  )   , [0, 4, 5, 6, 7, 8, 2]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Any							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	List[Any]							=				["""LAGVS""", """WCB"""]
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer(__snake_case   , padding=__snake_case  )["""input_ids"""]

											self.assertListEqual(__snake_case   , [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				self.tokenizer_class(self.vocab_file  )

											_SCREAMING_SNAKE_CASE			:	str							=				["""LAGVS""", """WCB"""]
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				tokenizer(__snake_case   , padding=__snake_case  )["""input_ids"""]

											self.assertListEqual(__snake_case   , [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				self.get_tokenizers(fast=__snake_case  )
											for tokenizer in tokenizers:
															with self.subTest(f"""{tokenizer.__class__.__name__}"""  ):
																			_SCREAMING_SNAKE_CASE			:	Optional[int]							=				"""<unk>"""
																			_SCREAMING_SNAKE_CASE			:	Optional[int]							=				"""<mask>"""

																			_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer.tokenize(__snake_case  )
																			_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				tokenizer.tokenize(__snake_case  )

																			self.assertEqual(len(__snake_case  )   , 1  )
																			self.assertEqual(len(__snake_case  )   , 1  )
																			self.assertEqual(token_a[0]   , __snake_case  )
																			self.assertEqual(token_a[0]   , __snake_case  )




