

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

A_	= random.Random()







def   _UpperCamelCase	(		__UpperCamelCase    ,__UpperCamelCase=1.0    ,__UpperCamelCase=None    ,__UpperCamelCase=None	)       ->			Union[str, Any]:
					if rng is None:
										lowerCamelCase_  				=      global_rng

					lowerCamelCase_  				=      []
					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    ):

		'''simple docstring'''






		def __init__(					self   ,			SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_=7   ,			SCREAMING_SNAKE_CASE_=400   ,			SCREAMING_SNAKE_CASE_=2000   ,			SCREAMING_SNAKE_CASE_=10   ,			SCREAMING_SNAKE_CASE_=160   ,			SCREAMING_SNAKE_CASE_=8   ,			SCREAMING_SNAKE_CASE_=0.0   ,			SCREAMING_SNAKE_CASE_=4000   ,			SCREAMING_SNAKE_CASE_=False   ,			SCREAMING_SNAKE_CASE_=True   ,			)  ->     Optional[Any]:






							'''simple docstring'''



							lowerCamelCase_  				=      parent
							lowerCamelCase_  				=      batch_size
							lowerCamelCase_  				=      min_seq_length
							lowerCamelCase_  				=      max_seq_length
							lowerCamelCase_  				=      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							lowerCamelCase_  				=      padding_value
							lowerCamelCase_  				=      sampling_rate
							lowerCamelCase_  				=      return_attention_mask
							lowerCamelCase_  				=      do_normalize
							lowerCamelCase_  				=      feature_size
							lowerCamelCase_  				=      chunk_length
							lowerCamelCase_  				=      hop_length
		def 			UpperCamelCase(					self		)  ->     Tuple:






							'''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 			UpperCamelCase(					self   ,			SCREAMING_SNAKE_CASE_=False   ,			SCREAMING_SNAKE_CASE_=False		)  ->     Any:






							'''simple docstring'''



							def _flatten(SCREAMING_SNAKE_CASE_		):
												return list(itertools.chain(*SCREAMING_SNAKE_CASE_		)		)

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


@require_torch
@require_torchaudio
class  UpperCAmelCase (							UpperCAmelCase__  , unittest.TestCase    ):

		'''simple docstring'''






		SCREAMING_SNAKE_CASE_           =					WhisperFeatureExtractor if is_speech_available() else None
		def 			UpperCamelCase(					self		)  ->     Optional[int]:






							'''simple docstring'''



							lowerCamelCase_  				=      WhisperFeatureExtractionTester(self		)
		def 			UpperCamelCase(					self		)  ->     List[str]:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_dict		)

							with tempfile.TemporaryDirectory() as tmpdirname:
												lowerCamelCase_  				=      feat_extract_first.save_pretrained(SCREAMING_SNAKE_CASE_		)[0]
												check_json_file_has_correct_format(SCREAMING_SNAKE_CASE_		)
												lowerCamelCase_  				=      self.feature_extraction_class.from_pretrained(SCREAMING_SNAKE_CASE_		)

							lowerCamelCase_  				=      feat_extract_first.to_dict()
							lowerCamelCase_  				=      feat_extract_second.to_dict()
							lowerCamelCase_  				=      feat_extract_first.mel_filters
							lowerCamelCase_  				=      feat_extract_second.mel_filters
							self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		)		)
							self.assertEqual(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		)
		def 			UpperCamelCase(					self		)  ->     Dict:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_dict		)

							with tempfile.TemporaryDirectory() as tmpdirname:
												lowerCamelCase_  				=      os.path.join(SCREAMING_SNAKE_CASE_   ,			'feat_extract.json'		)
												feat_extract_first.to_json_file(SCREAMING_SNAKE_CASE_		)
												lowerCamelCase_  				=      self.feature_extraction_class.from_json_file(SCREAMING_SNAKE_CASE_		)

							lowerCamelCase_  				=      feat_extract_first.to_dict()
							lowerCamelCase_  				=      feat_extract_second.to_dict()
							lowerCamelCase_  				=      feat_extract_first.mel_filters
							lowerCamelCase_  				=      feat_extract_second.mel_filters
							self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		)		)
							self.assertEqual(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		)
		def 			UpperCamelCase(					self		)  ->     Union[str, Any]:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							# create three inputs of length 800, 1000, and 1200
							lowerCamelCase_  				=      [floats_list((1, x)		)[0] for x in range(800   ,			1400   ,			200		)]
							lowerCamelCase_  				=      [np.asarray(SCREAMING_SNAKE_CASE_		) for speech_input in speech_inputs]

							# Test feature size
							lowerCamelCase_  				=      feature_extractor(SCREAMING_SNAKE_CASE_   ,			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
							lowerCamelCase_  				=      feature_extractor(speech_inputs[0]   ,			return_tensors='np'		).input_features
							lowerCamelCase_  				=      feature_extractor(np_speech_inputs[0]   ,			return_tensors='np'		).input_features
							self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_   ,			atol=1E-3		)		)

							# Test batched
							lowerCamelCase_  				=      feature_extractor(SCREAMING_SNAKE_CASE_   ,			return_tensors='np'		).input_features
							lowerCamelCase_  				=      feature_extractor(SCREAMING_SNAKE_CASE_   ,			return_tensors='np'		).input_features
							for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		):
												self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_   ,			atol=1E-3		)		)

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

							# Test truncation required
							lowerCamelCase_  				=      [floats_list((1, x)		)[0] for x in range(200   ,			(feature_extractor.n_samples + 500)   ,			200		)]
							lowerCamelCase_  				=      [np.asarray(SCREAMING_SNAKE_CASE_		) for speech_input in speech_inputs]

							lowerCamelCase_  				=      [x[: feature_extractor.n_samples] for x in speech_inputs]
							lowerCamelCase_  				=      [np.asarray(SCREAMING_SNAKE_CASE_		) for speech_input in speech_inputs_truncated]

							lowerCamelCase_  				=      feature_extractor(SCREAMING_SNAKE_CASE_   ,			return_tensors='np'		).input_features
							lowerCamelCase_  				=      feature_extractor(SCREAMING_SNAKE_CASE_   ,			return_tensors='np'		).input_features
							for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		):
												self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_   ,			atol=1E-3		)		)
		def 			UpperCamelCase(					self		)  ->     List[str]:






							'''simple docstring'''



							import torch

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

							for inputs in [py_speech_inputs, np_speech_inputs]:
												lowerCamelCase_  				=      feature_extractor.pad([{'input_features': inputs}]   ,			return_tensors='np'		)
												self.assertTrue(np_processed.input_features.dtype == np.floataa		)
												lowerCamelCase_  				=      feature_extractor.pad([{'input_features': inputs}]   ,			return_tensors='pt'		)
												self.assertTrue(pt_processed.input_features.dtype == torch.floataa		)
		def 			UpperCamelCase(					self   ,			SCREAMING_SNAKE_CASE_		)  ->     Tuple:






							'''simple docstring'''



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

							return [x["array"] for x in speech_samples]
		def 			UpperCamelCase(					self		)  ->     str:






							'''simple docstring'''



							lowerCamelCase_  				=      torch.tensor(
							    [
							        0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
							        0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
							        0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
							        -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
							    ]		)
							# fmt: on

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






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






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
							lowerCamelCase_  				=      self._load_datasamples(1		)[0]
							lowerCamelCase_  				=      ((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
							lowerCamelCase_  				=      feat_extract.zero_mean_unit_var_norm([audio]   ,			attention_mask=SCREAMING_SNAKE_CASE_		)[0]

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





import pprint

import requests

lowerCamelCase__						=				"https://zenquotes.io/api"


def 				__A()		->					list:

		"""simple docstring"""






		return requests.get(API_ENDPOINT_URL + """/today"""			).json()


def 				__A()		->					list:

		"""simple docstring"""






		return requests.get(API_ENDPOINT_URL + """/random"""			).json()


if __name__ == "__main__":
		lowerCamelCase__						=				random_quotes()
		pprint.pprint(response)



