




from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


SCREAMING_SNAKE_CASE:     List[Any]									= '''\
@inproceedings{wang2019glue,
  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},
  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},
  note={In the Proceedings of ICLR.},
  year={2019}
}
'''

SCREAMING_SNAKE_CASE:     Optional[Any]									= '''\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
'''

SCREAMING_SNAKE_CASE:     Union[str, Any]									= '''
Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    "accuracy": Accuracy
    "f1": F1 score
    "pearson": Pearson Correlation
    "spearmanr": Spearman Correlation
    "matthews_correlation": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\')  # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0}

    >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\')  # \'mrpc\' or \'qqp\'
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0, \'f1\': 1.0}

    >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\')
    >>> references = [0., 1., 2., 3., 4., 5.]
    >>> predictions = [0., 1., 2., 3., 4., 5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)})
    {\'pearson\': 1.0, \'spearmanr\': 1.0}

    >>> glue_metric = datasets.load_metric(\'glue\', \'cola\')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'matthews_correlation\': 1.0}
'''


def 					_a							(					lowerCAmelCase    ,						lowerCAmelCase )->	int:
		return float((preds == labels).mean() )


def 					_a							(					lowerCAmelCase    ,						lowerCAmelCase )->	List[str]:
		SCREAMING_SNAKE_CASE_					      =							simple_accuracy(lowerCAmelCase    ,						lowerCAmelCase )
		SCREAMING_SNAKE_CASE_					      =							float(fa_score(y_true=lowerCAmelCase    ,						y_pred=lowerCAmelCase ) )
		return {
		    "accuracy": acc,
		    "f1": fa,
		}


def 					_a							(					lowerCAmelCase    ,						lowerCAmelCase )->	Optional[Any]:
		SCREAMING_SNAKE_CASE_					      =							float(pearsonr(lowerCAmelCase    ,						lowerCAmelCase )[0] )
		SCREAMING_SNAKE_CASE_					      =							float(spearmanr(lowerCAmelCase    ,						lowerCAmelCase )[0] )
		return {
		    "pearson": pearson_corr,
		    "spearmanr": spearman_corr,
		}




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



						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								if self.config_name not in [
								    "sst2",
								    "mnli",
								    "mnli_mismatched",
								    "mnli_matched",
								    "cola",
								    "stsb",
								    "mrpc",
								    "qqp",
								    "qnli",
								    "rte",
								    "wnli",
								    "hans",
								]:
										raise KeyError(
										    'You should supply a configuration name selected in '
										    '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
										    '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'  )
								return datasets.MetricInfo(
								    description=_DESCRIPTION					,							citation=_CITATION					,							inputs_description=_KWARGS_DESCRIPTION					,							features=datasets.Features(
								        {
								            'predictions': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'  ),
								            'references': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'  ),
								        }  )					,							codebase_urls=[]					,							reference_urls=[]					,							format='numpy'					,							)






						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Optional[int]					,							snake_case__	:   Optional[Any]  ):

								"""simple docstring"""

								if self.config_name == "cola":
										return {"matthews_correlation": matthews_corrcoef(snake_case__					,							snake_case__  )}
								elif self.config_name == "stsb":
										return pearson_and_spearman(snake_case__					,							snake_case__  )
								elif self.config_name in ["mrpc", "qqp"]:
										return acc_and_fa(snake_case__					,							snake_case__  )
								elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
										return {"accuracy": simple_accuracy(snake_case__					,							snake_case__  )}
								else:
										raise KeyError(
										    'You should supply a configuration name selected in '
										    '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
										    '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'  )





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



												@staticmethod
												def 	__a					(			*snake_case__	:   List[Any]					,							**snake_case__	:   List[Any]  ):

														"""simple docstring"""

														pass



@is_pipeline_test
@require_vision
@require_torch
class    lowercase_		(unittest.TestCase ):
						lowerCAmelCase__			      =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



						def 	__a					(			self	:   Any					,							snake_case__	:   List[str]					,							snake_case__	:   int					,							snake_case__	:   Optional[int]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

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



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Union[str, Any]					,							snake_case__	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							object_detector(examples[0]					,							threshold=0.0  )

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



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



						@require_torch
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'					,							candidate_labels=['cat', 'remote', 'couch']					,							threshold=0.64					,							)

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,							)

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]					,							threshold=0.64					,							)

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,							)



						@require_torch
						@slow
						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,							)

								SCREAMING_SNAKE_CASE_					      =							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(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,							)



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



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

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							0.2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							threshold=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,							)






						@require_torch
						@slow
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							top_k=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,							)