

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



    def    snake_case_     (   self    )  -> Tuple:





           '''simple docstring'''
           A_		   = tempfile.mkdtemp()

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

           A_		   = BlipProcessor(UpperCamelCase__						,			UpperCamelCase__    )

           processor.save_pretrained(self.tmpdirname    )



    def    snake_case_     (   self						,			**UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           return AutoProcessor.from_pretrained(self.tmpdirname						,			**UpperCamelCase__    ).tokenizer



    def    snake_case_     (   self						,			**UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           return AutoProcessor.from_pretrained(self.tmpdirname						,			**UpperCamelCase__    ).image_processor



    def    snake_case_     (   self    )  -> Tuple:





           '''simple docstring'''
           shutil.rmtree(self.tmpdirname    )



    def    snake_case_     (   self    )  -> Optional[Any]:





           '''simple docstring'''
           A_		   = [np.random.randint(255						,			size=(3, 30, 400)						,			dtype=np.uinta    )]

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

           return image_inputs



    def    snake_case_     (   self    )  -> Any:





           '''simple docstring'''
           A_		   = BlipProcessor(tokenizer=self.get_tokenizer()						,			image_processor=self.get_image_processor()    )
           processor.save_pretrained(self.tmpdirname    )

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

           A_		   = 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    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = self.get_image_processor()
           A_		   = self.get_tokenizer()

           A_		   = BlipProcessor(tokenizer=UpperCamelCase__						,			image_processor=UpperCamelCase__    )

           A_		   = self.prepare_image_inputs()

           A_		   = image_processor(UpperCamelCase__						,			return_tensors="""np"""    )
           A_		   = 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    snake_case_     (   self    )  -> Optional[Any]:





           '''simple docstring'''
           A_		   = self.get_image_processor()
           A_		   = self.get_tokenizer()

           A_		   = BlipProcessor(tokenizer=UpperCamelCase__						,			image_processor=UpperCamelCase__    )

           A_		   = """lower newer"""

           A_		   = processor(text=UpperCamelCase__    )

           A_		   = tokenizer(UpperCamelCase__						,			return_token_type_ids=UpperCamelCase__    )

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



    def    snake_case_     (   self    )  -> List[str]:





           '''simple docstring'''
           A_		   = self.get_image_processor()
           A_		   = self.get_tokenizer()

           A_		   = BlipProcessor(tokenizer=UpperCamelCase__						,			image_processor=UpperCamelCase__    )

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

           A_		   = 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    snake_case_     (   self    )  -> List[str]:





           '''simple docstring'''
           A_		   = self.get_image_processor()
           A_		   = self.get_tokenizer()

           A_		   = BlipProcessor(tokenizer=UpperCamelCase__						,			image_processor=UpperCamelCase__    )

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

           A_		   = processor.batch_decode(UpperCamelCase__    )
           A_		   = tokenizer.batch_decode(UpperCamelCase__    )

           self.assertListEqual(UpperCamelCase__						,			UpperCamelCase__    )







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





           '''simple docstring'''
           A_		   = self.get_image_processor()
           A_		   = self.get_tokenizer()

           A_		   = BlipProcessor(tokenizer=UpperCamelCase__						,			image_processor=UpperCamelCase__    )

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

           A_		   = 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 tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class    A__ ( _snake_case     ):



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = SMALL_MODEL_IDENTIFIER
           A_		   = """pt"""
           A_		   = """tf"""



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           A_		   = AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> List[str]:





           '''simple docstring'''
           A_		   = TFAutoModel.from_pretrained(self.test_model						,			from_pt=UpperCamelCase__    )
           model_tf.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = """mock_framework"""

           # Framework provided - return whatever the user provides
           A_		   = FeaturesManager.determine_framework(self.test_model						,			UpperCamelCase__    )
           self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )



    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )







    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Both in environment -> use PyTorch
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # Both not in environment -> raise error
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(self.test_model    )
