


"""simple docstring"""







import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

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


lowerCAmelCase__				    =       logging.get_logger(__name__)

lowerCAmelCase__				    =       '''▁'''

lowerCAmelCase__				    =       {
    '''vocab_file''': '''vocab.json''',
    '''spm_file''': '''sentencepiece.bpe.model''',
}

lowerCAmelCase__				    =       {
    '''vocab_file''': {
        '''facebook/s2t-small-librispeech-asr''': (
            '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json'''
        ),
    },
    '''spm_file''': {
        '''facebook/s2t-small-librispeech-asr''': (
            '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model'''
        )
    },
}

lowerCAmelCase__				    =       {
    '''facebook/s2t-small-librispeech-asr''': 1_024,
}

lowerCAmelCase__				    =       ['''pt''', '''fr''', '''ru''', '''nl''', '''ro''', '''it''', '''es''', '''de''']

lowerCAmelCase__				    =       {'''mustc''': MUSTC_LANGS}


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    List[str]       =VOCAB_FILES_NAMES
       a  :    Tuple       =PRETRAINED_VOCAB_FILES_MAP
       a  :    Tuple       =MAX_MODEL_INPUT_SIZES
       a  :    List[Any]       =["input_ids", "attention_mask"]

       a  :    List[int]       =[]

       def __init__(						self		,					snake_case__		,					snake_case__		,					snake_case__="<s>"		,					snake_case__="</s>"		,					snake_case__="<pad>"		,					snake_case__="<unk>"		,					snake_case__=False		,					snake_case__=False		,					snake_case__=None		,					snake_case__=None		,					snake_case__ = None		,					**snake_case__		,					):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							{} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=snake_case__		,					eos_token=snake_case__		,					unk_token=snake_case__		,					pad_token=snake_case__		,					do_upper_case=snake_case__		,					do_lower_case=snake_case__		,					tgt_lang=snake_case__		,					lang_codes=snake_case__		,					sp_model_kwargs=self.sp_model_kwargs		,					**snake_case__		,					)
         lowerCAmelCase					:					Optional[Any]								=							do_upper_case
         lowerCAmelCase					:					Optional[Any]								=							do_lower_case

         lowerCAmelCase					:					List[Any]								=							load_json(snake_case__						)
         lowerCAmelCase					:					List[str]								=							{v: k for k, v in self.encoder.items()}
         lowerCAmelCase					:					str								=							spm_file
         lowerCAmelCase					:					Optional[int]								=							load_spm(snake_case__		,					self.sp_model_kwargs						)

         if lang_codes is not None:
           lowerCAmelCase					:					Union[str, Any]								=							lang_codes
           lowerCAmelCase					:					Optional[Any]								=							LANGUAGES[lang_codes]
           lowerCAmelCase					:					int								=							[f"""<lang:{lang}>""" for lang in self.langs]
           lowerCAmelCase					:					Union[str, Any]								=							{lang: self.sp_model.PieceToId(f"""<lang:{lang}>"""						) for lang in self.langs}

           lowerCAmelCase					:					List[Any]								=							self.lang_tokens
           lowerCAmelCase					:					Union[str, Any]								=							tgt_lang if tgt_lang is not None else self.langs[0]

           self.set_tgt_lang_special_tokens(self._tgt_lang						)
         else:
           lowerCAmelCase					:					Union[str, Any]								=							{}

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return len(self.encoder						)

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return self._tgt_lang

       @tgt_lang.setter
       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							new_tgt_lang
         self.set_tgt_lang_special_tokens(snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							self.lang_code_to_id[tgt_lang]
         lowerCAmelCase					:					str								=							[lang_code_id]

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         return self.sp_model.encode(snake_case__		,					out_type=snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         return self.encoder.get(snake_case__		,					self.encoder[self.unk_token]						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         return self.decoder.get(snake_case__		,					self.unk_token						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							[]
         lowerCAmelCase					:					Optional[Any]								=							""
         for token in tokens:
           # make sure that special tokens are not decoded using sentencepiece model
           if token in self.all_special_tokens:
             lowerCAmelCase					:					Any								=							self.sp_model.decode(snake_case__						)
             out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " "
             lowerCAmelCase					:					Optional[Any]								=							[]
           else:
             current_sub_tokens.append(snake_case__						)
         lowerCAmelCase					:					List[str]								=							self.sp_model.decode(snake_case__						)
         out_string += decoded.upper() if self.do_upper_case else decoded
         return out_string.strip()

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__=None						):






         """simple docstring"""





         if token_ids_a is None:
           return self.prefix_tokens + token_ids_a + [self.eos_token_id]
         # 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.eos_token_id]

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = None		,					snake_case__ = False						):






         """simple docstring"""





         if already_has_special_tokens:
           return super().get_special_tokens_mask(
               token_ids_a=snake_case__		,					token_ids_a=snake_case__		,					already_has_special_tokens=snake_case__						)

         lowerCAmelCase					:					Optional[Any]								=							[1] * len(self.prefix_tokens						)
         lowerCAmelCase					:					Any								=							[1]
         if token_ids_a is None:
           return prefix_ones + ([0] * len(snake_case__						)) + suffix_ones
         return prefix_ones + ([0] * len(snake_case__						)) + ([0] * len(snake_case__						)) + suffix_ones

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							self.encoder.copy()
         vocab.update(self.added_tokens_encoder						)
         return vocab

       def __getstate__(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.__dict__.copy()
         lowerCAmelCase					:					Any								=							None
         return state

       def __setstate__(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							d

         # for backward compatibility
         if not hasattr(self		,					"sp_model_kwargs"						):
           lowerCAmelCase					:					List[Any]								=							{}

         lowerCAmelCase					:					str								=							load_spm(self.spm_file		,					self.sp_model_kwargs						)







       def 	lowercase__							(						self		,					snake_case__		,					snake_case__ = None						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							Path(snake_case__						)
         assert save_dir.is_dir(), f"""{save_directory} should be a directory"""
         lowerCAmelCase					:					str								=							save_dir / (
             (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["vocab_file"]
         )
         lowerCAmelCase					:					Any								=							save_dir / (
             (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["spm_file"]
         )

         save_json(self.encoder		,					snake_case__						)

         if os.path.abspath(self.spm_file						) != os.path.abspath(snake_case__						) and os.path.isfile(self.spm_file						):
           copyfile(self.spm_file		,					snake_case__						)
         elif not os.path.isfile(self.spm_file						):
           with open(snake_case__		,					"wb"						) as fi:
             lowerCAmelCase					:					Union[str, Any]								=							self.sp_model.serialized_model_proto()
             fi.write(snake_case__						)

         return (str(snake_case__						), str(snake_case__						))

def  a__      (    SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   Dict[str, Any]    ):


  '''simple docstring'''







  lowerCAmelCase					:					int								=							sentencepiece.SentencePieceProcessor(**SCREAMING_SNAKE_CASE    )
  spm.Load(str(SCREAMING_SNAKE_CASE    )    )
  return spm
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ):


  '''simple docstring'''







  with open(SCREAMING_SNAKE_CASE    ,				"r"    ) as f:
    return json.load(SCREAMING_SNAKE_CASE    )





def  a__      (    SCREAMING_SNAKE_CASE   :   Tuple    ,				SCREAMING_SNAKE_CASE   :   str    ):


  '''simple docstring'''







  with open(SCREAMING_SNAKE_CASE    ,				"w"    ) as f:
    json.dump(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				indent=2    )









"""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__    :
            """simple docstring"""


            @staticmethod
            def 	lowercase__							(						*snake_case__		,					**snake_case__						):






              """simple docstring"""





              pass


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


       a  :    Optional[Any]       =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							pipeline(
             "zero-shot-object-detection"		,					model="hf-internal-testing/tiny-random-owlvit-object-detection"						)

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

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							object_detector(examples[0]		,					threshold=0.0						)

         lowerCAmelCase					:					Dict								=							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 	lowercase__							(						self						):






         """simple docstring"""





         pass

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							pipeline(
             "zero-shot-object-detection"		,					model="hf-internal-testing/tiny-random-owlvit-object-detection"						)

         lowerCAmelCase					:					Tuple								=							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": 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}},
             ]		,					)

         lowerCAmelCase					:					Optional[Any]								=							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": 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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							pipeline("zero-shot-object-detection"						)

         lowerCAmelCase					:					Dict								=							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": 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}},
             ]		,					)

         lowerCAmelCase					:					Dict								=							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": 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 	lowercase__							(						self						):






         """simple docstring"""





         pass

       @require_torch
       @slow
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							0.2
         lowerCAmelCase					:					List[Any]								=							pipeline("zero-shot-object-detection"						)

         lowerCAmelCase					:					Union[str, Any]								=							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": 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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							2
         lowerCAmelCase					:					Any								=							pipeline("zero-shot-object-detection"						)

         lowerCAmelCase					:					Any								=							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": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                 {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
             ]		,					)





