






"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import OwlViTImageProcessor






class 						snake_case  (  unittest.TestCase       ):





    def __init__(							self	:					Optional[Any]				,		A	:					Optional[int]				,		A	:					Union[str, Any]=7				,		A	:					Optional[Any]=3				,		A	:					Union[str, Any]=1_8				,		A	:					int=3_0				,		A	:					Union[str, Any]=4_0_0				,		A	:					Tuple=True				,		A	:					List[str]=None				,		A	:					Dict=True				,		A	:					Tuple=None				,		A	:					int=True				,		A	:					str=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]				,		A	:					Union[str, Any]=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]				,		A	:					Optional[Any]=True				,		):


      '''simple docstring'''




      a     :			int				=   parent
      a     :			str				=   batch_size
      a     :			int				=   num_channels
      a     :			Union[str, Any]				=   image_size
      a     :			str				=   min_resolution
      a     :			Optional[Any]				=   max_resolution
      a     :			Tuple				=   do_resize
      a     :			int				=   size if size is not None else {'''height''': 1_8, '''width''': 1_8}
      a     :			Optional[Any]				=   do_center_crop
      a     :			Any				=   crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
      a     :			int				=   do_normalize
      a     :			str				=   image_mean
      a     :			Tuple				=   image_std
      a     :			int				=   do_convert_rgb





    def 				lowerCamelCase__      (							self	:					Union[str, Any]			):


      '''simple docstring'''




      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_convert_rgb": self.do_convert_rgb,
      }






@require_torch
@require_vision
class 						snake_case  (  __SCREAMING_SNAKE_CASE   , unittest.TestCase       ):
    __magic_name__         =     OwlViTImageProcessor if is_vision_available() else None





    def 				lowerCamelCase__      (							self	:					str			):


      '''simple docstring'''




      a     :			int				=   OwlViTImageProcessingTester(self			)





    @property
    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''simple docstring'''




      a     :			int				=   self.image_processing_class(**self.image_processor_dict			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'do_resize'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'size'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'do_center_crop'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'center_crop'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'do_normalize'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'image_mean'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'image_std'			)			)
      self.assertTrue(hasattr(UpperCamelCase__				,		'do_convert_rgb'			)			)





    def 				lowerCamelCase__      (							self	:					Optional[Any]			):


      '''simple docstring'''




      a     :			List[Any]				=   self.image_processing_class.from_dict(self.image_processor_dict			)
      self.assertEqual(image_processor.size				,		{'height': 1_8, 'width': 1_8}			)
      self.assertEqual(image_processor.crop_size				,		{'height': 1_8, 'width': 1_8}			)

      a     :			Union[str, Any]				=   self.image_processing_class.from_dict(self.image_processor_dict				,		size=4_2				,		crop_size=8_4			)
      self.assertEqual(image_processor.size				,		{'height': 4_2, 'width': 4_2}			)
      self.assertEqual(image_processor.crop_size				,		{'height': 8_4, 'width': 8_4}			)





    def 				lowerCamelCase__      (							self	:					int			):


      '''simple docstring'''




      a     :			Optional[int]				=   self.image_processing_class(**self.image_processor_dict			)
      # create random PIL images
      a     :			Tuple				=   prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__			)

      for image in image_inputs:
        self.assertIsInstance(UpperCamelCase__				,		Image.Image			)

      # Test not batched input
      a     :			Tuple				=   image_processing(image_inputs[0]				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)

      # Test batched
      a     :			Optional[int]				=   image_processing(UpperCamelCase__				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)





    def 				lowerCamelCase__      (							self	:					str			):


      '''simple docstring'''




      a     :			Tuple				=   self.image_processing_class(**self.image_processor_dict			)
      # create random numpy tensors
      a     :			Tuple				=   prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__				,		numpify=UpperCamelCase__			)
      for image in image_inputs:
        self.assertIsInstance(UpperCamelCase__				,		np.ndarray			)

      # Test not batched input
      a     :			List[str]				=   image_processing(image_inputs[0]				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)

      # Test batched
      a     :			Any				=   image_processing(UpperCamelCase__				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)





    def 				lowerCamelCase__      (							self	:					Any			):


      '''simple docstring'''




      a     :			List[Any]				=   self.image_processing_class(**self.image_processor_dict			)
      # create random PyTorch tensors
      a     :			List[Any]				=   prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__				,		torchify=UpperCamelCase__			)
      for image in image_inputs:
        self.assertIsInstance(UpperCamelCase__				,		torch.Tensor			)

      # Test not batched input
      a     :			List[Any]				=   image_processing(image_inputs[0]				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)

      # Test batched
      a     :			Any				=   image_processing(UpperCamelCase__				,		return_tensors='pt'			).pixel_values
      self.assertEqual(
          encoded_images.shape				,		(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )				,		)






'''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      snake_case__    (	unittest.TestCase ):







		"""simple docstring"""

		lowerCamelCase							=		EsmTokenizer



		def 	lowerCAmelCase							(	self							:       List[Any]	)		->       Any:






				"""simple docstring"""







				super().setUp()
				snake_case   :    Optional[int]			       =		tempfile.mkdtemp()
				# fmt: off
				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
				snake_case   :    List[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 	lowerCAmelCase							(	self							:       str   ,     **UpperCamelCase__							:       Union[str, Any]	)		->       List[PreTrainedTokenizerBase]:






				"""simple docstring"""







				return [self.get_tokenizer(**UpperCamelCase__	)]



		def 	lowerCAmelCase							(	self							:       Union[str, Any]   ,     **UpperCamelCase__							:       Tuple	)		->       PreTrainedTokenizer:






				"""simple docstring"""







				return self.tokenizer_class.from_pretrained(self.tmpdirname   ,     **UpperCamelCase__	)



		def 	lowerCAmelCase							(	self							:       List[str]	)		->       Tuple:






				"""simple docstring"""







				snake_case   :    str			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    Optional[int]			       =		tokenizer.tokenize('''LAGVS'''	)
				self.assertListEqual(UpperCamelCase__   ,     ['''L''', '''A''', '''G''', '''V''', '''S''']	)
				self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__	)   ,     [4, 5, 6, 7, 8]	)



		def 	lowerCAmelCase							(	self							:       List[Any]	)		->       int:






				"""simple docstring"""







				snake_case   :    Dict			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    List[Any]			       =		'''LAGVS'''
				self.assertListEqual(tokenizer.encode(UpperCamelCase__	)   ,     [0, 4, 5, 6, 7, 8, 2]	)



		def 	lowerCAmelCase							(	self							:       List[str]	)		->       int:






				"""simple docstring"""







				snake_case   :    Union[str, Any]			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    int			       =		['''LAGVS''', '''WCB''']
				snake_case   :    List[str]			       =		tokenizer(UpperCamelCase__   ,     padding=UpperCamelCase__	)['''input_ids''']

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



		def 	lowerCAmelCase							(	self							:       int	)		->       List[str]:






				"""simple docstring"""







				snake_case   :    Dict			       =		self.tokenizer_class(self.vocab_file	)

				snake_case   :    List[str]			       =		['''LAGVS''', '''WCB''']
				snake_case   :    Any			       =		tokenizer(UpperCamelCase__   ,     padding=UpperCamelCase__	)['''input_ids''']

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



		def 	lowerCAmelCase							(	self							:       Any	)		->       int:






				"""simple docstring"""







				snake_case   :    List[str]			       =		self.get_tokenizers(fast=UpperCamelCase__	)
				for tokenizer in tokenizers:
						with self.subTest(f'{tokenizer.__class__.__name__}'	):
								snake_case   :    int			       =		'''<unk>'''
								snake_case   :    int			       =		'''<mask>'''

								snake_case   :    Any			       =		tokenizer.tokenize(UpperCamelCase__	)
								snake_case   :    Any			       =		tokenizer.tokenize(UpperCamelCase__	)

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


