
"""simple docstring"""


import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

						from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
						A_    							=					False

if is_vision_available():
						from PIL import Image

						from transformers import PixaStructImageProcessor




class  __lowerCamelCase  ( unittest.TestCase			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase=7							,						UpperCAmelCase=3							,						UpperCAmelCase=18							,						UpperCAmelCase=30							,						UpperCAmelCase=400							,						UpperCAmelCase=None							,						UpperCAmelCase=True							,						UpperCAmelCase=True							,						UpperCAmelCase=None							,						):
							lowerCamelCase_					=  size if size is not None else {'''height''': 20, '''width''': 20}
							lowerCamelCase_					=  parent
							lowerCamelCase_					=  batch_size
							lowerCamelCase_					=  num_channels
							lowerCamelCase_					=  image_size
							lowerCamelCase_					=  min_resolution
							lowerCamelCase_					=  max_resolution
							lowerCamelCase_					=  size
							lowerCamelCase_					=  do_normalize
							lowerCamelCase_					=  do_convert_rgb
							lowerCamelCase_					=  [512, 1024, 2048, 4096]
							lowerCamelCase_					=  patch_size if patch_size is not None else {'''height''': 16, '''width''': 16}




				def        UpperCAmelCase__						(				self   ):
							return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'''
							lowerCamelCase_					=  Image.open(requests.get(UpperCAmelCase							,						stream=UpperCAmelCase   ).raw   ).convert('''RGB'''   )
							return raw_image




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11						,	reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'						,	)
@require_torch
@require_vision
class  __lowerCamelCase  ( lowerCAmelCase						,	unittest.TestCase			):
				a__:      Union[str, Any]          =    PixaStructImageProcessor if is_vision_available() else None




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  PixaStructImageProcessingTester(self   )




				@property
				def        UpperCAmelCase__						(				self   ):
							return self.image_processor_tester.prepare_image_processor_dict()




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(UpperCAmelCase							,						'''do_normalize'''   )   )
							self.assertTrue(hasattr(UpperCAmelCase							,						'''do_convert_rgb'''   )   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.image_processor_tester.prepare_dummy_image()

							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							lowerCamelCase_					=  2048

							lowerCamelCase_					=  image_processor(UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   )
							self.assertTrue(torch.allclose(inputs.flattened_patches.mean()							,						torch.tensor(0.0_6_0_6   )							,						atol=1e-3							,						rtol=1e-3   )   )




				def        UpperCAmelCase__						(				self   ):
							# Initialize image_processor
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							lowerCamelCase_					=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=UpperCAmelCase   )
							for image in image_inputs:
										self.assertIsInstance(UpperCAmelCase							,						Image.Image   )

							# Test not batched input
							lowerCamelCase_					=  (
							    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowerCamelCase_					=  image_processor(
										    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(1, max_patch, expected_hidden_dim)							,						)

										# Test batched
										lowerCamelCase_					=  image_processor(
										    UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,						)




				def        UpperCAmelCase__						(				self   ):
							# Initialize image_processor
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							lowerCamelCase_					=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=UpperCAmelCase   )
							for image in image_inputs:
										self.assertIsInstance(UpperCAmelCase							,						Image.Image   )

							# Test not batched input
							lowerCamelCase_					=  (
							    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
							    * self.image_processor_tester.num_channels
							) + 2

							lowerCamelCase_					=  True

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										with self.assertRaises(UpperCAmelCase   ):
													lowerCamelCase_					=  image_processor(
													    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches

										lowerCamelCase_					=  '''Hello'''

										lowerCamelCase_					=  image_processor(
										    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase							,						header_text=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(1, max_patch, expected_hidden_dim)							,						)

										# Test batched
										lowerCamelCase_					=  image_processor(
										    UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase							,						header_text=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,						)




				def        UpperCAmelCase__						(				self   ):
							# Initialize image_processor
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							# create random numpy tensors
							lowerCamelCase_					=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=UpperCAmelCase							,						numpify=UpperCAmelCase   )
							for image in image_inputs:
										self.assertIsInstance(UpperCAmelCase							,						np.ndarray   )

							lowerCamelCase_					=  (
							    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowerCamelCase_					=  image_processor(
										    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(1, max_patch, expected_hidden_dim)							,						)

										# Test batched
										lowerCamelCase_					=  image_processor(
										    UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,						)





				def        UpperCAmelCase__						(				self   ):
							# Initialize image_processor
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							# create random PyTorch tensors
							lowerCamelCase_					=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=UpperCAmelCase							,						torchify=UpperCAmelCase   )
							for image in image_inputs:
										self.assertIsInstance(UpperCAmelCase							,						torch.Tensor   )

							# Test not batched input
							lowerCamelCase_					=  (
							    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowerCamelCase_					=  image_processor(
										    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(1, max_patch, expected_hidden_dim)							,						)

										# Test batched
										lowerCamelCase_					=  image_processor(
										    UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,						)




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11						,	reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'						,	)
@require_torch
@require_vision
class  __lowerCamelCase  ( lowerCAmelCase						,	unittest.TestCase			):
				a__:      Any          =    PixaStructImageProcessor if is_vision_available() else None




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  PixaStructImageProcessingTester(self							,						num_channels=4   )
							lowerCamelCase_					=  3




				@property
				def        UpperCAmelCase__						(				self   ):
							return self.image_processor_tester.prepare_image_processor_dict()




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(UpperCAmelCase							,						'''do_normalize'''   )   )
							self.assertTrue(hasattr(UpperCAmelCase							,						'''do_convert_rgb'''   )   )





				def        UpperCAmelCase__						(				self   ):
							# Initialize image_processor
							lowerCamelCase_					=  self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							lowerCamelCase_					=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=UpperCAmelCase   )
							for image in image_inputs:
										self.assertIsInstance(UpperCAmelCase							,						Image.Image   )

							# Test not batched input
							lowerCamelCase_					=  (
							    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
							    * (self.image_processor_tester.num_channels - 1)
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
										# Test not batched input
										lowerCamelCase_					=  image_processor(
										    image_inputs[0]							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(1, max_patch, expected_hidden_dim)							,						)

										# Test batched
										lowerCamelCase_					=  image_processor(
										    UpperCAmelCase							,						return_tensors='''pt'''							,						max_patches=UpperCAmelCase   ).flattened_patches
										self.assertEqual(
										    encoded_images.shape							,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,						)










import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


a__: Union[str, Any]							   =						'hf-internal-testing/tiny-random-bert'
a__: Optional[int]							   =						os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
a__: List[str]							   =						'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'




class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def 							UpperCamelCase     (				self					):
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase					)
        # Should have downloaded the file in here
        self.assertTrue(os.path.isdir(__lowerCamelCase					)					)
        # Cache should contain at least those three subfolders:
        for subfolder in ["blobs", "refs", "snapshots"]:
               self.assertTrue(os.path.isdir(os.path.join(__lowerCamelCase,__lowerCamelCase					)					)					)
        with open(os.path.join(__lowerCamelCase,'''refs''','''main'''					)					) as f:
               A__		   =		f.read()
        self.assertEqual(__lowerCamelCase,os.path.join(__lowerCamelCase,'''snapshots''',__lowerCamelCase,__lowerCamelCase					)					)
        self.assertTrue(os.path.isfile(__lowerCamelCase					)					)

        # File is cached at the same place the second time.
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase					)
        self.assertEqual(__lowerCamelCase,__lowerCamelCase					)

        # Using a specific revision to test the full commit hash.
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase,revision='''9b8c223'''					)
        self.assertEqual(__lowerCamelCase,os.path.join(__lowerCamelCase,'''snapshots''',__lowerCamelCase,__lowerCamelCase					)					)



 def 							UpperCamelCase     (				self					):
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid model identifier'''					):
               A__		   =		cached_file('''tiny-random-bert''',__lowerCamelCase					)

        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid git identifier'''					):
               A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase,revision='''aaaa'''					)

        with self.assertRaisesRegex(__lowerCamelCase,'''does not appear to have a file named'''					):
               A__		   =		cached_file(__lowerCamelCase,'''conf'''					)



 def 							UpperCamelCase     (				self					):
        with self.assertRaisesRegex(__lowerCamelCase,'''does not appear to have a file named'''					):
               A__		   =		cached_file(__lowerCamelCase,'''conf'''					)

        with open(os.path.join(__lowerCamelCase,'''refs''','''main'''					)					) as f:
               A__		   =		f.read()
        self.assertTrue(os.path.isfile(os.path.join(__lowerCamelCase,'''.no_exist''',__lowerCamelCase,'''conf'''					)					)					)

        A__		   =		cached_file(__lowerCamelCase,'''conf''',_raise_exceptions_for_missing_entries=__lowerCamelCase					)
        self.assertIsNone(__lowerCamelCase					)

        A__		   =		cached_file(__lowerCamelCase,'''conf''',local_files_only=__lowerCamelCase,_raise_exceptions_for_missing_entries=__lowerCamelCase					)
        self.assertIsNone(__lowerCamelCase					)

        A__		   =		mock.Mock()
        A__		   =		500
        A__		   =		{}
        A__		   =		HTTPError
        A__		   =		{}

        # Under the mock environment we get a 500 error when trying to reach the tokenizer.
        with mock.patch('''requests.Session.request''',return_value=__lowerCamelCase					) as mock_head:
               A__		   =		cached_file(__lowerCamelCase,'''conf''',_raise_exceptions_for_connection_errors=__lowerCamelCase					)
               self.assertIsNone(__lowerCamelCase					)
               # This check we did call the fake head request
               mock_head.assert_called()



 def 							UpperCamelCase     (				self					):
        self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)
        self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)
        self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)



 def 							UpperCamelCase     (				self					):
        # `get_file_from_repo` returns None if the file does not exist
        self.assertIsNone(get_file_from_repo('''bert-base-cased''','''ahah.txt'''					)					)

        # The function raises if the repository does not exist.
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid model identifier'''					):
               get_file_from_repo('''bert-base-case''',__lowerCamelCase					)

        # The function raises if the revision does not exist.
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid git identifier'''					):
               get_file_from_repo('''bert-base-cased''',__lowerCamelCase,revision='''ahaha'''					)

        A__		   =		get_file_from_repo('''bert-base-cased''',__lowerCamelCase					)
        # The name is the cached name which is not very easy to test, so instead we load the content.
        A__		   =		json.loads(open(__lowerCamelCase,'''r'''					).read()					)
        self.assertEqual(config['''hidden_size'''],768					)




 def 							UpperCamelCase     (				self					):
        with tempfile.TemporaryDirectory() as tmp_dir:
               A__		   =		Path(__lowerCamelCase					) / '''a.txt'''
               filename.touch()
               self.assertEqual(get_file_from_repo(__lowerCamelCase,'''a.txt'''					),str(__lowerCamelCase					)					)

               self.assertIsNone(get_file_from_repo(__lowerCamelCase,'''b.txt'''					)					)





