
'''simple docstring'''





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 					_SCREAMING_SNAKE_CASE  :
           @staticmethod
           def A_			(			*lowerCamelCase		,	**lowerCamelCase ):
               pass




@is_pipeline_test
@require_vision
@require_torch
class 					_SCREAMING_SNAKE_CASE  (      unittest.TestCase ):
      _A						:		List[str]    	=     MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				pipeline(
              "zero-shot-object-detection"		,	model="hf-internal-testing/tiny-random-owlvit-object-detection" )

          snake_case__						=				[
              {
                  "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                  "candidate_labels": ["cat", "remote", "couch"],
              }
          ]
          return object_detector, examples
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase ):
          snake_case__						=				object_detector(examples[0]		,	threshold=0.0 )

          snake_case__						=				len(A_ )
          self.assertGreater(A_		,	0 )
          self.assertEqual(
              A_		,	[
                  {
                      "score": ANY(A_ ),
                      "label": ANY(A_ ),
                      "box": {"xmin": ANY(A_ ), "ymin": ANY(A_ ), "xmax": ANY(A_ ), "ymax": ANY(A_ )},
                  }
                  for i in range(A_ )
              ]		,	)
      @require_tf
      @unittest.skip("Zero Shot Object Detection not implemented in TF" )
      def A_			(			self ):
          pass
      @require_torch
      def A_			(			self ):
          snake_case__						=				pipeline(
              "zero-shot-object-detection"		,	model="hf-internal-testing/tiny-random-owlvit-object-detection" )

          snake_case__						=				object_detector(
              "./tests/fixtures/tests_samples/COCO/000000039769.png"		,	candidate_labels=["cat", "remote", "couch"]		,	threshold=0.6_4		,	)

          self.assertEqual(
              nested_simplify(A_		,	decimals=4 )		,	[
                  {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                  {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
                  {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
              ]		,	)

          snake_case__						=				object_detector(
              [
                  {
                      "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                      "candidate_labels": ["cat", "remote", "couch"],
                  }
              ]		,	threshold=0.6_4		,	)

          self.assertEqual(
              nested_simplify(A_		,	decimals=4 )		,	[
                  [
                      {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                      {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                      {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                      {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                      {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                      {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                      {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                      {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
                      {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                  ]
              ]		,	)
      @require_torch
      @slow
      def A_			(			self ):
          snake_case__						=				pipeline("zero-shot-object-detection" )

          snake_case__						=				object_detector(
              "http://images.cocodataset.org/val2017/000000039769.jpg"		,	candidate_labels=["cat", "remote", "couch"]		,	)
          self.assertEqual(
              nested_simplify(A_		,	decimals=4 )		,	[
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
              ]		,	)

          snake_case__						=				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(A_		,	decimals=4 )		,	[
                  [
                      {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                      {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                      {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                      {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                      {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
                  ],
                  [
                      {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                      {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                      {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                      {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                      {"score": 0.1_2_0_8, "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 ):
          pass
      @require_torch
      @slow
      def A_			(			self ):
          snake_case__						=				0.2
          snake_case__						=				pipeline("zero-shot-object-detection" )

          snake_case__						=				object_detector(
              "http://images.cocodataset.org/val2017/000000039769.jpg"		,	candidate_labels=["cat", "remote", "couch"]		,	threshold=A_		,	)
          self.assertEqual(
              nested_simplify(A_		,	decimals=4 )		,	[
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
              ]		,	)



      @require_torch
      @slow
      def A_			(			self ):
          snake_case__						=				2
          snake_case__						=				pipeline("zero-shot-object-detection" )

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





import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
     from .tokenization_pegasus import PegasusTokenizer
else:
     __magic_name__            =  None


__magic_name__            =  logging.get_logger(__name__)


__magic_name__            =  '''▁'''

__magic_name__            =  {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''}

__magic_name__            =  {
    '''vocab_file''': {'''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'''},
    '''tokenizer_file''': {
        '''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json'''
    },
}

__magic_name__            =  {
    '''google/pegasus-xsum''': 512,
}




class 					_SCREAMING_SNAKE_CASE  (      __UpperCamelCase ):
      _A						:		int    	=     VOCAB_FILES_NAMES
      _A						:		Dict    	=     PRETRAINED_VOCAB_FILES_MAP
      _A						:		Any    	=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      _A						:		Tuple    	=     PegasusTokenizer
      _A						:		str    	=     ['input_ids', 'attention_mask']
      def __init__(			self		,	lowerCamelCase=None		,	lowerCamelCase=None		,	lowerCamelCase="<pad>"		,	lowerCamelCase="</s>"		,	lowerCamelCase="<unk>"		,	lowerCamelCase="<mask_2>"		,	lowerCamelCase="<mask_1>"		,	lowerCamelCase=None		,	lowerCamelCase=1_03		,	**lowerCamelCase		,	):
          snake_case__						=				offset

          if additional_special_tokens is not None:
              if not isinstance(lowerCamelCase		,	lowerCamelCase ):
                  raise TypeError(
                      F"""additional_special_tokens should be of type {type(lowerCamelCase )}, but is"""
                      F""" {type(lowerCamelCase )}""" )

              snake_case__						=				(
                  ([mask_token_sent] + additional_special_tokens)
                  if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
                  else additional_special_tokens
              )
              # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
              additional_special_tokens_extended += [
                  F"""<unk_{i}>""" for i in range(len(lowerCamelCase )		,	self.offset - 1 )
              ]

              if len(set(lowerCamelCase ) ) != len(lowerCamelCase ):
                  raise ValueError(
                      "Please make sure that the provided additional_special_tokens do not contain an incorrectly"
                      F""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.""" )
              snake_case__						=				additional_special_tokens_extended
          else:
              snake_case__						=				[mask_token_sent] if mask_token_sent is not None else []
              additional_special_tokens += [F"""<unk_{i}>""" for i in range(2		,	self.offset )]

          super().__init__(
              lowerCamelCase		,	tokenizer_file=lowerCamelCase		,	pad_token=lowerCamelCase		,	eos_token=lowerCamelCase		,	unk_token=lowerCamelCase		,	mask_token=lowerCamelCase		,	mask_token_sent=lowerCamelCase		,	offset=lowerCamelCase		,	additional_special_tokens=lowerCamelCase		,	**lowerCamelCase		,	)
          snake_case__						=				vocab_file
          snake_case__						=				False if not self.vocab_file else True
      def A_			(			self		,	lowerCamelCase ):
          snake_case__						=				set(self.all_special_ids )  # call it once instead of inside list comp
          all_special_ids.remove(self.unk_token_id )  # <unk> is only sometimes special

          if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ):
              raise ValueError(
                  "There should be 3 special tokens: mask_token, pad_token, and eos_token +"
                  F""" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}""" )

          return [1 if x in all_special_ids else 0 for x in seq]
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase = None		,	lowerCamelCase = False ):
          if already_has_special_tokens:
              return self._special_token_mask(lowerCamelCase )
          elif token_ids_a is None:
              return self._special_token_mask(lowerCamelCase ) + [1]
          else:
              return self._special_token_mask(token_ids_a + token_ids_a ) + [1]
      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase=None ):
          if token_ids_a is None:
              return token_ids_a + [self.eos_token_id]
          # We don't expect to process pairs, but leave the pair logic for API consistency
          return token_ids_a + token_ids_a + [self.eos_token_id]



      def A_			(			self		,	lowerCamelCase		,	lowerCamelCase = None ):
          if not self.can_save_slow_tokenizer:
              raise ValueError(
                  "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
                  "tokenizer." )

          if not os.path.isdir(lowerCamelCase ):
              logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" )
              return
          snake_case__						=				os.path.join(
              lowerCamelCase		,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] )

          if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase ):
              copyfile(self.vocab_file		,	lowerCamelCase )

          return (out_vocab_file,)



