

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




									"""simple docstring"""







									@staticmethod
									def        _SCREAMING_SNAKE_CASE			( *lowerCAmelCase__  :     Tuple   ,						**lowerCAmelCase__  :     Optional[Any]):
															pass



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




			"""simple docstring"""







			_UpperCAmelCase   :							Union[str, Any]										=     MODEL_FOR_OBJECT_DETECTION_MAPPING



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Tuple):
									SCREAMING_SNAKE_CASE_: List[str]  		=       ObjectDetectionPipeline(model=_a   ,						image_processor=_a)
									return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Dict  		=       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

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

									SCREAMING_SNAKE_CASE_: Tuple  		=       [
									    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"],
									]
									SCREAMING_SNAKE_CASE_: int  		=       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        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									pass



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

									SCREAMING_SNAKE_CASE_: Tuple  		=       AutoModelForObjectDetection.from_pretrained(_a)
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       AutoFeatureExtractor.from_pretrained(_a)
									SCREAMING_SNAKE_CASE_: int  		=       ObjectDetectionPipeline(model=_a   ,						feature_extractor=_a)

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

									SCREAMING_SNAKE_CASE_: 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.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        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									SCREAMING_SNAKE_CASE_: Any  		=       "facebook/detr-resnet-50"

									SCREAMING_SNAKE_CASE_: Dict  		=       AutoModelForObjectDetection.from_pretrained(_a)
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       AutoFeatureExtractor.from_pretrained(_a)
									SCREAMING_SNAKE_CASE_: str  		=       ObjectDetectionPipeline(model=_a   ,						feature_extractor=_a)

									SCREAMING_SNAKE_CASE_: 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": 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}},
									    ]   ,						)

									SCREAMING_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.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        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       "facebook/detr-resnet-50"

									SCREAMING_SNAKE_CASE_: Any  		=       pipeline("object-detection"   ,						model=_a)

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

									SCREAMING_SNAKE_CASE_: int  		=       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": 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        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: List[str]  		=       0.9985
									SCREAMING_SNAKE_CASE_: Any  		=       "facebook/detr-resnet-50"

									SCREAMING_SNAKE_CASE_: Tuple  		=       pipeline("object-detection"   ,						model=_a)

									SCREAMING_SNAKE_CASE_: Dict  		=       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": 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        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: int  		=       "Narsil/layoutlmv3-finetuned-funsd"
									SCREAMING_SNAKE_CASE_: List[Any]  		=       0.9993

									SCREAMING_SNAKE_CASE_: Any  		=       pipeline("object-detection"   ,						model=_a   ,						threshold=_a)

									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       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": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
									        {"score": 0.9993, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
									    ]   ,						)









'''simple docstring'''
import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


lowerCamelCase :      List[Any]					 =     "\\n    @inproceedings{kakwani2020indicnlpsuite,\n    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n    year={2020},\n    booktitle={Findings of EMNLP},\n}\n"

lowerCamelCase :      Optional[Any]					 =     "\\n    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n"

lowerCamelCase :      int					 =     "\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n    predictions: list of predictions to score (as int64),\n        except for 'cvit-mkb-clsr' where each prediction is a vector (of float32).\n    references: list of ground truth labels corresponding to the predictions (as int64),\n        except for 'cvit-mkb-clsr' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n    \"accuracy\": Accuracy\n    \"f1\": F1 score\n    \"precision\": Precision@10\nExamples:\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wnli')  # 'wnli' or any of [\"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wiki-ner')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0, 'f1': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'cvit-mkb-clsr')\n    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'precision@10': 1.0}\n\n"


def        _lowerCAmelCase					(						_UpperCamelCase						:     List[str]							, _UpperCamelCase						:     Tuple				)      ->			List[Any]:
     """simple docstring"""




     return float((preds == labels).mean()				)


def        _lowerCAmelCase					(						_UpperCamelCase						:     Union[str, Any]							, _UpperCamelCase						:     Any				)      ->			List[str]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =simple_accuracy(_UpperCamelCase							, _UpperCamelCase				)
     _SCREAMING_SNAKE_CASE							    =float(fa_score(y_true=_UpperCamelCase							, y_pred=_UpperCamelCase				)				)
     return {
         "accuracy": acc,
         "f1": fa,
     }


def        _lowerCAmelCase					(						_UpperCamelCase						:     Any							, _UpperCamelCase						:     int				)      ->			str:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =np.array(_UpperCamelCase				)
     _SCREAMING_SNAKE_CASE							    =np.array(_UpperCamelCase				)
     _SCREAMING_SNAKE_CASE							    =en_sentvecs.shape[0]

     # mean centering
     _SCREAMING_SNAKE_CASE							    =en_sentvecs - np.mean(_UpperCamelCase							, axis=0				)
     _SCREAMING_SNAKE_CASE							    =in_sentvecs - np.mean(_UpperCamelCase							, axis=0				)

     _SCREAMING_SNAKE_CASE							    =cdist(_UpperCamelCase							, _UpperCamelCase							, 'cosine'				)
     _SCREAMING_SNAKE_CASE							    =np.array(range(_UpperCamelCase				)				)
     _SCREAMING_SNAKE_CASE							    =sim.argsort(axis=1				)[:, :10]
     _SCREAMING_SNAKE_CASE							    =np.any(preds == actual[:, None]							, axis=1				)
     return float(matches.mean()				)







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





  def 						A  (		self      :	Any )	->    List[str]:





       '''simple docstring'''






       if self.config_name not in [
           "wnli",
           "copa",
           "sna",
           "csqa",
           "wstp",
           "inltkh",
           "bbca",
           "cvit-mkb-clsr",
           "iitp-mr",
           "iitp-pr",
           "actsa-sc",
           "md",
           "wiki-ner",
       ]:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '
                '"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '
                '"wiki-ner"]' )
       return datasets.MetricInfo(
           description=_DESCRIPTION							,					citation=_CITATION							,					inputs_description=_KWARGS_DESCRIPTION							,					features=datasets.Features(
               {
                   'predictions': datasets.Value('int64' )
                   if self.config_name != 'cvit-mkb-clsr'
                   else datasets.Sequence(datasets.Value('float32' ) ),
                   'references': datasets.Value('int64' )
                   if self.config_name != 'cvit-mkb-clsr'
                   else datasets.Sequence(datasets.Value('float32' ) ),
               } )							,					codebase_urls=[]							,					reference_urls=[]							,					format='numpy' if self.config_name != 'cvit-mkb-clsr' else None							,					)





  def 						A  (		self      :	List[str]							,					_a      :	Tuple							,					_a      :	Optional[int] )	->    int:





       '''simple docstring'''






       if self.config_name == "cvit-mkb-clsr":
            return {"precision@10": precision_at_aa(_a							,					_a )}
       elif self.config_name in ["wiki-ner"]:
            return acc_and_fa(_a							,					_a )
       elif self.config_name in [
           "wnli",
           "copa",
           "sna",
           "csqa",
           "wstp",
           "inltkh",
           "bbca",
           "iitp-mr",
           "iitp-pr",
           "actsa-sc",
           "md",
       ]:
            return {"accuracy": simple_accuracy(_a							,					_a )}
       else:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '
                '"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '
                '"wiki-ner"]' )
