


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





									'''simple docstring'''






									@staticmethod
									def     __SCREAMING_SNAKE_CASE				(						*lowerCamelCase_   ,       **lowerCamelCase_  )			->				Union[str, Any]:
																pass




@is_pipeline_test
@require_vision
@require_torch
class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  Optional[Any]      		= MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[Any]:
														lowerCAmelCase__			  =      pipeline(
														    '''zero-shot-object-detection'''   ,       model='''hf-internal-testing/tiny-random-owlvit-object-detection'''  )

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[str]:
														lowerCAmelCase__			  =      object_detector(examples[0]   ,       threshold=0.0  )

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







							@require_tf
							@unittest.skip('''Zero Shot Object Detection not implemented in TF'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														pass







							@require_torch
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														lowerCAmelCase__			  =      pipeline(
														    '''zero-shot-object-detection'''   ,       model='''hf-internal-testing/tiny-random-owlvit-object-detection'''  )

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

														self.assertEqual(
														    nested_simplify(lowerCamelCase_   ,       decimals=4  )   ,       [
														        {'''score''': 0.7_235, '''label''': '''cat''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_456, '''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.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														    ]   ,       )

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

														self.assertEqual(
														    nested_simplify(lowerCamelCase_   ,       decimals=4  )   ,       [
														        [
														            {'''score''': 0.7_235, '''label''': '''cat''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_456, '''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.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														        ]
														    ]   ,       )







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      object_detector(
														    '''http://images.cocodataset.org/val2017/000000039769.jpg'''   ,       candidate_labels=['''cat''', '''remote''', '''couch''']   ,       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_   ,       decimals=4  )   ,       [
														        {'''score''': 0.2_868, '''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.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														        {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														        {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
														    ]   ,       )

														lowerCAmelCase__			  =      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(lowerCamelCase_   ,       decimals=4  )   ,       [
														        [
														            {'''score''': 0.2_868, '''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.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														            {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														            {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
														        ],
														        [
														            {'''score''': 0.2_868, '''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.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														            {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														            {'''score''': 0.1_208, '''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     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														pass







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      0.2
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      object_detector(
														    '''http://images.cocodataset.org/val2017/000000039769.jpg'''   ,       candidate_labels=['''cat''', '''remote''', '''couch''']   ,       threshold=lowerCamelCase_   ,       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_   ,       decimals=4  )   ,       [
														        {'''score''': 0.2_868, '''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.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														    ]   ,       )







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      2
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      object_detector(
														    '''http://images.cocodataset.org/val2017/000000039769.jpg'''   ,       candidate_labels=['''cat''', '''remote''', '''couch''']   ,       top_k=lowerCamelCase_   ,       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_   ,       decimals=4  )   ,       [
														        {'''score''': 0.2_868, '''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}},
														    ]   ,       )






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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


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

_UpperCAmelCase							:			List[str]        =							{"""vocab_file""": """spiece.model"""}

_UpperCAmelCase							:			Tuple        =							{
    """vocab_file""": {
        """bert_for_seq_generation""": (
            """https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model"""
        ),
    }
}

_UpperCAmelCase							:			List[str]        =							{"""bert_for_seq_generation""": 5_12}

class   lowerCAmelCase				(   __UpperCamelCase     ):
 UpperCAmelCase__						=						VOCAB_FILES_NAMES
 UpperCAmelCase__						=						PRETRAINED_VOCAB_FILES_MAP
 UpperCAmelCase__						=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 UpperCAmelCase__						=						[]
 UpperCAmelCase__						=						["""input_ids""", """attention_mask"""]
 def __init__( self    : Union[str, Any]     ,	UpperCAmelCase    : List[Any]     ,	UpperCAmelCase    : int="<s>"     ,	UpperCAmelCase    : Any="</s>"     ,	UpperCAmelCase    : List[Any]="<unk>"     ,	UpperCAmelCase    : Any="<pad>"     ,	UpperCAmelCase    : Dict="<::::>"     ,	UpperCAmelCase    : Optional[Dict[str, Any]] = None     ,	**UpperCAmelCase    : Any     ,	)     ->      None:
     lowerCamelCase__       : str   					=						{} if sp_model_kwargs is None else sp_model_kwargs

     # Add extra_ids to the special token list
     super().__init__(
         bos_token=UpperCAmelCase     ,	eos_token=UpperCAmelCase     ,	unk_token=UpperCAmelCase     ,	pad_token=UpperCAmelCase     ,	sep_token=UpperCAmelCase     ,	sp_model_kwargs=self.sp_model_kwargs     ,	**UpperCAmelCase     ,	)

     lowerCamelCase__       : List[str]   					=						vocab_file

     lowerCamelCase__       : List[str]   					=						spm.SentencePieceProcessor(**self.sp_model_kwargs      )
     self.sp_model.Load(UpperCAmelCase      )
 @property
 def 	A_      ( self    : List[str]      )     ->      Optional[int]:
     return self.sp_model.get_piece_size()
 def 	A_      ( self    : Tuple      )     ->      Any:
     lowerCamelCase__       : Tuple   					=						{self.convert_ids_to_tokens(UpperCAmelCase      ): i for i in range(self.vocab_size      )}
     vocab.update(self.added_tokens_encoder      )
     return vocab
 def __getstate__( self    : Union[str, Any]      )     ->      Optional[Any]:
     lowerCamelCase__       : List[str]   					=						self.__dict__.copy()
     lowerCamelCase__       : List[Any]   					=						None
     return state
 def __setstate__( self    : Dict     ,	UpperCAmelCase    : Optional[int]      )     ->      List[Any]:
     lowerCamelCase__       : Optional[int]   					=						d

     # for backward compatibility
     if not hasattr(self     ,	'sp_model_kwargs'      ):
         lowerCamelCase__       : Dict   					=						{}

     lowerCamelCase__       : Optional[int]   					=						spm.SentencePieceProcessor(**self.sp_model_kwargs      )
     self.sp_model.Load(self.vocab_file      )
 def 	A_      ( self    : Dict     ,	UpperCAmelCase    : str      )     ->      List[str]:
     return self.sp_model.encode(UpperCAmelCase     ,	out_type=UpperCAmelCase      )
 def 	A_      ( self    : str     ,	UpperCAmelCase    : Tuple      )     ->      Union[str, Any]:
     return self.sp_model.piece_to_id(UpperCAmelCase      )
 def 	A_      ( self    : Optional[Any]     ,	UpperCAmelCase    : Tuple      )     ->      Dict:
     lowerCamelCase__       : str   					=						self.sp_model.IdToPiece(UpperCAmelCase      )
     return token
 def 	A_      ( self    : Optional[Any]     ,	UpperCAmelCase    : int      )     ->      Union[str, Any]:
     lowerCamelCase__       : str   					=						[]
     lowerCamelCase__       : List[str]   					=						''
     for token in tokens:
         # make sure that special tokens are not decoded using sentencepiece model
         if token in self.all_special_tokens:
             out_string += self.sp_model.decode(UpperCAmelCase      ) + token
             lowerCamelCase__       : Tuple   					=						[]
         else:
             current_sub_tokens.append(UpperCAmelCase      )
     out_string += self.sp_model.decode(UpperCAmelCase      )
     return out_string.strip()







 def 	A_      ( self    : List[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Optional[str] = None      )     ->      Tuple[str]:
     if not os.path.isdir(UpperCAmelCase      ):
         logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""      )
         return
     lowerCamelCase__       : Any   					=						os.path.join(
         UpperCAmelCase     ,	(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']      )

     if os.path.abspath(self.vocab_file      ) != os.path.abspath(UpperCAmelCase      ) and os.path.isfile(self.vocab_file      ):
         copyfile(self.vocab_file     ,	UpperCAmelCase      )
     elif not os.path.isfile(self.vocab_file      ):
         with open(UpperCAmelCase     ,	'wb'      ) as fi:
             lowerCamelCase__       : int   					=						self.sp_model.serialized_model_proto()
             fi.write(UpperCAmelCase      )

     return (out_vocab_file,)





