






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		:







					@staticmethod
					def 							UpperCamelCase				(     *UpperCAmelCase_:  int				,     **UpperCAmelCase_:  Union[str, Any]					):







											'''simple docstring'''




											pass



@is_pipeline_test
@require_vision
@require_timm
@require_torch
class 				__UpperCAmelCase		(unittest.TestCase   ):
			__snake_case    :				Tuple									=							MODEL_FOR_OBJECT_DETECTION_MAPPING







			def 							UpperCamelCase				(     self:  Dict				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  List[str]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  ObjectDetectionPipeline(model=UpperCAmelCase_				,     image_processor=UpperCAmelCase_					)
									return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







			def 							UpperCamelCase				(     self:  str				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  Optional[int]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  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

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

									_SCREAMING_SNAKE_CASE     				=  [
									    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"""],
									]
									_SCREAMING_SNAKE_CASE     				=  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 							UpperCamelCase				(     self:  List[Any]					):







									'''simple docstring'''




									pass







			@require_torch
			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  """hf-internal-testing/tiny-detr-mobilenetsv3"""

									_SCREAMING_SNAKE_CASE     				=  AutoModelForObjectDetection.from_pretrained(UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  AutoFeatureExtractor.from_pretrained(UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  ObjectDetectionPipeline(model=UpperCAmelCase_				,     feature_extractor=UpperCAmelCase_					)

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

									self.assertEqual(
									    nested_simplify(UpperCAmelCase_				,     decimals=4					)				,     [
									        {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									        {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									    ]				,     )

									_SCREAMING_SNAKE_CASE     				=  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.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									            {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									        ],
									        [
									            {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									            {"""score""": 0.33_76, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
									        ],
									    ]				,     )







			@require_torch
			@slow
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  """facebook/detr-resnet-50"""

									_SCREAMING_SNAKE_CASE     				=  AutoModelForObjectDetection.from_pretrained(UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  AutoFeatureExtractor.from_pretrained(UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  ObjectDetectionPipeline(model=UpperCAmelCase_				,     feature_extractor=UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
									self.assertEqual(
									    nested_simplify(UpperCAmelCase_				,     decimals=4					)				,     [
									        {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									        {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									        {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									        {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									        {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									    ]				,     )

									_SCREAMING_SNAKE_CASE     				=  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.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									            {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									            {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									            {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									            {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									        ],
									        [
									            {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									            {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									            {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									            {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									            {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									        ],
									    ]				,     )







			@require_torch
			@slow
			def 							UpperCamelCase				(     self:  str					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  """facebook/detr-resnet-50"""

									_SCREAMING_SNAKE_CASE     				=  pipeline("""object-detection"""				,     model=UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
									self.assertEqual(
									    nested_simplify(UpperCAmelCase_				,     decimals=4					)				,     [
									        {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									        {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									        {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									        {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									        {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									    ]				,     )

									_SCREAMING_SNAKE_CASE     				=  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.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									            {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									            {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									            {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									            {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									        ],
									        [
									            {"""score""": 0.99_82, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
									            {"""score""": 0.99_60, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
									            {"""score""": 0.99_55, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
									            {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									            {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									        ],
									    ]				,     )







			@require_torch
			@slow
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  0.99_85
									_SCREAMING_SNAKE_CASE     				=  """facebook/detr-resnet-50"""

									_SCREAMING_SNAKE_CASE     				=  pipeline("""object-detection"""				,     model=UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""				,     threshold=UpperCAmelCase_					)
									self.assertEqual(
									    nested_simplify(UpperCAmelCase_				,     decimals=4					)				,     [
									        {"""score""": 0.99_88, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
									        {"""score""": 0.99_87, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
									    ]				,     )







			@require_torch
			@require_pytesseract
			@slow
			def 							UpperCamelCase				(     self:  Tuple					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  """Narsil/layoutlmv3-finetuned-funsd"""
									_SCREAMING_SNAKE_CASE     				=  0.99_93

									_SCREAMING_SNAKE_CASE     				=  pipeline("""object-detection"""				,     model=UpperCAmelCase_				,     threshold=UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  object_detector(
									    """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"""					)
									self.assertEqual(
									    nested_simplify(UpperCAmelCase_				,     decimals=4					)				,     [
									        {"""score""": 0.99_93, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
									        {"""score""": 0.99_93, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
									    ]				,     )









import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class 				__UpperCAmelCase		(_UpperCAmelCase	,unittest.TestCase   ):
			__snake_case    :				Optional[Any]									=							OpenAIGPTTokenizer
			__snake_case    :				Dict									=							OpenAIGPTTokenizerFast
			__snake_case    :				Optional[Any]									=							True
			__snake_case    :				Dict									=							False







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







									'''simple docstring'''




									super().setUp()

									# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
									_SCREAMING_SNAKE_CASE     				=  [
									    """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>""",
									]
									_SCREAMING_SNAKE_CASE     				=  dict(zip(UpperCAmelCase_				,     range(len(UpperCAmelCase_					)					)					)					)
									_SCREAMING_SNAKE_CASE     				=  ["""#version: 0.2""", """l o""", """lo w""", """e r</w>""", """"""]

									_SCREAMING_SNAKE_CASE     				=  os.path.join(self.tmpdirname				,     VOCAB_FILES_NAMES["""vocab_file"""]					)
									_SCREAMING_SNAKE_CASE     				=  os.path.join(self.tmpdirname				,     VOCAB_FILES_NAMES["""merges_file"""]					)
									with open(self.vocab_file				,     """w"""					) as fp:
															fp.write(json.dumps(UpperCAmelCase_					)					)
									with open(self.merges_file				,     """w"""					) as fp:
															fp.write("""\n""".join(UpperCAmelCase_					)					)







			def 							UpperCamelCase				(     self:  List[Any]				,     UpperCAmelCase_:  Dict					):







									'''simple docstring'''




									return "lower newer", "lower newer"







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  OpenAIGPTTokenizer(self.vocab_file				,     self.merges_file					)

									_SCREAMING_SNAKE_CASE     				=  """lower"""
									_SCREAMING_SNAKE_CASE     				=  ["""low""", """er</w>"""]
									_SCREAMING_SNAKE_CASE     				=  tokenizer.tokenize(UpperCAmelCase_					)
									self.assertListEqual(UpperCAmelCase_				,     UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  tokens + ["""<unk>"""]
									_SCREAMING_SNAKE_CASE     				=  [14, 15, 20]
									self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_					)				,     UpperCAmelCase_					)







			def 							UpperCamelCase				(     self:  Optional[Any]				,     UpperCAmelCase_:  Union[str, Any]=15					):







									'''simple docstring'''




									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
															with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'					):
																					_SCREAMING_SNAKE_CASE     				=  self.rust_tokenizer_class.from_pretrained(UpperCAmelCase_				,     **UpperCAmelCase_					)

																					# Simple input
																					_SCREAMING_SNAKE_CASE     				=  """This is a simple input"""
																					_SCREAMING_SNAKE_CASE     				=  ["""This is a simple input 1""", """This is a simple input 2"""]
																					_SCREAMING_SNAKE_CASE     				=  ("""This is a simple input""", """This is a pair""")
																					_SCREAMING_SNAKE_CASE     				=  [
																					    ("""This is a simple input 1""", """This is a simple input 2"""),
																					    ("""This is a simple pair 1""", """This is a simple pair 2"""),
																					]

																					# Simple input tests
																					self.assertRaises(UpperCAmelCase_				,     tokenizer_r.encode				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""					)

																					# Simple input
																					self.assertRaises(UpperCAmelCase_				,     tokenizer_r.encode_plus				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""					)

																					# Simple input
																					self.assertRaises(
																					    UpperCAmelCase_				,     tokenizer_r.batch_encode_plus				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""				,     )

																					# Pair input
																					self.assertRaises(UpperCAmelCase_				,     tokenizer_r.encode				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""					)

																					# Pair input
																					self.assertRaises(UpperCAmelCase_				,     tokenizer_r.encode_plus				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""					)

																					# Pair input
																					self.assertRaises(
																					    UpperCAmelCase_				,     tokenizer_r.batch_encode_plus				,     UpperCAmelCase_				,     max_length=UpperCAmelCase_				,     padding="""max_length"""				,     )







			def 							UpperCamelCase				(     self:  List[Any]					):







									'''simple docstring'''




									pass



@require_ftfy
@require_spacy
@require_tokenizers
class 				__UpperCAmelCase		(_UpperCAmelCase   ):
			pass

