




import unittest

import numpy as np

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

if is_vision_available():
						from PIL import Image

						from transformers import OwlViTImageProcessor





class 						SCREAMING_SNAKE_CASE__		(unittest.TestCase ):
					def __init__(	self  :		Dict					,	__lowerCamelCase  :		str					,	__lowerCamelCase  :		Dict=7					,	__lowerCamelCase  :		List[Any]=3					,	__lowerCamelCase  :		str=18					,	__lowerCamelCase  :		Dict=30					,	__lowerCamelCase  :		int=4_00					,	__lowerCamelCase  :		List[str]=True					,	__lowerCamelCase  :		Optional[int]=None					,	__lowerCamelCase  :		Dict=True					,	__lowerCamelCase  :		str=None					,	__lowerCamelCase  :		int=True					,	__lowerCamelCase  :		List[Any]=[0.4814_5466, 0.457_8275, 0.4082_1073]					,	__lowerCamelCase  :		Union[str, Any]=[0.2686_2954, 0.2613_0258, 0.2757_7711]					,	__lowerCamelCase  :		Any=True					,	):



							"""simple docstring"""
							lowerCAmelCase__								=			parent
							lowerCAmelCase__								=			batch_size
							lowerCAmelCase__								=			num_channels
							lowerCAmelCase__								=			image_size
							lowerCAmelCase__								=			min_resolution
							lowerCAmelCase__								=			max_resolution
							lowerCAmelCase__								=			do_resize
							lowerCAmelCase__								=			size if size is not None else {'''height''': 18, '''width''': 18}
							lowerCAmelCase__								=			do_center_crop
							lowerCAmelCase__								=			crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
							lowerCAmelCase__								=			do_normalize
							lowerCAmelCase__								=			image_mean
							lowerCAmelCase__								=			image_std
							lowerCAmelCase__								=			do_convert_rgb



					def 					A__			(	self  :		Union[str, Any]	):



							"""simple docstring"""
							return {
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "do_normalize": self.do_normalize,
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_convert_rgb": self.do_convert_rgb,
							}





@require_torch
@require_vision
class 						SCREAMING_SNAKE_CASE__		(UpperCAmelCase_						,				unittest.TestCase ):
					lowercase_   :			Tuple   =   OwlViTImageProcessor if is_vision_available() else None
					def 					A__			(	self  :		Any	):



							"""simple docstring"""
							lowerCAmelCase__								=			OwlViTImageProcessingTester(self	)
					@property
					def 					A__			(	self  :		List[Any]	):



							"""simple docstring"""
							return self.image_processor_tester.prepare_image_processor_dict()
					def 					A__			(	self  :		int	):



							"""simple docstring"""
							lowerCAmelCase__								=			self.image_processing_class(**self.image_processor_dict	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''do_resize'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''size'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''do_center_crop'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''center_crop'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''do_normalize'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''image_mean'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''image_std'''	)	)
							self.assertTrue(hasattr(__UpperCamelCase					,	'''do_convert_rgb'''	)	)
					def 					A__			(	self  :		Union[str, Any]	):



							"""simple docstring"""
							lowerCAmelCase__								=			self.image_processing_class.from_dict(self.image_processor_dict	)
							self.assertEqual(image_processor.size					,	{'''height''': 18, '''width''': 18}	)
							self.assertEqual(image_processor.crop_size					,	{'''height''': 18, '''width''': 18}	)

							lowerCAmelCase__								=			self.image_processing_class.from_dict(self.image_processor_dict					,	size=42					,	crop_size=84	)
							self.assertEqual(image_processor.size					,	{'''height''': 42, '''width''': 42}	)
							self.assertEqual(image_processor.crop_size					,	{'''height''': 84, '''width''': 84}	)
					def 					A__			(	self  :		int	):



							"""simple docstring"""
							# Initialize image_processing
							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__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)

							# Test batched
							lowerCAmelCase__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)
					def 					A__			(	self  :		Optional[int]	):



							"""simple docstring"""
							# Initialize image_processing
							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	)

							# Test not batched input
							lowerCAmelCase__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)

							# Test batched
							lowerCAmelCase__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)



					def 					A__			(	self  :		List[str]	):



							"""simple docstring"""
							# Initialize image_processing
							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__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)

							# Test batched
							lowerCAmelCase__								=			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.crop_size['''height'''],
							        self.image_processor_tester.crop_size['''width'''],
							    )					,	)







import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


__magic_name__     :     List[Any]  							=					get_logger(__name__)


if TYPE_CHECKING:
      from .builder import DatasetBuilder
def 		a_       (   __lowerCAmelCase      ,   __lowerCAmelCase = None							):
  lowerCAmelCase__								=			importlib.import_module(__lowerCAmelCase							)

  # TODO(QL): always update the module to add subsequent new authentication
  if hasattr(__lowerCAmelCase      ,   '''_patched_for_streaming'''							) and module._patched_for_streaming:
    return

  def wrap_auth(__lowerCAmelCase							):
    @wraps(__lowerCAmelCase							)
    def wrapper(*__lowerCAmelCase      ,   **__lowerCAmelCase							):
      return function(*__lowerCAmelCase      ,   use_auth_token=__lowerCAmelCase      ,   **__lowerCAmelCase							)

    lowerCAmelCase__								=			'''wrap_auth'''
    return wrapper

  # open files in a streaming fashion
  patch_submodule(__lowerCAmelCase      ,   '''open'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.listdir'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.walk'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''glob.glob'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  # allow to navigate in remote zip files
  patch_submodule(__lowerCAmelCase      ,   '''os.path.join'''      ,   __lowerCAmelCase							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.dirname'''      ,   __lowerCAmelCase							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.basename'''      ,   __lowerCAmelCase							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.relpath'''      ,   __lowerCAmelCase							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.split'''      ,   __lowerCAmelCase							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.splitext'''      ,   __lowerCAmelCase							).start()
  # allow checks on paths
  patch_submodule(__lowerCAmelCase      ,   '''os.path.exists'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.isdir'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.isfile'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''os.path.getsize'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''pathlib.Path'''      ,   __lowerCAmelCase							).start()
  # file readers
  patch_submodule(__lowerCAmelCase      ,   '''gzip.open'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''numpy.load'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''pandas.read_csv'''      ,   wrap_auth(__lowerCAmelCase							)      ,   attrs=['''__version__''']							).start()
  patch_submodule(__lowerCAmelCase      ,   '''pandas.read_excel'''      ,   wrap_auth(__lowerCAmelCase							)      ,   attrs=['''__version__''']							).start()
  patch_submodule(__lowerCAmelCase      ,   '''scipy.io.loadmat'''      ,   wrap_auth(__lowerCAmelCase							)      ,   attrs=['''__version__''']							).start()
  patch_submodule(__lowerCAmelCase      ,   '''xml.etree.ElementTree.parse'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  patch_submodule(__lowerCAmelCase      ,   '''xml.dom.minidom.parse'''      ,   wrap_auth(__lowerCAmelCase							)							).start()
  lowerCAmelCase__								=			True




def 		a_       (   __lowerCAmelCase							):
  extend_module_for_streaming(builder.__module__      ,   use_auth_token=builder.use_auth_token							)
  # if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
  if not builder.__module__.startswith('''datasets.'''							):  # check that it's not a packaged builder like csv
    for imports in get_imports(inspect.getfile(builder.__class__							)							):
      if imports[0] == "internal":
        lowerCAmelCase__								=			imports[1]
        lowerCAmelCase__								=			'''.'''.join(builder.__module__.split('''.'''							)[:-1] + [internal_import_name]							)
        extend_module_for_streaming(__lowerCAmelCase      ,   use_auth_token=builder.use_auth_token							)

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
  from .builder import DatasetBuilder

  lowerCAmelCase__								=			[
      cls.__module__
      for cls in type(__lowerCAmelCase							).__mro__[1:]  # make sure it's not the same module we've already patched
      if issubclass(cls      ,   __lowerCAmelCase							) and cls.__module__ != DatasetBuilder.__module__
  ]  # check it's not a standard builder from datasets.builder
  for module in parent_builder_modules:
    extend_module_for_streaming(__lowerCAmelCase      ,   use_auth_token=builder.use_auth_token							)

