


'''simple docstring'''



import re
from pathlib import Path
from unittest import TestCase

import pytest





@pytest.mark.integration
class    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     str:

         with open(SCREAMING_SNAKE_CASE_			,     encoding='utf-8'	) as input_file:
              __lowerCamelCase   :	Any								=		re.compile(r'(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)'	)
              __lowerCamelCase   :	Optional[int]								=		input_file.read()
              __lowerCamelCase   :	List[str]								=		regexp.search(SCREAMING_SNAKE_CASE_	)

         return match







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     List[Any]:

         with open(SCREAMING_SNAKE_CASE_			,     encoding='utf-8'	) as input_file:
              __lowerCamelCase   :	Optional[int]								=		re.compile(r'#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()'			,     re.DOTALL	)
              __lowerCamelCase   :	Dict								=		input_file.read()
              # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
              __lowerCamelCase   :	Optional[Any]								=		regexp.finditer(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[Any]								=		[match for match in matches if match is not None and match.group(1	) is not None]
         return matches[0] if matches else None







    def       lowercase_				(							self	)				->     Any:
         __lowerCamelCase   :	int								=		Path('./datasets'	)
         __lowerCamelCase   :	Any								=		list(dataset_paths.absolute().glob('**/*.py'	)	)

         for dataset in dataset_files:
              if self._no_encoding_on_file_open(str(SCREAMING_SNAKE_CASE_	)	):
                   raise AssertionError(f'open(...) must use utf-8 encoding in {dataset}'	)







    def       lowercase_				(							self	)				->     Optional[Any]:
         __lowerCamelCase   :	Optional[Any]								=		Path('./datasets'	)
         __lowerCamelCase   :	Union[str, Any]								=		list(dataset_paths.absolute().glob('**/*.py'	)	)

         for dataset in dataset_files:
              if self._no_print_statements(str(SCREAMING_SNAKE_CASE_	)	):
                   raise AssertionError(f'print statement found in {dataset}. Use datasets.logger/logging instead.'	)










'''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  ):





    """simple docstring"""






    @slow
    def       lowercase_				(							self	)				->     List[str]:
         __lowerCamelCase   :	Any								=		AutoImageProcessor.from_pretrained('microsoft/dit-base-finetuned-rvlcdip'	)
         __lowerCamelCase   :	Tuple								=		AutoModelForImageClassification.from_pretrained('microsoft/dit-base-finetuned-rvlcdip'	)
         model.to(SCREAMING_SNAKE_CASE_	)

         from datasets import load_dataset

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

         __lowerCamelCase   :	List[Any]								=		dataset['train'][0]['image'].convert('RGB'	)

         __lowerCamelCase   :	str								=		image_processor(SCREAMING_SNAKE_CASE_			,     return_tensors='pt'	).to(SCREAMING_SNAKE_CASE_	)

         # forward pass
         with torch.no_grad():
              __lowerCamelCase   :	str								=		model(**SCREAMING_SNAKE_CASE_	)
              __lowerCamelCase   :	int								=		outputs.logits

         __lowerCamelCase   :	List[Any]								=		torch.Size((1, 16)	)
         self.assertEqual(logits.shape			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[Any]								=		torch.tensor(
             [-0.4_1_5_8, -0.4_0_9_2, -0.4_3_4_7]			,     device=SCREAMING_SNAKE_CASE_			,     dtype=torch.float			,     )
         self.assertTrue(torch.allclose(logits[0, :3]			,     SCREAMING_SNAKE_CASE_			,     atol=1E-4	)	)






