



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 		lowercase						( unittest.TestCase):






	"""simple docstring"""


	def __init__(	self	:						List[Any]						,  __UpperCAmelCase	:						Optional[int]						,  __UpperCAmelCase	:						Optional[int]=7						,  __UpperCAmelCase	:						Tuple=3						,  __UpperCAmelCase	:						List[str]=18						,  __UpperCAmelCase	:						Union[str, Any]=30						,  __UpperCAmelCase	:						Optional[int]=400						,  __UpperCAmelCase	:						Optional[int]=True						,  __UpperCAmelCase	:						str=None						,  __UpperCAmelCase	:						List[Any]=True						,  __UpperCAmelCase	:						Union[str, Any]=None						,  __UpperCAmelCase	:						Any=True						,  __UpperCAmelCase	:						Optional[int]=[0.48_145_466, 0.4_578_275, 0.40_821_073]						,  __UpperCAmelCase	:						Dict=[0.26_862_954, 0.26_130_258, 0.27_577_711]						,  __UpperCAmelCase	:						Optional[int]=True						,  )			-> Optional[Any]:
							UpperCAmelCase_=   parent
							UpperCAmelCase_=   batch_size
							UpperCAmelCase_=   num_channels
							UpperCAmelCase_=   image_size
							UpperCAmelCase_=   min_resolution
							UpperCAmelCase_=   max_resolution
							UpperCAmelCase_=   do_resize
							UpperCAmelCase_=   size if size is not None else {"""height""": 18, """width""": 18}
							UpperCAmelCase_=   do_center_crop
							UpperCAmelCase_=   crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
							UpperCAmelCase_=   do_normalize
							UpperCAmelCase_=   image_mean
							UpperCAmelCase_=   image_std
							UpperCAmelCase_=   do_convert_rgb




	def   _SCREAMING_SNAKE_CASE		(	self	:						List[Any]     )			-> Optional[Any]:
							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 		lowercase						( lowerCAmelCase__ ,    unittest.TestCase):






	"""simple docstring"""


	a__      :					int		   =							OwlViTImageProcessor if is_vision_available() else None




	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[Any]     )			-> str:
							UpperCAmelCase_=   OwlViTImageProcessingTester(self     )




	@property
	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[Any]     )			-> List[Any]:
							return self.image_processor_tester.prepare_image_processor_dict()




	def   _SCREAMING_SNAKE_CASE		(	self	:						Tuple     )			-> str:
							UpperCAmelCase_=   self.image_processing_class(**self.image_processor_dict     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """do_resize"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """size"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """do_center_crop"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """center_crop"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """do_normalize"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """image_mean"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """image_std"""     )     )
							self.assertTrue(hasattr(_lowerCamelCase						,  """do_convert_rgb"""     )     )




	def   _SCREAMING_SNAKE_CASE		(	self	:						int     )			-> Union[str, Any]:
							UpperCAmelCase_=   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}     )

							UpperCAmelCase_=   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   _SCREAMING_SNAKE_CASE		(	self	:						int     )			-> Any:
							UpperCAmelCase_=   self.image_processing_class(**self.image_processor_dict     )
							# create random PIL images
							UpperCAmelCase_=   prepare_image_inputs(self.image_processor_tester						,  equal_resolution=_lowerCamelCase     )

							for image in image_inputs:
													self.assertIsInstance(_lowerCamelCase						,  Image.Image     )

							# Test not batched input
							UpperCAmelCase_=   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
							UpperCAmelCase_=   image_processing(_lowerCamelCase						,  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   _SCREAMING_SNAKE_CASE		(	self	:						Optional[int]     )			-> Optional[int]:
							UpperCAmelCase_=   self.image_processing_class(**self.image_processor_dict     )
							# create random numpy tensors
							UpperCAmelCase_=   prepare_image_inputs(self.image_processor_tester						,  equal_resolution=_lowerCamelCase						,  numpify=_lowerCamelCase     )
							for image in image_inputs:
													self.assertIsInstance(_lowerCamelCase						,  np.ndarray     )

							# Test not batched input
							UpperCAmelCase_=   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
							UpperCAmelCase_=   image_processing(_lowerCamelCase						,  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   _SCREAMING_SNAKE_CASE		(	self	:						Union[str, Any]     )			-> Optional[Any]:
							UpperCAmelCase_=   self.image_processing_class(**self.image_processor_dict     )
							# create random PyTorch tensors
							UpperCAmelCase_=   prepare_image_inputs(self.image_processor_tester						,  equal_resolution=_lowerCamelCase						,  torchify=_lowerCamelCase     )
							for image in image_inputs:
													self.assertIsInstance(_lowerCamelCase						,  torch.Tensor     )

							# Test not batched input
							UpperCAmelCase_=   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
							UpperCAmelCase_=   image_processing(_lowerCamelCase						,  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"""],
							    )						,  )











'''simple docstring'''



import csv
import json
import os

import datasets


# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_SCREAMING_SNAKE_CASE    =			'''\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2020}
}
'''

# TODO: Add description of the dataset here
# You can copy an official description
_SCREAMING_SNAKE_CASE    =			'''\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
'''

# TODO: Add a link to an official homepage for the dataset here
_SCREAMING_SNAKE_CASE    =			''''''

# TODO: Add the licence for the dataset here if you can find it
_SCREAMING_SNAKE_CASE    =			''''''

# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_SCREAMING_SNAKE_CASE    =			{
    '''first_domain''': '''https://huggingface.co/great-new-dataset-first_domain.zip''',
    '''second_domain''': '''https://huggingface.co/great-new-dataset-second_domain.zip''',
}






class 							__lowercase				(					datasets.GeneratorBasedBuilder  ):







	'''simple docstring'''

	a			: Tuple      =				datasets.Version("1.1.0"  )

	# This is an example of a dataset with multiple configurations.
	# If you don't want/need to define several sub-sets in your dataset,
	# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.

	# If you need to make complex sub-parts in the datasets with configurable options
	# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
	# BUILDER_CONFIG_CLASS = MyBuilderConfig

	# You will be able to load one or the other configurations in the following list with
	# data = datasets.load_dataset('my_dataset', 'first_domain')
	# data = datasets.load_dataset('my_dataset', 'second_domain')
	a			: List[Any]      =				[
	    datasets.BuilderConfig(name="first_domain"       ,					version=lowerCAmelCase__       ,					description="This part of my dataset covers a first domain"  ),
	    datasets.BuilderConfig(name="second_domain"       ,					version=lowerCAmelCase__       ,					description="This part of my dataset covers a second domain"  ),
	]

	a			: Dict      =				"first_domain"  # It's not mandatory to have a default configuration. Just use one if it make sense.


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







				'''simple docstring'''





				if self.config.name == "first_domain":  # This is the name of the configuration selected in BUILDER_CONFIGS above
							__lowercase   =							datasets.Features(
							    {
							        '''sentence''': datasets.Value('''string'''       ),
							        '''option1''': datasets.Value('''string'''       ),
							        '''answer''': datasets.Value('''string'''       )
							        # These are the features of your dataset like images, labels ...
							    }       )
				else:  # This is an example to show how to have different features for "first_domain" and "second_domain"
							__lowercase   =							datasets.Features(
							    {
							        '''sentence''': datasets.Value('''string'''       ),
							        '''option2''': datasets.Value('''string'''       ),
							        '''second_domain_answer''': datasets.Value('''string'''       )
							        # These are the features of your dataset like images, labels ...
							    }       )
				return datasets.DatasetInfo(
				    # This is the description that will appear on the datasets page.
				    description=_DESCRIPTION   ,features=_lowerCamelCase   ,homepage=_HOMEPAGE   ,license=_LICENSE   ,citation=_CITATION   ,)


	def   _UpperCAmelCase   (self   ,_lowerCamelCase       )							->							Dict:







				'''simple docstring'''





				__lowercase   =							_URLS[self.config.name]
				__lowercase   =							dl_manager.download_and_extract(_lowerCamelCase       )
				return [
				    datasets.SplitGenerator(
				        name=datasets.Split.TRAIN   ,gen_kwargs={
				            '''filepath''': os.path.join(_lowerCamelCase   ,'''train.jsonl'''       ),
				            '''split''': '''train''',
				        }   ,),
				    datasets.SplitGenerator(
				        name=datasets.Split.VALIDATION   ,gen_kwargs={
				            '''filepath''': os.path.join(_lowerCamelCase   ,'''dev.jsonl'''       ),
				            '''split''': '''dev''',
				        }   ,),
				    datasets.SplitGenerator(
				        name=datasets.Split.TEST   ,gen_kwargs={
				            '''filepath''': os.path.join(_lowerCamelCase   ,'''test.jsonl'''       ),
				            '''split''': '''test'''
				        }   ,),
				]


	def   _UpperCAmelCase   (self   ,_lowerCamelCase   ,_lowerCamelCase       )							->							str:







				'''simple docstring'''





				with open(_lowerCamelCase   ,encoding='''utf-8'''       ) as f:
							for key, row in enumerate(_lowerCamelCase       ):
										__lowercase   =							json.loads(_lowerCamelCase       )
										if self.config.name == "first_domain":
													# Yields examples as (key, example) tuples
													yield key, {
													    "sentence": data["sentence"],
													    "option1": data["option1"],
													    "answer": "" if split == "test" else data["answer"],
													}
										else:
													yield key, {
													    "sentence": data["sentence"],
													    "option2": data["option2"],
													    "second_domain_answer": "" if split == "test" else data["second_domain_answer"],
													}





