






'''simple docstring'''



import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
      from PIL import Image
else:
      class UpperCAmelCase_       :




            """simple docstring"""




            @staticmethod
            def 					A__   (			*UpperCAmelCase      :		Optional[int]  ,   **UpperCAmelCase      :		Any		)	->	int:


             '''simple docstring'''

             pass
@is_pipeline_test
@require_vision
@require_timm
@require_torch
class UpperCAmelCase_       (     unittest.TestCase					):




      """simple docstring"""




      UpperCamelCase_             =      MODEL_FOR_OBJECT_DETECTION_MAPPING






      def 					A__   (			self      :		Optional[Any]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		Optional[Any]  ,   UpperCAmelCase      :		Dict		)	->	Optional[int]:


       '''simple docstring'''

       lowercase       : Union[str, Any]						=ObjectDetectionPipeline(model=UpperCAmelCase  ,   image_processor=UpperCAmelCase		)
       return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]






      def 					A__   (			self      :		Optional[Any]  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		Optional[Any]		)	->	Any:


       '''simple docstring'''

       lowercase       : List[str]						=object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  ,   threshold=0.0		)

       self.assertGreater(len(UpperCAmelCase		)  ,   0		)
       for detected_object in outputs:
        self.assertEqual(
            UpperCAmelCase  ,   {
                '''score''': ANY(UpperCAmelCase		),
                '''label''': ANY(UpperCAmelCase		),
                '''box''': {'''xmin''': ANY(UpperCAmelCase		), '''ymin''': ANY(UpperCAmelCase		), '''xmax''': ANY(UpperCAmelCase		), '''ymax''': ANY(UpperCAmelCase		)},
            }  ,   )

       import datasets

       lowercase       : int						=datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''  ,   '''image'''  ,   split='''test'''		)

       lowercase       : Any						=[
           Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		),
           '''http://images.cocodataset.org/val2017/000000039769.jpg''',
           # RGBA
           dataset[0]['''file'''],
           # LA
           dataset[1]['''file'''],
           # L
           dataset[2]['''file'''],
       ]
       lowercase       : Optional[Any]						=object_detector(UpperCAmelCase  ,   threshold=0.0		)

       self.assertEqual(len(UpperCAmelCase		)  ,   len(UpperCAmelCase		)		)
       for outputs in batch_outputs:
        self.assertGreater(len(UpperCAmelCase		)  ,   0		)
        for detected_object in outputs:
         self.assertEqual(
             UpperCAmelCase  ,   {
                 '''score''': ANY(UpperCAmelCase		),
                 '''label''': ANY(UpperCAmelCase		),
                 '''box''': {'''xmin''': ANY(UpperCAmelCase		), '''ymin''': ANY(UpperCAmelCase		), '''xmax''': ANY(UpperCAmelCase		), '''ymax''': ANY(UpperCAmelCase		)},
             }  ,   )






      @require_tf
      @unittest.skip('''Object detection not implemented in TF'''		)
      def 					A__   (			self      :		Any		)	->	Dict:


       '''simple docstring'''

       pass






      @require_torch
      def 					A__   (			self      :		Union[str, Any]		)	->	Union[str, Any]:


       '''simple docstring'''

       lowercase       : Union[str, Any]						='''hf-internal-testing/tiny-detr-mobilenetsv3'''

       lowercase       : List[str]						=AutoModelForObjectDetection.from_pretrained(UpperCAmelCase		)
       lowercase       : List[Any]						=AutoFeatureExtractor.from_pretrained(UpperCAmelCase		)
       lowercase       : Tuple						=ObjectDetectionPipeline(model=UpperCAmelCase  ,   feature_extractor=UpperCAmelCase		)

       lowercase       : List[str]						=object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''  ,   threshold=0.0		)

       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
           ]  ,   )

       lowercase       : Union[str, Any]						=object_detector(
           [
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
           ]  ,   threshold=0.0  ,   )

       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               [
                   {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
                   {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               ],
               [
                   {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
                   {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               ],
           ]  ,   )






      @require_torch
      @slow
      def 					A__   (			self      :		Dict		)	->	str:


       '''simple docstring'''

       lowercase       : Optional[int]						='''facebook/detr-resnet-50'''

       lowercase       : Any						=AutoModelForObjectDetection.from_pretrained(UpperCAmelCase		)
       lowercase       : str						=AutoFeatureExtractor.from_pretrained(UpperCAmelCase		)
       lowercase       : int						=ObjectDetectionPipeline(model=UpperCAmelCase  ,   feature_extractor=UpperCAmelCase		)

       lowercase       : Optional[Any]						=object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
               {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
               {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
               {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ]  ,   )

       lowercase       : Optional[int]						=object_detector(
           [
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
           ]		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               [
                   {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
               [
                   {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
           ]  ,   )






      @require_torch
      @slow
      def 					A__   (			self      :		Optional[int]		)	->	Dict:


       '''simple docstring'''

       lowercase       : List[str]						='''facebook/detr-resnet-50'''

       lowercase       : List[str]						=pipeline('''object-detection'''  ,   model=UpperCAmelCase		)

       lowercase       : List[str]						=object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
               {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
               {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
               {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ]  ,   )

       lowercase       : Any						=object_detector(
           [
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
           ]		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               [
                   {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
               [
                   {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
           ]  ,   )






      @require_torch
      @slow
      def 					A__   (			self      :		Union[str, Any]		)	->	Dict:


       '''simple docstring'''

       lowercase       : Any						=0.9_9_8_5
       lowercase       : int						='''facebook/detr-resnet-50'''

       lowercase       : Any						=pipeline('''object-detection'''  ,   model=UpperCAmelCase		)

       lowercase       : str						=object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''  ,   threshold=UpperCAmelCase		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ]  ,   )






      @require_torch
      @require_pytesseract
      @slow
      def 					A__   (			self      :		Optional[Any]		)	->	List[Any]:


       '''simple docstring'''

       lowercase       : Optional[int]						='''Narsil/layoutlmv3-finetuned-funsd'''
       lowercase       : Dict						=0.9_9_9_3

       lowercase       : Any						=pipeline('''object-detection'''  ,   model=UpperCAmelCase  ,   threshold=UpperCAmelCase		)

       lowercase       : Any						=object_detector(
           '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'''		)
       self.assertEqual(
           nested_simplify(UpperCAmelCase  ,   decimals=4		)  ,   [
               {'''score''': 0.9_9_9_3, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
               {'''score''': 0.9_9_9_3, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
           ]  ,   )








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_													=      {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}






class     __a			(  SCREAMING_SNAKE_CASE					):
	SCREAMING_SNAKE_CASE  	=							"distilbert"
	SCREAMING_SNAKE_CASE  	=							{
	    "hidden_size": "dim",
	    "num_attention_heads": "n_heads",
	    "num_hidden_layers": "n_layers",
	}



	def __init__(    self				:     Optional[Any]					,						snake_case_				:     Tuple=3_05_22					,						snake_case_				:     Any=5_12					,						snake_case_				:     Dict=False					,						snake_case_				:     Tuple=6					,						snake_case_				:     Union[str, Any]=12					,						snake_case_				:     List[str]=7_68					,						snake_case_				:     Optional[Any]=4 * 7_68					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     Dict=0.1					,						snake_case_				:     Union[str, Any]="gelu"					,						snake_case_				:     Any=0.0_2					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     List[Any]=0.2					,						snake_case_				:     Optional[Any]=0					,						**snake_case_				:     List[Any]					,						)->				Tuple:
						__lowerCAmelCase						 =vocab_size
						__lowerCAmelCase						 =max_position_embeddings
						__lowerCAmelCase						 =sinusoidal_pos_embds
						__lowerCAmelCase						 =n_layers
						__lowerCAmelCase						 =n_heads
						__lowerCAmelCase						 =dim
						__lowerCAmelCase						 =hidden_dim
						__lowerCAmelCase						 =dropout
						__lowerCAmelCase						 =attention_dropout
						__lowerCAmelCase						 =activation
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =qa_dropout
						__lowerCAmelCase						 =seq_classif_dropout
						super().__init__(**snake_case_					,						pad_token_id=snake_case_)






class     __a			(  SCREAMING_SNAKE_CASE					):



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




