






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import CLIPImageProcessor, CLIPProcessor



@require_vision
class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					def  _lowercase  (      self  :						int ):
						snake_case__				:   Dict     =						tempfile.mkdtemp()

						# fmt: off
						snake_case__				:   Optional[int]     =						["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
						# fmt: on
						snake_case__				:   Tuple     =						dict(zip(_lowercase							,	range(len(_lowercase ) ) ) )
						snake_case__				:   int     =						["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
						snake_case__				:   List[str]     =						{"""unk_token""": """<unk>"""}

						snake_case__				:   Tuple     =						os.path.join(self.tmpdirname							,	VOCAB_FILES_NAMES["vocab_file"] )
						snake_case__				:   Optional[Any]     =						os.path.join(self.tmpdirname							,	VOCAB_FILES_NAMES["merges_file"] )
						with open(self.vocab_file							,	"w"							,	encoding="utf-8" ) as fp:
							fp.write(json.dumps(_lowercase ) + "\n" )
						with open(self.merges_file							,	"w"							,	encoding="utf-8" ) as fp:
							fp.write("\n".join(_lowercase ) )

						snake_case__				:   Optional[Any]     =						{
						    """do_resize""": True,
						    """size""": 2_0,
						    """do_center_crop""": True,
						    """crop_size""": 1_8,
						    """do_normalize""": True,
						    """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
						    """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
						}
						snake_case__				:   Tuple     =						os.path.join(self.tmpdirname							,	_lowercase )
						with open(self.image_processor_file							,	"w"							,	encoding="utf-8" ) as fp:
							json.dump(_lowercase							,	_lowercase )



					def  _lowercase  (      self  :						List[Any]							,	**__A  :						List[str] ):
						return CLIPTokenizer.from_pretrained(self.tmpdirname							,	**_lowercase )



					def  _lowercase  (      self  :						List[Any]							,	**__A  :						str ):
						return CLIPTokenizerFast.from_pretrained(self.tmpdirname							,	**_lowercase )



					def  _lowercase  (      self  :						Union[str, Any]							,	**__A  :						int ):
						return CLIPImageProcessor.from_pretrained(self.tmpdirname							,	**_lowercase )



					def  _lowercase  (      self  :						Optional[Any] ):
						shutil.rmtree(self.tmpdirname )



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   Tuple     =						[np.random.randint(2_5_5							,	size=(3, 3_0, 4_0_0)							,	dtype=np.uinta )]

						snake_case__				:   Optional[int]     =						[Image.fromarray(np.moveaxis(_lowercase							,	0							,	-1 ) ) for x in image_inputs]

						return image_inputs



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   Any     =						self.get_tokenizer()
						snake_case__				:   Union[str, Any]     =						self.get_rust_tokenizer()
						snake_case__				:   List[Any]     =						self.get_image_processor()

						snake_case__				:   Dict     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )
						processor_slow.save_pretrained(self.tmpdirname )
						snake_case__				:   Optional[Any]     =						CLIPProcessor.from_pretrained(self.tmpdirname							,	use_fast=_lowercase )

						snake_case__				:   Optional[int]     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )
						processor_fast.save_pretrained(self.tmpdirname )
						snake_case__				:   int     =						CLIPProcessor.from_pretrained(self.tmpdirname )

						self.assertEqual(processor_slow.tokenizer.get_vocab()							,	tokenizer_slow.get_vocab() )
						self.assertEqual(processor_fast.tokenizer.get_vocab()							,	tokenizer_fast.get_vocab() )
						self.assertEqual(tokenizer_slow.get_vocab()							,	tokenizer_fast.get_vocab() )
						self.assertIsInstance(processor_slow.tokenizer							,	_lowercase )
						self.assertIsInstance(processor_fast.tokenizer							,	_lowercase )

						self.assertEqual(processor_slow.image_processor.to_json_string()							,	image_processor.to_json_string() )
						self.assertEqual(processor_fast.image_processor.to_json_string()							,	image_processor.to_json_string() )
						self.assertIsInstance(processor_slow.image_processor							,	_lowercase )
						self.assertIsInstance(processor_fast.image_processor							,	_lowercase )



					def  _lowercase  (      self  :						Tuple ):
						snake_case__				:   Optional[Any]     =						CLIPProcessor(tokenizer=self.get_tokenizer()							,	image_processor=self.get_image_processor() )
						processor.save_pretrained(self.tmpdirname )

						snake_case__				:   Tuple     =						self.get_tokenizer(bos_token="(BOS)"							,	eos_token="(EOS)" )
						snake_case__				:   List[str]     =						self.get_image_processor(do_normalize=_lowercase							,	padding_value=1.0 )

						snake_case__				:   int     =						CLIPProcessor.from_pretrained(
						    self.tmpdirname							,	bos_token="(BOS)"							,	eos_token="(EOS)"							,	do_normalize=_lowercase							,	padding_value=1.0 )

						self.assertEqual(processor.tokenizer.get_vocab()							,	tokenizer_add_kwargs.get_vocab() )
						self.assertIsInstance(processor.tokenizer							,	_lowercase )

						self.assertEqual(processor.image_processor.to_json_string()							,	image_processor_add_kwargs.to_json_string() )
						self.assertIsInstance(processor.image_processor							,	_lowercase )



					def  _lowercase  (      self  :						Dict ):
						snake_case__				:   Optional[Any]     =						self.get_image_processor()
						snake_case__				:   Dict     =						self.get_tokenizer()

						snake_case__				:   Dict     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )

						snake_case__				:   str     =						self.prepare_image_inputs()

						snake_case__				:   Union[str, Any]     =						image_processor(_lowercase							,	return_tensors="np" )
						snake_case__				:   Any     =						processor(images=_lowercase							,	return_tensors="np" )

						for key in input_image_proc.keys():
							self.assertAlmostEqual(input_image_proc[key].sum()							,	input_processor[key].sum()							,	delta=1e-2 )



					def  _lowercase  (      self  :						Optional[Any] ):
						snake_case__				:   List[Any]     =						self.get_image_processor()
						snake_case__				:   List[str]     =						self.get_tokenizer()

						snake_case__				:   Optional[int]     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )

						snake_case__				:   int     =						"""lower newer"""

						snake_case__				:   List[str]     =						processor(text=_lowercase )

						snake_case__				:   Optional[int]     =						tokenizer(_lowercase )

						for key in encoded_tok.keys():
							self.assertListEqual(encoded_tok[key]							,	encoded_processor[key] )



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   int     =						self.get_image_processor()
						snake_case__				:   str     =						self.get_tokenizer()

						snake_case__				:   Union[str, Any]     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )

						snake_case__				:   str     =						"""lower newer"""
						snake_case__				:   Tuple     =						self.prepare_image_inputs()

						snake_case__				:   Dict     =						processor(text=_lowercase							,	images=_lowercase )

						self.assertListEqual(list(inputs.keys() )							,	["input_ids", "attention_mask", "pixel_values"] )

						# test if it raises when no input is passed
						with pytest.raises(_lowercase ):
							processor()



					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   Tuple     =						self.get_image_processor()
						snake_case__				:   Optional[Any]     =						self.get_tokenizer()

						snake_case__				:   Optional[int]     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )

						snake_case__				:   Tuple     =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						snake_case__				:   str     =						processor.batch_decode(_lowercase )
						snake_case__				:   Any     =						tokenizer.batch_decode(_lowercase )

						self.assertListEqual(_lowercase							,	_lowercase )






					def  _lowercase  (      self  :						str ):
						snake_case__				:   Dict     =						self.get_image_processor()
						snake_case__				:   int     =						self.get_tokenizer()

						snake_case__				:   List[Any]     =						CLIPProcessor(tokenizer=_lowercase							,	image_processor=_lowercase )

						snake_case__				:   Union[str, Any]     =						"""lower newer"""
						snake_case__				:   Optional[Any]     =						self.prepare_image_inputs()

						snake_case__				:   Optional[Any]     =						processor(text=_lowercase							,	images=_lowercase )

						self.assertListEqual(list(inputs.keys() )							,	processor.model_input_names )











# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

__lowerCamelCase      :    Union[str, Any]   		=   """2.13.1"""

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse("""3.7"""):
	raise ImportWarning(
	    """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition."""
	)

if version.parse(pyarrow.__version__).major < 8:
	raise ImportWarning(
	    """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n"""
	    """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`."""
	)

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

__lowerCamelCase      :    List[Any]   		=   concatenate_datasets
__lowerCamelCase      :    List[str]   		=   DownloadConfig
__lowerCamelCase      :    Union[str, Any]   		=   DownloadManager
__lowerCamelCase      :    str   		=   DownloadMode
__lowerCamelCase      :    Union[str, Any]   		=   DownloadConfig
__lowerCamelCase      :    List[str]   		=   DownloadMode
__lowerCamelCase      :    Dict   		=   DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager



