


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 						_lowerCamelCase					:




  '''simple docstring'''


  @staticmethod
  def  snake_case__   (      *__lowercase      ,      **__lowercase						):

   """simple docstring"""







   pass

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




 '''simple docstring'''
 __lowercase				:		Optional[Any]    							=						MODEL_FOR_OBJECT_DETECTION_MAPPING


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







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


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







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

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

  import datasets

  __A  :		Optional[int]       =			datasets.load_dataset('hf-internal-testing/fixtures_image_utils'      ,      'image'      ,      split='test'						)

  __A  :		str       =			[
      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'],
  ]
  __A  :		Optional[Any]       =			object_detector(__lowercase      ,      threshold=0.0						)

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


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

  """simple docstring"""







  pass


 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			'hf-internal-testing/tiny-detr-mobilenetsv3'

  __A  :		List[str]       =			AutoModelForObjectDetection.from_pretrained(__lowercase						)
  __A  :		List[str]       =			AutoFeatureExtractor.from_pretrained(__lowercase						)
  __A  :		Optional[int]       =			ObjectDetectionPipeline(model=__lowercase      ,      feature_extractor=__lowercase						)

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

  self.assertEqual(
      nested_simplify(__lowercase      ,      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}},
      ]      ,      )

  __A  :		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(__lowercase      ,      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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			'facebook/detr-resnet-50'

  __A  :		List[str]       =			AutoModelForObjectDetection.from_pretrained(__lowercase						)
  __A  :		int       =			AutoFeatureExtractor.from_pretrained(__lowercase						)
  __A  :		Any       =			ObjectDetectionPipeline(model=__lowercase      ,      feature_extractor=__lowercase						)

  __A  :		Dict       =			object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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}},
      ]      ,      )

  __A  :		Optional[Any]       =			object_detector(
      [
          'http://images.cocodataset.org/val2017/000000039769.jpg',
          'http://images.cocodataset.org/val2017/000000039769.jpg',
      ]						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Tuple       =			'facebook/detr-resnet-50'

  __A  :		List[Any]       =			pipeline('object-detection'      ,      model=__lowercase						)

  __A  :		Any       =			object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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}},
      ]      ,      )

  __A  :		Optional[int]       =			object_detector(
      [
          'http://images.cocodataset.org/val2017/000000039769.jpg',
          'http://images.cocodataset.org/val2017/000000039769.jpg',
      ]						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			0.9_9_8_5
  __A  :		Union[str, Any]       =			'facebook/detr-resnet-50'

  __A  :		str       =			pipeline('object-detection'      ,      model=__lowercase						)

  __A  :		int       =			object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'      ,      threshold=__lowercase						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			'Narsil/layoutlmv3-finetuned-funsd'
  __A  :		Optional[Any]       =			0.9_9_9_3

  __A  :		str       =			pipeline('object-detection'      ,      model=__lowercase      ,      threshold=__lowercase						)

  __A  :		List[str]       =			object_detector(
      'https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'						)
  self.assertEqual(
      nested_simplify(__lowercase      ,      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}},
      ]      ,      )










'''simple docstring'''
import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin

class 						_lowerCamelCase					(    snake_case_    ,		unittest.TestCase				):




 '''simple docstring'''
 __lowercase				:		Dict    							=						XLMTokenizer
 __lowercase				:		Any    							=						False


 def  snake_case__   (      self						):

  """simple docstring"""







  super().setUp()

  # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
  __A  :		Optional[int]       =			[
      'l',
      'o',
      'w',
      'e',
      'r',
      's',
      't',
      'i',
      'd',
      'n',
      'w</w>',
      'r</w>',
      't</w>',
      'lo',
      'low',
      'er</w>',
      'low</w>',
      'lowest</w>',
      'newer</w>',
      'wider</w>',
      '<unk>',
  ]
  __A  :		int       =			dict(zip(__lowercase      ,      range(len(__lowercase						)						)						)						)
  __A  :		str       =			['l o 123', 'lo w 1456', 'e r</w> 1789', '']

  __A  :		Dict       =			os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES['vocab_file']						)
  __A  :		List[Any]       =			os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES['merges_file']						)
  with open(self.vocab_file      ,      'w'						) as fp:
   fp.write(json.dumps(__lowercase						)						)
  with open(self.merges_file      ,      'w'						) as fp:
   fp.write('\n'.join(__lowercase						)						)


 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Any       =			'lower newer'
  __A  :		int       =			'lower newer'
  return input_text, output_text


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Any       =			XLMTokenizer(self.vocab_file      ,      self.merges_file						)

  __A  :		Optional[Any]       =			'lower'
  __A  :		Union[str, Any]       =			['low', 'er</w>']
  __A  :		Any       =			tokenizer.tokenize(__lowercase						)
  self.assertListEqual(__lowercase      ,      __lowercase						)

  __A  :		Optional[int]       =			tokens + ['<unk>']
  __A  :		Any       =			[14, 15, 20]
  self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase						)      ,      __lowercase						)







 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			XLMTokenizer.from_pretrained('xlm-mlm-en-2048'						)

  __A  :		Tuple       =			tokenizer.encode('sequence builders'      ,      add_special_tokens=__lowercase						)
  __A  :		Union[str, Any]       =			tokenizer.encode('multi-sequence build'      ,      add_special_tokens=__lowercase						)

  __A  :		int       =			tokenizer.build_inputs_with_special_tokens(__lowercase						)
  __A  :		Tuple       =			tokenizer.build_inputs_with_special_tokens(__lowercase      ,      __lowercase						)

  assert encoded_sentence == [0] + text + [1]
  assert encoded_pair == [0] + text + [1] + text_a + [1]






