






'''simple docstring'''







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


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





class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Any:
							a_		:  Any											=      0






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Tuple:
							self.assertIsNotNone(transformers.models.auto.__spec__					)
							self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			List[str]:
							a_		:  Optional[int]											=      AutoConfig.from_pretrained('''bert-base-uncased'''					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Any:
							a_		:  str											=      AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Dict:
							a_		:  Optional[Any]											=      AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Union[str, Any]:
							a_		:  str											=      AutoConfig.for_model('''roberta'''					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Any:
							try:
											AutoConfig.register('''custom'''  ,						__SCREAMING_SNAKE_CASE					)
											# Wrong model type will raise an error
											with self.assertRaises(__SCREAMING_SNAKE_CASE					):
															AutoConfig.register('''model'''  ,						__SCREAMING_SNAKE_CASE					)
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(__SCREAMING_SNAKE_CASE					):
															AutoConfig.register('''bert'''  ,						__SCREAMING_SNAKE_CASE					)

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

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			List[str]:
							with self.assertRaisesRegex(
							    __SCREAMING_SNAKE_CASE  ,						'''bert-base is not a local folder and is not a valid model identifier'''					):
											a_		:  List[Any]											=      AutoConfig.from_pretrained('''bert-base'''					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Any:
							with self.assertRaisesRegex(
							    __SCREAMING_SNAKE_CASE  ,						r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
											a_		:  Optional[int]											=      AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  ,						revision='''aaaaaa'''					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[str]:
							with self.assertRaisesRegex(
							    __SCREAMING_SNAKE_CASE  ,						'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''  ,						):
											a_		:  Tuple											=      AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Any:
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(__SCREAMING_SNAKE_CASE					):
											a_		:  Union[str, Any]											=      AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''					)
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(__SCREAMING_SNAKE_CASE					):
											a_		:  Tuple											=      AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,						trust_remote_code=__SCREAMING_SNAKE_CASE					)

							a_		:  List[Any]											=      AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,						trust_remote_code=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(config.__class__.__name__  ,						'''NewModelConfig'''					)

							# Test config can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(__SCREAMING_SNAKE_CASE					)
											a_		:  Dict											=      AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  ,						trust_remote_code=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(reloaded_config.__class__.__name__  ,						'''NewModelConfig'''					)







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





							class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
										snake_case__  =				"new-model"

							try:
											AutoConfig.register('''new-model'''  ,						__SCREAMING_SNAKE_CASE					)
											# If remote code is not set, the default is to use local
											a_		:  Optional[Any]											=      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.
											a_		:  str											=      AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''  ,						trust_remote_code=__SCREAMING_SNAKE_CASE					)
											self.assertEqual(config.__class__.__name__  ,						'''NewModelConfigLocal'''					)

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

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












'''simple docstring'''







import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Image
from .base import TaskTemplate





@dataclass(frozen=SCREAMING_SNAKE_CASE_       )
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				field(default="image-classification"	,   metadata={"include_in_asdict_even_if_is_default": True}       )
			snake_case__  =				Features({"image": Image()}       )
			snake_case__  =				Features({"labels": ClassLabel}       )
			snake_case__  =				"image"
			snake_case__  =				"labels"






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]					)			->			Any:
							if self.label_column not in features:
											raise ValueError(f'Column {self.label_column} is not present in features.'					)
							if not isinstance(features[self.label_column]  ,						__SCREAMING_SNAKE_CASE					):
											raise ValueError(f'Column {self.label_column} is not a ClassLabel.'					)
							a_		:  Optional[int]											=      copy.deepcopy(self					)
							a_		:  int											=      self.label_schema.copy()
							a_		:  Tuple											=      features[self.label_column]
							a_		:  str											=      label_schema
							return task_template







			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Dict[str, str]:
							return {
							    self.image_column: "image",
							    self.label_column: "labels",
							}




