


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




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Any       =			tempfile.mkdtemp()

  __A  :		int       =			BlipImageProcessor()
  __A  :		Dict       =			GPTaTokenizer.from_pretrained('hf-internal-testing/tiny-random-GPT2Model'						)
  __A  :		Optional[Any]       =			BertTokenizerFast.from_pretrained('hf-internal-testing/tiny-random-bert'						)

  __A  :		Any       =			InstructBlipProcessor(__lowercase      ,      __lowercase      ,      __lowercase						)

  processor.save_pretrained(self.tmpdirname						)


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







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


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







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


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







  shutil.rmtree(self.tmpdirname						)


 def  snake_case__   (      self						):

  """simple docstring"""







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

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

  return image_inputs


 def  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		Optional[int]       =			self.get_tokenizer(bos_token='(BOS)'      ,      eos_token='(EOS)'						)
  __A  :		int       =			self.get_image_processor(do_normalize=__lowercase      ,      padding_value=1.0						)

  __A  :		Optional[Any]       =			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  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		List[str]       =			InstructBlipProcessor(
      tokenizer=__lowercase      ,      image_processor=__lowercase      ,      qformer_tokenizer=__lowercase						)

  __A  :		Any       =			self.prepare_image_inputs()

  __A  :		Optional[int]       =			image_processor(__lowercase      ,      return_tensors='np'						)
  __A  :		Optional[Any]       =			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  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		Optional[int]       =			InstructBlipProcessor(
      tokenizer=__lowercase      ,      image_processor=__lowercase      ,      qformer_tokenizer=__lowercase						)

  __A  :		str       =			'lower newer'

  __A  :		List[Any]       =			processor(text=__lowercase						)

  __A  :		Optional[int]       =			tokenizer(__lowercase      ,      return_token_type_ids=__lowercase						)
  __A  :		List[Any]       =			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  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		Tuple       =			InstructBlipProcessor(
      tokenizer=__lowercase      ,      image_processor=__lowercase      ,      qformer_tokenizer=__lowercase						)

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

  __A  :		Union[str, Any]       =			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  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		int       =			InstructBlipProcessor(
      tokenizer=__lowercase      ,      image_processor=__lowercase      ,      qformer_tokenizer=__lowercase						)

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

  __A  :		Optional[int]       =			processor.batch_decode(__lowercase						)
  __A  :		List[str]       =			tokenizer.batch_decode(__lowercase						)

  self.assertListEqual(__lowercase      ,      __lowercase						)







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			self.get_image_processor()
  __A  :		Any       =			self.get_tokenizer()
  __A  :		Dict       =			self.get_qformer_tokenizer()

  __A  :		Dict       =			InstructBlipProcessor(
      tokenizer=__lowercase      ,      image_processor=__lowercase      ,      qformer_tokenizer=__lowercase						)

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

  __A  :		Optional[int]       =			processor(text=__lowercase      ,      images=__lowercase						)

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










'''simple docstring'''
import unittest

from transformers import DonutProcessor


UpperCAmelCase_ :     str								=      'naver-clova-ix/donut-base'

class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			DonutProcessor.from_pretrained(__lowercase						)







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			{
      'name': 'John Doe',
      'age': '99',
      'city': 'Atlanta',
      'state': 'GA',
      'zip': '30301',
      'phone': '123-4567',
      'nicknames': [{'nickname': 'Johnny'}, {'nickname': 'JD'}],
  }

  __A  :		Optional[Any]       =			(
      '<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>'
      '<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>'
      '<s_nicknames><s_nickname>Johnny</s_nickname>'
      '<sep/><s_nickname>JD</s_nickname></s_nicknames>'
  )
  __A  :		List[Any]       =			self.processor.tokenajson(__lowercase						)

  self.assertDictEqual(__lowercase      ,      __lowercase						)






