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






						'''simple docstring'''






						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					tempfile.mkdtemp()

												_lowerCAmelCase    		=					BlipImageProcessor()
												_lowerCAmelCase    		=					GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model""" )
												_lowerCAmelCase    		=					BertTokenizerFast.from_pretrained("""hf-internal-testing/tiny-random-bert""" )

												_lowerCAmelCase    		=					InstructBlipProcessor(_lowercase       ,  _lowercase       ,  _lowercase )

												processor.save_pretrained(self.tmpdirname )




						def    _lowercase      (   self       ,  **_lowercase ):

												"""simple docstring"""



												return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowercase ).tokenizer




						def    _lowercase      (   self       ,  **_lowercase ):

												"""simple docstring"""



												return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowercase ).image_processor




						def    _lowercase      (   self       ,  **_lowercase ):

												"""simple docstring"""



												return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowercase ).qformer_tokenizer




						def    _lowercase      (   self ):

												"""simple docstring"""



												shutil.rmtree(self.tmpdirname )




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					[np.random.randint(255       ,  size=(3, 30, 400)       ,  dtype=np.uinta )]

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

												return image_inputs




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=self.get_tokenizer()       ,  image_processor=self.get_image_processor()       ,  qformer_tokenizer=self.get_qformer_tokenizer()       ,  )
												processor.save_pretrained(self.tmpdirname )

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

												_lowerCAmelCase    		=					InstructBlipProcessor.from_pretrained(
												    self.tmpdirname       ,  bos_token="""(BOS)"""       ,  eos_token="""(EOS)"""       ,  do_normalize=_lowercase       ,  padding_value=1.0 )

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

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




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					self.get_image_processor()
												_lowerCAmelCase    		=					self.get_tokenizer()
												_lowerCAmelCase    		=					self.get_qformer_tokenizer()

												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=_lowercase       ,  image_processor=_lowercase       ,  qformer_tokenizer=_lowercase )

												_lowerCAmelCase    		=					self.prepare_image_inputs()

												_lowerCAmelCase    		=					image_processor(_lowercase       ,  return_tensors="""np""" )
												_lowerCAmelCase    		=					processor(images=_lowercase       ,  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    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					self.get_image_processor()
												_lowerCAmelCase    		=					self.get_tokenizer()
												_lowerCAmelCase    		=					self.get_qformer_tokenizer()

												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=_lowercase       ,  image_processor=_lowercase       ,  qformer_tokenizer=_lowercase )

												_lowerCAmelCase    		=					"""lower newer"""

												_lowerCAmelCase    		=					processor(text=_lowercase )

												_lowerCAmelCase    		=					tokenizer(_lowercase       ,  return_token_type_ids=_lowercase )
												_lowerCAmelCase    		=					qformer_tokenizer(_lowercase       ,  return_token_type_ids=_lowercase )

												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    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					self.get_image_processor()
												_lowerCAmelCase    		=					self.get_tokenizer()
												_lowerCAmelCase    		=					self.get_qformer_tokenizer()

												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=_lowercase       ,  image_processor=_lowercase       ,  qformer_tokenizer=_lowercase )

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

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

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




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					self.get_image_processor()
												_lowerCAmelCase    		=					self.get_tokenizer()
												_lowerCAmelCase    		=					self.get_qformer_tokenizer()

												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=_lowercase       ,  image_processor=_lowercase       ,  qformer_tokenizer=_lowercase )

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

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

												self.assertListEqual(_lowercase       ,  _lowercase )




						def    _lowercase      (   self ):

												"""simple docstring"""



												_lowerCAmelCase    		=					self.get_image_processor()
												_lowerCAmelCase    		=					self.get_tokenizer()
												_lowerCAmelCase    		=					self.get_qformer_tokenizer()

												_lowerCAmelCase    		=					InstructBlipProcessor(
												    tokenizer=_lowercase       ,  image_processor=_lowercase       ,  qformer_tokenizer=_lowercase )

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

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

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



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


UpperCAmelCase_							:					Optional[Any]		 =   {
    '''configuration_wav2vec2''': ['''WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Wav2Vec2Config'''],
    '''feature_extraction_wav2vec2''': ['''Wav2Vec2FeatureExtractor'''],
    '''processing_wav2vec2''': ['''Wav2Vec2Processor'''],
    '''tokenization_wav2vec2''': ['''Wav2Vec2CTCTokenizer''', '''Wav2Vec2Tokenizer'''],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCAmelCase_							:					Optional[Any]		 =   [
			    '''WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''Wav2Vec2ForAudioFrameClassification''',
			    '''Wav2Vec2ForCTC''',
			    '''Wav2Vec2ForMaskedLM''',
			    '''Wav2Vec2ForPreTraining''',
			    '''Wav2Vec2ForSequenceClassification''',
			    '''Wav2Vec2ForXVector''',
			    '''Wav2Vec2Model''',
			    '''Wav2Vec2PreTrainedModel''',
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCAmelCase_							:					List[Any]		 =   [
			    '''TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''TFWav2Vec2ForCTC''',
			    '''TFWav2Vec2Model''',
			    '''TFWav2Vec2PreTrainedModel''',
			    '''TFWav2Vec2ForSequenceClassification''',
			]

try:
			if not is_flax_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCAmelCase_							:					Dict		 =   [
			    '''FlaxWav2Vec2ForCTC''',
			    '''FlaxWav2Vec2ForPreTraining''',
			    '''FlaxWav2Vec2Model''',
			    '''FlaxWav2Vec2PreTrainedModel''',
			]


if TYPE_CHECKING:
			from .configuration_wavaveca import WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, WavaVecaConfig
			from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
			from .processing_wavaveca import WavaVecaProcessor
			from .tokenization_wavaveca import WavaVecaCTCTokenizer, WavaVecaTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_wavaveca import (
						    WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    WavaVecaForAudioFrameClassification,
						    WavaVecaForCTC,
						    WavaVecaForMaskedLM,
						    WavaVecaForPreTraining,
						    WavaVecaForSequenceClassification,
						    WavaVecaForXVector,
						    WavaVecaModel,
						    WavaVecaPreTrainedModel,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_wavaveca import (
						    TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFWavaVecaForCTC,
						    TFWavaVecaForSequenceClassification,
						    TFWavaVecaModel,
						    TFWavaVecaPreTrainedModel,
						)

			try:
						if not is_flax_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_wavaveca import (
						    FlaxWavaVecaForCTC,
						    FlaxWavaVecaForPreTraining,
						    FlaxWavaVecaModel,
						    FlaxWavaVecaPreTrainedModel,
						)


else:
			import sys

			UpperCAmelCase_							:					Optional[Any]		 =   _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





