



"""simple docstring"""





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


class   UpperCAmelCase_			(					_a):
      lowerCamelCase__	:					Tuple										=						["image_processor", "tokenizer"]
      lowerCamelCase__	:					Optional[int]										=						"AutoImageProcessor"
      lowerCamelCase__	:					Dict										=						"AutoTokenizer"


      def __init__(     self	,    a	,    a							) ->      Any:
             super().__init__(a	,    a							)
             lowercase__    :	Optional[Any]     					=			self.image_processor


      def __call__(     self	,    a=None	,    a=None	,    a=None	,    **a							) ->      Tuple:

             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:
                    lowercase__    :	List[str]     					=			self.tokenizer(a	,    return_tensors=a	,    **a							)

             if images is not None:
                    lowercase__    :	Optional[int]     					=			self.image_processor(a	,    return_tensors=a	,    **a							)

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


      def 	_UpperCAmelCase (     self	,    *a	,    **a							) ->      Optional[Any]:
             return self.tokenizer.batch_decode(*a	,    **a							)


      def 	_UpperCAmelCase (     self	,    *a	,    **a							) ->      Optional[int]:
             return self.tokenizer.decode(*a	,    **a							)


      @property
      def 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             return ["input_ids", "attention_mask", "pixel_values"]


"""simple docstring"""





import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
       import torch

       from transformers import AutoModelForImageClassification

if is_vision_available():
       from transformers import AutoImageProcessor


@require_torch
@require_vision
class   UpperCAmelCase_			(					unittest.TestCase):


      @slow
      def 	_UpperCAmelCase (     self							) ->      str:
             lowercase__    :	Optional[Any]     					=			AutoImageProcessor.from_pretrained('microsoft/dit-base-finetuned-rvlcdip'							)
             lowercase__    :	Union[str, Any]     					=			AutoModelForImageClassification.from_pretrained('microsoft/dit-base-finetuned-rvlcdip'							)
             model.to(a							)

             from datasets import load_dataset

             lowercase__    :	str     					=			load_dataset('nielsr/rvlcdip-demo'							)

             lowercase__    :	Tuple     					=			dataset['train'][0]['image'].convert('RGB'							)

             lowercase__    :	int     					=			image_processor(a	,    return_tensors='pt'							).to(a							)

             # forward pass
             with torch.no_grad():
                    lowercase__    :	List[str]     					=			model(**a							)
                    lowercase__    :	List[Any]     					=			outputs.logits

             lowercase__    :	Union[str, Any]     					=			torch.Size((1, 1_6)							)
             self.assertEqual(logits.shape	,    a							)

             lowercase__    :	Tuple     					=			torch.tensor(
                 [-0.4_158, -0.4_092, -0.4_347]	,    device=a	,    dtype=torch.float	,    )
             self.assertTrue(torch.allclose(logits[0, :3]	,    a	,    atol=1e-4							)							)

