






"""simple docstring"""







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


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





class   UpperCamelCase_ (       unittest.TestCase  ):



  def 		UpperCamelCase_				(      self				)			->			Dict:



         """simple docstring"""





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

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

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



  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





         with self.assertRaisesRegex(snake_case__ ,		"""is not a valid model identifier"""				):
                UpperCAmelCase	    =			cached_file("""tiny-random-bert""" ,		snake_case__				)

         with self.assertRaisesRegex(snake_case__ ,		"""is not a valid git identifier"""				):
                UpperCAmelCase	    =			cached_file(snake_case__ ,		snake_case__ ,		revision="""aaaa"""				)

         with self.assertRaisesRegex(snake_case__ ,		"""does not appear to have a file named"""				):
                UpperCAmelCase	    =			cached_file(snake_case__ ,		"""conf"""				)



  def 		UpperCamelCase_				(      self				)			->			str:



         """simple docstring"""





         with self.assertRaisesRegex(snake_case__ ,		"""does not appear to have a file named"""				):
                UpperCAmelCase	    =			cached_file(snake_case__ ,		"""conf"""				)

         with open(os.path.join(snake_case__ ,		"""refs""" ,		"""main"""				)				) as f:
                UpperCAmelCase	    =			f.read()
         self.assertTrue(os.path.isfile(os.path.join(snake_case__ ,		""".no_exist""" ,		snake_case__ ,		"""conf"""				)				)				)

         UpperCAmelCase	    =			cached_file(snake_case__ ,		"""conf""" ,		_raise_exceptions_for_missing_entries=snake_case__				)
         self.assertIsNone(snake_case__				)

         UpperCAmelCase	    =			cached_file(snake_case__ ,		"""conf""" ,		local_files_only=snake_case__ ,		_raise_exceptions_for_missing_entries=snake_case__				)
         self.assertIsNone(snake_case__				)

         UpperCAmelCase	    =			mock.Mock()
         UpperCAmelCase	    =			5_00
         UpperCAmelCase	    =			{}
         UpperCAmelCase	    =			HTTPError
         UpperCAmelCase	    =			{}

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



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



         """simple docstring"""





         self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,		snake_case__				)				)
         self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,		snake_case__				)				)
         self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,		snake_case__				)				)



  def 		UpperCamelCase_				(      self				)			->			List[str]:



         """simple docstring"""





         self.assertIsNone(get_file_from_repo("""bert-base-cased""" ,		"""ahah.txt"""				)				)

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

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

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



  def 		UpperCamelCase_				(      self				)			->			Optional[Any]:



         """simple docstring"""





         with tempfile.TemporaryDirectory() as tmp_dir:
                UpperCAmelCase	    =			Path(snake_case__				) / """a.txt"""
                filename.touch()
                self.assertEqual(get_file_from_repo(snake_case__ ,		"""a.txt"""				) ,		str(snake_case__				)				)

                self.assertIsNone(get_file_from_repo(snake_case__ ,		"""b.txt"""				)				)








"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


lowerCAmelCase_	:    Union[str, Any]            =      logging.get_logger(__name__)





class   UpperCamelCase_ (       a_  ):
  _A       :					List[str]				=	['pixel_values']



  def __init__(      self ,		snake_case__ = True ,		snake_case__ = None ,		snake_case__ = 0.9 ,		snake_case__ = PILImageResampling.BICUBIC ,		snake_case__ = True ,		snake_case__ = None ,		snake_case__ = 1 / 2_55 ,		snake_case__ = True ,		snake_case__ = True ,		snake_case__ = None ,		snake_case__ = None ,		**snake_case__ ,		)			->			None:



         """simple docstring"""





         super().__init__(**snake_case__				)
         UpperCAmelCase	    =			size if size is not None else {"""shortest_edge""": 2_24}
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         UpperCAmelCase	    =			crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		param_name="""crop_size"""				)

         UpperCAmelCase	    =			do_resize
         UpperCAmelCase	    =			size
         UpperCAmelCase	    =			crop_pct
         UpperCAmelCase	    =			resample
         UpperCAmelCase	    =			do_center_crop
         UpperCAmelCase	    =			crop_size
         UpperCAmelCase	    =			do_rescale
         UpperCAmelCase	    =			rescale_factor
         UpperCAmelCase	    =			do_normalize
         UpperCAmelCase	    =			image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
         UpperCAmelCase	    =			image_std if image_std is not None else IMAGENET_DEFAULT_STD



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		snake_case__ = PILImageResampling.BICUBIC ,		snake_case__ = None ,		**snake_case__ ,		)			->			np.ndarray:



         """simple docstring"""





         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         if "shortest_edge" not in size and ("height" not in size or "width" not in size):
                raise ValueError(f'''size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'''				)
         if crop_pct is not None:
                if "shortest_edge" in size:
                       UpperCAmelCase	    =			int(size["""shortest_edge"""] / crop_pct				)
                elif "height" in size and "width" in size:
                       if size["height"] == size["width"]:
                              UpperCAmelCase	    =			int(size["""height"""] / crop_pct				)
                       else:
                              UpperCAmelCase	    =			(int(size["""height"""] / crop_pct				), int(size["""width"""] / crop_pct				))
                else:
                       raise ValueError("""Invalid size for resize: {}""".format(snake_case__				)				)

                UpperCAmelCase	    =			get_resize_output_image_size(snake_case__ ,		size=snake_case__ ,		default_to_square=snake_case__				)
         else:
                if "shortest_edge" in size:
                       UpperCAmelCase	    =			get_resize_output_image_size(snake_case__ ,		size=size["""shortest_edge"""] ,		default_to_square=snake_case__				)
                elif "height" in size and "width" in size:
                       UpperCAmelCase	    =			(size["""height"""], size["""width"""])
                else:
                       raise ValueError("""Invalid size for resize: {}""".format(snake_case__				)				)

         return resize(snake_case__ ,		size=snake_case__ ,		resample=snake_case__ ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			np.ndarray:



         """simple docstring"""





         UpperCAmelCase	    =			get_size_dict(snake_case__				)
         if "height" not in size or "width" not in size:
                raise ValueError(f'''size must contain \'height\' and \'width\' as keys. Got {size.keys()}'''				)
         return center_crop(snake_case__ ,		size=(size["""height"""], size["""width"""]) ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			Tuple:



         """simple docstring"""





         return rescale(snake_case__ ,		scale=snake_case__ ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			np.ndarray:



         """simple docstring"""





         return normalize(snake_case__ ,		mean=snake_case__ ,		std=snake_case__ ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = ChannelDimension.FIRST ,		**snake_case__ ,		)			->			PIL.Image.Image:



         """simple docstring"""





         UpperCAmelCase	    =			do_resize if do_resize is not None else self.do_resize
         UpperCAmelCase	    =			crop_pct if crop_pct is not None else self.crop_pct
         UpperCAmelCase	    =			resample if resample is not None else self.resample
         UpperCAmelCase	    =			do_center_crop if do_center_crop is not None else self.do_center_crop
         UpperCAmelCase	    =			do_rescale if do_rescale is not None else self.do_rescale
         UpperCAmelCase	    =			rescale_factor if rescale_factor is not None else self.rescale_factor
         UpperCAmelCase	    =			do_normalize if do_normalize is not None else self.do_normalize
         UpperCAmelCase	    =			image_mean if image_mean is not None else self.image_mean
         UpperCAmelCase	    =			image_std if image_std is not None else self.image_std

         UpperCAmelCase	    =			size if size is not None else self.size
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         UpperCAmelCase	    =			crop_size if crop_size is not None else self.crop_size
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		param_name="""crop_size"""				)

         UpperCAmelCase	    =			make_list_of_images(snake_case__				)

         if not valid_images(snake_case__				):
                raise ValueError(
                    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                    """torch.Tensor, tf.Tensor or jax.ndarray."""				)

         if do_resize and size is None or resample is None:
                raise ValueError("""Size and resample must be specified if do_resize is True."""				)

         if do_center_crop and crop_pct is None:
                raise ValueError("""Crop_pct must be specified if do_center_crop is True."""				)

         if do_rescale and rescale_factor is None:
                raise ValueError("""Rescale factor must be specified if do_rescale is True."""				)

         if do_normalize and (image_mean is None or image_std is None):
                raise ValueError("""Image mean and std must be specified if do_normalize is True."""				)

         # All transformations expect numpy arrays.
         UpperCAmelCase	    =			[to_numpy_array(snake_case__				) for image in images]

         if do_resize:
                UpperCAmelCase	    =			[self.resize(image=snake_case__ ,		size=snake_case__ ,		crop_pct=snake_case__ ,		resample=snake_case__				) for image in images]

         if do_center_crop:
                UpperCAmelCase	    =			[self.center_crop(image=snake_case__ ,		size=snake_case__				) for image in images]

         if do_rescale:
                UpperCAmelCase	    =			[self.rescale(image=snake_case__ ,		scale=snake_case__				) for image in images]

         if do_normalize:
                UpperCAmelCase	    =			[self.normalize(image=snake_case__ ,		mean=snake_case__ ,		std=snake_case__				) for image in images]

         UpperCAmelCase	    =			[to_channel_dimension_format(snake_case__ ,		snake_case__				) for image in images]

         UpperCAmelCase	    =			{"""pixel_values""": images}
         return BatchFeature(data=snake_case__ ,		tensor_type=snake_case__				)
