

'''simple docstring'''





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
							from transformers import WhisperFeatureExtractor

if is_torch_available():
							import torch

UpperCamelCase_    						=       random.Random()






def 				_lowerCAmelCase			(   __magic_name__						:     int   , __magic_name__						:     Tuple=1.0   , __magic_name__						:     Optional[Any]=None   , __magic_name__						:     Union[str, Any]=None						)				->  Dict:
						if rng is None:
												lowercase :							List[Any]    		=global_rng

						lowercase :							Any    		=[]
						for batch_idx in range(shape[0]						):
												values.append([]						)
												for _ in range(shape[1]						):
																		values[-1].append(rng.random() * scale						)

						return values





@require_torch
@require_torchaudio
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				def __init__(       self     :		Optional[Any]		,					UpperCAmelCase__     :		Union[str, Any]		,					UpperCAmelCase__     :		Tuple=7		,					UpperCAmelCase__     :		Tuple=400		,					UpperCAmelCase__     :		str=2000		,					UpperCAmelCase__     :		Union[str, Any]=10		,					UpperCAmelCase__     :		int=160		,					UpperCAmelCase__     :		Dict=8		,					UpperCAmelCase__     :		Union[str, Any]=0.0		,					UpperCAmelCase__     :		Optional[Any]=4000		,					UpperCAmelCase__     :		List[Any]=False		,					UpperCAmelCase__     :		Union[str, Any]=True		,					):
										'''simple docstring'''
										lowercase :							Any    		=parent
										lowercase :							Optional[Any]    		=batch_size
										lowercase :							Dict    		=min_seq_length
										lowercase :							Optional[Any]    		=max_seq_length
										lowercase :							Optional[Any]    		=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										lowercase :							Tuple    		=padding_value
										lowercase :							Dict    		=sampling_rate
										lowercase :							Union[str, Any]    		=return_attention_mask
										lowercase :							str    		=do_normalize
										lowercase :							List[Any]    		=feature_size
										lowercase :							List[str]    		=chunk_length
										lowercase :							Tuple    		=hop_length







				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										return {
										    "feature_size": self.feature_size,
										    "hop_length": self.hop_length,
										    "chunk_length": self.chunk_length,
										    "padding_value": self.padding_value,
										    "sampling_rate": self.sampling_rate,
										    "return_attention_mask": self.return_attention_mask,
										    "do_normalize": self.do_normalize,
										}







				def  lowerCamelCase_	(       self     :		Dict		,					UpperCAmelCase__     :		Dict=False		,					UpperCAmelCase__     :		List[str]=False   ):
										'''simple docstring'''
										def _flatten(UpperCAmelCase__     :		int   ):
																return list(itertools.chain(*UpperCAmelCase__   )   )

										if equal_length:
																lowercase :							List[str]    		=[floats_list((self.max_seq_length, self.feature_size)   ) for _ in range(self.batch_size   )]
										else:
																# make sure that inputs increase in size
																lowercase :							Any    		=[
																    floats_list((x, self.feature_size)   )
																    for x in range(self.min_seq_length		,					self.max_seq_length		,					self.seq_length_diff   )
																]
										if numpify:
																lowercase :							List[Any]    		=[np.asarray(UpperCAmelCase__   ) for x in speech_inputs]
										return speech_inputs





@require_torch
@require_torchaudio
class 		__SCREAMING_SNAKE_CASE							(			lowercase__						,							unittest.TestCase      ):
				lowerCamelCase_		       =    WhisperFeatureExtractor if is_speech_available() else None







				def  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										lowercase :							Tuple    		=WhisperFeatureExtractionTester(self   )







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.feature_extraction_class(**self.feat_extract_dict   )

										with tempfile.TemporaryDirectory() as tmpdirname:
																lowercase :							int    		=feat_extract_first.save_pretrained(UpperCAmelCase__   )[0]
																check_json_file_has_correct_format(UpperCAmelCase__   )
																lowercase :							List[Any]    		=self.feature_extraction_class.from_pretrained(UpperCAmelCase__   )

										lowercase :							List[Any]    		=feat_extract_first.to_dict()
										lowercase :							Tuple    		=feat_extract_second.to_dict()
										lowercase :							Dict    		=feat_extract_first.mel_filters
										lowercase :							List[Any]    		=feat_extract_second.mel_filters
										self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__   )   )
										self.assertEqual(UpperCAmelCase__		,					UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							Dict    		=self.feature_extraction_class(**self.feat_extract_dict   )

										with tempfile.TemporaryDirectory() as tmpdirname:
																lowercase :							Optional[int]    		=os.path.join(UpperCAmelCase__		,					'''feat_extract.json'''   )
																feat_extract_first.to_json_file(UpperCAmelCase__   )
																lowercase :							Tuple    		=self.feature_extraction_class.from_json_file(UpperCAmelCase__   )

										lowercase :							Dict    		=feat_extract_first.to_dict()
										lowercase :							List[str]    		=feat_extract_second.to_dict()
										lowercase :							List[str]    		=feat_extract_first.mel_filters
										lowercase :							Dict    		=feat_extract_second.mel_filters
										self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__   )   )
										self.assertEqual(UpperCAmelCase__		,					UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										# Tests that all call wrap to encode_plus and batch_encode_plus
										lowercase :							Union[str, Any]    		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
										# create three inputs of length 800, 1000, and 1200
										lowercase :							Optional[Any]    		=[floats_list((1, x)   )[0] for x in range(800		,					1400		,					200   )]
										lowercase :							int    		=[np.asarray(UpperCAmelCase__   ) for speech_input in speech_inputs]

										# Test feature size
										lowercase :							Dict    		=feature_extractor(UpperCAmelCase__		,					padding='''max_length'''		,					return_tensors='''np'''   ).input_features
										self.assertTrue(input_features.ndim == 3   )
										self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames   )
										self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size   )

										# Test not batched input
										lowercase :							str    		=feature_extractor(speech_inputs[0]		,					return_tensors='''np'''   ).input_features
										lowercase :							Any    		=feature_extractor(np_speech_inputs[0]		,					return_tensors='''np'''   ).input_features
										self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__		,					atol=1E-3   )   )

										# Test batched
										lowercase :							List[str]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										lowercase :							str    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__		,					UpperCAmelCase__   ):
																self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__		,					atol=1E-3   )   )

										# Test 2-D numpy arrays are batched.
										lowercase :							str    		=[floats_list((1, x)   )[0] for x in (800, 800, 800)]
										lowercase :							Dict    		=np.asarray(UpperCAmelCase__   )
										lowercase :							Union[str, Any]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										lowercase :							Optional[Any]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__		,					UpperCAmelCase__   ):
																self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__		,					atol=1E-3   )   )

										# Test truncation required
										lowercase :							Optional[Any]    		=[floats_list((1, x)   )[0] for x in range(200		,					(feature_extractor.n_samples + 500)		,					200   )]
										lowercase :							List[Any]    		=[np.asarray(UpperCAmelCase__   ) for speech_input in speech_inputs]

										lowercase :							Union[str, Any]    		=[x[: feature_extractor.n_samples] for x in speech_inputs]
										lowercase :							List[Any]    		=[np.asarray(UpperCAmelCase__   ) for speech_input in speech_inputs_truncated]

										lowercase :							List[str]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										lowercase :							List[Any]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''np'''   ).input_features
										for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__		,					UpperCAmelCase__   ):
																self.assertTrue(np.allclose(UpperCAmelCase__		,					UpperCAmelCase__		,					atol=1E-3   )   )







				def  lowerCamelCase_	(       self     :		Optional[int]   ):
										'''simple docstring'''
										import torch

										lowercase :							Tuple    		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
										lowercase :							Tuple    		=np.random.rand(100		,					32   ).astype(np.floataa   )
										lowercase :							Tuple    		=np_speech_inputs.tolist()

										for inputs in [py_speech_inputs, np_speech_inputs]:
																lowercase :							Optional[int]    		=feature_extractor.pad([{'''input_features''': inputs}]		,					return_tensors='''np'''   )
																self.assertTrue(np_processed.input_features.dtype == np.floataa   )
																lowercase :							Dict    		=feature_extractor.pad([{'''input_features''': inputs}]		,					return_tensors='''pt'''   )
																self.assertTrue(pt_processed.input_features.dtype == torch.floataa   )







				def  lowerCamelCase_	(       self     :		Any		,					UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=load_dataset('''hf-internal-testing/librispeech_asr_dummy'''		,					'''clean'''		,					split='''validation'''   )
										# automatic decoding with librispeech
										lowercase :							Dict    		=ds.sort('''id'''   ).select(range(UpperCAmelCase__   )   )[:num_samples]['''audio''']

										return [x["array"] for x in speech_samples]







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										# fmt: off
										lowercase :							List[Any]    		=torch.tensor(
										    [
										        0.11_93, -0.09_46, -0.10_98, -0.01_96, 0.02_25, -0.06_90, -0.17_36, 0.09_51,
										        0.09_71, -0.08_17, -0.07_02, 0.01_62, 0.02_60, 0.00_17, -0.01_92, -0.16_78,
										        0.07_09, -0.18_67, -0.06_55, -0.02_74, -0.02_34, -0.18_84, -0.05_16, -0.05_54,
										        -0.02_74, -0.14_25, -0.14_23, 0.08_37, 0.03_77, -0.08_54
										    ]   )
										# fmt: on

										lowercase :							Any    		=self._load_datasamples(1   )
										lowercase :							List[str]    		=WhisperFeatureExtractor()
										lowercase :							List[str]    		=feature_extractor(UpperCAmelCase__		,					return_tensors='''pt'''   ).input_features
										self.assertEqual(input_features.shape		,					(1, 80, 3000)   )
										self.assertTrue(torch.allclose(input_features[0, 0, :30]		,					UpperCAmelCase__		,					atol=1E-4   )   )







				def  lowerCamelCase_	(       self     :		Tuple   ):
										'''simple docstring'''
										lowercase :							List[str]    		=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
										lowercase :							Optional[int]    		=self._load_datasamples(1   )[0]
										lowercase :							Tuple    		=((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
										lowercase :							Tuple    		=feat_extract.zero_mean_unit_var_norm([audio]		,					attention_mask=UpperCAmelCase__   )[0]

										self.assertTrue(np.all(np.mean(UpperCAmelCase__   ) < 1E-3   )   )
										self.assertTrue(np.all(np.abs(np.var(UpperCAmelCase__   ) - 1   ) < 1E-3   )   )








'''simple docstring'''





import datasets


UpperCamelCase_    						=       """\
@InProceedings{conneau2018xnli,
  author = \"Conneau, Alexis
                 and Rinott, Ruty
                 and Lample, Guillaume
                 and Williams, Adina
                 and Bowman, Samuel R.
                 and Schwenk, Holger
                 and Stoyanov, Veselin\",
  title = \"XNLI: Evaluating Cross-lingual Sentence Representations\",
  booktitle = \"Proceedings of the 2018 Conference on Empirical Methods
               in Natural Language Processing\",
  year = \"2018\",
  publisher = \"Association for Computational Linguistics\",
  location = \"Brussels, Belgium\",
}
"""

UpperCamelCase_    						=       """\
XNLI is a subset of a few thousand examples from MNLI which has been translated
into a 14 different languages (some low-ish resource). As with MNLI, the goal is
to predict textual entailment (does sentence A imply/contradict/neither sentence
B) and is a classification task (given two sentences, predict one of three
labels).
"""

UpperCamelCase_    						=       """
Computes XNLI score which is just simple accuracy.
Args:
    predictions: Predicted labels.
    references: Ground truth labels.
Returns:
    'accuracy': accuracy
Examples:

    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> xnli_metric = datasets.load_metric(\"xnli\")
    >>> results = xnli_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}
"""






def 				_lowerCAmelCase			(   __magic_name__						:     Dict   , __magic_name__						:     Union[str, Any]						)				->  Union[str, Any]:
						return (preds == labels).mean()







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







				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										return datasets.MetricInfo(
										    description=_DESCRIPTION		,					citation=_CITATION		,					inputs_description=_KWARGS_DESCRIPTION		,					features=datasets.Features(
										        {
										            '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''   ),
										            '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''   ),
										        }   )		,					codebase_urls=[]		,					reference_urls=[]		,					format='''numpy'''		,					)







				def  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		Optional[Any]   ):
										'''simple docstring'''
										return {"accuracy": simple_accuracy(UpperCAmelCase__		,					UpperCAmelCase__   )}





