





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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


UpperCAmelCase_					:   Any 							=						logging.get_logger(__name__)






def 							_lowerCAmelCase (  _a   :					Optional[Any]			)	->			List[List[ImageInput]]:
     if isinstance(_a					,   (list, tuple)			) and isinstance(videos[0]					,   (list, tuple)			) and is_valid_image(videos[0][0]			):
          return videos

     elif isinstance(_a					,   (list, tuple)			) and is_valid_image(videos[0]			):
          return [videos]

     elif is_valid_image(_a			):
          return [[videos]]

     raise ValueError(F'Could not make batched video from {videos}'			)







class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  ["""pixel_values"""]







  def __init__(	self    ,						_lowercase = True    ,						_lowercase = None    ,						_lowercase = PILImageResampling.BILINEAR    ,						_lowercase = True    ,						_lowercase = None    ,						_lowercase = True    ,						_lowercase = 1 / 255    ,						_lowercase = True    ,						_lowercase = None    ,						_lowercase = None    ,						**_lowercase    ,						):
       super().__init__(**_lowercase						)
       lowerCAmelCase_					:       Tuple    =		size if size is not None else {"""shortest_edge""": 224}
       lowerCAmelCase_					:       int    =		get_size_dict(_lowercase    ,						default_to_square=_lowercase						)
       lowerCAmelCase_					:       List[str]    =		crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
       lowerCAmelCase_					:       Union[str, Any]    =		get_size_dict(_lowercase    ,						param_name="""crop_size"""						)

       lowerCAmelCase_					:       List[Any]    =		do_resize
       lowerCAmelCase_					:       int    =		size
       lowerCAmelCase_					:       Union[str, Any]    =		do_center_crop
       lowerCAmelCase_					:       int    =		crop_size
       lowerCAmelCase_					:       int    =		resample
       lowerCAmelCase_					:       Any    =		do_rescale
       lowerCAmelCase_					:       int    =		rescale_factor
       lowerCAmelCase_					:       Union[str, Any]    =		do_normalize
       lowerCAmelCase_					:       str    =		image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       lowerCAmelCase_					:       List[str]    =		image_std if image_std is not None else IMAGENET_STANDARD_STD







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase = PILImageResampling.BILINEAR    ,						_lowercase = None    ,						**_lowercase    ,						):
       lowerCAmelCase_					:       int    =		get_size_dict(_lowercase    ,						default_to_square=_lowercase						)
       if "shortest_edge" in size:
            lowerCAmelCase_					:       int    =		get_resize_output_image_size(_lowercase    ,						size["""shortest_edge"""]    ,						default_to_square=_lowercase						)
       elif "height" in size and "width" in size:
            lowerCAmelCase_					:       str    =		(size["""height"""], size["""width"""])
       else:
            raise ValueError(F'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'						)
       return resize(_lowercase    ,						size=_lowercase    ,						resample=_lowercase    ,						data_format=_lowercase    ,						**_lowercase						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase = None    ,						**_lowercase    ,						):
       lowerCAmelCase_					:       Dict    =		get_size_dict(_lowercase						)
       if "height" not in size or "width" not in size:
            raise ValueError(F'Size must have \'height\' and \'width\' as keys. Got {size.keys()}'						)
       return center_crop(_lowercase    ,						size=(size["""height"""], size["""width"""])    ,						data_format=_lowercase    ,						**_lowercase						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase = None    ,						**_lowercase    ,						):
       return rescale(_lowercase    ,						scale=_lowercase    ,						data_format=_lowercase    ,						**_lowercase						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase = None    ,						**_lowercase    ,						):
       return normalize(_lowercase    ,						mean=_lowercase    ,						std=_lowercase    ,						data_format=_lowercase    ,						**_lowercase						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = ChannelDimension.FIRST    ,						):
       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_size is None:
            raise ValueError("""Crop size 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.
       lowerCAmelCase_					:       List[Any]    =		to_numpy_array(_lowercase						)

       if do_resize:
            lowerCAmelCase_					:       List[Any]    =		self.resize(image=_lowercase    ,						size=_lowercase    ,						resample=_lowercase						)

       if do_center_crop:
            lowerCAmelCase_					:       str    =		self.center_crop(_lowercase    ,						size=_lowercase						)

       if do_rescale:
            lowerCAmelCase_					:       Optional[int]    =		self.rescale(image=_lowercase    ,						scale=_lowercase						)

       if do_normalize:
            lowerCAmelCase_					:       Dict    =		self.normalize(image=_lowercase    ,						mean=_lowercase    ,						std=_lowercase						)

       lowerCAmelCase_					:       str    =		to_channel_dimension_format(_lowercase    ,						_lowercase						)
       return image







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = None    ,						_lowercase = ChannelDimension.FIRST    ,						**_lowercase    ,						):
       lowerCAmelCase_					:       Any    =		do_resize if do_resize is not None else self.do_resize
       lowerCAmelCase_					:       str    =		resample if resample is not None else self.resample
       lowerCAmelCase_					:       int    =		do_center_crop if do_center_crop is not None else self.do_center_crop
       lowerCAmelCase_					:       int    =		do_rescale if do_rescale is not None else self.do_rescale
       lowerCAmelCase_					:       Tuple    =		rescale_factor if rescale_factor is not None else self.rescale_factor
       lowerCAmelCase_					:       Any    =		do_normalize if do_normalize is not None else self.do_normalize
       lowerCAmelCase_					:       List[Any]    =		image_mean if image_mean is not None else self.image_mean
       lowerCAmelCase_					:       Tuple    =		image_std if image_std is not None else self.image_std

       lowerCAmelCase_					:       Tuple    =		size if size is not None else self.size
       lowerCAmelCase_					:       List[Any]    =		get_size_dict(_lowercase    ,						default_to_square=_lowercase						)
       lowerCAmelCase_					:       List[str]    =		crop_size if crop_size is not None else self.crop_size
       lowerCAmelCase_					:       str    =		get_size_dict(_lowercase    ,						param_name="""crop_size"""						)

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

       lowerCAmelCase_					:       List[str]    =		make_batched(_lowercase						)

       lowerCAmelCase_					:       Any    =		[
           [
               self._preprocess_image(
                   image=_lowercase    ,						do_resize=_lowercase    ,						size=_lowercase    ,						resample=_lowercase    ,						do_center_crop=_lowercase    ,						crop_size=_lowercase    ,						do_rescale=_lowercase    ,						rescale_factor=_lowercase    ,						do_normalize=_lowercase    ,						image_mean=_lowercase    ,						image_std=_lowercase    ,						data_format=_lowercase    ,						)
               for img in video
           ]
           for video in videos
       ]

       lowerCAmelCase_					:       Dict    =		{"""pixel_values""": videos}
       return BatchFeature(data=_lowercase    ,						tensor_type=_lowercase						)







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


UpperCAmelCase_					:   List[str] 							=						"""hf-internal-testing/tiny-random-bert"""
UpperCAmelCase_					:   List[Any] 							=						os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
UpperCAmelCase_					:   Tuple 							=						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""





class      lowercase__    (							unittest.TestCase  ):







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

       # File is cached at the same place the second time.
       lowerCAmelCase_					:       Dict    =		cached_file(_lowercase    ,						_lowercase						)
       self.assertEqual(_lowercase    ,						_lowercase						)

       # Using a specific revision to test the full commit hash.
       lowerCAmelCase_					:       Optional[int]    =		cached_file(_lowercase    ,						_lowercase    ,						revision="""9b8c223"""						)
       self.assertEqual(_lowercase    ,						os.path.join(_lowercase    ,						"""snapshots"""    ,						_lowercase    ,						_lowercase						)						)







  def 	UpperCAmelCase__		(	self						):
       with self.assertRaisesRegex(_lowercase    ,						"""is not a valid model identifier"""						):
            lowerCAmelCase_					:       Dict    =		cached_file("""tiny-random-bert"""    ,						_lowercase						)

       with self.assertRaisesRegex(_lowercase    ,						"""is not a valid git identifier"""						):
            lowerCAmelCase_					:       Tuple    =		cached_file(_lowercase    ,						_lowercase    ,						revision="""aaaa"""						)

       with self.assertRaisesRegex(_lowercase    ,						"""does not appear to have a file named"""						):
            lowerCAmelCase_					:       List[Any]    =		cached_file(_lowercase    ,						"""conf"""						)







  def 	UpperCAmelCase__		(	self						):
       with self.assertRaisesRegex(_lowercase    ,						"""does not appear to have a file named"""						):
            lowerCAmelCase_					:       Any    =		cached_file(_lowercase    ,						"""conf"""						)

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

       lowerCAmelCase_					:       Union[str, Any]    =		cached_file(_lowercase    ,						"""conf"""    ,						_raise_exceptions_for_missing_entries=_lowercase						)
       self.assertIsNone(_lowercase						)

       lowerCAmelCase_					:       Any    =		cached_file(_lowercase    ,						"""conf"""    ,						local_files_only=_lowercase    ,						_raise_exceptions_for_missing_entries=_lowercase						)
       self.assertIsNone(_lowercase						)

       lowerCAmelCase_					:       Any    =		mock.Mock()
       lowerCAmelCase_					:       Optional[Any]    =		500
       lowerCAmelCase_					:       Any    =		{}
       lowerCAmelCase_					:       List[str]    =		HTTPError
       lowerCAmelCase_					:       List[str]    =		{}

       # Under the mock environment we get a 500 error when trying to reach the tokenizer.
       with mock.patch("""requests.Session.request"""    ,						return_value=_lowercase						) as mock_head:
            lowerCAmelCase_					:       Union[str, Any]    =		cached_file(_lowercase    ,						"""conf"""    ,						_raise_exceptions_for_connection_errors=_lowercase						)
            self.assertIsNone(_lowercase						)
            # 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"""    ,						_lowercase						)						)
       self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""    ,						_lowercase						)						)
       self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""    ,						_lowercase						)						)







  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(_lowercase    ,						"""is not a valid model identifier"""						):
            get_file_from_repo("""bert-base-case"""    ,						_lowercase						)

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

       lowerCAmelCase_					:       Union[str, Any]    =		get_file_from_repo("""bert-base-cased"""    ,						_lowercase						)
       # The name is the cached name which is not very easy to test, so instead we load the content.
       lowerCAmelCase_					:       Any    =		json.loads(open(_lowercase    ,						"""r"""						).read()						)
       self.assertEqual(config["""hidden_size"""]    ,						768						)







  def 	UpperCAmelCase__		(	self						):
       with tempfile.TemporaryDirectory() as tmp_dir:
            lowerCAmelCase_					:       Optional[Any]    =		Path(_lowercase						) / """a.txt"""
            filename.touch()
            self.assertEqual(get_file_from_repo(_lowercase    ,						"""a.txt"""						)    ,						str(_lowercase						)						)

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