





import itertools
import random
import unittest

import numpy as np

from transformers import EncodecFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
						import torch


_SCREAMING_SNAKE_CASE   						=		random.Random()


def 				SCREAMING_SNAKE_CASE__    (     __a ,			__a=1.0 ,			__a=None ,			__a=None ):
	if rng is None:
		snake_case_		:      Optional[int]           =						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
@require_torch
class SCREAMING_SNAKE_CASE_    (       unittest.TestCase       ):






						def __init__(   self   :   Optional[Any]		, _A   :   Any		, _A   :   Tuple=7		, _A   :   Any=400		, _A   :   Dict=2000		, _A   :   Tuple=1		, _A   :   Tuple=0.0		, _A   :   Optional[Any]=24000		, _A   :   Union[str, Any]=True		, )				->     Dict:







							"""simple docstring"""


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






						def UpperCAmelCase_					(   self   :   List[str]						)				->     str:







							"""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,
							}






						def UpperCAmelCase_					(   self   :   Any		, _A   :   List[Any]=False		, _A   :   int=False						)				->     int:







							"""simple docstring"""


							def _flatten(_A   :   Dict						):
								return list(itertools.chain(*_A						)						)

							if equal_length:
								snake_case_		:      List[str]           =						floats_list((self.batch_size, self.max_seq_length)						)
							else:
								# make sure that inputs increase in size
								snake_case_		:      str           =						[
								    _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_		:      Tuple           =						[np.asarray(_A						) for x in audio_inputs]

							return audio_inputs
@require_torch
class SCREAMING_SNAKE_CASE_    (       snake_case_							,					unittest.TestCase       ):
						__magic_name__:      int			   =							EncodecFeatureExtractor






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







							"""simple docstring"""


							snake_case_		:      Any           =						EnCodecFeatureExtractionTester(self						)






						def UpperCAmelCase_					(   self   :   Dict						)				->     int:







							"""simple docstring"""


							snake_case_		:      Optional[Any]           =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
							# create three inputs of length 800, 1000, and 1200
							snake_case_		:      Optional[Any]           =						[floats_list((1, x)						)[0] for x in range(800		, 1400		, 200						)]
							snake_case_		:      Any           =						[np.asarray(_A						) for audio_input in audio_inputs]

							# Test not batched input
							snake_case_		:      str           =						feat_extract(audio_inputs[0]		, return_tensors='np'						).input_values
							snake_case_		:      Tuple           =						feat_extract(np_audio_inputs[0]		, return_tensors='np'						).input_values
							self.assertTrue(np.allclose(_A		, _A		, atol=1E-3						)						)

							# Test batched
							snake_case_		:      Dict           =						feat_extract(_A		, padding=_A		, return_tensors='np'						).input_values
							snake_case_		:      Optional[int]           =						feat_extract(_A		, padding=_A		, return_tensors='np'						).input_values
							for enc_seq_a, enc_seq_a in zip(_A		, _A						):
								self.assertTrue(np.allclose(_A		, _A		, atol=1E-3						)						)






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







							"""simple docstring"""


							snake_case_		:      Optional[int]           =						self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
							snake_case_		:      str           =						np.random.rand(100						).astype(np.floataa						)
							snake_case_		:      Optional[Any]           =						np_audio_inputs.tolist()

							for inputs in [py_audio_inputs, np_audio_inputs]:
								snake_case_		:      Optional[Any]           =						feature_extractor.pad([{'input_values': inputs}]		, return_tensors='np'						)
								self.assertTrue(np_processed.input_values.dtype == np.floataa						)
								snake_case_		:      List[str]           =						feature_extractor.pad([{'input_values': inputs}]		, return_tensors='pt'						)
								self.assertTrue(pt_processed.input_values.dtype == torch.floataa						)






						def UpperCAmelCase_					(   self   :   Optional[Any]		, _A   :   Optional[Any]						)				->     List[Any]:







							"""simple docstring"""


							from datasets import load_dataset

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

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






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







							"""simple docstring"""


							snake_case_		:      Optional[int]           =						torch.tensor(
							    [2.3804E-03, 2.0752E-03, 1.9836E-03, 2.1057E-03, 1.6174E-03,
							     3.0518E-04, 9.1553E-05, 3.3569E-04, 9.7656E-04, 1.8311E-03,
							     2.0142E-03, 2.1057E-03, 1.7395E-03, 4.5776E-04, -3.9673E-04,
							     4.5776E-04, 1.0071E-03, 9.1553E-05, 4.8828E-04, 1.1597E-03,
							     7.3242E-04, 9.4604E-04, 1.8005E-03, 1.8311E-03, 8.8501E-04,
							     4.2725E-04, 4.8828E-04, 7.3242E-04, 1.0986E-03, 2.1057E-03]						)
							# fmt: on
							snake_case_		:      List[Any]           =						self._load_datasamples(1						)
							snake_case_		:      Dict           =						EncodecFeatureExtractor()
							snake_case_		:      str           =						feature_extractor(_A		, return_tensors='pt'						).input_values
							self.assertEquals(input_values.shape		, (1, 1, 93680)						)
							self.assertTrue(torch.allclose(input_values[0, 0, :30]		, _A		, atol=1E-6						)						)






						def UpperCAmelCase_					(   self   :   Any						)				->     Any:







							"""simple docstring"""


							snake_case_		:      Optional[Any]           =						torch.tensor(
							    [2.3804E-03, 2.0752E-03, 1.9836E-03, 2.1057E-03, 1.6174E-03,
							     3.0518E-04, 9.1553E-05, 3.3569E-04, 9.7656E-04, 1.8311E-03,
							     2.0142E-03, 2.1057E-03, 1.7395E-03, 4.5776E-04, -3.9673E-04,
							     4.5776E-04, 1.0071E-03, 9.1553E-05, 4.8828E-04, 1.1597E-03,
							     7.3242E-04, 9.4604E-04, 1.8005E-03, 1.8311E-03, 8.8501E-04,
							     4.2725E-04, 4.8828E-04, 7.3242E-04, 1.0986E-03, 2.1057E-03]						)
							# fmt: on
							snake_case_		:      int           =						self._load_datasamples(1						)
							snake_case_		:      List[str]           =						[np.tile(input_audio[0][None]		, reps=(2, 1)						)]
							input_audio[0][1] *= 0.5
							snake_case_		:      List[str]           =						EncodecFeatureExtractor(feature_size=2						)
							snake_case_		:      Dict           =						feature_extractor(_A		, return_tensors='pt'						).input_values
							self.assertEquals(input_values.shape		, (1, 2, 93680)						)
							self.assertTrue(torch.allclose(input_values[0, 0, :30]		, _A		, atol=1E-6						)						)
							self.assertTrue(torch.allclose(input_values[0, 1, :30]		, EXPECTED_INPUT_VALUES * 0.5		, atol=1E-6						)						)






						def UpperCAmelCase_					(   self   :   str						)				->     List[Any]:







							"""simple docstring"""


							snake_case_		:      Optional[int]           =						self._load_datasamples(2						)
							# would be easier if the stride was like
							snake_case_		:      Dict           =						EncodecFeatureExtractor(feature_size=1		, chunk_length_s=1		, overlap=0.0_1						)

							# pad and trunc raise an error ?
							with self.assertRaisesRegex(
							    _A		, '^Both padding and truncation were set. Make sure you only set one.$'		, ):
								snake_case_		:      Dict           =						feature_extractor(
								    _A		, padding='max_length'		, truncation=_A		, return_tensors='pt'						).input_values

							# truncate to chunk
							snake_case_		:      Optional[int]           =						feature_extractor(_A		, truncation=_A		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (2, 1, 71520)						)  # 2 chunks

							# force truncate to max_length
							snake_case_		:      Optional[int]           =						feature_extractor(
							    _A		, truncation=_A		, max_length=48000		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (2, 1, 48000)						)

							# pad to chunk
							snake_case_		:      str           =						feature_extractor(_A		, padding=_A		, return_tensors='pt'						).input_values
							self.assertEquals(padded_outputs.shape		, (2, 1, 95280)						)

							# pad to chunk
							snake_case_		:      Dict           =						feature_extractor(_A		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (2, 1, 95280)						)

							# force pad to max length
							snake_case_		:      Tuple           =						feature_extractor(
							    _A		, padding='max_length'		, max_length=100000		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (2, 1, 100000)						)

							# force no pad
							with self.assertRaisesRegex(
							    _A		, '^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'		, ):
								snake_case_		:      Optional[Any]           =						feature_extractor(_A		, padding=_A		, return_tensors='pt'						).input_values

							snake_case_		:      Optional[int]           =						feature_extractor(input_audio[0]		, padding=_A		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (1, 1, 93680)						)

							# no pad if no chunk_length_s
							snake_case_		:      List[Any]           =						None
							with self.assertRaisesRegex(
							    _A		, '^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'		, ):
								snake_case_		:      Optional[int]           =						feature_extractor(_A		, padding=_A		, return_tensors='pt'						).input_values

							snake_case_		:      Optional[Any]           =						feature_extractor(input_audio[0]		, padding=_A		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (1, 1, 93680)						)

							# no pad if no overlap
							snake_case_		:      Optional[int]           =						2
							snake_case_		:      List[Any]           =						None
							with self.assertRaisesRegex(
							    _A		, '^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'		, ):
								snake_case_		:      List[str]           =						feature_extractor(_A		, padding=_A		, return_tensors='pt'						).input_values

							snake_case_		:      Any           =						feature_extractor(input_audio[0]		, padding=_A		, return_tensors='pt'						).input_values
							self.assertEquals(truncated_outputs.shape		, (1, 1, 93680)						)








import os
import shutil
import unittest
from unittest.mock import patch

from transformers.testing_utils import CaptureStd, is_pt_tf_cross_test, require_torch
class SCREAMING_SNAKE_CASE_    (       unittest.TestCase       ):






						@patch('sys.argv'		, ['fakeprogrampath', 'env']						)
						def UpperCAmelCase_					(   self   :   Tuple						)				->     Dict:







							"""simple docstring"""


							import transformers.commands.transformers_cli

							with CaptureStd() as cs:
								transformers.commands.transformers_cli.main()
							self.assertIn('Python version'		, cs.out						)
							self.assertIn('Platform'		, cs.out						)
							self.assertIn('Using distributed or parallel set-up in script?'		, cs.out						)






						@is_pt_tf_cross_test
						@patch(
						    'sys.argv'		, ['fakeprogrampath', 'pt-to-tf', '--model-name', 'hf-internal-testing/tiny-random-gptj', '--no-pr']						)
						def UpperCAmelCase_					(   self   :   Union[str, Any]						)				->     Optional[Any]:







							"""simple docstring"""


							import transformers.commands.transformers_cli

							shutil.rmtree('/tmp/hf-internal-testing/tiny-random-gptj'		, ignore_errors=_A						)  # cleans potential past runs
							transformers.commands.transformers_cli.main()

							# The original repo has no TF weights -- if they exist, they were created by the CLI
							self.assertTrue(os.path.exists('/tmp/hf-internal-testing/tiny-random-gptj/tf_model.h5'						)						)






						@require_torch
						@patch('sys.argv'		, ['fakeprogrampath', 'download', 'hf-internal-testing/tiny-random-gptj', '--cache-dir', '/tmp']						)
						def UpperCAmelCase_					(   self   :   List[Any]						)				->     str:







							"""simple docstring"""


							import transformers.commands.transformers_cli

							# # remove any previously downloaded model to start clean
							shutil.rmtree('/tmp/models--hf-internal-testing--tiny-random-gptj'		, ignore_errors=_A						)

							# run the command
							transformers.commands.transformers_cli.main()

							# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--tiny-random-gptj
							self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/blobs'						)						)
							self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/refs'						)						)
							self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--tiny-random-gptj/snapshots'						)						)






						@require_torch
						@patch(
						    'sys.argv'		, [
						        'fakeprogrampath',
						        'download',
						        'hf-internal-testing/test_dynamic_model_with_tokenizer',
						        '--trust-remote-code',
						        '--cache-dir',
						        '/tmp',
						    ]		, )
						def UpperCAmelCase_					(   self   :   Optional[Any]						)				->     str:







							"""simple docstring"""


							import transformers.commands.transformers_cli

							# # remove any previously downloaded model to start clean
							shutil.rmtree('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer'		, ignore_errors=_A						)

							# run the command
							transformers.commands.transformers_cli.main()

							# check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer
							self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/blobs'						)						)
							self.assertTrue(os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/refs'						)						)
							self.assertTrue(
							    os.path.exists('/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/snapshots'						)						)

