






"""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        a     :







        @staticmethod
        def 			UpperCamelCase_      (			*_lowerCamelCase							,		**_lowerCamelCase       ):
          pass

@is_pipeline_test
@require_vision
@require_timm
@require_torch
class        a     (   unittest.TestCase	):
    UpperCAmelCase_ :     Optional[int]	     =MODEL_FOR_OBJECT_DETECTION_MAPPING







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       ObjectDetectionPipeline(model=_lowerCamelCase							,		image_processor=_lowerCamelCase       )
      return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       object_detector('./tests/fixtures/tests_samples/COCO/000000039769.png'							,		threshold=0.0       )

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

      import datasets

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

      lowercase							    =       [
          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							    =       object_detector(_lowerCamelCase							,		threshold=0.0       )

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







    @require_tf
    @unittest.skip('Object detection not implemented in TF'       )
    def 			UpperCamelCase_      (			self       ):
      pass







    @require_torch
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       'hf-internal-testing/tiny-detr-mobilenetsv3'

      lowercase							    =       AutoModelForObjectDetection.from_pretrained(_lowerCamelCase       )
      lowercase							    =       AutoFeatureExtractor.from_pretrained(_lowerCamelCase       )
      lowercase							    =       ObjectDetectionPipeline(model=_lowerCamelCase							,		feature_extractor=_lowerCamelCase       )

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

      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
              {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
          ]							,		)

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

      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              [
                  {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
                  {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
              ],
              [
                  {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
                  {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_5_9, 'ymin': 1_2_0, 'xmax': 4_8_0, 'ymax': 3_5_9}},
              ],
          ]							,		)







    @require_torch
    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       'facebook/detr-resnet-50'

      lowercase							    =       AutoModelForObjectDetection.from_pretrained(_lowerCamelCase       )
      lowercase							    =       AutoFeatureExtractor.from_pretrained(_lowerCamelCase       )
      lowercase							    =       ObjectDetectionPipeline(model=_lowerCamelCase							,		feature_extractor=_lowerCamelCase       )

      lowercase							    =       object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
              {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
              {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
              {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
              {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
          ]							,		)

      lowercase							    =       object_detector(
          [
              'http://images.cocodataset.org/val2017/000000039769.jpg',
              'http://images.cocodataset.org/val2017/000000039769.jpg',
          ]       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              [
                  {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
                  {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
                  {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
                  {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
                  {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
              ],
              [
                  {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
                  {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
                  {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
                  {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
                  {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
              ],
          ]							,		)







    @require_torch
    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       'facebook/detr-resnet-50'

      lowercase							    =       pipeline('object-detection'							,		model=_lowerCamelCase       )

      lowercase							    =       object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
              {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
              {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
              {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
              {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
          ]							,		)

      lowercase							    =       object_detector(
          [
              'http://images.cocodataset.org/val2017/000000039769.jpg',
              'http://images.cocodataset.org/val2017/000000039769.jpg',
          ]       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              [
                  {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
                  {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
                  {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
                  {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
                  {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
              ],
              [
                  {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_0, 'xmax': 1_7_5, 'ymax': 1_1_7}},
                  {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_3_3, 'ymin': 7_2, 'xmax': 3_6_8, 'ymax': 1_8_7}},
                  {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_3_9, 'ymax': 4_7_3}},
                  {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
                  {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
              ],
          ]							,		)







    @require_torch
    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       0.9_9_8_5
      lowercase							    =       'facebook/detr-resnet-50'

      lowercase							    =       pipeline('object-detection'							,		model=_lowerCamelCase       )

      lowercase							    =       object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'							,		threshold=_lowerCamelCase       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 1_3, 'ymin': 5_2, 'xmax': 3_1_4, 'ymax': 4_7_0}},
              {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_4_5, 'ymin': 2_3, 'xmax': 6_4_0, 'ymax': 3_6_8}},
          ]							,		)







    @require_torch
    @require_pytesseract
    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       'Narsil/layoutlmv3-finetuned-funsd'
      lowercase							    =       0.9_9_9_3

      lowercase							    =       pipeline('object-detection'							,		model=_lowerCamelCase							,		threshold=_lowerCamelCase       )

      lowercase							    =       object_detector(
          'https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[
              {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_9_4, 'ymin': 2_5_4, 'xmax': 3_4_3, 'ymax': 2_6_4}},
              {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_9_4, 'ymin': 2_5_4, 'xmax': 3_4_3, 'ymax': 2_6_4}},
          ]							,		)










import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin
class       _UpperCAmelCase					(       A__		,unittest.TestCase	):

				"""simple docstring"""




				lowercase__											=       CTRLTokenizer
				lowercase__											=       False
				lowercase__											=       False




				def 				lowercase__   (     self						:    Union[str, Any]  ):

								'''simple docstring'''
								super().setUp()

								# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
								lowercase__    =			['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>''']
								lowercase__    =			dict(zip(lowerCamelCase,  range(len(lowerCamelCase  )  )  )  )
								lowercase__    =			['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', '''''']
								lowercase__    =			{'''unk_token''': '''<unk>'''}

								lowercase__    =			os.path.join(self.tmpdirname,  VOCAB_FILES_NAMES['''vocab_file''']  )
								lowercase__    =			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(lowerCamelCase  ) + '''\n'''  )
								with open(self.merges_file,  '''w''',  encoding='''utf-8'''  ) as fp:
												fp.write('''\n'''.join(lowerCamelCase  )  )




				def 				lowercase__   (     self						:    Union[str, Any],  **lowerCamelCase						:    Dict  ):

								'''simple docstring'''
								kwargs.update(self.special_tokens_map  )
								return CTRLTokenizer.from_pretrained(self.tmpdirname,  **lowerCamelCase  )




				def 				lowercase__   (     self						:    Dict,  lowerCamelCase						:    Optional[int]  ):

								'''simple docstring'''
								lowercase__    =			'''adapt react readapt apt'''
								lowercase__    =			'''adapt react readapt apt'''
								return input_text, output_text




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								lowercase__    =			CTRLTokenizer(self.vocab_file,  self.merges_file,  **self.special_tokens_map  )
								lowercase__    =			'''adapt react readapt apt'''
								lowercase__    =			'''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split()
								lowercase__    =			tokenizer.tokenize(lowerCamelCase  )
								self.assertListEqual(lowerCamelCase,  lowerCamelCase  )

								lowercase__    =			tokens + [tokenizer.unk_token]

								lowercase__    =			[0, 1, 2, 4, 5, 1, 0, 3, 6]
								self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase  ),  lowerCamelCase  )




