




'''simple docstring'''

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

import datasets


a    :							Optional[int]										=    '\\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'

a    :							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'

a    :							Optional[Any]										=    '\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 						__magic_name__       (      __UpperCAmelCase, __UpperCAmelCase      )						->   Optional[int]:

					'''simple docstring'''

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


def 						__magic_name__       (      __UpperCAmelCase, __UpperCAmelCase      )						->   Any:

					'''simple docstring'''

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


def 						__magic_name__       (      __UpperCAmelCase, __UpperCAmelCase      )						->   Optional[int]:

					'''simple docstring'''

					snake_case_		     =							np.array(__UpperCAmelCase      )
					snake_case_		     =							np.array(__UpperCAmelCase      )
					snake_case_		     =							en_sentvecs.shape[0]

					# mean centering
					snake_case_		     =							en_sentvecs - np.mean(__UpperCAmelCase, axis=0      )
					snake_case_		     =							in_sentvecs - np.mean(__UpperCAmelCase, axis=0      )

					snake_case_		     =							cdist(__UpperCAmelCase, __UpperCAmelCase, '''cosine'''      )
					snake_case_		     =							np.array(range(__UpperCAmelCase      )      )
					snake_case_		     =							sim.argsort(axis=1      )[:, :10]
					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       :   Union[str, Any]   ):
									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       :   Optional[int]			,							lowercase_       :   Optional[Any]			,							lowercase_       :   Optional[int]   ):
									if self.config_name == "cvit-mkb-clsr":
														return {"precision@10": precision_at_aa(lowercase_			,							lowercase_   )}
									elif self.config_name in ["wiki-ner"]:
														return acc_and_fa(lowercase_			,							lowercase_   )
									elif self.config_name in [
									    "wnli",
									    "copa",
									    "sna",
									    "csqa",
									    "wstp",
									    "inltkh",
									    "bbca",
									    "iitp-mr",
									    "iitp-pr",
									    "actsa-sc",
									    "md",
									]:
														return {"accuracy": simple_accuracy(lowercase_			,							lowercase_   )}
									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"]'''   )







'''simple docstring'''

import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_torch, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available


if is_torch_available():
							import torch


if is_vision_available():
							from PIL import Image

							from transformers import MgpstrProcessor, ViTImageProcessor






@require_torch
@require_vision
class 		a							(  unittest.TestCase	):
				snake_case_           =						ViTImageProcessor if is_vision_available() else None


				@property
				def     A_					(						self       :   List[str]   ):
									return self.image_processor_tester.prepare_image_processor_dict()


				def     A_					(						self       :   Union[str, Any]   ):
									snake_case_		     =							(3, 32, 128)
									snake_case_		     =							tempfile.mkdtemp()

									# fmt: off
									snake_case_		     =							['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
									# fmt: on
									snake_case_		     =							dict(zip(lowercase_			,							range(len(lowercase_   )   )   )   )

									snake_case_		     =							os.path.join(self.tmpdirname			,							VOCAB_FILES_NAMES['''vocab_file''']   )
									with open(self.vocab_file			,							'''w'''			,							encoding='''utf-8'''   ) as fp:
														fp.write(json.dumps(lowercase_   ) + '''\n'''   )

									snake_case_		     =							{
									    '''do_normalize''': False,
									    '''do_resize''': True,
									    '''image_processor_type''': '''ViTImageProcessor''',
									    '''resample''': 3,
									    '''size''': {'''height''': 32, '''width''': 128},
									}
									snake_case_		     =							os.path.join(self.tmpdirname			,							lowercase_   )
									with open(self.image_processor_file			,							'''w'''			,							encoding='''utf-8'''   ) as fp:
														json.dump(lowercase_			,							lowercase_   )


				def     A_					(						self       :   Any			,							**lowercase_       :   str   ):
									return MgpstrTokenizer.from_pretrained(self.tmpdirname			,							**lowercase_   )


				def     A_					(						self       :   List[str]			,							**lowercase_       :   List[Any]   ):
									return ViTImageProcessor.from_pretrained(self.tmpdirname			,							**lowercase_   )


				def     A_					(						self       :   Union[str, Any]   ):
									shutil.rmtree(self.tmpdirname   )


				def     A_					(						self       :   int   ):
									snake_case_		     =							np.random.randint(255			,							size=(3, 30, 400)			,							dtype=np.uinta   )

									snake_case_		     =							Image.fromarray(np.moveaxis(lowercase_			,							0			,							-1   )   )

									return image_input


				def     A_					(						self       :   int   ):
									snake_case_		     =							self.get_tokenizer()
									snake_case_		     =							self.get_image_processor()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )
									processor.save_pretrained(self.tmpdirname   )
									snake_case_		     =							MgpstrProcessor.from_pretrained(self.tmpdirname			,							use_fast=lowercase_   )

									self.assertEqual(processor.char_tokenizer.get_vocab()			,							tokenizer.get_vocab()   )
									self.assertIsInstance(processor.char_tokenizer			,							lowercase_   )

									self.assertEqual(processor.image_processor.to_json_string()			,							image_processor.to_json_string()   )
									self.assertIsInstance(processor.image_processor			,							lowercase_   )


				def     A_					(						self       :   str   ):
									snake_case_		     =							self.get_tokenizer()
									snake_case_		     =							self.get_image_processor()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )
									processor.save_pretrained(self.tmpdirname   )

									snake_case_		     =							self.get_tokenizer(bos_token='''(BOS)'''			,							eos_token='''(EOS)'''   )
									snake_case_		     =							self.get_image_processor(do_normalize=lowercase_			,							padding_value=1.0   )

									snake_case_		     =							MgpstrProcessor.from_pretrained(
									    self.tmpdirname			,							bos_token='''(BOS)'''			,							eos_token='''(EOS)'''			,							do_normalize=lowercase_			,							padding_value=1.0   )

									self.assertEqual(processor.char_tokenizer.get_vocab()			,							tokenizer_add_kwargs.get_vocab()   )
									self.assertIsInstance(processor.char_tokenizer			,							lowercase_   )

									self.assertEqual(processor.image_processor.to_json_string()			,							image_processor_add_kwargs.to_json_string()   )
									self.assertIsInstance(processor.image_processor			,							lowercase_   )


				def     A_					(						self       :   Tuple   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							self.prepare_image_inputs()

									snake_case_		     =							image_processor(lowercase_			,							return_tensors='''np'''   )
									snake_case_		     =							processor(images=lowercase_			,							return_tensors='''np'''   )

									for key in input_image_proc.keys():
														self.assertAlmostEqual(input_image_proc[key].sum()			,							input_processor[key].sum()			,							delta=1e-2   )


				def     A_					(						self       :   Union[str, Any]   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							'''test'''

									snake_case_		     =							processor(text=lowercase_   )

									snake_case_		     =							tokenizer(lowercase_   )
									for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key]			,							encoded_processor[key]   )


				def     A_					(						self       :   Dict   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							'''test'''
									snake_case_		     =							self.prepare_image_inputs()

									snake_case_		     =							processor(text=lowercase_			,							images=lowercase_   )

									self.assertListEqual(list(inputs.keys()   )			,							['''pixel_values''', '''labels''']   )

									# test if it raises when no input is passed
									with pytest.raises(lowercase_   ):
														processor()


				def     A_					(						self       :   int   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]]

									snake_case_		     =							processor.char_decode(lowercase_   )
									snake_case_		     =							tokenizer.batch_decode(lowercase_   )
									snake_case_		     =							[seq.replace(''' '''			,							''''''   ) for seq in decoded_tok]

									self.assertListEqual(lowercase_			,							lowercase_   )


				def     A_					(						self       :   Optional[Any]   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							None
									snake_case_		     =							self.prepare_image_inputs()

									snake_case_		     =							processor(text=lowercase_			,							images=lowercase_   )

									self.assertListEqual(list(inputs.keys()   )			,							processor.model_input_names   )


				def     A_					(						self       :   Dict   ):
									snake_case_		     =							self.get_image_processor()
									snake_case_		     =							self.get_tokenizer()

									snake_case_		     =							MgpstrProcessor(tokenizer=lowercase_			,							image_processor=lowercase_   )

									snake_case_		     =							torch.randn(1			,							27			,							38   )
									snake_case_		     =							torch.randn(1			,							27			,							5_0257   )
									snake_case_		     =							torch.randn(1			,							27			,							3_0522   )

									snake_case_		     =							processor.batch_decode([char_input, bpe_input, wp_input]   )

									self.assertListEqual(list(results.keys()   )			,							['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds''']   )

