


"""simple docstring"""







import json
import os
import unittest

from transformers import BatchEncoding, MvpTokenizer, MvpTokenizerFast
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, require_torch
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors


@require_tokenizers
class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   unittest.TestCase    ):
       """simple docstring"""


       a  :    Any       =MvpTokenizer
       a  :    List[str]       =MvpTokenizerFast
       a  :    Optional[Any]       =True
       a  :    List[str]       =filter_roberta_detectors

       def 	lowercase__							(						self						):






         """simple docstring"""





         super().setUp()
         lowerCAmelCase					:					List[str]								=							[
             "l",
             "o",
             "w",
             "e",
             "r",
             "s",
             "t",
             "i",
             "d",
             "n",
             "\u0120",
             "\u0120l",
             "\u0120n",
             "\u0120lo",
             "\u0120low",
             "er",
             "\u0120lowest",
             "\u0120newer",
             "\u0120wider",
             "<unk>",
         ]
         lowerCAmelCase					:					Union[str, Any]								=							dict(zip(snake_case__		,					range(len(snake_case__						)						)						)						)
         lowerCAmelCase					:					Union[str, Any]								=							["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
         lowerCAmelCase					:					Dict								=							{"unk_token": "<unk>"}

         lowerCAmelCase					:					str								=							os.path.join(self.tmpdirname		,					VOCAB_FILES_NAMES["vocab_file"]						)
         lowerCAmelCase					:					Dict								=							os.path.join(self.tmpdirname		,					VOCAB_FILES_NAMES["merges_file"]						)
         with open(self.vocab_file		,					"w"		,					encoding="utf-8"						) as fp:
           fp.write(json.dumps(snake_case__						) + "\n"						)
         with open(self.merges_file		,					"w"		,					encoding="utf-8"						) as fp:
           fp.write("\n".join(snake_case__						)						)

       def 	lowercase__							(						self		,					**snake_case__						):






         """simple docstring"""





         kwargs.update(self.special_tokens_map						)
         return self.tokenizer_class.from_pretrained(self.tmpdirname		,					**snake_case__						)

       def 	lowercase__							(						self		,					**snake_case__						):






         """simple docstring"""





         kwargs.update(self.special_tokens_map						)
         return self.rust_tokenizer_class.from_pretrained(self.tmpdirname		,					**snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         return "lower newer", "lower newer"

       @cached_property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return MvpTokenizer.from_pretrained("RUCAIBox/mvp"						)

       @cached_property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return MvpTokenizerFast.from_pretrained("RUCAIBox/mvp"						)

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							["A long paragraph for summarization.", "Another paragraph for summarization."]
         lowerCAmelCase					:					Union[str, Any]								=							[0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2]

         for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
           lowerCAmelCase					:					Optional[int]								=							tokenizer(snake_case__		,					max_length=len(snake_case__						)		,					padding=snake_case__		,					return_tensors="pt"						)
           self.assertIsInstance(snake_case__		,					snake_case__						)

           self.assertEqual((2, 9)		,					batch.input_ids.shape						)
           self.assertEqual((2, 9)		,					batch.attention_mask.shape						)
           lowerCAmelCase					:					List[Any]								=							batch.input_ids.tolist()[0]
           self.assertListEqual(snake_case__		,					snake_case__						)
           # Test that special tokens are reset

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							["A long paragraph for summarization.", "Another paragraph for summarization."]
         for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
           lowerCAmelCase					:					List[Any]								=							tokenizer(snake_case__		,					padding=snake_case__		,					return_tensors="pt"						)
           # check if input_ids are returned and no labels
           self.assertIn("input_ids"		,					snake_case__						)
           self.assertIn("attention_mask"		,					snake_case__						)
           self.assertNotIn("labels"		,					snake_case__						)
           self.assertNotIn("decoder_attention_mask"		,					snake_case__						)

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							[
             "Summary of the text.",
             "Another summary.",
         ]
         for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
           lowerCAmelCase					:					Tuple								=							tokenizer(text_target=snake_case__		,					max_length=32		,					padding="max_length"		,					return_tensors="pt"						)
           self.assertEqual(32		,					targets["input_ids"].shape[1]						)

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
           lowerCAmelCase					:					Any								=							tokenizer(
               ["I am a small frog" * 1_024, "I am a small frog"]		,					padding=snake_case__		,					truncation=snake_case__		,					return_tensors="pt"						)
           self.assertIsInstance(snake_case__		,					snake_case__						)
           self.assertEqual(batch.input_ids.shape		,					(2, 1_024)						)

       @require_torch
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							["A long paragraph for summarization."]
         lowerCAmelCase					:					List[str]								=							[
             "Summary of the text.",
         ]
         for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
           lowerCAmelCase					:					Union[str, Any]								=							tokenizer(snake_case__		,					text_target=snake_case__		,					return_tensors="pt"						)
           lowerCAmelCase					:					Optional[Any]								=							inputs["input_ids"]
           lowerCAmelCase					:					List[str]								=							inputs["labels"]
           self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item()						)
           self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item()						)
           self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item()						)
           self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item()						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         pass







       def 	lowercase__							(						self						):






         """simple docstring"""





         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
           with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
             lowerCAmelCase					:					Union[str, Any]								=							self.rust_tokenizer_class.from_pretrained(snake_case__		,					**snake_case__						)
             lowerCAmelCase					:					int								=							self.tokenizer_class.from_pretrained(snake_case__		,					**snake_case__						)
             lowerCAmelCase					:					Tuple								=							"A, <mask> AllenNLP sentence."
             lowerCAmelCase					:					str								=							tokenizer_r.encode_plus(snake_case__		,					add_special_tokens=snake_case__		,					return_token_type_ids=snake_case__						)
             lowerCAmelCase					:					int								=							tokenizer_p.encode_plus(snake_case__		,					add_special_tokens=snake_case__		,					return_token_type_ids=snake_case__						)

             # token_type_ids should put 0 everywhere
             self.assertEqual(sum(tokens_r["token_type_ids"]						)		,					sum(tokens_p["token_type_ids"]						)						)

             # attention_mask should put 1 everywhere, so sum over length should be 1
             self.assertEqual(
                 sum(tokens_r["attention_mask"]						) / len(tokens_r["attention_mask"]						)		,					sum(tokens_p["attention_mask"]						) / len(tokens_p["attention_mask"]						)		,					)

             lowerCAmelCase					:					List[str]								=							tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"]						)
             lowerCAmelCase					:					Dict								=							tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"]						)

             # Rust correctly handles the space before the mask while python doesnt
             self.assertSequenceEqual(tokens_p["input_ids"]		,					[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]						)
             self.assertSequenceEqual(tokens_r["input_ids"]		,					[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]						)

             self.assertSequenceEqual(
                 snake_case__		,					["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"]						)
             self.assertSequenceEqual(
                 snake_case__		,					["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"]						)









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





