





'''simple docstring'''

import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    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__   (*A__      :	Optional[Any]							,			**A__      :	List[str]				)					-> str:
									pass

@is_pipeline_test
@require_vision
@require_torch
class       UpperCAmelCase (						unittest.TestCase  ):
		UpperCAmelCase					:	List[str] 			=		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING




		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Dict							,			A__      :	Optional[int]							,			A__      :	int				)					-> Optional[int]:
						lowercase         = pipeline(
						    "zero-shot-object-detection"							,			model="hf-internal-testing/tiny-random-owlvit-object-detection"				)

						lowercase         = [
						    {
						        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
						        "candidate_labels": ["cat", "remote", "couch"],
						    }
						]
						return object_detector, examples




		def 				UpperCAmelCase__   (self      :	Union[str, Any]							,			A__      :	Optional[int]							,			A__      :	int				)					-> Dict:
						lowercase         = object_detector(examples[0]							,			threshold=0.0				)

						lowercase         = len(A__				)
						self.assertGreater(A__							,			0				)
						self.assertEqual(
						    A__							,			[
						        {
						            "score": ANY(A__				),
						            "label": ANY(A__				),
						            "box": {"xmin": ANY(A__				), "ymin": ANY(A__				), "xmax": ANY(A__				), "ymax": ANY(A__				)},
						        }
						        for i in range(A__				)
						    ]							,			)




		@require_tf
		@unittest.skip("Zero Shot Object Detection not implemented in TF"				)
		def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> Tuple:
						pass




		@require_torch
		def 				UpperCAmelCase__   (self      :	Tuple				)					-> List[Any]:
						lowercase         = pipeline(
						    "zero-shot-object-detection"							,			model="hf-internal-testing/tiny-random-owlvit-object-detection"				)

						lowercase         = object_detector(
						    "./tests/fixtures/tests_samples/COCO/000000039769.png"							,			candidate_labels=["cat", "remote", "couch"]							,			threshold=0.6_4							,			)

						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						        {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						        {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						        {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						        {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						        {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						        {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
						        {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
						        {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
						    ]							,			)

						lowercase         = object_detector(
						    [
						        {
						            "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
						            "candidate_labels": ["cat", "remote", "couch"],
						        }
						    ]							,			threshold=0.6_4							,			)

						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        [
						            {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						            {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						            {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
						            {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						            {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						            {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
						            {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
						            {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
						            {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
						        ]
						    ]							,			)




		@require_torch
		@slow
		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> List[str]:
						lowercase         = pipeline("zero-shot-object-detection"				)

						lowercase         = object_detector(
						    "http://images.cocodataset.org/val2017/000000039769.jpg"							,			candidate_labels=["cat", "remote", "couch"]							,			)
						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
						        {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
						        {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
						        {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
						        {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
						    ]							,			)

						lowercase         = object_detector(
						    [
						        {
						            "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
						            "candidate_labels": ["cat", "remote", "couch"],
						        },
						        {
						            "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
						            "candidate_labels": ["cat", "remote", "couch"],
						        },
						    ]							,			)
						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        [
						            {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
						            {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
						            {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
						            {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
						            {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
						        ],
						        [
						            {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
						            {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
						            {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
						            {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
						            {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
						        ],
						    ]							,			)




		@require_tf
		@unittest.skip("Zero Shot Object Detection not implemented in TF"				)
		def 				UpperCAmelCase__   (self      :	List[str]				)					-> int:
						pass




		@require_torch
		@slow
		def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> Optional[Any]:
						lowercase         = 0.2
						lowercase         = pipeline("zero-shot-object-detection"				)

						lowercase         = object_detector(
						    "http://images.cocodataset.org/val2017/000000039769.jpg"							,			candidate_labels=["cat", "remote", "couch"]							,			threshold=A__							,			)
						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
						        {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
						        {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
						    ]							,			)





		@require_torch
		@slow
		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Tuple:
						lowercase         = 2
						lowercase         = pipeline("zero-shot-object-detection"				)

						lowercase         = object_detector(
						    "http://images.cocodataset.org/val2017/000000039769.jpg"							,			candidate_labels=["cat", "remote", "couch"]							,			top_k=A__							,			)
						self.assertEqual(
						    nested_simplify(A__							,			decimals=4				)							,			[
						        {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
						        {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
						    ]							,			)











'''simple docstring'''

import datasets

from .evaluate import evaluate


__lowerCamelCase : Optional[Any]											=			"\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

__lowerCamelCase : Any											=			"\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

__lowerCamelCase : Union[str, Any]											=			"\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,       _KWARGS_DESCRIPTION  )
class       UpperCAmelCase (						datasets.Metric  ):




		def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> Union[str, Any]:
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
						        {
						            "predictions": {"id": datasets.Value("string"				), "prediction_text": datasets.Value("string"				)},
						            "references": {
						                "id": datasets.Value("string"				),
						                "answers": datasets.features.Sequence(
						                    {
						                        "text": datasets.Value("string"				),
						                        "answer_start": datasets.Value("int32"				),
						                    }				),
						            },
						        }				)							,			codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			)





		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	List[Any]							,			A__      :	List[str]				)					-> str:
						lowercase         = {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
						lowercase         = [
						    {
						        "paragraphs": [
						            {
						                "qas": [
						                    {
						                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
						                        "id": ref["id"],
						                    }
						                    for ref in references
						                ]
						            }
						        ]
						    }
						]
						lowercase         = evaluate(dataset=A__							,			predictions=A__				)
						return score




