





'''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						: Union[str, Any]		=   get_tests_dir('fixtures/dummy-config.json')







class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Optional[Any]:
    _UpperCamelCase       			=0


  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						Dict:
    self.assertIsNotNone(transformers.models.auto.__spec__  )
    self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''  )  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Dict:
    _UpperCamelCase       			=AutoConfig.from_pretrained('''bert-base-uncased'''  )
    self.assertIsInstance(UpperCamelCase__							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					int  )     ->						List[Any]:
    _UpperCamelCase       			=AutoConfig.from_pretrained(UpperCamelCase__  )
    self.assertIsInstance(UpperCamelCase__							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Tuple:
    _UpperCamelCase       			=AutoConfig.from_pretrained(UpperCamelCase__  )
    self.assertIsInstance(UpperCamelCase__							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					List[str]  )     ->						List[str]:
    _UpperCamelCase       			=AutoConfig.for_model('''roberta'''  )
    self.assertIsInstance(UpperCamelCase__							,	UpperCamelCase__  )


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


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

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

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


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Optional[int]:
    with self.assertRaisesRegex(
        UpperCamelCase__							,	'''bert-base is not a local folder and is not a valid model identifier'''  ):
      _UpperCamelCase       			=AutoConfig.from_pretrained('''bert-base'''  )


  def   UpperCamelCase__      (				self    :					int  )     ->						Tuple:
    with self.assertRaisesRegex(
        UpperCamelCase__							,	R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''  ):
      _UpperCamelCase       			=AutoConfig.from_pretrained(UpperCamelCase__							,	revision='''aaaaaa'''  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Union[str, Any]:
    with self.assertRaisesRegex(
        UpperCamelCase__							,	'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''							,	):
      _UpperCamelCase       			=AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''  )


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

    _UpperCamelCase       			=AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,	trust_remote_code=UpperCamelCase__  )
    self.assertEqual(config.__class__.__name__							,	'''NewModelConfig'''  )

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




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







    class 		UpperCAmelCase   (				lowercase_):


      """simple docstring"""







      lowerCAmelCase_					=				"""new-model"""

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

      # If remote is enabled, we load from the Hub
      _UpperCamelCase       			=AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,	trust_remote_code=UpperCamelCase__  )
      self.assertEqual(config.__class__.__name__							,	'''NewModelConfig'''  )

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













'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


__lowerCamelCase						: str		=   {
    'configuration_speech_to_text': ['SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Speech2TextConfig'],
    'processing_speech_to_text': ['Speech2TextProcessor'],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: List[str]		=   ['Speech2TextTokenizer']

try:
      if not is_speech_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: str		=   ['Speech2TextFeatureExtractor']

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: Optional[Any]		=   [
          'TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'TFSpeech2TextForConditionalGeneration',
          'TFSpeech2TextModel',
          'TFSpeech2TextPreTrainedModel',
      ]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: Union[str, Any]		=   [
          'SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'Speech2TextForConditionalGeneration',
          'Speech2TextModel',
          'Speech2TextPreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
      from .processing_speech_to_text import SpeechaTextProcessor

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_speech_to_text import SpeechaTextTokenizer

      try:
            if not is_speech_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_speech_to_text import (
                TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFSpeechaTextForConditionalGeneration,
                TFSpeechaTextModel,
                TFSpeechaTextPreTrainedModel,
            )

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_speech_to_text import (
                SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
                SpeechaTextForConditionalGeneration,
                SpeechaTextModel,
                SpeechaTextPreTrainedModel,
            )

else:
      import sys

      __lowerCamelCase						: Optional[int]		=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






