



import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
						from transformers import SpeechaTextFeatureExtractor

UpperCAmelCase_							:      Tuple      =random.Random()
def  UpperCamelCase						(       _A			:				Optional[Any]			,							_A			:				List[Any]=1.0			,							_A			:				List[Any]=None			,							_A			:				Optional[Any]=None    )->       Optional[int]:





						"""simple docstring"""





						if rng is None:
												A__							=       global_rng

						A__							=       []
						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      UpperCamelCase    (						unittest.TestCase     ):



	def __init__(       self			,	UpperCAmelCase__			,	UpperCAmelCase__=7			,	UpperCAmelCase__=400			,	UpperCAmelCase__=2_000			,	UpperCAmelCase__=24			,	UpperCAmelCase__=24			,	UpperCAmelCase__=0.0			,	UpperCAmelCase__=16_000			,	UpperCAmelCase__=True			,	UpperCAmelCase__=True			,	):
							A__							=       parent
							A__							=       batch_size
							A__							=       min_seq_length
							A__							=       max_seq_length
							A__							=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							A__							=       feature_size
							A__							=       num_mel_bins
							A__							=       padding_value
							A__							=       sampling_rate
							A__							=       return_attention_mask
							A__							=       do_normalize



	def 			__A							(       self		):
							return {
							    "feature_size": self.feature_size,
							    "num_mel_bins": self.num_mel_bins,
							    "padding_value": self.padding_value,
							    "sampling_rate": self.sampling_rate,
							    "return_attention_mask": self.return_attention_mask,
							    "do_normalize": self.do_normalize,
							}



	def 			__A							(       self			,	UpperCAmelCase__=False			,	UpperCAmelCase__=False		):
							def _flatten(UpperCAmelCase__		):
													return list(itertools.chain(*lowercase_		)		)

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






@require_torch
@require_torchaudio
class      UpperCamelCase    (						lowercase__       ,    unittest.TestCase     ):
	lowerCAmelCase	:   List[Any]				=	SpeechaTextFeatureExtractor if is_speech_available() else None



	def 			__A							(       self		):
							A__							=       SpeechaTextFeatureExtractionTester(self		)



	def 			__A							(       self			,	UpperCAmelCase__		):
							self.assertTrue(np.all(np.mean(lowercase_			,	axis=0		) < 1e-3		)		)
							self.assertTrue(np.all(np.abs(np.var(lowercase_			,	axis=0		) - 1		) < 1e-3		)		)



	def 			__A							(       self		):
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							# create three inputs of length 800, 1000, and 1200
							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]
							A__							=       [np.asarray(lowercase_		) for speech_input in speech_inputs]

							# Test feature size
							A__							=       feature_extractor(lowercase_			,	padding=lowercase_			,	return_tensors="np"		).input_features
							self.assertTrue(input_features.ndim == 3		)
							self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size		)

							# Test not batched input
							A__							=       feature_extractor(speech_inputs[0]			,	return_tensors="np"		).input_features
							A__							=       feature_extractor(np_speech_inputs[0]			,	return_tensors="np"		).input_features
							self.assertTrue(np.allclose(lowercase_			,	lowercase_			,	atol=1e-3		)		)

							# Test batched
							A__							=       feature_extractor(lowercase_			,	return_tensors="np"		).input_features
							A__							=       feature_extractor(lowercase_			,	return_tensors="np"		).input_features
							for enc_seq_a, enc_seq_a in zip(lowercase_			,	lowercase_		):
													self.assertTrue(np.allclose(lowercase_			,	lowercase_			,	atol=1e-3		)		)

							# Test 2-D numpy arrays are batched.
							A__							=       [floats_list((1, x)		)[0] for x in (800, 800, 800)]
							A__							=       np.asarray(lowercase_		)
							A__							=       feature_extractor(lowercase_			,	return_tensors="np"		).input_features
							A__							=       feature_extractor(lowercase_			,	return_tensors="np"		).input_features
							for enc_seq_a, enc_seq_a in zip(lowercase_			,	lowercase_		):
													self.assertTrue(np.allclose(lowercase_			,	lowercase_			,	atol=1e-3		)		)



	def 			__A							(       self		):
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]

							A__							=       ["longest", "max_length", "do_not_pad"]
							A__							=       [None, 16, None]
							for max_length, padding in zip(lowercase_			,	lowercase_		):
													A__							=       feature_extractor(
													    lowercase_			,	padding=lowercase_			,	max_length=lowercase_			,	return_attention_mask=lowercase_		)
													A__							=       inputs.input_features
													A__							=       inputs.attention_mask
													A__							=       [np.sum(lowercase_		) for x in attention_mask]

													self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
													self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
													self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)



	def 			__A							(       self		):
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]

							A__							=       ["longest", "max_length", "do_not_pad"]
							A__							=       [None, 16, None]
							for max_length, padding in zip(lowercase_			,	lowercase_		):
													A__							=       feature_extractor(
													    lowercase_			,	max_length=lowercase_			,	padding=lowercase_			,	return_tensors="np"			,	return_attention_mask=lowercase_		)
													A__							=       inputs.input_features
													A__							=       inputs.attention_mask
													A__							=       [np.sum(lowercase_		) for x in attention_mask]

													self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
													self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6		)
													self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
													self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6		)
													self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)



	def 			__A							(       self		):
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]
							A__							=       feature_extractor(
							    lowercase_			,	padding="max_length"			,	max_length=4			,	truncation=lowercase_			,	return_tensors="np"			,	return_attention_mask=lowercase_			,	)
							A__							=       inputs.input_features
							A__							=       inputs.attention_mask
							A__							=       np.sum(attention_mask == 1			,	axis=1		)

							self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
							self._check_zero_mean_unit_variance(input_features[1]		)
							self._check_zero_mean_unit_variance(input_features[2]		)



	def 			__A							(       self		):
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]
							A__							=       feature_extractor(
							    lowercase_			,	padding="longest"			,	max_length=4			,	truncation=lowercase_			,	return_tensors="np"			,	return_attention_mask=lowercase_			,	)
							A__							=       inputs.input_features
							A__							=       inputs.attention_mask
							A__							=       np.sum(attention_mask == 1			,	axis=1		)

							self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
							self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
							self._check_zero_mean_unit_variance(input_features[2]		)

							# make sure that if max_length < longest -> then pad to max_length
							self.assertEqual(input_features.shape			,	(3, 4, 24)		)

							A__							=       [floats_list((1, x)		)[0] for x in range(800			,	1_400			,	200		)]
							A__							=       feature_extractor(
							    lowercase_			,	padding="longest"			,	max_length=16			,	truncation=lowercase_			,	return_tensors="np"			,	return_attention_mask=lowercase_			,	)
							A__							=       inputs.input_features
							A__							=       inputs.attention_mask
							A__							=       np.sum(attention_mask == 1			,	axis=1		)

							self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
							self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
							self._check_zero_mean_unit_variance(input_features[2]		)

							# make sure that if max_length < longest -> then pad to max_length
							self.assertEqual(input_features.shape			,	(3, 6, 24)		)



	def 			__A							(       self		):
							import torch

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

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



	def 			__A							(       self			,	UpperCAmelCase__		):
							from datasets import load_dataset

							A__							=       load_dataset("hf-internal-testing/librispeech_asr_dummy"			,	"clean"			,	split="validation"		)
							# automatic decoding with librispeech
							A__							=       ds.sort("id"		).select(range(lowercase_		)		)[:num_samples]["audio"]

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



	def 			__A							(       self		):
							A__							=       np.array([
							    -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
							    -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
							    -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
							]		)
							# fmt: on

							A__							=       self._load_datasamples(1		)
							A__							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							A__							=       feature_extractor(lowercase_			,	return_tensors="pt"		).input_features
							self.assertEquals(input_features.shape			,	(1, 584, 24)		)
							self.assertTrue(np.allclose(input_features[0, 0, :30]			,	lowercase_			,	atol=1e-4		)		)










import datasets

from .evaluate import evaluate


UpperCAmelCase_							:      List[Any]      =  "\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

UpperCAmelCase_							:      Any      =  "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

UpperCAmelCase_							:      Tuple      =  "\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"






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



	def 			__A							(       self		):
							return datasets.MetricInfo(
							    description=_DESCRIPTION			,	citation=_CITATION			,	inputs_description=_KWARGS_DESCRIPTION			,	features=datasets.Features(
							        {
							            "predictions": {"id": datasets.Value("string"		), "prediction_text": datasets.Value("string"		)},
							            "references": {
							                "id": datasets.Value("string"		),
							                "answers": datasets.features.Sequence(
							                    {
							                        "text": datasets.Value("string"		),
							                        "answer_start": datasets.Value("int32"		),
							                    }		),
							            },
							        }		)			,	codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]			,	reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]			,	)



	def 			__A							(       self			,	UpperCAmelCase__			,	UpperCAmelCase__		):
							A__							=       {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
							A__							=       [
							    {
							        "paragraphs": [
							            {
							                "qas": [
							                    {
							                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
							                        "id": ref["id"],
							                    }
							                    for ref in references
							                ]
							            }
							        ]
							    }
							]
							A__							=       evaluate(dataset=UpperCAmelCase__			,	predictions=UpperCAmelCase__		)
							return score





