





"""simple docstring"""





import warnings

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

class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = ['image_processor', 'tokenizer']
      _SCREAMING_SNAKE_CASE							      = 'CLIPImageProcessor'
      _SCREAMING_SNAKE_CASE							      = ('CLIPTokenizer', 'CLIPTokenizerFast')





      def __init__(			self ,		lowercase=None ,		lowercase=None ,		**lowercase					)						->	List[Any]:
            lowerCAmelCase    		=			None
            if "feature_extractor" in kwargs:
                  warnings.warn(
                      """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
                      """ instead.""" ,		lowercase ,		)
                  lowerCAmelCase    		=			kwargs.pop("""feature_extractor"""					)

            lowerCAmelCase    		=			image_processor if image_processor is not None else feature_extractor
            if image_processor is None:
                  raise ValueError("""You need to specify an `image_processor`."""					)
            if tokenizer is None:
                  raise ValueError("""You need to specify a `tokenizer`."""					)

            super().__init__(lowercase ,		lowercase					)





      def __call__(			self ,		lowercase=None ,		lowercase=None ,		lowercase=None ,		**lowercase					)						->	Optional[Any]:

            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:
                  lowerCAmelCase    		=			self.tokenizer(lowercase ,		return_tensors=lowercase ,		**lowercase					)

            if images is not None:
                  lowerCAmelCase    		=			self.image_processor(lowercase ,		return_tensors=lowercase ,		**lowercase					)

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





      def        _snake_case     (			self ,		*lowercase ,		**lowercase					)						->	int:
            return self.tokenizer.batch_decode(*lowercase ,		**lowercase					)





      def        _snake_case     (			self ,		*lowercase ,		**lowercase					)						->	str:
            return self.tokenizer.decode(*lowercase ,		**lowercase					)





      @property
      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.tokenizer.model_input_names
            lowerCAmelCase    		=			self.image_processor.model_input_names
            return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names					)					)





      @property
      def        _snake_case     (			self					)						->	Union[str, Any]:
            warnings.warn(
                """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" ,		lowercase ,		)
            return self.image_processor_class





      @property
      def        _snake_case     (			self					)						->	Optional[int]:
            warnings.warn(
                """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" ,		lowercase ,		)
            return self.image_processor













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





      @slow
      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            lowerCAmelCase    		=			AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            model.to(lowercase					)

            from datasets import load_dataset

            lowerCAmelCase    		=			load_dataset("""nielsr/rvlcdip-demo"""					)

            lowerCAmelCase    		=			dataset["""train"""][0]["""image"""].convert("""RGB"""					)

            lowerCAmelCase    		=			image_processor(lowercase ,		return_tensors="""pt"""					).to(lowercase					)

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase    		=			model(**lowercase					)
                  lowerCAmelCase    		=			outputs.logits

            lowerCAmelCase    		=			torch.Size((1, 16)					)
            self.assertEqual(logits.shape ,		lowercase					)

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






