



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







       '''simple docstring'''


       def 					lowercase__    (							self      :  Any   )			->						List[str]:


             '''simple docstring'''




             UpperCAmelCase_      			=							tempfile.mkdtemp()

             UpperCAmelCase_      			=							BlipImageProcessor()
             UpperCAmelCase_      			=							BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel"   )

             UpperCAmelCase_      			=							BlipProcessor(_UpperCAmelCase		,    _UpperCAmelCase   )

             processor.save_pretrained(self.tmpdirname   )



       def 					lowercase__    (							self      :  Optional[int]		,    **_UpperCAmelCase      :  Any   )			->						Dict:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Dict		,    **_UpperCAmelCase      :  Union[str, Any]   )			->						Tuple:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Union[str, Any]   )			->						Optional[Any]:


             '''simple docstring'''




             shutil.rmtree(self.tmpdirname   )



       def 					lowercase__    (							self      :  Union[str, Any]   )			->						List[Any]:


             '''simple docstring'''




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

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

             return image_inputs



       def 					lowercase__    (							self      :  Optional[Any]   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							BlipProcessor(tokenizer=self.get_tokenizer()		,    image_processor=self.get_image_processor()   )
             processor.save_pretrained(self.tmpdirname   )

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

             UpperCAmelCase_      			=							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 					lowercase__    (							self      :  Optional[int]   )			->						List[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.get_image_processor()
             UpperCAmelCase_      			=							self.get_tokenizer()

             UpperCAmelCase_      			=							BlipProcessor(tokenizer=_UpperCAmelCase		,    image_processor=_UpperCAmelCase   )

             UpperCAmelCase_      			=							self.prepare_image_inputs()

             UpperCAmelCase_      			=							image_processor(_UpperCAmelCase		,    return_tensors="np"   )
             UpperCAmelCase_      			=							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 					lowercase__    (							self      :  List[str]   )			->						Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.get_image_processor()
             UpperCAmelCase_      			=							self.get_tokenizer()

             UpperCAmelCase_      			=							BlipProcessor(tokenizer=_UpperCAmelCase		,    image_processor=_UpperCAmelCase   )

             UpperCAmelCase_      			=							"lower newer"

             UpperCAmelCase_      			=							processor(text=_UpperCAmelCase   )

             UpperCAmelCase_      			=							tokenizer(_UpperCAmelCase		,    return_token_type_ids=_UpperCAmelCase   )

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



       def 					lowercase__    (							self      :  Union[str, Any]   )			->						Any:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.get_image_processor()
             UpperCAmelCase_      			=							self.get_tokenizer()

             UpperCAmelCase_      			=							BlipProcessor(tokenizer=_UpperCAmelCase		,    image_processor=_UpperCAmelCase   )

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

             UpperCAmelCase_      			=							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 					lowercase__    (							self      :  str   )			->						Tuple:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.get_image_processor()
             UpperCAmelCase_      			=							self.get_tokenizer()

             UpperCAmelCase_      			=							BlipProcessor(tokenizer=_UpperCAmelCase		,    image_processor=_UpperCAmelCase   )

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

             UpperCAmelCase_      			=							processor.batch_decode(_UpperCAmelCase   )
             UpperCAmelCase_      			=							tokenizer.batch_decode(_UpperCAmelCase   )

             self.assertListEqual(_UpperCAmelCase		,    _UpperCAmelCase   )







       def 					lowercase__    (							self      :  Union[str, Any]   )			->						Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.get_image_processor()
             UpperCAmelCase_      			=							self.get_tokenizer()

             UpperCAmelCase_      			=							BlipProcessor(tokenizer=_UpperCAmelCase		,    image_processor=_UpperCAmelCase   )

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

             UpperCAmelCase_      			=							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 ..utils import DummyObject, requires_backends
class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Any   )			->						List[str]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Tuple   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  List[Any]		,    **_UpperCAmelCase      :  str   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  Tuple   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  int   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Dict   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  List[Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  Dict		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Union[str, Any]   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Any		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Tuple   )			->						Optional[int]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  List[str]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  int		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  int   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  str		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  Dict   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[Any]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  List[Any]   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  int   )			->						Optional[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Dict		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  Optional[Any]   )			->						int:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  List[str]   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Optional[Any]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Tuple   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Union[str, Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Optional[Any]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  int   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  List[Any]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  str   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  List[str]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  str   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  List[str]   )			->						Optional[int]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Optional[Any]		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Dict   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Any		,    *_UpperCAmelCase      :  Tuple		,    **_UpperCAmelCase      :  Optional[int]   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  int   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  Any   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  str   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  int		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  List[Any]   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  int		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  int   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Dict   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Any   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )




