





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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

if is_vision_available():
      from PIL import Image

      from transformers import DonutImageProcessor





class      lowercase__    (							unittest.TestCase  ):







  def __init__(	self    ,						_lowercase    ,						_lowercase=7    ,						_lowercase=3    ,						_lowercase=18    ,						_lowercase=30    ,						_lowercase=400    ,						_lowercase=True    ,						_lowercase=None    ,						_lowercase=True    ,						_lowercase=False    ,						_lowercase=True    ,						_lowercase=True    ,						_lowercase=[0.5, 0.5, 0.5]    ,						_lowercase=[0.5, 0.5, 0.5]    ,						):
       lowerCAmelCase_					:       Optional[Any]    =		parent
       lowerCAmelCase_					:       Any    =		batch_size
       lowerCAmelCase_					:       str    =		num_channels
       lowerCAmelCase_					:       Union[str, Any]    =		image_size
       lowerCAmelCase_					:       Union[str, Any]    =		min_resolution
       lowerCAmelCase_					:       str    =		max_resolution
       lowerCAmelCase_					:       str    =		do_resize
       lowerCAmelCase_					:       List[str]    =		size if size is not None else {"""height""": 18, """width""": 20}
       lowerCAmelCase_					:       Tuple    =		do_thumbnail
       lowerCAmelCase_					:       str    =		do_align_axis
       lowerCAmelCase_					:       Optional[Any]    =		do_pad
       lowerCAmelCase_					:       List[str]    =		do_normalize
       lowerCAmelCase_					:       Tuple    =		image_mean
       lowerCAmelCase_					:       Any    =		image_std







  def 	UpperCAmelCase__		(	self						):
       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_thumbnail": self.do_thumbnail,
           "do_align_long_axis": self.do_align_axis,
           "do_pad": self.do_pad,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
       }





@require_torch
@require_vision
class      lowercase__    (							__A     ,					unittest.TestCase  ):
  __UpperCamelCase	     =  DonutImageProcessor if is_vision_available() else None







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       List[str]    =		DonutImageProcessingTester(self						)







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







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_resize"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""size"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_thumbnail"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_align_long_axis"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_pad"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""do_normalize"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""image_mean"""						)						)
       self.assertTrue(hasattr(_lowercase    ,						"""image_std"""						)						)







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       str    =		self.image_processing_class.from_dict(self.image_processor_dict						)
       self.assertEqual(image_processor.size    ,						{"""height""": 18, """width""": 20}						)

       lowerCAmelCase_					:       str    =		self.image_processing_class.from_dict(self.image_processor_dict    ,						size=42						)
       self.assertEqual(image_processor.size    ,						{"""height""": 42, """width""": 42}						)

       # Previous config had dimensions in (width, height) order
       lowerCAmelCase_					:       Dict    =		self.image_processing_class.from_dict(self.image_processor_dict    ,						size=(42, 84)						)
       self.assertEqual(image_processor.size    ,						{"""height""": 84, """width""": 42}						)







  def 	UpperCAmelCase__		(	self						):
       pass







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       # create random PIL images
       lowerCAmelCase_					:       Optional[Any]    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						Image.Image						)

       # Test not batched input
       lowerCAmelCase_					:       int    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       Union[str, Any]    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       str    =		self.image_processing_class(**self.image_processor_dict						)
       # create random numpy tensors
       lowerCAmelCase_					:       int    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase    ,						numpify=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						np.ndarray						)

       # Test not batched input
       lowerCAmelCase_					:       Optional[Any]    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       Any    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







  @is_flaky()
  def 	UpperCAmelCase__		(	self						):
       # Initialize image_processing
       lowerCAmelCase_					:       Union[str, Any]    =		self.image_processing_class(**self.image_processor_dict						)
       # create random PyTorch tensors
       lowerCAmelCase_					:       List[str]    =		prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=_lowercase    ,						torchify=_lowercase						)
       for image in image_inputs:
            self.assertIsInstance(_lowercase    ,						torch.Tensor						)

       # Test not batched input
       lowerCAmelCase_					:       List[str]    =		image_processing(image_inputs[0]    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)

       # Test batched
       lowerCAmelCase_					:       str    =		image_processing(_lowercase    ,						return_tensors="""pt"""						).pixel_values
       self.assertEqual(
           encoded_images.shape    ,						(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )    ,						)







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