




"""simple docstring"""




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch






class 					__magic_name__							( __A       ):






				'''simple docstring'''





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

							lowerCamelCase       =      '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

							lowerCamelCase       =      '''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

							# Force fetching the files so that we can use the cache
							lowerCamelCase       =      '''hf-internal-testing/tiny-random-bert'''
							BertConfig.from_pretrained(_a				)
							BertModel.from_pretrained(_a				)
							BertTokenizer.from_pretrained(_a				)
							pipeline(task="""fill-mask"""	,				model=_a				)

							# baseline - just load from_pretrained with normal network
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, run, mock]				)]

							# should succeed
							lowerCamelCase       =      self.get_env()
							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							lowerCamelCase       =      '''1'''
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

							lowerCamelCase       =      '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

							lowerCamelCase       =      '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

							# Force fetching the files so that we can use the cache
							lowerCamelCase       =      '''hf-internal-testing/tiny-random-bert'''
							BertConfig.from_pretrained(_a				)
							BertModel.from_pretrained(_a				)
							BertTokenizer.from_pretrained(_a				)
							pipeline(task="""fill-mask"""	,				model=_a				)

							# baseline - just load from_pretrained with normal network
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, run, mock]				)]

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      '''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

							lowerCamelCase       =      '''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

							lowerCamelCase       =      '''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

							# baseline - just load from_pretrained with normal network
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, run]				)]

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)

							# next emulate no network
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, mock, run]				)]

							# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
							# env["TRANSFORMERS_OFFLINE"] = "0"
							# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
							# self.assertEqual(result.returncode, 1, result.stderr)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							lowerCamelCase       =      '''1'''
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      '''
from transformers import pipeline
        '''
							lowerCamelCase       =      '''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

							lowerCamelCase       =      '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      '''1'''
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, mock, run]				)]
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				1	,				result.stderr				)
							self.assertIn(
							    """You cannot infer task automatically within `pipeline` when using offline mode"""	,				result.stderr.decode().replace("""\n"""	,				""""""				)	,				)





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      '''
from transformers import AutoModel
        '''
							lowerCamelCase       =      '''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

							# baseline - just load from_pretrained with normal network
							lowerCamelCase       =      [sys.executable, '''-c''', '''\n'''.join([load, run]				)]

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							lowerCamelCase       =      '''1'''
							lowerCamelCase       =      subprocess.run(_a	,				env=_a	,				check=_a	,				capture_output=_a				)
							self.assertEqual(result.returncode	,				0	,				result.stderr				)
							self.assertIn("""success"""	,				result.stdout.decode()				)









"""simple docstring"""




from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase							:    List[str]  							=							{
    """configuration_autoformer""": [
        """AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """AutoformerConfig""",
    ],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase							:    Tuple  							=							[
					    """AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """AutoformerForPrediction""",
					    """AutoformerModel""",
					    """AutoformerPreTrainedModel""",
					]


if TYPE_CHECKING:
					from .configuration_autoformer import (
					    AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    AutoformerConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_autoformer import (
										    AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    AutoformerForPrediction,
										    AutoformerModel,
										    AutoformerPreTrainedModel,
										)

else:
					import sys

					lowerCAmelCase							:    str  							=							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



