




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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import TvltImageProcessor



def    lowerCamelCase_   (  _lowercase ,     _lowercase=10 ,     _lowercase=10 ,     _lowercase=False ,     _lowercase=False    )							->	Union[str, Any]:
		__A							:   str	  =				[]
		for i in range(image_processor_tester.num_frames    ):
				video.append(np.random.randint(255 ,     size=(image_processor_tester.num_channels, width, height) ,     dtype=np.uinta    )    )

		if not numpify and not torchify:
				# PIL expects the channel dimension as last dimension
				__A							:   Union[str, Any]	  =				[Image.fromarray(np.moveaxis(_lowercase ,     0 ,     -1    )    ) for frame in video]

		if torchify:
				__A							:   Dict	  =				[torch.from_numpy(_lowercase    ) for frame in video]

		return video



def    lowerCamelCase_   (  _lowercase ,     _lowercase=False ,     _lowercase=False ,     _lowercase=False    )							->	Union[str, Any]:
		assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

		__A							:   List[Any]	  =				[]
		for i in range(image_processor_tester.batch_size    ):
				if equal_resolution:
						__A							:   int	  =				image_processor_tester.max_resolution
				else:
						__A							:   Tuple	  =				np.random.choice(
						    np.arange(image_processor_tester.min_resolution ,     image_processor_tester.max_resolution    ) ,     2    )
						__A							:   Optional[int]	  =				prepare_video(
						    image_processor_tester=_lowercase ,     width=_lowercase ,     height=_lowercase ,     numpify=_lowercase ,     torchify=_lowercase ,     )
				video_inputs.append(_lowercase    )

		return video_inputs


class _a       (	unittest.TestCase ):

	'''simple docstring'''



	def __init__(       self							,			__UpperCAmelCase							,			__UpperCAmelCase=7							,			__UpperCAmelCase=3							,			__UpperCAmelCase=4							,			__UpperCAmelCase=18							,			__UpperCAmelCase=30							,			__UpperCAmelCase=400							,			__UpperCAmelCase=True							,			__UpperCAmelCase=None							,			__UpperCAmelCase=True							,			__UpperCAmelCase=[0.5, 0.5, 0.5]							,			__UpperCAmelCase=[0.5, 0.5, 0.5]							,			__UpperCAmelCase=True							,			__UpperCAmelCase=None							,			):
			__A							:   List[str]	  =				size if size is not None else {'shortest_edge': 18}
			__A							:   Tuple	  =				crop_size if crop_size is not None else {'height': 18, 'width': 18}

			__A							:   Dict	  =				parent
			__A							:   str	  =				batch_size
			__A							:   int	  =				num_channels
			__A							:   Optional[Any]	  =				num_frames
			__A							:   Any	  =				image_size
			__A							:   List[str]	  =				min_resolution
			__A							:   List[Any]	  =				max_resolution
			__A							:   Tuple	  =				do_resize
			__A							:   List[str]	  =				size
			__A							:   str	  =				do_normalize
			__A							:   str	  =				image_mean
			__A							:   int	  =				image_std
			__A							:   Optional[Any]	  =				do_center_crop
			__A							:   Union[str, Any]	  =				crop_size







	def 					__UpperCAmelCase(       self   ):
			return {
			    "image_mean": self.image_mean,
			    "image_std": self.image_std,
			    "do_normalize": self.do_normalize,
			    "do_resize": self.do_resize,
			    "size": self.size,
			    "do_center_crop": self.do_center_crop,
			    "crop_size": self.crop_size,
			}


@require_torch
@require_vision
class _a       (	snake_case__   ,							unittest.TestCase ):

	'''simple docstring'''



	lowerCamelCase_   :      Optional[Any]       =	TvltImageProcessor if is_vision_available() else None

	def 					__UpperCAmelCase(       self   ):
			__A							:   str	  =				TvltImageProcessorTester(self   )

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

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.image_processing_class(**self.image_processor_dict   )
			self.assertTrue(hasattr(_A							,			"image_mean"   )   )
			self.assertTrue(hasattr(_A							,			"image_std"   )   )
			self.assertTrue(hasattr(_A							,			"do_normalize"   )   )
			self.assertTrue(hasattr(_A							,			"do_resize"   )   )
			self.assertTrue(hasattr(_A							,			"do_center_crop"   )   )
			self.assertTrue(hasattr(_A							,			"size"   )   )

	def 					__UpperCAmelCase(       self   ):
			# Initialize image_processor
			__A							:   List[str]	  =				self.image_processing_class(**self.image_processor_dict   )
			# create random PIL videos
			__A							:   List[str]	  =				prepare_video_inputs(self.image_processor_tester							,			equal_resolution=_A   )
			for video in video_inputs:
					self.assertIsInstance(_A							,			_A   )
					self.assertIsInstance(video[0]							,			Image.Image   )

			# Test not batched input
			__A							:   str	  =				image_processor(video_inputs[0]							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        1,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)

			# Test batched
			__A							:   List[Any]	  =				image_processor(_A							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)

	def 					__UpperCAmelCase(       self   ):
			# Initialize image_processor
			__A							:   Any	  =				self.image_processing_class(**self.image_processor_dict   )
			# create random numpy tensors
			__A							:   Dict	  =				prepare_video_inputs(self.image_processor_tester							,			equal_resolution=_A							,			numpify=_A   )
			for video in video_inputs:
					self.assertIsInstance(_A							,			_A   )
					self.assertIsInstance(video[0]							,			np.ndarray   )

			# Test not batched input
			__A							:   Dict	  =				image_processor(video_inputs[0]							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        1,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)

			# Test batched
			__A							:   Optional[int]	  =				image_processor(_A							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)







	def 					__UpperCAmelCase(       self   ):
			# Initialize image_processor
			__A							:   Any	  =				self.image_processing_class(**self.image_processor_dict   )
			# create random PyTorch tensors
			__A							:   Dict	  =				prepare_video_inputs(self.image_processor_tester							,			equal_resolution=_A							,			torchify=_A   )
			for video in video_inputs:
					self.assertIsInstance(_A							,			_A   )
					self.assertIsInstance(video[0]							,			torch.Tensor   )

			# Test not batched input
			__A							:   Optional[Any]	  =				image_processor(video_inputs[0]							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        1,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)

			# Test batched
			__A							:   int	  =				image_processor(_A							,			return_tensors="pt"   ).pixel_values
			self.assertEqual(
			    encoded_videos.shape							,			(
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_frames,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size["height"],
			        self.image_processor_tester.crop_size["width"],
			    )							,			)






import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata



def    lowerCamelCase_   (  _lowercase    )							->	str:
		__A							:   Optional[int]	  =				min(re.search("^ +" ,     _lowercase    ).end() if t.startswith(" "    ) else 0 for t in string.splitlines()    )
		return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(_lowercase    )]    )


UpperCamelCase      			=     '\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n'

UpperCamelCase      			=     '\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n'


UpperCamelCase      			=     '\\n# Begin of markdown\n\nSome cool dataset card\n'





class _a       (	unittest.TestCase ):

	'''simple docstring'''



	def 					__UpperCAmelCase(       self   ):
			with tempfile.TemporaryDirectory() as tmp_dir:
					__A							:   List[Any]	  =				Path(__UpperCAmelCase   ) / "README.md"
					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Tuple	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertDictEqual(__UpperCAmelCase							,			{"language": ["zh", "en"], "task_ids": ["sentiment-classification"]}   )

					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Any	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertDictEqual(__UpperCAmelCase							,			{}   )

					with open(__UpperCAmelCase							,			"w+"   ) as readme_file:
							readme_file.write(__UpperCAmelCase   )
					__A							:   Union[str, Any]	  =				DatasetMetadata.from_readme(__UpperCAmelCase   )
					self.assertEqual(__UpperCAmelCase							,			{}   )







	def 					__UpperCAmelCase(       self   ):
			__A							:   List[str]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            "   )
			assert DatasetMetadata.from_yaml_string(__UpperCAmelCase   )

			__A							:   List[Any]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            "   )
			with self.assertRaises(__UpperCAmelCase   ):
					DatasetMetadata.from_yaml_string(__UpperCAmelCase   )

			__A							:   Optional[int]	  =				_dedent(
			    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the \"Researcher\") has requested permission to use the ImageNet database (the \"Database\") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            "   )
			assert DatasetMetadata.from_yaml_string(__UpperCAmelCase   )





