






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





						"""simple docstring"""






						@staticmethod
						def  _lowercase  (      *__A  :						List[Any]							,	**__A  :						Optional[Any] ):
							pass



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





					"""simple docstring"""






					a_			=	MODEL_FOR_OBJECT_DETECTION_MAPPING



					def  _lowercase  (      self  :						Optional[int]							,	__A  :						Optional[int]							,	__A  :						Optional[int]							,	__A  :						Any ):
						snake_case__				:   Dict     =						ObjectDetectionPipeline(model=__A							,	image_processor=__A )
						return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]



					def  _lowercase  (      self  :						str							,	__A  :						int							,	__A  :						str ):
						snake_case__				:   List[str]     =						object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png"							,	threshold=0.0 )

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

						import datasets

						snake_case__				:   List[Any]     =						datasets.load_dataset("hf-internal-testing/fixtures_image_utils"							,	"image"							,	split="test" )

						snake_case__				:   List[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"],
						]
						snake_case__				:   List[Any]     =						object_detector(__A							,	threshold=0.0 )

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



					@require_tf
					@unittest.skip("Object detection not implemented in TF" )
					def  _lowercase  (      self  :						List[str] ):
						pass



					@require_torch
					def  _lowercase  (      self  :						str ):
						snake_case__				:   Optional[int]     =						"hf-internal-testing/tiny-detr-mobilenetsv3"

						snake_case__				:   List[str]     =						AutoModelForObjectDetection.from_pretrained(__A )
						snake_case__				:   str     =						AutoFeatureExtractor.from_pretrained(__A )
						snake_case__				:   Tuple     =						ObjectDetectionPipeline(model=__A							,	feature_extractor=__A )

						snake_case__				:   List[Any]     =						object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							,	threshold=0.0 )

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

						snake_case__				:   List[Any]     =						object_detector(
						    [
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						    ]							,	threshold=0.0							,	)

						self.assertEqual(
						    nested_simplify(__A							,	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  _lowercase  (      self  :						Union[str, Any] ):
						snake_case__				:   Optional[Any]     =						"facebook/detr-resnet-50"

						snake_case__				:   Tuple     =						AutoModelForObjectDetection.from_pretrained(__A )
						snake_case__				:   Union[str, Any]     =						AutoFeatureExtractor.from_pretrained(__A )
						snake_case__				:   Dict     =						ObjectDetectionPipeline(model=__A							,	feature_extractor=__A )

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

						snake_case__				:   List[str]     =						object_detector(
						    [
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						    ] )
						self.assertEqual(
						    nested_simplify(__A							,	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  _lowercase  (      self  :						Dict ):
						snake_case__				:   Dict     =						"facebook/detr-resnet-50"

						snake_case__				:   Optional[Any]     =						pipeline("object-detection"							,	model=__A )

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

						snake_case__				:   Optional[Any]     =						object_detector(
						    [
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						        "http://images.cocodataset.org/val2017/000000039769.jpg",
						    ] )
						self.assertEqual(
						    nested_simplify(__A							,	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  _lowercase  (      self  :						Optional[Any] ):
						snake_case__				:   int     =						0.9_9_8_5
						snake_case__				:   List[Any]     =						"facebook/detr-resnet-50"

						snake_case__				:   int     =						pipeline("object-detection"							,	model=__A )

						snake_case__				:   int     =						object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							,	threshold=__A )
						self.assertEqual(
						    nested_simplify(__A							,	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  _lowercase  (      self  :						Any ):
						snake_case__				:   Any     =						"Narsil/layoutlmv3-finetuned-funsd"
						snake_case__				:   List[Any]     =						0.9_9_9_3

						snake_case__				:   Union[str, Any]     =						pipeline("object-detection"							,	model=__A							,	threshold=__A )

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











from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowerCamelCase      :    List[str]   		=   {"""configuration_xlnet""": ["""XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLNetConfig"""]}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase      :    str   		=   ["""XLNetTokenizer"""]

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase      :    Dict   		=   ["""XLNetTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase      :    str   		=   [
	    """XLNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """XLNetForMultipleChoice""",
	    """XLNetForQuestionAnswering""",
	    """XLNetForQuestionAnsweringSimple""",
	    """XLNetForSequenceClassification""",
	    """XLNetForTokenClassification""",
	    """XLNetLMHeadModel""",
	    """XLNetModel""",
	    """XLNetPreTrainedModel""",
	    """load_tf_weights_in_xlnet""",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase      :    Dict   		=   [
	    """TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TFXLNetForMultipleChoice""",
	    """TFXLNetForQuestionAnsweringSimple""",
	    """TFXLNetForSequenceClassification""",
	    """TFXLNetForTokenClassification""",
	    """TFXLNetLMHeadModel""",
	    """TFXLNetMainLayer""",
	    """TFXLNetModel""",
	    """TFXLNetPreTrainedModel""",
	]


if TYPE_CHECKING:
	from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlnet import XLNetTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlnet_fast import XLNetTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xlnet import (
		    XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XLNetForMultipleChoice,
		    XLNetForQuestionAnswering,
		    XLNetForQuestionAnsweringSimple,
		    XLNetForSequenceClassification,
		    XLNetForTokenClassification,
		    XLNetLMHeadModel,
		    XLNetModel,
		    XLNetPreTrainedModel,
		    load_tf_weights_in_xlnet,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_xlnet import (
		    TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXLNetForMultipleChoice,
		    TFXLNetForQuestionAnsweringSimple,
		    TFXLNetForSequenceClassification,
		    TFXLNetForTokenClassification,
		    TFXLNetLMHeadModel,
		    TFXLNetMainLayer,
		    TFXLNetModel,
		    TFXLNetPreTrainedModel,
		)

else:
	import sys

	__lowerCamelCase      :    int   		=   _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



