



"""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 		snake_case_   (	*A	:      Optional[int]	,      **A	:      Optional[Any]		):
            pass






@is_pipeline_test
@require_vision
@require_torch
class       UpperCAmelCase_    (							unittest.TestCase		):
      __SCREAMING_SNAKE_CASE		:  Union[str, Any]						=					MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



      def 		snake_case_   (	self	:      Optional[Any]	,      A	:      List[Any]	,      A	:      Optional[Any]	,      A	:      List[str]		):
           _UpperCAmelCase     :      Optional[int]														=       pipeline(
               "zero-shot-object-detection"	,      model="hf-internal-testing/tiny-random-owlvit-object-detection"		)

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



      def 		snake_case_   (	self	:      Dict	,      A	:      Tuple	,      A	:      str		):
           _UpperCAmelCase     :      List[str]														=       object_detector(examples[0]	,      threshold=0.0		)

           _UpperCAmelCase     :      Dict														=       len(_A		)
           self.assertGreater(_A	,      0		)
           self.assertEqual(
               _A	,      [
                   {
                       "score": ANY(_A		),
                       "label": ANY(_A		),
                       "box": {"xmin": ANY(_A		), "ymin": ANY(_A		), "xmax": ANY(_A		), "ymax": ANY(_A		)},
                   }
                   for i in range(_A		)
               ]	,      )



      @require_tf
      @unittest.skip("Zero Shot Object Detection not implemented in TF"		)
      def 		snake_case_   (	self	:      List[Any]		):
           pass



      @require_torch
      def 		snake_case_   (	self	:      Optional[int]		):
           _UpperCAmelCase     :      Tuple														=       pipeline(
               "zero-shot-object-detection"	,      model="hf-internal-testing/tiny-random-owlvit-object-detection"		)

           _UpperCAmelCase     :      Any														=       object_detector(
               "./tests/fixtures/tests_samples/COCO/000000039769.png"	,      candidate_labels=["cat", "remote", "couch"]	,      threshold=0.64	,      )

           self.assertEqual(
               nested_simplify(_A	,      decimals=4		)	,      [
                   {"score": 0.7_235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                   {"score": 0.7_218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                   {"score": 0.7_184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                   {"score": 0.6_748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                   {"score": 0.6_656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                   {"score": 0.6_614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                   {"score": 0.6_456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
                   {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
                   {"score": 0.6_419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
               ]	,      )

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

           self.assertEqual(
               nested_simplify(_A	,      decimals=4		)	,      [
                   [
                       {"score": 0.7_235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                       {"score": 0.7_218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                       {"score": 0.7_184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                       {"score": 0.6_748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                       {"score": 0.6_656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                       {"score": 0.6_614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                       {"score": 0.6_456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
                       {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
                       {"score": 0.6_419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
                   ]
               ]	,      )



      @require_torch
      @slow
      def 		snake_case_   (	self	:      Dict		):
           _UpperCAmelCase     :      List[str]														=       pipeline("zero-shot-object-detection"		)

           _UpperCAmelCase     :      List[Any]														=       object_detector(
               "http://images.cocodataset.org/val2017/000000039769.jpg"	,      candidate_labels=["cat", "remote", "couch"]	,      )
           self.assertEqual(
               nested_simplify(_A	,      decimals=4		)	,      [
                   {"score": 0.2_868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                   {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                   {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
                   {"score": 0.1_474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
                   {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
               ]	,      )

           _UpperCAmelCase     :      int														=       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(_A	,      decimals=4		)	,      [
                   [
                       {"score": 0.2_868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                       {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                       {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
                       {"score": 0.1_474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
                       {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
                   ],
                   [
                       {"score": 0.2_868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                       {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                       {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
                       {"score": 0.1_474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
                       {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
                   ],
               ]	,      )



      @require_tf
      @unittest.skip("Zero Shot Object Detection not implemented in TF"		)
      def 		snake_case_   (	self	:      List[Any]		):
           pass



      @require_torch
      @slow
      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase     :      List[Any]														=       0.2
           _UpperCAmelCase     :      List[str]														=       pipeline("zero-shot-object-detection"		)

           _UpperCAmelCase     :      Tuple														=       object_detector(
               "http://images.cocodataset.org/val2017/000000039769.jpg"	,      candidate_labels=["cat", "remote", "couch"]	,      threshold=_A	,      )
           self.assertEqual(
               nested_simplify(_A	,      decimals=4		)	,      [
                   {"score": 0.2_868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                   {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                   {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
               ]	,      )



      @require_torch
      @slow
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Dict														=       2
           _UpperCAmelCase     :      Optional[int]														=       pipeline("zero-shot-object-detection"		)

           _UpperCAmelCase     :      Dict														=       object_detector(
               "http://images.cocodataset.org/val2017/000000039769.jpg"	,      candidate_labels=["cat", "remote", "couch"]	,      top_k=_A	,      )
           self.assertEqual(
               nested_simplify(_A	,      decimals=4		)	,      [
                   {"score": 0.2_868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                   {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
               ]	,      )

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase       =		logging.get_logger(__name__)

_lowerCAmelCase       =		{
    "microsoft/trocr-base-handwritten": (
        "https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json"
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}
class     lowerCAmelCase_     (       __lowercase ):
							UpperCAmelCase				   =     "trocr"
							UpperCAmelCase				   =     ["past_key_values"]
							UpperCAmelCase				   =     {
							    "num_attention_heads": "decoder_attention_heads",
							    "hidden_size": "d_model",
							    "num_hidden_layers": "decoder_layers",
							}







							def __init__(   self      :     List[str] ,			_A      :     Optional[Any]=5_0265 ,			_A      :     Optional[Any]=1024 ,			_A      :     Optional[Any]=12 ,			_A      :     Any=16 ,			_A      :     Any=4096 ,			_A      :     Optional[Any]="gelu" ,			_A      :     Union[str, Any]=512 ,			_A      :     Dict=0.1 ,			_A      :     List[str]=0.0 ,			_A      :     Optional[Any]=0.0 ,			_A      :     Union[str, Any]=2 ,			_A      :     Any=0.02 ,			_A      :     List[str]=0.0 ,			_A      :     List[str]=True ,			_A      :     str=False ,			_A      :     List[str]=True ,			_A      :     Optional[Any]=True ,			_A      :     Optional[int]=1 ,			_A      :     int=0 ,			_A      :     Any=2 ,			**_A      :     Optional[int] ,			):
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     d_model
												_UpperCamelCase			     =     decoder_layers
												_UpperCamelCase			     =     decoder_attention_heads
												_UpperCamelCase			     =     decoder_ffn_dim
												_UpperCamelCase			     =     activation_function
												_UpperCamelCase			     =     max_position_embeddings
												_UpperCamelCase			     =     dropout
												_UpperCamelCase			     =     attention_dropout
												_UpperCamelCase			     =     activation_dropout
												_UpperCamelCase			     =     init_std
												_UpperCamelCase			     =     decoder_layerdrop
												_UpperCamelCase			     =     use_cache
												_UpperCamelCase			     =     scale_embedding
												_UpperCamelCase			     =     use_learned_position_embeddings
												_UpperCamelCase			     =     layernorm_embedding

												super().__init__(
												    pad_token_id=_A ,			bos_token_id=_A ,			eos_token_id=_A ,			decoder_start_token_id=_A ,			**_A ,			)
