
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   _lowerCamelCase							(   *a						:Tuple	,  **a						:Any		)						->					int:
										pass




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




	'''simple docstring'''




	_A								=	MODEL_FOR_OBJECT_DETECTION_MAPPING







	def   _lowerCamelCase							(   self						:int	,  a						:Any	,  a						:Union[str, Any]	,  a						:List[str]		)						->					str:
								__UpperCamelCase      :  Union[str, Any]       =   ObjectDetectionPipeline(model=a	,  image_processor=a		)
								return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







	def   _lowerCamelCase							(   self						:Optional[Any]	,  a						:Tuple	,  a						:Any		)						->					Dict:
								__UpperCamelCase      :  Tuple       =   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

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

								__UpperCamelCase      :  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"],
								]
								__UpperCamelCase      :  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   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Any:
								pass







	@require_torch
	def   _lowerCamelCase							(   self						:Optional[int]		)						->					Optional[int]:
								__UpperCamelCase      :  Any       =   "hf-internal-testing/tiny-detr-mobilenetsv3"

								__UpperCamelCase      :  Optional[int]       =   AutoModelForObjectDetection.from_pretrained(a		)
								__UpperCamelCase      :  Tuple       =   AutoFeatureExtractor.from_pretrained(a		)
								__UpperCamelCase      :  List[Any]       =   ObjectDetectionPipeline(model=a	,  feature_extractor=a		)

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

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

								__UpperCamelCase      :  Dict       =   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.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								        ],
								        [
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								        ],
								    ]	,  )







	@require_torch
	@slow
	def   _lowerCamelCase							(   self						:int		)						->					int:
								__UpperCamelCase      :  List[str]       =   "facebook/detr-resnet-50"

								__UpperCamelCase      :  Union[str, Any]       =   AutoModelForObjectDetection.from_pretrained(a		)
								__UpperCamelCase      :  List[str]       =   AutoFeatureExtractor.from_pretrained(a		)
								__UpperCamelCase      :  Dict       =   ObjectDetectionPipeline(model=a	,  feature_extractor=a		)

								__UpperCamelCase      :  Optional[Any]       =   object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"		)
								self.assertEqual(
								    nested_simplify(a	,  decimals=4		)	,  [
								        {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								        {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								        {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]	,  )

								__UpperCamelCase      :  Union[str, 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.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								    ]	,  )







	@require_torch
	@slow
	def   _lowerCamelCase							(   self						:Tuple		)						->					Tuple:
								__UpperCamelCase      :  Union[str, Any]       =   "facebook/detr-resnet-50"

								__UpperCamelCase      :  Union[str, Any]       =   pipeline("object-detection"	,  model=a		)

								__UpperCamelCase      :  List[Any]       =   object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"		)
								self.assertEqual(
								    nested_simplify(a	,  decimals=4		)	,  [
								        {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								        {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								        {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]	,  )

								__UpperCamelCase      :  Dict       =   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.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								    ]	,  )







	@require_torch
	@slow
	def   _lowerCamelCase							(   self						:Tuple		)						->					str:
								__UpperCamelCase      :  Optional[Any]       =   0.9985
								__UpperCamelCase      :  Dict       =   "facebook/detr-resnet-50"

								__UpperCamelCase      :  List[Any]       =   pipeline("object-detection"	,  model=a		)

								__UpperCamelCase      :  Optional[int]       =   object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"	,  threshold=a		)
								self.assertEqual(
								    nested_simplify(a	,  decimals=4		)	,  [
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]	,  )







	@require_torch
	@require_pytesseract
	@slow
	def   _lowerCamelCase							(   self						:Tuple		)						->					str:
								__UpperCamelCase      :  Dict       =   "Narsil/layoutlmv3-finetuned-funsd"
								__UpperCamelCase      :  List[str]       =   0.9993

								__UpperCamelCase      :  Any       =   pipeline("object-detection"	,  model=a	,  threshold=a		)

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

import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
		import tensorflow as tf

if is_tensorflow_text_available():
		from transformers.models.bert import TFBertTokenizer


lowercase    :   Union[str, Any]						=			['bert-base-uncased', 'bert-base-cased']
lowercase    :   Any						=			'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():




		class 					lowerCamelCase__				(							tf.keras.Model):




			'''simple docstring'''




			def __init__(   self						:List[Any]	,  a						:int		)						->					int:
										super().__init__()
										__UpperCamelCase      :  Any       =   tokenizer
										__UpperCamelCase      :  Union[str, Any]       =   AutoConfig.from_pretrained(a		)
										__UpperCamelCase      :  Dict       =   TFAutoModel.from_config(a		)







			def   _lowerCamelCase							(   self						:List[Any]	,  a						:Dict		)						->					Any:
										__UpperCamelCase      :  int       =   self.tokenizer(a		)
										__UpperCamelCase      :  int       =   self.bert(**a		)
										return out["pooler_output"]




@require_tf
@require_tensorflow_text
class 					lowerCamelCase__				(							unittest.TestCase):




	'''simple docstring'''




	def   _lowerCamelCase							(   self						:str		)						->					str:
								super().setUp()

								__UpperCamelCase      :  Dict       =   [
								    BertTokenizer.from_pretrained(a		) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
								]  # repeat for when fast_bert_tokenizer=false
								__UpperCamelCase      :  List[Any]       =   [TFBertTokenizer.from_pretrained(a		) for checkpoint in TOKENIZER_CHECKPOINTS] + [
								    TFBertTokenizer.from_pretrained(a	,  use_fast_bert_tokenizer=a		)
								    for checkpoint in TOKENIZER_CHECKPOINTS
								]
								assert len(self.tokenizers		) == len(self.tf_tokenizers		)

								__UpperCamelCase      :  Dict       =   [
								    "This is a straightforward English test sentence.",
								    "This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.",
								    "Now we're going to add some Chinese: 一 二 三 一二三",
								    "And some much more rare Chinese: 齉 堃 齉堃",
								    "Je vais aussi écrire en français pour tester les accents",
								    "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ",
								]
								__UpperCamelCase      :  Dict       =   list(zip(self.test_sentences	,  self.test_sentences[::-1]		)		)







	def   _lowerCamelCase							(   self						:List[Any]		)						->					int:
								for tokenizer, tf_tokenizer in zip(self.tokenizers	,  self.tf_tokenizers		):
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						__UpperCamelCase      :  str       =   tokenizer(a	,  return_tensors="tf"	,  padding="longest"		)
																						__UpperCamelCase      :  Any       =   tf_tokenizer(a		)

																						for key in python_outputs.keys():
																													self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape		)		)
																													self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]	,  tf.intaa		) == tf_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Any:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  List[str]       =   tf_tokenizer(self.paired_sentences		)
															__UpperCamelCase      :  Union[str, Any]       =   tf_tokenizer(
															    text=[sentence[0] for sentence in self.paired_sentences]	,  text_pair=[sentence[1] for sentence in self.paired_sentences]	,  )
															for key in merged_outputs.keys():
																						self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]	,  tf.intaa		) == separated_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:Any		)						->					Optional[int]:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  Optional[Any]       =   tf.function(a		)
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						__UpperCamelCase      :  Tuple       =   tf.constant(a		)
																						__UpperCamelCase      :  List[str]       =   compiled_tokenizer(a		)
																						__UpperCamelCase      :  Any       =   tf_tokenizer(a		)

																						for key in eager_outputs.keys():
																													self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]		)		)







	@slow
	def   _lowerCamelCase							(   self						:List[Any]		)						->					Tuple:
								for tf_tokenizer in self.tf_tokenizers:
															__UpperCamelCase      :  str       =   ModelToSave(tokenizer=a		)
															__UpperCamelCase      :  List[Any]       =   tf.convert_to_tensor(self.test_sentences		)
															__UpperCamelCase      :  Optional[int]       =   model(a		)  # Build model with some sample inputs
															with TemporaryDirectory() as tempdir:
																						__UpperCamelCase      :  Optional[Any]       =   Path(a		) / "saved.model"
																						model.save(a		)
																						__UpperCamelCase      :  List[Any]       =   tf.keras.models.load_model(a		)
															__UpperCamelCase      :  Optional[Any]       =   loaded_model(a		)
															# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
															self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output		)		)	,  1E-5		)