




"""simple docstring"""







import os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class 				snake_case_			(					lowerCamelCase_	, unittest.TestCase			):







   """simple docstring"""




   A_				      =       PhobertTokenizer
   A_				      =       False







   def   UpperCAmelCase__			(		self)				->   Dict:
       super().setUp()

       # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
       UpperCamelCase              =	['''T@@''', '''i''', '''I''', '''R@@''', '''r''', '''e@@''']
       UpperCamelCase              =	dict(zip(lowerCamelCase_       ,					range(len(lowerCamelCase_))))
       UpperCamelCase              =	['''#version: 0.2''', '''l à</w>''']
       UpperCamelCase              =	{'''unk_token''': '''<unk>'''}

       UpperCamelCase              =	os.path.join(self.tmpdirname       ,					VOCAB_FILES_NAMES['''vocab_file'''])
       UpperCamelCase              =	os.path.join(self.tmpdirname       ,					VOCAB_FILES_NAMES['''merges_file'''])

       with open(self.vocab_file       ,					'''w'''       ,					encoding='''utf-8''') as fp:
           for token in vocab_tokens:
               fp.write(F'{token} {vocab_tokens[token]}\n')
       with open(self.merges_file       ,					'''w'''       ,					encoding='''utf-8''') as fp:
           fp.write('''\n'''.join(lowerCamelCase_))







   def   UpperCAmelCase__			(		self       ,					**lowerCamelCase_)				->   Any:
       kwargs.update(self.special_tokens_map)
       return PhobertTokenizer.from_pretrained(self.tmpdirname       ,					**lowerCamelCase_)







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_)				->   Union[str, Any]:
       UpperCamelCase              =	'''Tôi là VinAI Research'''
       UpperCamelCase              =	'''T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'''
       return input_text, output_text







   def   UpperCAmelCase__			(		self)				->   Dict:
       UpperCamelCase              =	PhobertTokenizer(self.vocab_file       ,					self.merges_file       ,					**self.special_tokens_map)
       UpperCamelCase              =	'''Tôi là VinAI Research'''
       UpperCamelCase              =	'''T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'''.split()
       UpperCamelCase              =	tokenizer.tokenize(lowerCamelCase_)
       print(lowerCamelCase_)
       self.assertListEqual(lowerCamelCase_       ,					lowerCamelCase_)

       UpperCamelCase              =	tokens + [tokenizer.unk_token]

       UpperCamelCase              =	[4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
       self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_)       ,					lowerCamelCase_)




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




