



"""simple docstring"""

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

import datasets


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

A_						:    int				=      '\\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_						:    Dict				=      '\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  __snake_case							(	__A      :						Tuple							,		__A      :						Optional[int]     )     ->       Tuple:






				'''simple docstring'''
				return float((preds == labels).mean()     )
def  __snake_case							(	__A      :						Tuple							,		__A      :						int     )     ->       str:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							Dict							=     simple_accuracy(__A							,		__A     )
				SCREAMING_SNAKE_CASE						:							List[Any]							=     float(fa_score(y_true=__A							,		y_pred=__A     )     )
				return {
				    "accuracy": acc,
				    "f1": fa,
				}
def  __snake_case							(	__A      :						int							,		__A      :						Dict     )     ->       int:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							List[Any]							=     np.array(__A     )
				SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     np.array(__A     )
				SCREAMING_SNAKE_CASE						:							Dict							=     en_sentvecs.shape[0]

				# mean centering
				SCREAMING_SNAKE_CASE						:							Tuple							=     en_sentvecs - np.mean(__A							,		axis=0     )
				SCREAMING_SNAKE_CASE						:							Dict							=     in_sentvecs - np.mean(__A							,		axis=0     )

				SCREAMING_SNAKE_CASE						:							Any							=     cdist(__A							,		__A							,		'cosine'     )
				SCREAMING_SNAKE_CASE						:							Dict							=     np.array(range(__A     )     )
				SCREAMING_SNAKE_CASE						:							str							=     sim.argsort(axis=1     )[:, :10]
				SCREAMING_SNAKE_CASE						:							Optional[Any]							=     np.any(preds == actual[:, None]							,		axis=1     )
				return float(matches.mean()     )







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


	'''simple docstring'''







	def 			_lowerCAmelCase     (      self				:       Union[str, Any]    )						->	int:





					"""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 			_lowerCAmelCase     (      self				:       Dict     ,       _SCREAMING_SNAKE_CASE				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       List[str]    )						->	List[str]:





					"""simple docstring"""


					if self.config_name == "cvit-mkb-clsr":
									return {"precision@10": precision_at_aa(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )}
					elif self.config_name in ["wiki-ner"]:
									return acc_and_fa(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
					elif self.config_name in [
					    "wnli",
					    "copa",
					    "sna",
					    "csqa",
					    "wstp",
					    "inltkh",
					    "bbca",
					    "iitp-mr",
					    "iitp-pr",
					    "actsa-sc",
					    "md",
					]:
									return {"accuracy": simple_accuracy(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )}
					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 sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


A_						:    int				=      get_tests_dir('fixtures')
A_						:    int				=      get_tests_dir('fixtures/dummy_feature_extractor_config.json')
A_						:    Dict				=      get_tests_dir('fixtures/dummy-config.json')
class     lowerCAmelCase__						(	unittest.TestCase ):


	'''simple docstring'''







	def 			_lowerCAmelCase     (      self				:       int    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Tuple							=     0





	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Any							=     AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'    )
					self.assertIsInstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Dict    )						->	Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							str							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
					self.assertIsInstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Optional[Any]    )						->	Optional[int]:





					"""simple docstring"""


					with tempfile.TemporaryDirectory() as tmpdirname:
									SCREAMING_SNAKE_CASE						:							List[Any]							=     WavaVecaConfig()

									# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
									SCREAMING_SNAKE_CASE						:							Optional[Any]							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    ).to_dict()

									config_dict.pop('feature_extractor_type'    )
									SCREAMING_SNAKE_CASE						:							int							=     WavaVecaFeatureExtractor(**_SCREAMING_SNAKE_CASE    )

									# save in new folder
									model_config.save_pretrained(_SCREAMING_SNAKE_CASE    )
									config.save_pretrained(_SCREAMING_SNAKE_CASE    )

									SCREAMING_SNAKE_CASE						:							List[Any]							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )

									# make sure private variable is not incorrectly saved
									SCREAMING_SNAKE_CASE						:							Optional[int]							=     json.loads(config.to_json_string()    )
									self.assertTrue('_processor_class' not in dict_as_saved    )

					self.assertIsInstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
					self.assertIsInstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Dict    )						->	Union[str, Any]:





					"""simple docstring"""


					with self.assertRaisesRegex(
					    _SCREAMING_SNAKE_CASE     ,       'bert-base is not a local folder and is not a valid model identifier'    ):
									SCREAMING_SNAKE_CASE						:							str							=     AutoFeatureExtractor.from_pretrained('bert-base'    )





	def 			_lowerCAmelCase     (      self				:       str    )						->	str:





					"""simple docstring"""


					with self.assertRaisesRegex(
					    _SCREAMING_SNAKE_CASE     ,       R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'    ):
									SCREAMING_SNAKE_CASE						:							List[str]							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE     ,       revision='aaaaaa'    )





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





					"""simple docstring"""


					with self.assertRaisesRegex(
					    _SCREAMING_SNAKE_CASE     ,       'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'     ,       ):
									SCREAMING_SNAKE_CASE						:							int							=     AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'    )





	def 			_lowerCAmelCase     (      self				:       Optional[Any]    )						->	str:





					"""simple docstring"""


					with self.assertRaises(_SCREAMING_SNAKE_CASE    ):
									SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     AutoFeatureExtractor.from_pretrained(
									    'hf-internal-testing/test_dynamic_feature_extractor'    )
					# If remote code is disabled, we can't load this config.
					with self.assertRaises(_SCREAMING_SNAKE_CASE    ):
									SCREAMING_SNAKE_CASE						:							int							=     AutoFeatureExtractor.from_pretrained(
									    'hf-internal-testing/test_dynamic_feature_extractor'     ,       trust_remote_code=_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Optional[int]							=     AutoFeatureExtractor.from_pretrained(
					    'hf-internal-testing/test_dynamic_feature_extractor'     ,       trust_remote_code=_SCREAMING_SNAKE_CASE    )
					self.assertEqual(feature_extractor.__class__.__name__     ,       'NewFeatureExtractor'    )

					# Test feature extractor can be reloaded.
					with tempfile.TemporaryDirectory() as tmp_dir:
									feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							List[str]							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE     ,       trust_remote_code=_SCREAMING_SNAKE_CASE    )
					self.assertEqual(reloaded_feature_extractor.__class__.__name__     ,       'NewFeatureExtractor'    )





	def 			_lowerCAmelCase     (      self				:       Any    )						->	Optional[int]:





					"""simple docstring"""


					try:
									AutoConfig.register('custom'     ,       _SCREAMING_SNAKE_CASE    )
									AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									# Trying to register something existing in the Transformers library will raise an error
									with self.assertRaises(_SCREAMING_SNAKE_CASE    ):
													AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )

									# Now that the config is registered, it can be used as any other config with the auto-API
									SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
									with tempfile.TemporaryDirectory() as tmp_dir:
													feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE    )
													SCREAMING_SNAKE_CASE						:							Dict							=     AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE    )
													self.assertIsInstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )

					finally:
									if "custom" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["custom"]
									if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
													del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]







	def 			_lowerCAmelCase     (      self				:       Tuple    )						->	str:





					"""simple docstring"""


					class     lowerCAmelCase__						(	_lowerCamelCase ):


						'''simple docstring'''







						_SCREAMING_SNAKE_CASE	:		str  						= True

					try:
									AutoConfig.register('custom'     ,       _SCREAMING_SNAKE_CASE    )
									AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									# If remote code is not set, the default is to use local
									SCREAMING_SNAKE_CASE						:							str							=     AutoFeatureExtractor.from_pretrained(
									    'hf-internal-testing/test_dynamic_feature_extractor'    )
									self.assertEqual(feature_extractor.__class__.__name__     ,       'NewFeatureExtractor'    )
									self.assertTrue(feature_extractor.is_local    )

									# If remote code is disabled, we load the local one.
									SCREAMING_SNAKE_CASE						:							List[Any]							=     AutoFeatureExtractor.from_pretrained(
									    'hf-internal-testing/test_dynamic_feature_extractor'     ,       trust_remote_code=_SCREAMING_SNAKE_CASE    )
									self.assertEqual(feature_extractor.__class__.__name__     ,       'NewFeatureExtractor'    )
									self.assertTrue(feature_extractor.is_local    )

									# If remote is enabled, we load from the Hub
									SCREAMING_SNAKE_CASE						:							Optional[int]							=     AutoFeatureExtractor.from_pretrained(
									    'hf-internal-testing/test_dynamic_feature_extractor'     ,       trust_remote_code=_SCREAMING_SNAKE_CASE    )
									self.assertEqual(feature_extractor.__class__.__name__     ,       'NewFeatureExtractor'    )
									self.assertTrue(not hasattr(_SCREAMING_SNAKE_CASE     ,       'is_local'    )    )

					finally:
									if "custom" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["custom"]
									if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
													del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





