




"""simple docstring"""




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

lowerCAmelCase							:			int     							=							random.Random()



def     a__ (				snake_case__       , snake_case__=1.0       , snake_case__=None       , snake_case__=None				)						->					List[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






class     __magic_name__							(       unittest.TestCase				):






				'''simple docstring'''





				def __init__(       self		, _a		, _a=7		, _a=400		, _a=2_000		, _a=2_048		, _a=128		, _a=1		, _a=512		, _a=30		, _a=44_100		, ):





							"""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			      =					spectrogram_length
							lowerCamelCase			      =					feature_size
							lowerCamelCase			      =					num_audio_channels
							lowerCamelCase			      =					hop_length
							lowerCamelCase			      =					chunk_length
							lowerCamelCase			      =					sampling_rate





				def    _lowerCAmelCase				(       self ):





							"""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    _lowerCAmelCase				(       self		, _a=False		, _a=False ):





							"""simple docstring"""
							def _flatten(_a ):
										return list(itertools.chain(*_a ) )

							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(_a ) for x in speech_inputs]
							return speech_inputs






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






				'''simple docstring'''
				__UpperCamelCase			  =						TvltFeatureExtractor





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					TvltFeatureExtractionTester(self )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.feature_extraction_class(**self.feat_extract_dict )
							self.assertTrue(hasattr(_a		, """spectrogram_length""" ) )
							self.assertTrue(hasattr(_a		, """feature_size""" ) )
							self.assertTrue(hasattr(_a		, """num_audio_channels""" ) )
							self.assertTrue(hasattr(_a		, """hop_length""" ) )
							self.assertTrue(hasattr(_a		, """chunk_length""" ) )
							self.assertTrue(hasattr(_a		, """sampling_rate""" ) )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.feature_extraction_class(**self.feat_extract_dict )

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

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.feature_extraction_class(**self.feat_extract_dict )

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

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							# Initialize feature_extractor
							lowerCamelCase			      =					self.feature_extraction_class(**self.feat_extract_dict )

							# create three inputs of length 800, 1000, and 1200
							lowerCamelCase			      =					[floats_list((1, x) )[0] for x in range(800		, 1_400		, 200 )]
							lowerCamelCase			      =					[np.asarray(_a ) for speech_input in speech_inputs]

							# Test not batched input
							lowerCamelCase			      =					feature_extractor(np_speech_inputs[0]		, return_tensors="""np"""		, sampling_rate=44_100 ).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
							lowerCamelCase			      =					feature_extractor(_a		, return_tensors="""np"""		, sampling_rate=44_100 ).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
							lowerCamelCase			      =					feature_extractor(
							    _a		, return_tensors="""np"""		, sampling_rate=44_100		, mask_audio=_a ).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.
							lowerCamelCase			      =					[floats_list((1, x) )[0] for x in (800, 800, 800)]
							lowerCamelCase			      =					np.asarray(_a )
							lowerCamelCase			      =					feature_extractor(_a		, return_tensors="""np"""		, sampling_rate=44_100 ).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    _lowerCAmelCase				(       self		, _a ):





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

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self._load_datasamples(1 )
							lowerCamelCase			      =					TvltFeatureExtractor()
							lowerCamelCase			      =					feature_extractor(_a		, return_tensors="""pt""" ).audio_values

							self.assertEquals(audio_values.shape		, (1, 1, 192, 128) )

							lowerCamelCase			      =					torch.tensor([[-0.3_032, -0.2_708], [-0.4_434, -0.4_007]] )
							self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]		, _a		, atol=1e-4 ) )









"""simple docstring"""




import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor






@require_tokenizers
@require_vision
class     __magic_name__							(       unittest.TestCase				):






				'''simple docstring'''





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					tempfile.mkdtemp()

							# fmt: off
							lowerCamelCase			      =					["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
							# fmt: on
							lowerCamelCase			      =					os.path.join(self.tmpdirname		, VOCAB_FILES_NAMES["""vocab_file"""] )
							with open(self.vocab_file		, """w"""		, encoding="""utf-8""" ) as vocab_writer:
										vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) )

							lowerCamelCase			      =					{
							    """do_resize""": True,
							    """size""": {"""height""": 18, """width""": 18},
							    """do_normalize""": True,
							    """image_mean""": [0.5, 0.5, 0.5],
							    """image_std""": [0.5, 0.5, 0.5],
							}
							lowerCamelCase			      =					os.path.join(self.tmpdirname		, _a )
							with open(self.image_processor_file		, """w"""		, encoding="""utf-8""" ) as fp:
										json.dump(_a		, _a )





				def    _lowerCAmelCase				(       self		, **_a ):





							"""simple docstring"""
							return BertTokenizer.from_pretrained(self.tmpdirname		, **_a )





				def    _lowerCAmelCase				(       self		, **_a ):





							"""simple docstring"""
							return ViTImageProcessor.from_pretrained(self.tmpdirname		, **_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							shutil.rmtree(self.tmpdirname )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					[np.random.randint(255		, size=(3, 30, 400)		, dtype=np.uinta )]

							lowerCamelCase			      =					[Image.fromarray(np.moveaxis(_a		, 0		, -1 ) ) for x in image_inputs]

							return image_inputs





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_tokenizer()
							lowerCamelCase			      =					self.get_image_processor()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							processor.save_pretrained(self.tmpdirname )
							lowerCamelCase			      =					VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname )

							self.assertEqual(processor.tokenizer.get_vocab()		, tokenizer.get_vocab() )
							self.assertIsInstance(processor.tokenizer		, (BertTokenizer, BertTokenizerFast) )

							self.assertEqual(processor.image_processor.to_json_string()		, image_processor.to_json_string() )
							self.assertIsInstance(processor.image_processor		, _a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					VisionTextDualEncoderProcessor(
							    tokenizer=self.get_tokenizer()		, image_processor=self.get_image_processor() )
							processor.save_pretrained(self.tmpdirname )

							lowerCamelCase			      =					self.get_tokenizer(bos_token="""(BOS)"""		, eos_token="""(EOS)""" )
							lowerCamelCase			      =					self.get_image_processor(do_normalize=_a		, padding_value=1.0 )

							lowerCamelCase			      =					VisionTextDualEncoderProcessor.from_pretrained(
							    self.tmpdirname		, bos_token="""(BOS)"""		, eos_token="""(EOS)"""		, do_normalize=_a		, padding_value=1.0 )

							self.assertEqual(processor.tokenizer.get_vocab()		, tokenizer_add_kwargs.get_vocab() )
							self.assertIsInstance(processor.tokenizer		, (BertTokenizer, BertTokenizerFast) )

							self.assertEqual(processor.image_processor.to_json_string()		, image_processor_add_kwargs.to_json_string() )
							self.assertIsInstance(processor.image_processor		, _a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_image_processor()
							lowerCamelCase			      =					self.get_tokenizer()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							lowerCamelCase			      =					self.prepare_image_inputs()

							lowerCamelCase			      =					image_processor(_a		, return_tensors="""np""" )
							lowerCamelCase			      =					processor(images=_a		, return_tensors="""np""" )

							for key in input_feat_extract.keys():
										self.assertAlmostEqual(input_feat_extract[key].sum()		, input_processor[key].sum()		, delta=1e-2 )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_image_processor()
							lowerCamelCase			      =					self.get_tokenizer()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							lowerCamelCase			      =					"""lower newer"""

							lowerCamelCase			      =					processor(text=_a )

							lowerCamelCase			      =					tokenizer(_a )

							for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key]		, encoded_processor[key] )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_image_processor()
							lowerCamelCase			      =					self.get_tokenizer()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							lowerCamelCase			      =					"""lower newer"""
							lowerCamelCase			      =					self.prepare_image_inputs()

							lowerCamelCase			      =					processor(text=_a		, images=_a )

							self.assertListEqual(list(inputs.keys() )		, ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] )

							# test if it raises when no input is passed
							with self.assertRaises(_a ):
										processor()





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_image_processor()
							lowerCamelCase			      =					self.get_tokenizer()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							lowerCamelCase			      =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							lowerCamelCase			      =					processor.batch_decode(_a )
							lowerCamelCase			      =					tokenizer.batch_decode(_a )

							self.assertListEqual(_a		, _a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.get_image_processor()
							lowerCamelCase			      =					self.get_tokenizer()

							lowerCamelCase			      =					VisionTextDualEncoderProcessor(tokenizer=_a		, image_processor=_a )

							lowerCamelCase			      =					"""lower newer"""
							lowerCamelCase			      =					self.prepare_image_inputs()

							lowerCamelCase			      =					processor(text=_a		, images=_a )

							self.assertListEqual(list(inputs.keys() )		, processor.model_input_names )



