

'''simple docstring'''
import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 							A__    (       A__  ):
  A__		 =				['image_processor', 'tokenizer']
  A__		 =				'LayoutLMv2ImageProcessor'
  A__		 =				('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast')





  def __init__(		self      :	Tuple							,					_a      :	List[Any]=None							,					_a      :	Any=None							,					**_a      :	int )	->    str:





       '''simple docstring'''






       if "feature_extractor" in kwargs:
            warnings.warn(
                'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                ' instead.'							,					_a							,					)
            _SCREAMING_SNAKE_CASE							    =kwargs.pop('feature_extractor' )

       _SCREAMING_SNAKE_CASE							    =image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
            raise ValueError('You need to specify an `image_processor`.' )
       if tokenizer is None:
            raise ValueError('You need to specify a `tokenizer`.' )

       super().__init__(_a							,					_a )





  def __call__(		self      :	int							,					_a      :	List[str]							,					_a      :	Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None							,					_a      :	Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None							,					_a      :	Union[List[List[int]], List[List[List[int]]]] = None							,					_a      :	Optional[Union[List[int], List[List[int]]]] = None							,					_a      :	bool = True							,					_a      :	Union[bool, str, PaddingStrategy] = False							,					_a      :	Union[bool, str, TruncationStrategy] = None							,					_a      :	Optional[int] = None							,					_a      :	int = 0							,					_a      :	Optional[int] = None							,					_a      :	Optional[bool] = None							,					_a      :	Optional[bool] = None							,					_a      :	bool = False							,					_a      :	bool = False							,					_a      :	bool = False							,					_a      :	bool = False							,					_a      :	bool = True							,					_a      :	Optional[Union[str, TensorType]] = None							,					**_a      :	List[str]							,					)	->    BatchEncoding:





       '''simple docstring'''






       if self.image_processor.apply_ocr and (boxes is not None):
            raise ValueError(
                'You cannot provide bounding boxes '
                'if you initialized the image processor with apply_ocr set to True.' )

       if self.image_processor.apply_ocr and (word_labels is not None):
            raise ValueError(
                'You cannot provide word labels if you initialized the image processor with apply_ocr set to True.' )

       if return_overflowing_tokens is True and return_offsets_mapping is False:
            raise ValueError('You cannot return overflowing tokens without returning the offsets mapping.' )

       # first, apply the image processor
       _SCREAMING_SNAKE_CASE							    =self.image_processor(images=_a							,					return_tensors=_a )

       # second, apply the tokenizer
       if text is not None and self.image_processor.apply_ocr and text_pair is None:
            if isinstance(_a							,					_a ):
                 _SCREAMING_SNAKE_CASE							    =[text]  # add batch dimension (as the image processor always adds a batch dimension)
            _SCREAMING_SNAKE_CASE							    =features['words']

       _SCREAMING_SNAKE_CASE							    =self.tokenizer(
           text=text if text is not None else features['words']							,					text_pair=text_pair if text_pair is not None else None							,					boxes=boxes if boxes is not None else features['boxes']							,					word_labels=_a							,					add_special_tokens=_a							,					padding=_a							,					truncation=_a							,					max_length=_a							,					stride=_a							,					pad_to_multiple_of=_a							,					return_token_type_ids=_a							,					return_attention_mask=_a							,					return_overflowing_tokens=_a							,					return_special_tokens_mask=_a							,					return_offsets_mapping=_a							,					return_length=_a							,					verbose=_a							,					return_tensors=_a							,					**_a							,					)

       # add pixel values
       _SCREAMING_SNAKE_CASE							    =features.pop('pixel_values' )
       if return_overflowing_tokens is True:
            _SCREAMING_SNAKE_CASE							    =self.get_overflowing_images(_a							,					encoded_inputs['overflow_to_sample_mapping'] )
       _SCREAMING_SNAKE_CASE							    =images

       return encoded_inputs





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =[]
       for sample_idx in overflow_to_sample_mapping:
            images_with_overflow.append(images[sample_idx] )

       if len(_a ) != len(_a ):
            raise ValueError(
                'Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'
                f" {len(_a )} and {len(_a )}" )

       return images_with_overflow





  def 						A  (		self      :	Dict							,					*_a      :	Any							,					**_a      :	Tuple )	->    int:





       '''simple docstring'''






       return self.tokenizer.batch_decode(*_a							,					**_a )





  def 						A  (		self      :	List[Any]							,					*_a      :	Any							,					**_a      :	Optional[int] )	->    List[Any]:





       '''simple docstring'''






       return self.tokenizer.decode(*_a							,					**_a )





  @property
  def 						A  (		self      :	List[Any] )	->    Optional[Any]:





       '''simple docstring'''






       return ["input_ids", "bbox", "attention_mask", "image"]





  @property
  def 						A  (		self      :	int )	->    List[Any]:





       '''simple docstring'''






       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'							,					_a							,					)
       return self.image_processor_class





  @property
  def 						A  (		self      :	List[Any] )	->    Dict:





       '''simple docstring'''






       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'							,					_a							,					)
       return self.image_processor



'''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"]' )
