from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowercase											=      logging.get_logger(__name__)

__lowercase											=      {
    '''facebook/data2vec-text-base''': '''https://huggingface.co/data2vec/resolve/main/config.json''',
}


class 			lowerCamelCase_  (     UpperCAmelCase_				):






    '''simple docstring'''






    a__			:		Dict      			=   """data2vec-text"""







    def __init__(			self				,				__lowercase=30_522				,				__lowercase=768				,				__lowercase=12				,				__lowercase=12				,				__lowercase=3_072				,				__lowercase="gelu"				,				__lowercase=0.1				,				__lowercase=0.1				,				__lowercase=512				,				__lowercase=2				,				__lowercase=0.02				,				__lowercase=1E-1_2				,				__lowercase=1				,				__lowercase=0				,				__lowercase=2				,				__lowercase="absolute"				,				__lowercase=True				,				__lowercase=None				,				**__lowercase				,				) ->			Tuple:
        super().__init__(pad_token_id=__lowercase				,				bos_token_id=__lowercase				,				eos_token_id=__lowercase				,				**__lowercase)

        __UpperCamelCase  :Any    						=	vocab_size
        __UpperCamelCase  :Union[str, Any]    						=	hidden_size
        __UpperCamelCase  :str    						=	num_hidden_layers
        __UpperCamelCase  :Optional[int]    						=	num_attention_heads
        __UpperCamelCase  :str    						=	hidden_act
        __UpperCamelCase  :int    						=	intermediate_size
        __UpperCamelCase  :Any    						=	hidden_dropout_prob
        __UpperCamelCase  :List[Any]    						=	attention_probs_dropout_prob
        __UpperCamelCase  :Optional[int]    						=	max_position_embeddings
        __UpperCamelCase  :Dict    						=	type_vocab_size
        __UpperCamelCase  :int    						=	initializer_range
        __UpperCamelCase  :Any    						=	layer_norm_eps
        __UpperCamelCase  :List[str]    						=	position_embedding_type
        __UpperCamelCase  :List[str]    						=	use_cache
        __UpperCamelCase  :Tuple    						=	classifier_dropout


class 			lowerCamelCase_  (     UpperCAmelCase_				):






    '''simple docstring'''






    @property
    def     UpperCamelCase__     (			self) ->			Mapping[str, Mapping[int, str]]:
        if self.task == "multiple-choice":
            __UpperCamelCase  :int    						=	{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
        else:
            __UpperCamelCase  :Optional[Any]    						=	{0: '''batch''', 1: '''sequence'''}
        return OrderedDict(
            [
                ('''input_ids''', dynamic_axis),
                ('''attention_mask''', dynamic_axis),
            ])






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     UpperCamelCase__     (			*__lowercase				,				**__lowercase) ->			Dict:
            pass


@is_pipeline_test
@require_vision
@require_torch
class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    a__			:		str      			=   MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase) ->			str:
        __UpperCamelCase  :Union[str, Any]    						=	pipeline(
            '''zero-shot-object-detection'''				,				model='''hf-internal-testing/tiny-random-owlvit-object-detection''')

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







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase) ->			Optional[Any]:
        __UpperCamelCase  :Optional[Any]    						=	object_detector(examples[0]				,				threshold=0.0)

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







    @require_tf
    @unittest.skip('''Zero Shot Object Detection not implemented in TF''')
    def     UpperCamelCase__     (			self) ->			int:
        pass







    @require_torch
    def     UpperCamelCase__     (			self) ->			Any:
        __UpperCamelCase  :Tuple    						=	pipeline(
            '''zero-shot-object-detection'''				,				model='''hf-internal-testing/tiny-random-owlvit-object-detection''')

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

        self.assertEqual(
            nested_simplify(__lowercase				,				decimals=4)				,				[
                {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
                {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
            ]				,				)

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

        self.assertEqual(
            nested_simplify(__lowercase				,				decimals=4)				,				[
                [
                    {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                    {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                    {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                    {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                    {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                    {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                    {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                    {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
                    {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                ]
            ]				,				)







    @require_torch
    @slow
    def     UpperCamelCase__     (			self) ->			Optional[int]:
        __UpperCamelCase  :List[str]    						=	pipeline('''zero-shot-object-detection''')

        __UpperCamelCase  :List[str]    						=	object_detector(
            '''http://images.cocodataset.org/val2017/000000039769.jpg'''				,				candidate_labels=['''cat''', '''remote''', '''couch''']				,				)
        self.assertEqual(
            nested_simplify(__lowercase				,				decimals=4)				,				[
                {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
            ]				,				)

        __UpperCamelCase  :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(__lowercase				,				decimals=4)				,				[
                [
                    {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                    {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                    {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                    {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                    {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
                ],
                [
                    {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                    {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                    {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                    {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                    {'''score''': 0.12_08, '''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) ->			Dict:
        pass







    @require_torch
    @slow
    def     UpperCamelCase__     (			self) ->			str:
        __UpperCamelCase  :Tuple    						=	0.2
        __UpperCamelCase  :str    						=	pipeline('''zero-shot-object-detection''')

        __UpperCamelCase  :Any    						=	object_detector(
            '''http://images.cocodataset.org/val2017/000000039769.jpg'''				,				candidate_labels=['''cat''', '''remote''', '''couch''']				,				threshold=__lowercase				,				)
        self.assertEqual(
            nested_simplify(__lowercase				,				decimals=4)				,				[
                {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
            ]				,				)







    @require_torch
    @slow
    def     UpperCamelCase__     (			self) ->			Any:
        __UpperCamelCase  :Optional[int]    						=	2
        __UpperCamelCase  :Union[str, Any]    						=	pipeline('''zero-shot-object-detection''')

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





