





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      (    a,     a=1.0,     a=None,     a=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 SCREAMING_SNAKE_CASE_ (					unittest.TestCase   ):





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase=7	,  _lowerCAmelCase=400	,  _lowerCAmelCase=2000	,  _lowerCAmelCase=10	,  _lowerCAmelCase=160	,  _lowerCAmelCase=8	,  _lowerCAmelCase=0.0	,  _lowerCAmelCase=4000	,  _lowerCAmelCase=False	,  _lowerCAmelCase=True	,  ):
										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 					__magic_name__ ( self	):
										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 					__magic_name__ ( self	,  _lowerCAmelCase=False	,  _lowerCAmelCase=False	):
										def _flatten(_lowerCAmelCase	):
															return list(itertools.chain(*_lowerCAmelCase	)	)

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

@require_torch
@require_torchaudio
class SCREAMING_SNAKE_CASE_ (					lowercase_							,       unittest.TestCase   ):





					"""simple docstring"""


					A__				 =    WhisperFeatureExtractor if is_speech_available() else None





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       WhisperFeatureExtractionTester(self	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.feature_extraction_class(**self.feat_extract_dict	)

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

										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(_lowerCAmelCase	,  _lowerCAmelCase	)	)
										self.assertEqual(_lowerCAmelCase	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.feature_extraction_class(**self.feat_extract_dict	)

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

										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(_lowerCAmelCase	,  _lowerCAmelCase	)	)
										self.assertEqual(_lowerCAmelCase	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										# Tests that all call wrap to encode_plus and batch_encode_plus
										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(_lowerCAmelCase	) for speech_input in speech_inputs]

										# Test feature size
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  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(_lowerCAmelCase	,  _lowerCAmelCase	,  atol=1E-3	)	)

										# Test batched
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										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.
										lowerCamelCase__       =       [floats_list((1, x)	)[0] for x in (800, 800, 800)]
										lowerCamelCase__       =       np.asarray(_lowerCAmelCase	)
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase	,  _lowerCAmelCase	):
															self.assertTrue(np.allclose(_lowerCAmelCase	,  _lowerCAmelCase	,  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(_lowerCAmelCase	) for speech_input in speech_inputs]

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

										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										lowerCamelCase__       =       feature_extractor(_lowerCAmelCase	,  return_tensors="np"	).input_features
										for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase	,  _lowerCAmelCase	):
															self.assertTrue(np.allclose(_lowerCAmelCase	,  _lowerCAmelCase	,  atol=1E-3	)	)





					def 					__magic_name__ ( self	):
										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 					__magic_name__ ( self	,  _lowerCAmelCase	):
										lowerCamelCase__       =       load_dataset("hf-internal-testing/librispeech_asr_dummy"	,  "clean"	,  split="validation"	)
										# automatic decoding with librispeech
										lowerCamelCase__       =       ds.sort("id"	).select(range(_lowerCAmelCase	)	)[:num_samples]["audio"]

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





					def 					__magic_name__ ( self	):
										# fmt: off
										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(_lowerCAmelCase	,  return_tensors="pt"	).input_features
										self.assertEqual(input_features.shape	,  (1, 80, 3000)	)
										self.assertTrue(torch.allclose(input_features[0, 0, :30]	,  _lowerCAmelCase	,  atol=1E-4	)	)






					def 					__magic_name__ ( self	):
										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())) * 6_5535  # Rescale to [0, 65535] to show issue
										lowerCamelCase__       =       feat_extract.zero_mean_unit_var_norm([audio]	,  attention_mask=_lowerCAmelCase	)[0]

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








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 SCREAMING_SNAKE_CASE_ (					unittest.TestCase   ):





					"""simple docstring"""


					def 					__magic_name__ ( self	):
										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	,  _lowerCAmelCase	)
										with open(self.image_processor_file	,  "w"	,  encoding="utf-8"	) as fp:
															json.dump(_lowerCAmelCase	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	,  **_lowerCAmelCase	):
										return BertTokenizer.from_pretrained(self.tmpdirname	,  **_lowerCAmelCase	)





					def 					__magic_name__ ( self	,  **_lowerCAmelCase	):
										return ViTImageProcessor.from_pretrained(self.tmpdirname	,  **_lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										shutil.rmtree(self.tmpdirname	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       [np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta	)]

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

										return image_inputs





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_tokenizer()
										lowerCamelCase__       =       self.get_image_processor()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										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	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										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=_lowerCAmelCase	,  padding_value=1.0	)

										lowerCamelCase__       =       VisionTextDualEncoderProcessor.from_pretrained(
										    self.tmpdirname	,  bos_token="(BOS)"	,  eos_token="(EOS)"	,  do_normalize=_lowerCAmelCase	,  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	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       self.prepare_image_inputs()

										lowerCamelCase__       =       image_processor(_lowerCAmelCase	,  return_tensors="np"	)
										lowerCamelCase__       =       processor(images=_lowerCAmelCase	,  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 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       "lower newer"

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	)

										lowerCamelCase__       =       tokenizer(_lowerCAmelCase	)

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





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

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

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	,  images=_lowerCAmelCase	)

										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(_lowerCAmelCase	):
															processor()





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

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

										lowerCamelCase__       =       processor.batch_decode(_lowerCAmelCase	)
										lowerCamelCase__       =       tokenizer.batch_decode(_lowerCAmelCase	)

										self.assertListEqual(_lowerCAmelCase	,  _lowerCAmelCase	)






					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

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

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	,  images=_lowerCAmelCase	)

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

