






'''simple docstring'''







import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


_A	:			Union[str, Any]         =get_tests_dir('''fixtures''')

class _lowercase	(				unittest.TestCase		):




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														# A mock response for an HTTP head request to emulate server down
														lowerCamelCase__					:	Union[str, Any]         =       mock.Mock()
														lowerCamelCase__					:	Tuple         =       500
														lowerCamelCase__					:	Optional[int]         =       {}
														lowerCamelCase__					:	Optional[Any]         =       HTTPError
														lowerCamelCase__					:	Dict         =       {}

														# Download this model to make sure it's in the cache.
														lowerCamelCase__					:	Optional[int]         =       WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""     )
														# Under the mock environment we get a 500 error when trying to reach the model.
														with mock.patch("""requests.Session.request"""			,							return_value=UpperCamelCase__     ) as mock_head:
																					lowerCamelCase__					:	int         =       WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2"""     )
																					# This check we did call the fake head request
																					mock_head.assert_called()




							def 		lowerCamelCase_				(							self:				List[Any]     ):
														# This test is for deprecated behavior and can be removed in v5
														lowerCamelCase__					:	Optional[int]         =       WavaVecaFeatureExtractor.from_pretrained(
														    """https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"""     )



@is_staging_test
class _lowercase	(				unittest.TestCase		):




							@classmethod
							def 		lowerCamelCase_				(							cls:				Union[str, Any]     ):
														lowerCamelCase__					:	Optional[Any]         =       TOKEN
														HfFolder.save_token(UpperCamelCase__     )




							@classmethod
							def 		lowerCamelCase_				(							cls:				int     ):
														try:
																					delete_repo(token=cls._token			,							repo_id="""test-feature-extractor"""     )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token			,							repo_id="""valid_org/test-feature-extractor-org"""     )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token			,							repo_id="""test-dynamic-feature-extractor"""     )
														except HTTPError:
																					pass




							def 		lowerCamelCase_				(							self:				List[Any]     ):
														lowerCamelCase__					:	Tuple         =       WavaVecaFeatureExtractor.from_pretrained(UpperCamelCase__     )
														feature_extractor.push_to_hub("""test-feature-extractor"""			,							use_auth_token=self._token     )

														lowerCamelCase__					:	str         =       WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''     )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(UpperCamelCase__			,							getattr(UpperCamelCase__			,							UpperCamelCase__     )     )

														# Reset repo
														delete_repo(token=self._token			,							repo_id="""test-feature-extractor"""     )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(
																					    UpperCamelCase__			,							repo_id="""test-feature-extractor"""			,							push_to_hub=UpperCamelCase__			,							use_auth_token=self._token     )

														lowerCamelCase__					:	Dict         =       WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''     )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(UpperCamelCase__			,							getattr(UpperCamelCase__			,							UpperCamelCase__     )     )




							def 		lowerCamelCase_				(							self:				str     ):
														lowerCamelCase__					:	Dict         =       WavaVecaFeatureExtractor.from_pretrained(UpperCamelCase__     )
														feature_extractor.push_to_hub("""valid_org/test-feature-extractor"""			,							use_auth_token=self._token     )

														lowerCamelCase__					:	Dict         =       WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor"""     )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(UpperCamelCase__			,							getattr(UpperCamelCase__			,							UpperCamelCase__     )     )

														# Reset repo
														delete_repo(token=self._token			,							repo_id="""valid_org/test-feature-extractor"""     )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(
																					    UpperCamelCase__			,							repo_id="""valid_org/test-feature-extractor-org"""			,							push_to_hub=UpperCamelCase__			,							use_auth_token=self._token     )

														lowerCamelCase__					:	Optional[int]         =       WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor-org"""     )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(UpperCamelCase__			,							getattr(UpperCamelCase__			,							UpperCamelCase__     )     )




							def 		lowerCamelCase_				(							self:				Any     ):
														CustomFeatureExtractor.register_for_auto_class()
														lowerCamelCase__					:	Any         =       CustomFeatureExtractor.from_pretrained(UpperCamelCase__     )

														feature_extractor.push_to_hub("""test-dynamic-feature-extractor"""			,							use_auth_token=self._token     )

														# This has added the proper auto_map field to the config
														self.assertDictEqual(
														    feature_extractor.auto_map			,							{"""AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor"""}			,							)

														lowerCamelCase__					:	Any         =       AutoFeatureExtractor.from_pretrained(
														    F'''{USER}/test-dynamic-feature-extractor'''			,							trust_remote_code=UpperCamelCase__     )
														# Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
														self.assertEqual(new_feature_extractor.__class__.__name__			,							"""CustomFeatureExtractor"""     )









'''simple docstring'''







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:				str			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				Any=7			,							UpperCamelCase__:				Optional[int]=3			,							UpperCamelCase__:				List[str]=18			,							UpperCamelCase__:				Union[str, Any]=30			,							UpperCamelCase__:				List[str]=400			,							UpperCamelCase__:				Any=True			,							UpperCamelCase__:				Union[str, Any]=None			,							UpperCamelCase__:				List[Any]=True			,							UpperCamelCase__:				List[Any]=False			,							UpperCamelCase__:				Tuple=True			,							UpperCamelCase__:				Tuple=True			,							UpperCamelCase__:				Any=[0.5, 0.5, 0.5]			,							UpperCamelCase__:				Optional[Any]=[0.5, 0.5, 0.5]			,							):
														lowerCamelCase__					:	int         =       parent
														lowerCamelCase__					:	Any         =       batch_size
														lowerCamelCase__					:	Optional[int]         =       num_channels
														lowerCamelCase__					:	Union[str, Any]         =       image_size
														lowerCamelCase__					:	Optional[int]         =       min_resolution
														lowerCamelCase__					:	Optional[Any]         =       max_resolution
														lowerCamelCase__					:	Union[str, Any]         =       do_resize
														lowerCamelCase__					:	Optional[Any]         =       size if size is not None else {"""height""": 18, """width""": 20}
														lowerCamelCase__					:	Dict         =       do_thumbnail
														lowerCamelCase__					:	Optional[int]         =       do_align_axis
														lowerCamelCase__					:	Any         =       do_pad
														lowerCamelCase__					:	Optional[Any]         =       do_normalize
														lowerCamelCase__					:	Union[str, Any]         =       image_mean
														lowerCamelCase__					:	Union[str, Any]         =       image_std




							def 		lowerCamelCase_				(							self:				str     ):
														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	(				_lowercase				,      unittest.TestCase		):
							a				      =       DonutImageProcessor if is_vision_available() else None




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														lowerCamelCase__					:	Any         =       DonutImageProcessingTester(self     )




							@property
							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														return self.image_processor_tester.prepare_image_processor_dict()




							def 		lowerCamelCase_				(							self:				Tuple     ):
														lowerCamelCase__					:	Dict         =       self.image_processing_class(**self.image_processor_dict     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_resize"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""size"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_thumbnail"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_align_long_axis"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_pad"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_normalize"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""image_mean"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""image_std"""     )     )




							def 		lowerCamelCase_				(							self:				Optional[Any]     ):
														lowerCamelCase__					:	Optional[int]         =       self.image_processing_class.from_dict(self.image_processor_dict     )
														self.assertEqual(image_processor.size			,							{"""height""": 18, """width""": 20}     )

														lowerCamelCase__					:	Optional[int]         =       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__					:	List[str]         =       self.image_processing_class.from_dict(self.image_processor_dict			,							size=(42, 84)     )
														self.assertEqual(image_processor.size			,							{"""height""": 84, """width""": 42}     )




							def 		lowerCamelCase_				(							self:				List[str]     ):
														pass




							@is_flaky()
							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														# Initialize image_processing
														lowerCamelCase__					:	str         =       self.image_processing_class(**self.image_processor_dict     )
														# create random PIL images
														lowerCamelCase__					:	Dict         =       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__					:	Union[str, 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__					:	List[str]         =       image_processing(UpperCamelCase__			,							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 		lowerCamelCase_				(							self:				Optional[int]     ):
														# Initialize image_processing
														lowerCamelCase__					:	Dict         =       self.image_processing_class(**self.image_processor_dict     )
														# create random numpy tensors
														lowerCamelCase__					:	Dict         =       prepare_image_inputs(self.image_processor_tester			,							equal_resolution=UpperCamelCase__			,							numpify=UpperCamelCase__     )
														for image in image_inputs:
																					self.assertIsInstance(UpperCamelCase__			,							np.ndarray     )

														# Test not batched input
														lowerCamelCase__					:	List[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__					:	Optional[Any]         =       image_processing(UpperCamelCase__			,							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 		lowerCamelCase_				(							self:				Dict     ):
														# Initialize image_processing
														lowerCamelCase__					:	int         =       self.image_processing_class(**self.image_processor_dict     )
														# create random PyTorch tensors
														lowerCamelCase__					:	Optional[int]         =       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__					:	Union[str, 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__					:	Tuple         =       image_processing(UpperCamelCase__			,							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"""],
														    )			,							)

