





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   :






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

@is_pipeline_test
@require_vision
@require_timm
@require_torch
class     __SCREAMING_SNAKE_CASE   (       unittest.TestCase    ):
							snake_case       : Any         =    MODEL_FOR_OBJECT_DETECTION_MAPPING






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






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     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

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

														UpperCamelCase__			       =     [
														    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__			       =     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 			_lowerCamelCase						(  self					):
														pass






							@require_torch
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     """hf-internal-testing/tiny-detr-mobilenetsv3"""

														UpperCamelCase__			       =     AutoModelForObjectDetection.from_pretrained(__lowerCAmelCase					)
														UpperCamelCase__			       =     AutoFeatureExtractor.from_pretrained(__lowerCAmelCase					)
														UpperCamelCase__			       =     ObjectDetectionPipeline(model=__lowerCAmelCase					,	feature_extractor=__lowerCAmelCase					)

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

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

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






							@require_torch
							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     """facebook/detr-resnet-50"""

														UpperCamelCase__			       =     AutoModelForObjectDetection.from_pretrained(__lowerCAmelCase					)
														UpperCamelCase__			       =     AutoFeatureExtractor.from_pretrained(__lowerCAmelCase					)
														UpperCamelCase__			       =     ObjectDetectionPipeline(model=__lowerCAmelCase					,	feature_extractor=__lowerCAmelCase					)

														UpperCamelCase__			       =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
														self.assertEqual(
														    nested_simplify(__lowerCAmelCase					,	decimals=4					)					,	[
														        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														    ]					,	)

														UpperCamelCase__			       =     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.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														        ],
														        [
														            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														        ],
														    ]					,	)






							@require_torch
							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     """facebook/detr-resnet-50"""

														UpperCamelCase__			       =     pipeline("""object-detection"""					,	model=__lowerCAmelCase					)

														UpperCamelCase__			       =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
														self.assertEqual(
														    nested_simplify(__lowerCAmelCase					,	decimals=4					)					,	[
														        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														    ]					,	)

														UpperCamelCase__			       =     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.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														        ],
														        [
														            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
														            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
														            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
														            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
														            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
														        ],
														    ]					,	)






							@require_torch
							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     0.9985
														UpperCamelCase__			       =     """facebook/detr-resnet-50"""

														UpperCamelCase__			       =     pipeline("""object-detection"""					,	model=__lowerCAmelCase					)

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







							@require_torch
							@require_pytesseract
							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     """Narsil/layoutlmv3-finetuned-funsd"""
														UpperCamelCase__			       =     0.9993

														UpperCamelCase__			       =     pipeline("""object-detection"""					,	model=__lowerCAmelCase					,	threshold=__lowerCAmelCase					)

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











from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__     =	logging.get_logger(__name__)

UpperCamelCase__     =	{
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class     __SCREAMING_SNAKE_CASE   (       _a    ):
							snake_case       : Dict         =    """speech_to_text_2"""
							snake_case       : List[Any]         =    ["""past_key_values"""]
							snake_case       : List[str]         =    {"""num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model"""}






							def __init__(  self					,	__lowerCAmelCase=10000					,	__lowerCAmelCase=6					,	__lowerCAmelCase=2048					,	__lowerCAmelCase=4					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=True					,	__lowerCAmelCase="relu"					,	__lowerCAmelCase=256					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=0.0					,	__lowerCAmelCase=0.02					,	__lowerCAmelCase=2					,	__lowerCAmelCase=True					,	__lowerCAmelCase=1					,	__lowerCAmelCase=0					,	__lowerCAmelCase=2					,	__lowerCAmelCase=1024					,	**__lowerCAmelCase					,	):
														UpperCamelCase__			       =     vocab_size
														UpperCamelCase__			       =     d_model
														UpperCamelCase__			       =     decoder_ffn_dim
														UpperCamelCase__			       =     decoder_layers
														UpperCamelCase__			       =     decoder_attention_heads
														UpperCamelCase__			       =     dropout
														UpperCamelCase__			       =     attention_dropout
														UpperCamelCase__			       =     activation_dropout
														UpperCamelCase__			       =     activation_function
														UpperCamelCase__			       =     init_std
														UpperCamelCase__			       =     decoder_layerdrop
														UpperCamelCase__			       =     use_cache
														UpperCamelCase__			       =     decoder_layers
														UpperCamelCase__			       =     scale_embedding  # scale factor will be sqrt(d_model) if True
														UpperCamelCase__			       =     max_target_positions

														super().__init__(
														    pad_token_id=__lowerCAmelCase					,	bos_token_id=__lowerCAmelCase					,	eos_token_id=__lowerCAmelCase					,	decoder_start_token_id=__lowerCAmelCase					,	**__lowerCAmelCase					,	)




