




import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_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_torch_available():
		import torch

if is_datasets_available():
		from datasets import load_dataset

lowercase__				:     Tuple 		=						random.Random()


def 						lowerCamelCase__    (		_A     ,	_A=1.0     ,	_A=None     ,	_A=None							):







	'''simple docstring'''





	if rng is None:
		snake_case_		    =     global_rng

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

	return values
class 		UpperCAmelCase   (				unittest.TestCase			):







						'''simple docstring'''





						def __init__(						self							:     Any       ,	__lowercase							:     int       ,	__lowercase							:     Optional[int]=7       ,	__lowercase							:     int=4_00       ,	__lowercase							:     List[str]=20_00       ,	__lowercase							:     Dict=20_48       ,	__lowercase							:     int=1_28       ,	__lowercase							:     List[Any]=1       ,	__lowercase							:     str=5_12       ,	__lowercase							:     Any=30       ,	__lowercase							:     Dict=4_41_00       ,	):






							"""simple docstring"""






							snake_case_		    =     parent
							snake_case_		    =     batch_size
							snake_case_		    =     min_seq_length
							snake_case_		    =     max_seq_length
							snake_case_		    =     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							snake_case_		    =     spectrogram_length
							snake_case_		    =     feature_size
							snake_case_		    =     num_audio_channels
							snake_case_		    =     hop_length
							snake_case_		    =     chunk_length
							snake_case_		    =     sampling_rate






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							return {
							    "spectrogram_length": self.spectrogram_length,
							    "feature_size": self.feature_size,
							    "num_audio_channels": self.num_audio_channels,
							    "hop_length": self.hop_length,
							    "chunk_length": self.chunk_length,
							    "sampling_rate": self.sampling_rate,
							}






						def        snake_case__						(						self							:     List[Any]       ,	__lowercase							:     Optional[int]=False       ,	__lowercase							:     Union[str, Any]=False						):






							"""simple docstring"""






							def _flatten(__lowercase							:     str						):
								return list(itertools.chain(*__lowercase						)						)

							if equal_length:
								snake_case_		    =     [floats_list((self.max_seq_length, self.feature_size)						) for _ in range(self.batch_size						)]
							else:
								# make sure that inputs increase in size
								snake_case_		    =     [
								    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_		    =     [np.asarray(__lowercase						) for x in speech_inputs]
							return speech_inputs







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







						'''simple docstring'''





						lowerCAmelCase_     		=   TvltFeatureExtractor






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     TvltFeatureExtractionTester(self						)






						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     self.feature_extraction_class(**self.feat_extract_dict						)
							self.assertTrue(hasattr(__lowercase       ,	"spectrogram_length"						)						)
							self.assertTrue(hasattr(__lowercase       ,	"feature_size"						)						)
							self.assertTrue(hasattr(__lowercase       ,	"num_audio_channels"						)						)
							self.assertTrue(hasattr(__lowercase       ,	"hop_length"						)						)
							self.assertTrue(hasattr(__lowercase       ,	"chunk_length"						)						)
							self.assertTrue(hasattr(__lowercase       ,	"sampling_rate"						)						)






						def        snake_case__						(						self							:     Optional[int]						):






							"""simple docstring"""






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

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

							snake_case_		    =     feat_extract_first.to_dict()
							snake_case_		    =     feat_extract_second.to_dict()
							snake_case_		    =     dict_first.pop("mel_filters"						)
							snake_case_		    =     dict_second.pop("mel_filters"						)
							self.assertTrue(np.allclose(__lowercase       ,	__lowercase						)						)
							self.assertEqual(__lowercase       ,	__lowercase						)






						def        snake_case__						(						self							:     Dict						):






							"""simple docstring"""






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

							with tempfile.TemporaryDirectory() as tmpdirname:
								snake_case_		    =     os.path.join(__lowercase       ,	"feat_extract.json"						)
								feat_extract_first.to_json_file(__lowercase						)
								snake_case_		    =     self.feature_extraction_class.from_json_file(__lowercase						)

							snake_case_		    =     feat_extract_first.to_dict()
							snake_case_		    =     feat_extract_second.to_dict()
							snake_case_		    =     dict_first.pop("mel_filters"						)
							snake_case_		    =     dict_second.pop("mel_filters"						)
							self.assertTrue(np.allclose(__lowercase       ,	__lowercase						)						)
							self.assertEqual(__lowercase       ,	__lowercase						)






						def        snake_case__						(						self							:     Dict						):






							"""simple docstring"""






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

							# create three inputs of length 800, 1000, and 1200
							snake_case_		    =     [floats_list((1, x)						)[0] for x in range(8_00       ,	14_00       ,	2_00						)]
							snake_case_		    =     [np.asarray(__lowercase						) for speech_input in speech_inputs]

							# Test not batched input
							snake_case_		    =     feature_extractor(np_speech_inputs[0]       ,	return_tensors="np"       ,	sampling_rate=4_41_00						).audio_values

							self.assertTrue(encoded_audios.ndim == 4						)
							self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size						)
							self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length						)
							self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels						)

							# Test batched
							snake_case_		    =     feature_extractor(__lowercase       ,	return_tensors="np"       ,	sampling_rate=4_41_00						).audio_values

							self.assertTrue(encoded_audios.ndim == 4						)
							self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size						)
							self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length						)
							self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels						)

							# Test audio masking
							snake_case_		    =     feature_extractor(
							    __lowercase       ,	return_tensors="np"       ,	sampling_rate=4_41_00       ,	mask_audio=__lowercase						).audio_values

							self.assertTrue(encoded_audios.ndim == 4						)
							self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size						)
							self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length						)
							self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels						)

							# Test 2-D numpy arrays are batched.
							snake_case_		    =     [floats_list((1, x)						)[0] for x in (8_00, 8_00, 8_00)]
							snake_case_		    =     np.asarray(__lowercase						)
							snake_case_		    =     feature_extractor(__lowercase       ,	return_tensors="np"       ,	sampling_rate=4_41_00						).audio_values
							self.assertTrue(encoded_audios.ndim == 4						)
							self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size						)
							self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length						)
							self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels						)






						def        snake_case__						(						self							:     str       ,	__lowercase							:     Tuple						):






							"""simple docstring"""






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

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






						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     self._load_datasamples(1						)
							snake_case_		    =     TvltFeatureExtractor()
							snake_case_		    =     feature_extractor(__lowercase       ,	return_tensors="pt"						).audio_values

							self.assertEquals(audio_values.shape       ,	(1, 1, 1_92, 1_28)						)

							snake_case_		    =     torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]]						)
							self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]       ,	__lowercase       ,	atol=1E-4						)						)






def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     []
	snake_case_		    =     1

	while len(_A							) < 1E6:
		constant.append(str(_A							)							)
		i += 1

	snake_case_		    =     "".join(_A							)

	return (
	    int(constant[0]							)
	    * int(constant[9]							)
	    * int(constant[99]							)
	    * int(constant[999]							)
	    * int(constant[9999]							)
	    * int(constant[99999]							)
	    * int(constant[999999]							)
	)


if __name__ == "__main__":
		print(solution())
