





'''simple docstring'''






import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


A             =			random.Random()

if is_torch_available():
		import torch





def       lowercase_      (		lowercase__				,  lowercase__=1.0				,  lowercase__=None				,  lowercase__=None						)   ->str:
						if rng is None:
												_snake_case:   Any  					=		global_rng

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

						return values







class 			lowerCamelCase					(						unittest.TestCase  ):


					def __init__(					self	:  Optional[int]       ,     __snake_case	:  Optional[int]       ,     __snake_case	:  Dict=7       ,     __snake_case	:  Optional[Any]=4_00       ,     __snake_case	:  List[Any]=20_00       ,     __snake_case	:  List[Any]=1       ,     __snake_case	:  str=0.0       ,     __snake_case	:  str=1_60_00       ,     __snake_case	:  int=True       ,     __snake_case	:  List[Any]=True       ,     ):





											'''simple docstring'''





											_snake_case:   List[Any]  					=		parent
											_snake_case:   Optional[Any]  					=		batch_size
											_snake_case:   List[str]  					=		min_seq_length
											_snake_case:   Optional[int]  					=		max_seq_length
											_snake_case:   List[str]  					=		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											_snake_case:   Tuple  					=		feature_size
											_snake_case:   Dict  					=		padding_value
											_snake_case:   Dict  					=		sampling_rate
											_snake_case:   Optional[int]  					=		return_attention_mask
											_snake_case:   str  					=		do_normalize


					def    SCREAMING_SNAKE_CASE_		(					self	:  List[Any]       ):





											'''simple docstring'''





											return {
											    "feature_size": self.feature_size,
											    "padding_value": self.padding_value,
											    "sampling_rate": self.sampling_rate,
											    "return_attention_mask": self.return_attention_mask,
											    "do_normalize": self.do_normalize,
											}




					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ,     __snake_case	:  Optional[Any]=False       ,     __snake_case	:  Dict=False       ):





											'''simple docstring'''





											def _flatten(__snake_case	:  int       ):
																	return list(itertools.chain(*lowerCAmelCase_       )       )

											if equal_length:
																	_snake_case:   int  					=		floats_list((self.batch_size, self.max_seq_length)       )
											else:
																	# make sure that inputs increase in size
																	_snake_case:   Tuple  					=		[
																	    _flatten(floats_list((x, self.feature_size)       )       )
																	    for x in range(self.min_seq_length       ,     self.max_seq_length       ,     self.seq_length_diff       )
																	]

											if numpify:
																	_snake_case:   Union[str, Any]  					=		[np.asarray(lowerCAmelCase_       ) for x in speech_inputs]

											return speech_inputs







@require_torch
@require_torchaudio
class 			lowerCamelCase					(						__lowerCAmelCase     ,							unittest.TestCase  ):
					_SCREAMING_SNAKE_CASE							   =    ASTFeatureExtractor


					def    SCREAMING_SNAKE_CASE_		(					self	:  Any       ):





											'''simple docstring'''





											_snake_case:   int  					=		ASTFeatureExtractionTester(self       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  List[Any]       ):





											'''simple docstring'''





											_snake_case:   List[str]  					=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											# create three inputs of length 800, 1000, and 1200
											_snake_case:   Optional[int]  					=		[floats_list((1, x)       )[0] for x in range(8_00       ,     14_00       ,     2_00       )]
											_snake_case:   Tuple  					=		[np.asarray(lowerCAmelCase_       ) for speech_input in speech_inputs]

											# Test not batched input
											_snake_case:   List[str]  					=		feat_extract(speech_inputs[0]       ,     return_tensors='np'       ).input_values
											_snake_case:   Any  					=		feat_extract(np_speech_inputs[0]       ,     return_tensors='np'       ).input_values
											self.assertTrue(np.allclose(lowerCAmelCase_       ,     lowerCAmelCase_       ,     atol=1e-3       )       )

											# Test batched
											_snake_case:   List[str]  					=		feat_extract(lowerCAmelCase_       ,     padding=lowerCAmelCase_       ,     return_tensors='np'       ).input_values
											_snake_case:   Dict  					=		feat_extract(lowerCAmelCase_       ,     padding=lowerCAmelCase_       ,     return_tensors='np'       ).input_values
											for enc_seq_a, enc_seq_a in zip(lowerCAmelCase_       ,     lowerCAmelCase_       ):
																	self.assertTrue(np.allclose(lowerCAmelCase_       ,     lowerCAmelCase_       ,     atol=1e-3       )       )

											# Test 2-D numpy arrays are batched.
											_snake_case:   Tuple  					=		[floats_list((1, x)       )[0] for x in (8_00, 8_00, 8_00)]
											_snake_case:   List[str]  					=		np.asarray(lowerCAmelCase_       )
											_snake_case:   str  					=		feat_extract(lowerCAmelCase_       ,     return_tensors='np'       ).input_values
											_snake_case:   Union[str, Any]  					=		feat_extract(lowerCAmelCase_       ,     return_tensors='np'       ).input_values
											for enc_seq_a, enc_seq_a in zip(lowerCAmelCase_       ,     lowerCAmelCase_       ):
																	self.assertTrue(np.allclose(lowerCAmelCase_       ,     lowerCAmelCase_       ,     atol=1e-3       )       )


					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ):





											'''simple docstring'''





											import torch

											_snake_case:   Dict  					=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											_snake_case:   List[str]  					=		np.random.rand(1_00       ).astype(np.floataa       )
											_snake_case:   Optional[int]  					=		np_speech_inputs.tolist()

											for inputs in [py_speech_inputs, np_speech_inputs]:
																	_snake_case:   Any  					=		feature_extractor.pad([{'input_values': inputs}]       ,     return_tensors='np'       )
																	self.assertTrue(np_processed.input_values.dtype == np.floataa       )
																	_snake_case:   Any  					=		feature_extractor.pad([{'input_values': inputs}]       ,     return_tensors='pt'       )
																	self.assertTrue(pt_processed.input_values.dtype == torch.floataa       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ,     __snake_case	:  int       ):





											'''simple docstring'''





											from datasets import load_dataset

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

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




					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[int]       ):





											'''simple docstring'''





											_snake_case:   Tuple  					=		torch.tensor(
											    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
											     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
											     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
											     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]       )
											# fmt: on

											_snake_case:   Tuple  					=		self._load_datasamples(1       )
											_snake_case:   Union[str, Any]  					=		ASTFeatureExtractor()
											_snake_case:   Optional[Any]  					=		feature_extractor(lowerCAmelCase_       ,     return_tensors='pt'       ).input_values
											self.assertEquals(input_values.shape       ,     (1, 10_24, 1_28)       )
											self.assertTrue(torch.allclose(input_values[0, 0, :30]       ,     lowerCAmelCase_       ,     atol=1e-4       )       )












'''simple docstring'''






A   : List[str]             =			'\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n'

A   : List[str]             =			[{'type': 'code', 'content': INSTALL_CONTENT}]
A   : Dict             =			{
    '{processor_class}': 'FakeProcessorClass',
    '{model_class}': 'FakeModelClass',
    '{object_class}': 'FakeObjectClass',
}





