






import numpy as np

from transformers import Pipeline




def       _SCREAMING_SNAKE_CASE (							__snake_case )				->						Union[str, Any]:
       _UpperCAmelCase							=    np.max(__snake_case   ,      axis=-1   ,      keepdims=__snake_case )
       _UpperCAmelCase							=    np.exp(outputs - maxes )
       return shifted_exp / shifted_exp.sum(axis=-1   ,      keepdims=__snake_case )





class 		SCREAMING_SNAKE_CASE__	(  UpperCAmelCase     ):

  '''simple docstring'''







  def  lowerCamelCase       (      self		:			List[Any]       ,				**lowerCamelCase		:			Any							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    {}
         if "second_text" in kwargs:
                _UpperCAmelCase							=    kwargs["""second_text"""]
         return preprocess_kwargs, {}, {}


  def  lowerCamelCase       (      self		:			Union[str, Any]       ,				lowerCamelCase		:			Tuple       ,				lowerCamelCase		:			Any=None							)						->	Dict:


         """simple docstring"""
         return self.tokenizer(lowerCamelCase       ,				text_pair=lowerCamelCase       ,				return_tensors=self.framework							)


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


         """simple docstring"""
         return self.model(**lowerCamelCase							)





  def  lowerCamelCase       (      self		:			List[str]       ,				lowerCamelCase		:			List[Any]							)						->	int:


         """simple docstring"""
         _UpperCAmelCase							=    model_outputs.logits[0].numpy()
         _UpperCAmelCase							=    softmax(lowerCamelCase							)

         _UpperCAmelCase							=    np.argmax(lowerCamelCase							)
         _UpperCAmelCase							=    self.model.config.idalabel[best_class]
         _UpperCAmelCase							=    probabilities[best_class].item()
         _UpperCAmelCase							=    logits.tolist()
         return {"label": label, "score": score, "logits": logits}






"""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					)					)






