





'''simple docstring'''




import os
from typing import List, Optional, Union

from ...tokenization_utils import PreTrainedTokenizer
from ...tokenization_utils_base import AddedToken
from ...utils import logging


_A:       List[str]						    =					logging.get_logger(__name__)

_A:       Optional[Any]						    =					{"""vocab_file""": """vocab.txt"""}

_A:       List[Any]						    =					{
    """vocab_file""": {
        """facebook/esm2_t6_8M_UR50D""": """https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt""",
        """facebook/esm2_t12_35M_UR50D""": """https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt""",
    },
}

_A:       List[Any]						    =					{
    """facebook/esm2_t6_8M_UR50D""": 1_024,
    """facebook/esm2_t12_35M_UR50D""": 1_024,
}







def _lowerCAmelCase  (   _lowerCAmelCase     )->  Optional[Any]:
 with open(__UpperCamelCase							,     'r'     ) as f:
  __UpperCAmelCase       = f.read().splitlines()
  return [l.strip() for l in lines]






class UpperCAmelCase				(		UpperCAmelCase_				):
    _A			:      List[str]     =							VOCAB_FILES_NAMES
    _A			:      Dict     =							PRETRAINED_VOCAB_FILES_MAP
    _A			:      Tuple     =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    _A			:      Optional[int]     =							["input_ids", "attention_mask"]



    def __init__(	self  ,      __A  ,      __A="<unk>"  ,      __A="<cls>"  ,      __A="<pad>"  ,      __A="<mask>"  ,      __A="<eos>"  ,      **__A  ,      ):
     super().__init__(**_snake_case       )
     __UpperCAmelCase       = load_vocab_file(_snake_case       )
     __UpperCAmelCase       = dict(enumerate(self.all_tokens       )       )
     __UpperCAmelCase       = {tok: ind for ind, tok in enumerate(self.all_tokens       )}
     __UpperCAmelCase       = unk_token
     __UpperCAmelCase       = cls_token
     __UpperCAmelCase       = pad_token
     __UpperCAmelCase       = mask_token
     __UpperCAmelCase       = eos_token
     __UpperCAmelCase       = self.all_tokens
     self._create_trie(self.unique_no_split_tokens       )



    def        __lowerCamelCase    (	self  ,      __A       ):
     return self._id_to_token.get(_snake_case  ,      self.unk_token       )



    def        __lowerCamelCase    (	self  ,      __A       ):
     return self._token_to_id.get(_snake_case  ,      self._token_to_id.get(self.unk_token       )       )



    def        __lowerCamelCase    (	self  ,      __A  ,      **__A       ):
     return text.split()



    def        __lowerCamelCase    (	self  ,      __A=False       ):
     return len(self._id_to_token       )



    def        __lowerCamelCase    (	self       ):
     return {token: i for i, token in enumerate(self.all_tokens       )}



    def        __lowerCamelCase    (	self  ,      __A       ):
     return self._token_to_id.get(_snake_case  ,      self._token_to_id.get(self.unk_token       )       )



    def        __lowerCamelCase    (	self  ,      __A       ):
     return self._id_to_token.get(_snake_case  ,      self.unk_token       )



    def        __lowerCamelCase    (	self  ,      __A  ,      __A = None       ):
     __UpperCAmelCase       = [self.cls_token_id]
     __UpperCAmelCase       = [self.eos_token_id]  # No sep token in ESM vocabulary
     if token_ids_a is None:
      if self.eos_token_id is None:
       return cls + token_ids_a
      else:
       return cls + token_ids_a + sep
     elif self.eos_token_id is None:
      raise ValueError('Cannot tokenize multiple sequences when EOS token is not set!'       )
     return cls + token_ids_a + sep + token_ids_a + sep  # Multiple inputs always have an EOS token



    def        __lowerCamelCase    (	self  ,      __A  ,      __A = None  ,      __A = False       ):


     if already_has_special_tokens:
      if token_ids_a is not None:
       raise ValueError(
           'You should not supply a second sequence if the provided sequence of '
           'ids is already formatted with special tokens for the model.'       )

      return [1 if token in self.all_special_ids else 0 for token in token_ids_a]
     __UpperCAmelCase       = [1] + ([0] * len(_snake_case       )) + [1]
     if token_ids_a is not None:
      mask += [0] * len(_snake_case       ) + [1]
     return mask



    def        __lowerCamelCase    (	self  ,      __A  ,      __A       ):
     __UpperCAmelCase       = os.path.join(_snake_case  ,      (filename_prefix + '-' if filename_prefix else '') + 'vocab.txt'       )
     with open(_snake_case  ,      'w'       ) as f:
      f.write('\n'.join(self.all_tokens       )       )
     return (vocab_file,)



    @property
    def        __lowerCamelCase    (	self       ):
     return self.get_vocab_size(with_added_tokens=_snake_case       )




    def        __lowerCamelCase    (	self  ,      __A  ,      __A = False       ):
     return super()._add_tokens(_snake_case  ,      special_tokens=_snake_case       )










import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
							from PIL import Image
else:


							class   __lowerCAmelCase  :
												"""simple docstring"""






												@staticmethod
												def      _a				(  *_snake_case       :							int					,       **_snake_case       :							List[str]       ):






															"""simple docstring"""


															pass


@is_pipeline_test
@require_vision
@require_torch
class   __lowerCAmelCase  (		unittest.TestCase   ):
					"""simple docstring"""






					A__					:		List[str]      							=     MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



					def      _a				(  self       :							Any					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     pipeline(
								    'zero-shot-object-detection'					,       model='hf-internal-testing/tiny-random-owlvit-object-detection'       )

								A__	  =     [
								    {
								        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								        'candidate_labels': ['cat', 'remote', 'couch'],
								    }
								]
								return object_detector, examples



					def      _a				(  self       :							int					,       _snake_case       :							int					,       _snake_case       :							List[str]       ):






								"""simple docstring"""


								A__	  =     object_detector(examples[0]					,       threshold=0.0       )

								A__	  =     len(_snake_case       )
								self.assertGreater(_snake_case					,       0       )
								self.assertEqual(
								    _snake_case					,       [
								        {
								            'score': ANY(_snake_case       ),
								            'label': ANY(_snake_case       ),
								            'box': {'xmin': ANY(_snake_case       ), 'ymin': ANY(_snake_case       ), 'xmax': ANY(_snake_case       ), 'ymax': ANY(_snake_case       )},
								        }
								        for i in range(_snake_case       )
								    ]					,       )



					@require_tf
					@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								pass



					@require_torch
					def      _a				(  self       :							Optional[int]       ):






								"""simple docstring"""


								A__	  =     pipeline(
								    'zero-shot-object-detection'					,       model='hf-internal-testing/tiny-random-owlvit-object-detection'       )

								A__	  =     object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'					,       candidate_labels=['cat', 'remote', 'couch']					,       threshold=0.64					,       )

								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,       )

								A__	  =     object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]					,       threshold=0.64					,       )

								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        [
								            {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,       )



					@require_torch
					@slow
					def      _a				(  self       :							int       ):






								"""simple docstring"""


								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,       )

								A__	  =     object_detector(
								    [
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								    ]					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,       )



					@require_tf
					@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
					def      _a				(  self       :							int       ):






								"""simple docstring"""


								pass



					@require_torch
					@slow
					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__	  =     0.2
								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       threshold=_snake_case					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,       )



					@require_torch
					@slow
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     2
								A__	  =     pipeline('zero-shot-object-detection'       )

								A__	  =     object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,       candidate_labels=['cat', 'remote', 'couch']					,       top_k=_snake_case					,       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [
								        {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,       )




