




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 CLIPSegProcessor, ViTImageProcessor


@require_vision
class    SCREAMING_SNAKE_CASE_				(       unittest.TestCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						int	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      tempfile.mkdtemp()

						# fmt: off
						__UpperCamelCase					:	Any             =      ["""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
						__UpperCamelCase					:	Tuple             =      dict(zip(lowerCAmelCase					,     range(len(lowerCAmelCase	)	)	)	)
						__UpperCamelCase					:	List[Any]             =      ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
						__UpperCamelCase					:	List[Any]             =      {"""unk_token""": """<unk>"""}

						__UpperCamelCase					:	List[str]             =      os.path.join(self.tmpdirname					,     VOCAB_FILES_NAMES["""vocab_file"""]	)
						__UpperCamelCase					:	int             =      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(lowerCAmelCase	) + """\n"""	)
						with open(self.merges_file					,     """w"""					,     encoding="""utf-8"""	) as fp:
											fp.write("""\n""".join(lowerCAmelCase	)	)

						__UpperCamelCase					:	str             =      {
						    """do_resize""": True,
						    """size""": 20,
						    """do_center_crop""": True,
						    """crop_size""": 18,
						    """do_normalize""": True,
						    """image_mean""": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
						    """image_std""": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
						}
						__UpperCamelCase					:	Any             =      os.path.join(self.tmpdirname					,     lowerCAmelCase	)
						with open(self.image_processor_file					,     """w"""					,     encoding="""utf-8"""	) as fp:
											json.dump(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int					,     **lowerCAmelCase		:						Union[str, Any]	)	->       List[Any]:







						"""simple docstring"""







						return CLIPTokenizer.from_pretrained(self.tmpdirname					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						List[Any]					,     **lowerCAmelCase		:						Optional[int]	)	->       Tuple:







						"""simple docstring"""







						return CLIPTokenizerFast.from_pretrained(self.tmpdirname					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Tuple					,     **lowerCAmelCase		:						Tuple	)	->       int:







						"""simple docstring"""







						return ViTImageProcessor.from_pretrained(self.tmpdirname					,     **lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Dict:







						"""simple docstring"""







						shutil.rmtree(self.tmpdirname	)






	def 					lowerCamelCase__     (					self		:						List[str]	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	Union[str, Any]             =      [np.random.randint(255					,     size=(3, 30, 400)					,     dtype=np.uinta	)]

						__UpperCamelCase					:	List[Any]             =      [Image.fromarray(np.moveaxis(lowerCAmelCase					,     0					,     -1	)	) for x in image_inputs]

						return image_inputs






	def 					lowerCamelCase__     (					self		:						Any	)	->       int:







						"""simple docstring"""







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

						__UpperCamelCase					:	Dict             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)
						processor_slow.save_pretrained(self.tmpdirname	)
						__UpperCamelCase					:	List[Any]             =      CLIPSegProcessor.from_pretrained(self.tmpdirname					,     use_fast=lowerCAmelCase	)

						__UpperCamelCase					:	int             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)
						processor_fast.save_pretrained(self.tmpdirname	)
						__UpperCamelCase					:	Dict             =      CLIPSegProcessor.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					,     lowerCAmelCase	)
						self.assertIsInstance(processor_fast.tokenizer					,     lowerCAmelCase	)

						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					,     lowerCAmelCase	)
						self.assertIsInstance(processor_fast.image_processor					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Any	)	->       Union[str, Any]:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      CLIPSegProcessor(tokenizer=self.get_tokenizer()					,     image_processor=self.get_image_processor()	)
						processor.save_pretrained(self.tmpdirname	)

						__UpperCamelCase					:	Tuple             =      self.get_tokenizer(bos_token="""(BOS)"""					,     eos_token="""(EOS)"""	)
						__UpperCamelCase					:	str             =      self.get_image_processor(do_normalize=lowerCAmelCase					,     padding_value=1.0	)

						__UpperCamelCase					:	Dict             =      CLIPSegProcessor.from_pretrained(
						    self.tmpdirname					,     bos_token="""(BOS)"""					,     eos_token="""(EOS)"""					,     do_normalize=lowerCAmelCase					,     padding_value=1.0	)

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

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






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[Any]             =      self.get_image_processor()
						__UpperCamelCase					:	List[Any]             =      self.get_tokenizer()

						__UpperCamelCase					:	Optional[Any]             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)

						__UpperCamelCase					:	Optional[Any]             =      self.prepare_image_inputs()

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

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






	def 					lowerCamelCase__     (					self		:						Any	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      self.get_image_processor()
						__UpperCamelCase					:	List[Any]             =      self.get_tokenizer()

						__UpperCamelCase					:	str             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)

						__UpperCamelCase					:	int             =      """lower newer"""

						__UpperCamelCase					:	Any             =      processor(text=lowerCAmelCase	)

						__UpperCamelCase					:	str             =      tokenizer(lowerCAmelCase	)

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






	def 					lowerCamelCase__     (					self		:						int	)	->       Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	str             =      self.get_image_processor()
						__UpperCamelCase					:	Union[str, Any]             =      self.get_tokenizer()

						__UpperCamelCase					:	str             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)

						__UpperCamelCase					:	Dict             =      """lower newer"""
						__UpperCamelCase					:	Any             =      self.prepare_image_inputs()

						__UpperCamelCase					:	Optional[Any]             =      processor(text=lowerCAmelCase					,     images=lowerCAmelCase	)

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

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






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      self.get_image_processor()
						__UpperCamelCase					:	List[Any]             =      self.get_tokenizer()

						__UpperCamelCase					:	Optional[Any]             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)

						__UpperCamelCase					:	Dict             =      self.prepare_image_inputs()
						__UpperCamelCase					:	Tuple             =      self.prepare_image_inputs()

						__UpperCamelCase					:	List[str]             =      processor(images=lowerCAmelCase					,     visual_prompt=lowerCAmelCase	)

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

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






	def 					lowerCamelCase__     (					self		:						Any	)	->       Optional[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      self.get_image_processor()
						__UpperCamelCase					:	Tuple             =      self.get_tokenizer()

						__UpperCamelCase					:	Union[str, Any]             =      CLIPSegProcessor(tokenizer=lowerCAmelCase					,     image_processor=lowerCAmelCase	)

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

						__UpperCamelCase					:	str             =      processor.batch_decode(lowerCAmelCase	)
						__UpperCamelCase					:	List[Any]             =      tokenizer.batch_decode(lowerCAmelCase	)

						self.assertListEqual(lowerCAmelCase					,     lowerCAmelCase	)












import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402


a__		     =     get_tests_dir('''fixtures/dummy-config.json''')


class    SCREAMING_SNAKE_CASE_				(       unittest.TestCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						str	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[Any]             =      0






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       List[str]:







						"""simple docstring"""







						self.assertIsNotNone(transformers.models.auto.__spec__	)
						self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""	)	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      AutoConfig.from_pretrained("""bert-base-uncased"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Any	)	->       Optional[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Tuple	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      AutoConfig.for_model("""roberta"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       List[Any]:







						"""simple docstring"""







						with tempfile.TemporaryDirectory() as tmp_dir:
											# This model name contains bert and roberta, but roberta ends up being picked.
											__UpperCamelCase					:	Union[str, Any]             =      os.path.join(lowerCAmelCase					,     """fake-roberta"""	)
											os.makedirs(lowerCAmelCase					,     exist_ok=lowerCAmelCase	)
											with open(os.path.join(lowerCAmelCase					,     """config.json"""	)					,     """w"""	) as f:
																f.write(json.dumps({}	)	)
											__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
											self.assertEqual(type(lowerCAmelCase	)					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       str:







						"""simple docstring"""







						try:
											AutoConfig.register("""custom"""					,     lowerCAmelCase	)
											# Wrong model type will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""model"""					,     lowerCAmelCase	)
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""bert"""					,     lowerCAmelCase	)

											# Now that the config is registered, it can be used as any other config with the auto-API
											__UpperCamelCase					:	Optional[Any]             =      CustomConfig()
											with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(lowerCAmelCase	)
																__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
																self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)

						finally:
											if "custom" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["custom"]






	def 					lowerCamelCase__     (					self		:						int	)	->       Optional[Any]:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """bert-base is not a local folder and is not a valid model identifier"""	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""bert-base"""	)






	def 					lowerCamelCase__     (					self		:						Dict	)	->       Any:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""	):
											__UpperCamelCase					:	Optional[Any]             =      AutoConfig.from_pretrained(lowerCAmelCase					,     revision="""aaaaaa"""	)






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







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""					,     ):
											__UpperCamelCase					:	Union[str, Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Optional[int]:







						"""simple docstring"""







						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""	)
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)

						__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

						# Test config can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(lowerCAmelCase	)
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained(lowerCAmelCase					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(reloaded_config.__class__.__name__					,     """NewModelConfig"""	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       str:







						"""simple docstring"""







						class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



							"""simple docstring"""


							__magic_name__   :       int      	=		'new-model'

						try:
											AutoConfig.register("""new-model"""					,     lowerCAmelCase	)
											# If remote code is not set, the default is to use local
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfigLocal"""	)

											# If remote code is disabled, we load the local one.
											__UpperCamelCase					:	Any             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfigLocal"""	)

											# If remote is enabled, we load from the Hub
											__UpperCamelCase					:	List[Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

						finally:
											if "new-model" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["new-model"]






