





"""simple docstring"""






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import (
		    AutoProcessor,
		    BertTokenizerFast,
		    BlipImageProcessor,
		    GPTaTokenizer,
		    InstructBlipProcessor,
		    PreTrainedTokenizerFast,
		)




@require_vision
class  __lowercase	(	unittest.TestCase							):







					'''simple docstring'''




					def        _lowerCamelCase						(  self   ):
									__a       :  Optional[Any]					=     tempfile.mkdtemp()

									__a       :  List[Any]					=     BlipImageProcessor()
									__a       :  Optional[Any]					=     GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model'''   )
									__a       :  Union[str, Any]					=     BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert'''   )

									__a       :  str					=     InstructBlipProcessor(SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__   )

									processor.save_pretrained(self.tmpdirname   )







					def        _lowerCamelCase						(  self		,	**_UpperCAmelCase   ):
									return AutoProcessor.from_pretrained(self.tmpdirname		,	**SCREAMING_SNAKE_CASE__   ).tokenizer







					def        _lowerCamelCase						(  self		,	**_UpperCAmelCase   ):
									return AutoProcessor.from_pretrained(self.tmpdirname		,	**SCREAMING_SNAKE_CASE__   ).image_processor







					def        _lowerCamelCase						(  self		,	**_UpperCAmelCase   ):
									return AutoProcessor.from_pretrained(self.tmpdirname		,	**SCREAMING_SNAKE_CASE__   ).qformer_tokenizer







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







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

									__a       :  int					=     [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__		,	0		,	-1   )   ) for x in image_inputs]

									return image_inputs







					def        _lowerCamelCase						(  self   ):
									__a       :  Union[str, Any]					=     InstructBlipProcessor(
									    tokenizer=self.get_tokenizer()		,	image_processor=self.get_image_processor()		,	qformer_tokenizer=self.get_qformer_tokenizer()		,	)
									processor.save_pretrained(self.tmpdirname   )

									__a       :  str					=     self.get_tokenizer(bos_token='''(BOS)'''		,	eos_token='''(EOS)'''   )
									__a       :  Any					=     self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE__		,	padding_value=1.0   )

									__a       :  List[Any]					=     InstructBlipProcessor.from_pretrained(
									    self.tmpdirname		,	bos_token='''(BOS)'''		,	eos_token='''(EOS)'''		,	do_normalize=SCREAMING_SNAKE_CASE__		,	padding_value=1.0   )

									self.assertEqual(processor.tokenizer.get_vocab()		,	tokenizer_add_kwargs.get_vocab()   )
									self.assertIsInstance(processor.tokenizer		,	SCREAMING_SNAKE_CASE__   )

									self.assertEqual(processor.image_processor.to_json_string()		,	image_processor_add_kwargs.to_json_string()   )
									self.assertIsInstance(processor.image_processor		,	SCREAMING_SNAKE_CASE__   )
									self.assertIsInstance(processor.qformer_tokenizer		,	SCREAMING_SNAKE_CASE__   )







					def        _lowerCamelCase						(  self   ):
									__a       :  Optional[Any]					=     self.get_image_processor()
									__a       :  Any					=     self.get_tokenizer()
									__a       :  Tuple					=     self.get_qformer_tokenizer()

									__a       :  str					=     InstructBlipProcessor(
									    tokenizer=SCREAMING_SNAKE_CASE__		,	image_processor=SCREAMING_SNAKE_CASE__		,	qformer_tokenizer=SCREAMING_SNAKE_CASE__   )

									__a       :  List[Any]					=     self.prepare_image_inputs()

									__a       :  Union[str, Any]					=     image_processor(SCREAMING_SNAKE_CASE__		,	return_tensors='''np'''   )
									__a       :  Union[str, Any]					=     processor(images=SCREAMING_SNAKE_CASE__		,	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   ):
									__a       :  Union[str, Any]					=     self.get_image_processor()
									__a       :  str					=     self.get_tokenizer()
									__a       :  str					=     self.get_qformer_tokenizer()

									__a       :  str					=     InstructBlipProcessor(
									    tokenizer=SCREAMING_SNAKE_CASE__		,	image_processor=SCREAMING_SNAKE_CASE__		,	qformer_tokenizer=SCREAMING_SNAKE_CASE__   )

									__a       :  Union[str, Any]					=     """lower newer"""

									__a       :  int					=     processor(text=SCREAMING_SNAKE_CASE__   )

									__a       :  Any					=     tokenizer(SCREAMING_SNAKE_CASE__		,	return_token_type_ids=SCREAMING_SNAKE_CASE__   )
									__a       :  int					=     qformer_tokenizer(SCREAMING_SNAKE_CASE__		,	return_token_type_ids=SCREAMING_SNAKE_CASE__   )

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

									for key in encoded_tokens_qformer.keys():
													self.assertListEqual(encoded_tokens_qformer[key]		,	encoded_processor['''qformer_''' + key]   )







					def        _lowerCamelCase						(  self   ):
									__a       :  Union[str, Any]					=     self.get_image_processor()
									__a       :  Dict					=     self.get_tokenizer()
									__a       :  int					=     self.get_qformer_tokenizer()

									__a       :  Optional[Any]					=     InstructBlipProcessor(
									    tokenizer=SCREAMING_SNAKE_CASE__		,	image_processor=SCREAMING_SNAKE_CASE__		,	qformer_tokenizer=SCREAMING_SNAKE_CASE__   )

									__a       :  Optional[int]					=     """lower newer"""
									__a       :  Optional[int]					=     self.prepare_image_inputs()

									__a       :  Any					=     processor(text=SCREAMING_SNAKE_CASE__		,	images=SCREAMING_SNAKE_CASE__   )

									self.assertListEqual(
									    list(inputs.keys()   )		,	['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values''']		,	)

									# test if it raises when no input is passed
									with pytest.raises(SCREAMING_SNAKE_CASE__   ):
													processor()







					def        _lowerCamelCase						(  self   ):
									__a       :  Dict					=     self.get_image_processor()
									__a       :  int					=     self.get_tokenizer()
									__a       :  Any					=     self.get_qformer_tokenizer()

									__a       :  Union[str, Any]					=     InstructBlipProcessor(
									    tokenizer=SCREAMING_SNAKE_CASE__		,	image_processor=SCREAMING_SNAKE_CASE__		,	qformer_tokenizer=SCREAMING_SNAKE_CASE__   )

									__a       :  Union[str, Any]					=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									__a       :  Optional[int]					=     processor.batch_decode(SCREAMING_SNAKE_CASE__   )
									__a       :  Optional[int]					=     tokenizer.batch_decode(SCREAMING_SNAKE_CASE__   )

									self.assertListEqual(SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__   )







					def        _lowerCamelCase						(  self   ):
									__a       :  Optional[int]					=     self.get_image_processor()
									__a       :  Dict					=     self.get_tokenizer()
									__a       :  Tuple					=     self.get_qformer_tokenizer()

									__a       :  List[str]					=     InstructBlipProcessor(
									    tokenizer=SCREAMING_SNAKE_CASE__		,	image_processor=SCREAMING_SNAKE_CASE__		,	qformer_tokenizer=SCREAMING_SNAKE_CASE__   )

									__a       :  Optional[int]					=     """lower newer"""
									__a       :  Tuple					=     self.prepare_image_inputs()

									__a       :  Any					=     processor(text=SCREAMING_SNAKE_CASE__		,	images=SCREAMING_SNAKE_CASE__   )

									self.assertListEqual(
									    list(inputs.keys()   )		,	['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values''']		,	)

"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__      :    Optional[Any]    							= logging.get_logger(__name__)

UpperCAmelCase__      :    Dict    							= {
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[int]					       =   '''audio-spectrogram-transformer'''



				def __init__(self   ,       SCREAMING_SNAKE_CASE__=7_68   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=30_72   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=1E-12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10_24   ,       SCREAMING_SNAKE_CASE__=1_28   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Tuple:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       str           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       int           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       Tuple           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       Any           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       int           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       int           =   layer_norm_eps
											SCREAMING_SNAKE_CASE__						:       Dict           =   patch_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   qkv_bias
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   frequency_stride
											SCREAMING_SNAKE_CASE__						:       Any           =   time_stride
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   max_length
											SCREAMING_SNAKE_CASE__						:       Any           =   num_mel_bins




