





'''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, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast


@require_vision
class 			SCREAMING_SNAKE_CASE   (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			str								=	tempfile.mkdtemp()

          __A						:			Optional[int]								=	BlipImageProcessor()
          __A						:			Tuple								=	BertTokenizer.from_pretrained('hf-internal-testing/tiny-random-BertModel')

          __A						:			Dict								=	BlipProcessor(_UpperCAmelCase						,     _UpperCAmelCase)

          processor.save_pretrained(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self						,     **_UpperCAmelCase):

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						self						,     **_UpperCAmelCase):

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          shutil.rmtree(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          __A						:			Union[str, Any]								=	[Image.fromarray(np.moveaxis(_UpperCAmelCase						,     0						,     -1)) for x in image_inputs]

          return image_inputs


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[Any]								=	BlipProcessor(tokenizer=self.get_tokenizer()						,     image_processor=self.get_image_processor())
          processor.save_pretrained(self.tmpdirname)

          __A						:			Tuple								=	self.get_tokenizer(bos_token='(BOS)'						,     eos_token='(EOS)')
          __A						:			List[str]								=	self.get_image_processor(do_normalize=_UpperCAmelCase						,     padding_value=1.0)

          __A						:			List[str]								=	BlipProcessor.from_pretrained(
              self.tmpdirname						,     bos_token='(BOS)'						,     eos_token='(EOS)'						,     do_normalize=_UpperCAmelCase						,     padding_value=1.0)

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

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			List[str]								=	self.get_image_processor()
          __A						:			Tuple								=	self.get_tokenizer()

          __A						:			Any								=	BlipProcessor(tokenizer=_UpperCAmelCase						,     image_processor=_UpperCAmelCase)

          __A						:			str								=	self.prepare_image_inputs()

          __A						:			Optional[Any]								=	image_processor(_UpperCAmelCase						,     return_tensors='np')
          __A						:			Dict								=	processor(images=_UpperCAmelCase						,     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 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[int]								=	self.get_image_processor()
          __A						:			Dict								=	self.get_tokenizer()

          __A						:			List[Any]								=	BlipProcessor(tokenizer=_UpperCAmelCase						,     image_processor=_UpperCAmelCase)

          __A						:			int								=	'lower newer'

          __A						:			List[str]								=	processor(text=_UpperCAmelCase)

          __A						:			List[str]								=	tokenizer(_UpperCAmelCase						,     return_token_type_ids=_UpperCAmelCase)

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[int]								=	self.get_image_processor()
          __A						:			Any								=	self.get_tokenizer()

          __A						:			Union[str, Any]								=	BlipProcessor(tokenizer=_UpperCAmelCase						,     image_processor=_UpperCAmelCase)

          __A						:			Optional[int]								=	'lower newer'
          __A						:			Optional[int]								=	self.prepare_image_inputs()

          __A						:			Dict								=	processor(text=_UpperCAmelCase						,     images=_UpperCAmelCase)

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

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			str								=	self.get_image_processor()
          __A						:			List[Any]								=	self.get_tokenizer()

          __A						:			Tuple								=	BlipProcessor(tokenizer=_UpperCAmelCase						,     image_processor=_UpperCAmelCase)

          __A						:			Tuple								=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          __A						:			Dict								=	processor.batch_decode(_UpperCAmelCase)
          __A						:			int								=	tokenizer.batch_decode(_UpperCAmelCase)

          self.assertListEqual(_UpperCAmelCase						,     _UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			int								=	self.get_image_processor()
          __A						:			Tuple								=	self.get_tokenizer()

          __A						:			Tuple								=	BlipProcessor(tokenizer=_UpperCAmelCase						,     image_processor=_UpperCAmelCase)

          __A						:			int								=	'lower newer'
          __A						:			Union[str, Any]								=	self.prepare_image_inputs()

          __A						:			Optional[Any]								=	processor(text=_UpperCAmelCase						,     images=_UpperCAmelCase)

          # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
          self.assertListEqual(list(inputs.keys())						,     ['pixel_values', 'input_ids', 'attention_mask'])






'''simple docstring'''




from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 			SCREAMING_SNAKE_CASE   (a__							):
     lowerCAmelCase		      =	['''image_processor''', '''tokenizer''']
     lowerCAmelCase		      =	'''AutoImageProcessor'''
     lowerCAmelCase		      =	'''AutoTokenizer'''


     def __init__(						self						,     _UpperCAmelCase						,     _UpperCAmelCase):

          '''simple docstring'''






          super().__init__(_UpperCAmelCase						,     _UpperCAmelCase)
          __A						:			Tuple								=	self.image_processor


     def __call__(						self						,     _UpperCAmelCase=None						,     _UpperCAmelCase=None						,     _UpperCAmelCase=None						,     **_UpperCAmelCase):

          '''simple docstring'''






          if text is None and images is None:
               raise ValueError('You have to specify either text or images. Both cannot be none.')

          if text is not None:
               __A						:			Any								=	self.tokenizer(_UpperCAmelCase						,     return_tensors=_UpperCAmelCase						,     **_UpperCAmelCase)

          if images is not None:
               __A						:			Tuple								=	self.image_processor(_UpperCAmelCase						,     return_tensors=_UpperCAmelCase						,     **_UpperCAmelCase)

          if text is not None and images is not None:
               __A						:			Optional[int]								=	image_features.pixel_values
               return encoding
          elif text is not None:
               return encoding
          else:
               return BatchEncoding(data=dict(**_UpperCAmelCase)						,     tensor_type=_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self						,     *_UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          return self.tokenizer.batch_decode(*_UpperCAmelCase						,     **_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self						,     *_UpperCAmelCase						,     **_UpperCAmelCase):

          '''simple docstring'''






          return self.tokenizer.decode(*_UpperCAmelCase						,     **_UpperCAmelCase)


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return ["input_ids", "attention_mask", "pixel_values"]