





'''simple docstring'''






import subprocess
import sys

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







class 			lowerCamelCase					(						__UpperCAmelCase  ):


					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   Optional[Any]  					=		'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

											_snake_case:   str  					=		'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

											_snake_case:   List[str]  					=		'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

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

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

											# should succeed
											_snake_case:   str  					=		self.get_env()
											# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
											_snake_case:   int  					=		'1'
											_snake_case:   Any  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											self.assertEqual(result.returncode       ,     0       ,     result.stderr       )
											self.assertIn('success'       ,     result.stdout.decode()       )


					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   Dict  					=		'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

											_snake_case:   str  					=		'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

											_snake_case:   Dict  					=		'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

											# Force fetching the files so that we can use the cache
											_snake_case:   Optional[int]  					=		'hf-internal-testing/tiny-random-bert'
											BertConfig.from_pretrained(__snake_case       )
											BertModel.from_pretrained(__snake_case       )
											BertTokenizer.from_pretrained(__snake_case       )
											pipeline(task='fill-mask'       ,     model=__snake_case       )

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

											# should succeed
											_snake_case:   Dict  					=		self.get_env()
											_snake_case:   Any  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											self.assertEqual(result.returncode       ,     0       ,     result.stderr       )
											self.assertIn('success'       ,     result.stdout.decode()       )


					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ):





											'''simple docstring'''





											_snake_case:   Any  					=		'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

											_snake_case:   Union[str, Any]  					=		'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

											_snake_case:   Any  					=		'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

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

											# should succeed
											_snake_case:   List[str]  					=		self.get_env()
											_snake_case:   Dict  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											self.assertEqual(result.returncode       ,     0       ,     result.stderr       )
											self.assertIn('success'       ,     result.stdout.decode()       )

											# next emulate no network
											_snake_case:   int  					=		[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
											_snake_case:   List[Any]  					=		'1'
											_snake_case:   str  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											self.assertEqual(result.returncode       ,     0       ,     result.stderr       )
											self.assertIn('success'       ,     result.stdout.decode()       )


					@require_torch
					def    SCREAMING_SNAKE_CASE_		(					self	:  List[str]       ):





											'''simple docstring'''





											_snake_case:   Dict  					=		'\nfrom transformers import pipeline\n        '
											_snake_case:   int  					=		'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

											_snake_case:   Any  					=		'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
											_snake_case:   Optional[Any]  					=		self.get_env()
											_snake_case:   int  					=		'1'
											_snake_case:   List[Any]  					=		[sys.executable, '-c', '\n'.join([load, mock, run]       )]
											_snake_case:   Tuple  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											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    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ):





											'''simple docstring'''





											_snake_case:   int  					=		'\nfrom transformers import AutoModel\n        '
											_snake_case:   Union[str, Any]  					=		'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

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

											# should succeed
											_snake_case:   List[str]  					=		self.get_env()
											_snake_case:   List[str]  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											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
											_snake_case:   Optional[int]  					=		'1'
											_snake_case:   List[Any]  					=		subprocess.run(__snake_case       ,     env=__snake_case       ,     check=__snake_case       ,     capture_output=__snake_case       )
											self.assertEqual(result.returncode       ,     0       ,     result.stderr       )
											self.assertIn('success'       ,     result.stdout.decode()       )












'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


A   : Optional[Any]             =			{'configuration_deit': ['DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DeiTConfig', 'DeiTOnnxConfig']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A   : str             =			['DeiTFeatureExtractor']
		A   : Optional[int]             =			['DeiTImageProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A   : Optional[Any]             =			[
		    'DEIT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'DeiTForImageClassification',
		    'DeiTForImageClassificationWithTeacher',
		    'DeiTForMaskedImageModeling',
		    'DeiTModel',
		    'DeiTPreTrainedModel',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A   : int             =			[
		    'TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFDeiTForImageClassification',
		    'TFDeiTForImageClassificationWithTeacher',
		    'TFDeiTForMaskedImageModeling',
		    'TFDeiTModel',
		    'TFDeiTPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_deit import DeiTFeatureExtractor
				from .image_processing_deit import DeiTImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_deit import (
				    DEIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    DeiTForImageClassification,
				    DeiTForImageClassificationWithTeacher,
				    DeiTForMaskedImageModeling,
				    DeiTModel,
				    DeiTPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_deit import (
				    TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFDeiTForImageClassification,
				    TFDeiTForImageClassificationWithTeacher,
				    TFDeiTForMaskedImageModeling,
				    TFDeiTModel,
				    TFDeiTPreTrainedModel,
				)


else:
		import sys

		A   : Union[str, Any]             =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





