


'''simple docstring'''







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
   from PIL import Image







class A__	(						unittest.TestCase			,					UpperCAmelCase__				):






      def __UpperCAmelCase	(			self      :List[str]     )	-> Optional[int]:




             '''simple docstring'''
             _a   :			Tuple					 =load_tool("""image-question-answering"""     )
             self.tool.setup()
             _a   :			Dict					 =load_tool("""image-question-answering"""					,						remote=SCREAMING_SNAKE_CASE     )






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




             '''simple docstring'''
             _a   :			Union[str, Any]					 =Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""     )     ) / """000000039769.png"""     )
             _a   :			Any					 =self.tool(SCREAMING_SNAKE_CASE					,						"""How many cats are sleeping on the couch?"""     )
             self.assertEqual(SCREAMING_SNAKE_CASE					,						"""2"""     )






      def __UpperCAmelCase	(			self      :List[str]     )	-> int:




             '''simple docstring'''
             _a   :			str					 =Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""     )     ) / """000000039769.png"""     )
             _a   :			Union[str, Any]					 =self.remote_tool(SCREAMING_SNAKE_CASE					,						"""How many cats are sleeping on the couch?"""     )
             self.assertEqual(SCREAMING_SNAKE_CASE					,						"""2"""     )






      def __UpperCAmelCase	(			self      :Dict     )	-> List[str]:




             '''simple docstring'''
             _a   :			Any					 =Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""     )     ) / """000000039769.png"""     )
             _a   :			Any					 =self.tool(image=SCREAMING_SNAKE_CASE					,						question="""How many cats are sleeping on the couch?"""     )
             self.assertEqual(SCREAMING_SNAKE_CASE					,						"""2"""     )






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




             '''simple docstring'''
             _a   :			Any					 =Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""     )     ) / """000000039769.png"""     )
             _a   :			Optional[Any]					 =self.remote_tool(image=SCREAMING_SNAKE_CASE					,						question="""How many cats are sleeping on the couch?"""     )
             self.assertEqual(SCREAMING_SNAKE_CASE					,						"""2"""     )






'''simple docstring'''







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







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Optional[Any]     	=							["image_processor", "tokenizer"]
      __UpperCamelCase	:  Optional[Any]     	=							"EfficientNetImageProcessor"
      __UpperCamelCase	:  Union[str, Any]     	=							("BertTokenizer", "BertTokenizerFast")






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




             '''simple docstring'''
             super().__init__(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )






      def __call__(			self      :str					,						SCREAMING_SNAKE_CASE      :List[Any]=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :Union[str, Any]="max_length"					,						SCREAMING_SNAKE_CASE      :Any=6_4					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Optional[int]:




             '''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   :			Dict					 =self.tokenizer(
                        SCREAMING_SNAKE_CASE					,						padding=SCREAMING_SNAKE_CASE					,						max_length=SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if images is not None:
                    _a   :			Any					 =self.image_processor(SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if text is not None and images is not None:
                    _a   :			Dict					 =image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE     )					,						tensor_type=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Any					,						*SCREAMING_SNAKE_CASE      :Optional[int]					,						**SCREAMING_SNAKE_CASE      :str     )	-> Dict:




             '''simple docstring'''
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :int					,						*SCREAMING_SNAKE_CASE      :List[str]					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Union[str, Any]:




             '''simple docstring'''
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      @property
      def __UpperCAmelCase	(			self      :Tuple     )	-> Tuple:




             '''simple docstring'''
             _a   :			int					 =self.tokenizer.model_input_names
             _a   :			Union[str, Any]					 =self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )


