





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

from tokenizers import processors

from ...tokenization_utils import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
    from .tokenization_mbart import MBartTokenizer
else:
    snake_case_				=			None


snake_case_				=			logging.get_logger(__name__)


snake_case_				=			{'vocab_file': 'sentencepiece.bpe.model', 'tokenizer_file': 'tokenizer.json'}

snake_case_				=			{
    'vocab_file': {
        'facebook/mbart-large-en-ro': (
            'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model'
        ),
        'facebook/mbart-large-cc25': (
            'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model'
        ),
    },
    'tokenizer_file': {
        'facebook/mbart-large-en-ro': 'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json',
        'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json',
    },
}

snake_case_				=			{
    'facebook/mbart-large-en-ro': 1024,
    'facebook/mbart-large-cc25': 1024,
}

# fmt: off
snake_case_				=			['ar_AR', 'cs_CZ', 'de_DE', 'en_XX', 'es_XX', 'et_EE', 'fi_FI', 'fr_XX', 'gu_IN', 'hi_IN', 'it_IT', 'ja_XX', 'kk_KZ', 'ko_KR', 'lt_LT', 'lv_LV', 'my_MM', 'ne_NP', 'nl_XX', 'ro_RO', 'ru_RU', 'si_LK', 'tr_TR', 'vi_VN', 'zh_CN']

class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase  ):
      A_						:				Dict							=    VOCAB_FILES_NAMES
      A_						:				Optional[int]							=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      A_						:				str							=    PRETRAINED_VOCAB_FILES_MAP
      A_						:				int							=    ['input_ids', 'attention_mask']
      A_						:				Tuple							=    MBartTokenizer

      A_						:				List[int]							=    []
      A_						:				List[int]							=    []


      def __init__(self       :      Optional[int]				,			a__       :      str=None				,			a__       :      int=None				,			a__       :      Any="<s>"				,			a__       :      Any="</s>"				,			a__       :      Any="</s>"				,			a__       :      List[str]="<s>"				,			a__       :      List[str]="<unk>"				,			a__       :      Dict="<pad>"				,			a__       :      List[Any]="<mask>"				,			a__       :      Union[str, Any]=None				,			a__       :      Any=None				,			a__       :      Union[str, Any]=None				,			**a__       :      int				,			):





         """simple docstring"""



         __snake_case							       =   AddedToken(a__				,			lstrip=a__				,			rstrip=a__      ) if isinstance(a__				,			a__      ) else mask_token

         super().__init__(
             vocab_file=a__				,			tokenizer_file=a__				,			bos_token=a__				,			eos_token=a__				,			sep_token=a__				,			cls_token=a__				,			unk_token=a__				,			pad_token=a__				,			mask_token=a__				,			src_lang=a__				,			tgt_lang=a__				,			additional_special_tokens=a__				,			**a__				,			)

         __snake_case							       =   vocab_file
         __snake_case							       =   False if not self.vocab_file else True

         __snake_case							       =   FAIRSEQ_LANGUAGE_CODES.copy()

         if additional_special_tokens is not None:
            # Only add those special tokens if they are not already there.
            _additional_special_tokens.extend(
                [t for t in additional_special_tokens if t not in _additional_special_tokens]      )

         self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens}      )
         __snake_case							       =   {
             lang_code: self.convert_tokens_to_ids(a__      ) for lang_code in FAIRSEQ_LANGUAGE_CODES
         }

         __snake_case							       =   src_lang if src_lang is not None else '''en_XX'''
         __snake_case							       =   self.convert_tokens_to_ids(self._src_lang      )
         __snake_case							       =   tgt_lang
         self.set_src_lang_special_tokens(self._src_lang      )


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





         """simple docstring"""



         return self._src_lang


      @src_lang.setter
      def    a (self       :      Tuple				,			a__       :      str      ):





         """simple docstring"""



         __snake_case							       =   new_src_lang
         self.set_src_lang_special_tokens(self._src_lang      )


      def    a (self       :      Tuple				,			a__       :      List[int]				,			a__       :      Optional[List[int]] = None      ):





         """simple docstring"""



         if token_ids_a is None:
            return self.prefix_tokens + token_ids_a + self.suffix_tokens
         # We don't expect to process pairs, but leave the pair logic for API consistency
         return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens


      def    a (self       :      List[Any]				,			a__       :      List[int]				,			a__       :      Optional[List[int]] = None      ):





         """simple docstring"""



         __snake_case							       =   [self.sep_token_id]
         __snake_case							       =   [self.cls_token_id]

         if token_ids_a is None:
            return len(cls + token_ids_a + sep      ) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep      ) * [0]


      def    a (self       :      List[Any]				,			a__       :      Optional[Any]				,			a__       :      str				,			a__       :      Optional[str]				,			a__       :      Optional[str]				,			**a__       :      int      ):





         """simple docstring"""



         if src_lang is None or tgt_lang is None:
            raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model'''      )
         __snake_case							       =   src_lang
         __snake_case							       =   self(a__				,			add_special_tokens=a__				,			return_tensors=a__				,			**a__      )
         __snake_case							       =   self.convert_tokens_to_ids(a__      )
         __snake_case							       =   tgt_lang_id
         return inputs


      def    a (self       :      Tuple				,			a__       :      List[str]				,			a__       :      str = "en_XX"				,			a__       :      Optional[List[str]] = None				,			a__       :      str = "ro_RO"				,			**a__       :      Tuple				,			):





         """simple docstring"""



         __snake_case							       =   src_lang
         __snake_case							       =   tgt_lang
         return super().prepare_seqaseq_batch(a__				,			a__				,			**a__      )


      def    a (self       :      str      ):





         """simple docstring"""



         return self.set_src_lang_special_tokens(self.src_lang      )


      def    a (self       :      List[Any]      ):





         """simple docstring"""



         return self.set_tgt_lang_special_tokens(self.tgt_lang      )


      def    a (self       :      Optional[int]				,			a__       :      str      ):





         """simple docstring"""



         __snake_case							       =   self.convert_tokens_to_ids(a__      )
         __snake_case							       =   []
         __snake_case							       =   [self.eos_token_id, self.cur_lang_code]

         __snake_case							       =   self.convert_ids_to_tokens(self.prefix_tokens      )
         __snake_case							       =   self.convert_ids_to_tokens(self.suffix_tokens      )

         __snake_case							       =   processors.TemplateProcessing(
             single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str				,			pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str				,			special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str				,			self.prefix_tokens + self.suffix_tokens      )      )				,			)


      def    a (self       :      Union[str, Any]				,			a__       :      str      ):





         """simple docstring"""



         __snake_case							       =   self.convert_tokens_to_ids(a__      )
         __snake_case							       =   []
         __snake_case							       =   [self.eos_token_id, self.cur_lang_code]

         __snake_case							       =   self.convert_ids_to_tokens(self.prefix_tokens      )
         __snake_case							       =   self.convert_ids_to_tokens(self.suffix_tokens      )

         __snake_case							       =   processors.TemplateProcessing(
             single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str				,			pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str				,			special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str				,			self.prefix_tokens + self.suffix_tokens      )      )				,			)


      def    a (self       :      int				,			a__       :      str				,			a__       :      Optional[str] = None      ):





         """simple docstring"""



         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(a__      ):
            logger.error(f"""Vocabulary path ({save_directory}) should be a directory."""      )
            return
         __snake_case							       =   os.path.join(
             a__				,			(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']      )

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

         return (out_vocab_file,)


"""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        UpperCamelCase		:
          @staticmethod
          def 			_UpperCAmelCase   (			*__UpperCamelCase     ,**__UpperCamelCase			)	->      Union[str, Any]:


               '''simple docstring'''


               pass







@is_pipeline_test
@require_vision
@require_torch
class        UpperCamelCase		(			unittest.TestCase  ):
   lowercase							=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


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

        lowercase_	:				Optional[int]													=	[
            {
                'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                'candidate_labels': ['cat', 'remote', 'couch'],
            }
        ]
        return object_detector, examples
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      str:


        '''simple docstring'''


        lowercase_	:				Tuple													=	object_detector(examples[0]     ,threshold=0.0			)

        lowercase_	:				Any													=	len(__UpperCamelCase			)
        self.assertGreater(__UpperCamelCase     ,0			)
        self.assertEqual(
            __UpperCamelCase     ,[
                {
                    'score': ANY(__UpperCamelCase			),
                    'label': ANY(__UpperCamelCase			),
                    'box': {'xmin': ANY(__UpperCamelCase			), 'ymin': ANY(__UpperCamelCase			), 'xmax': ANY(__UpperCamelCase			), 'ymax': ANY(__UpperCamelCase			)},
                }
                for i in range(__UpperCamelCase			)
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        pass
   @require_torch
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

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

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
            ]     ,)

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

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                    {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                    {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                ]
            ]     ,)
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[str]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	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(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        pass
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	0.2
        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				str													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
            ]     ,)







   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	2
        lowercase_	:				Optional[Any]													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[Any]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,top_k=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
            ]     ,)

