

import subprocess
import sys

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

class 				a  (	UpperCAmelCase				):


						@require_torch
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[Any] 		=    "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

										_UpperCAmelCase       :		Tuple 		=    "\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        "

										_UpperCAmelCase       :		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
										_UpperCAmelCase       :		Dict 		=    "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
										_UpperCAmelCase       :		Optional[int] 		=    [sys.executable, "-c", "\n".join([load, run, mock]	)]

										# should succeed
										_UpperCAmelCase       :		int 		=    self.get_env()
										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										_UpperCAmelCase       :		int 		=    "1"
										_UpperCAmelCase       :		Any 		=    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        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[Any] 		=    "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

										_UpperCAmelCase       :		int 		=    "\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        "

										_UpperCAmelCase       :		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
										_UpperCAmelCase       :		int 		=    "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
										_UpperCAmelCase       :		Tuple 		=    [sys.executable, "-c", "\n".join([load, run, mock]	)]

										# should succeed
										_UpperCAmelCase       :		Optional[Any] 		=    self.get_env()
										_UpperCAmelCase       :		Optional[Any] 		=    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        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		Dict 		=    "\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

										_UpperCAmelCase       :		Dict 		=    "\nmname = \"hf-internal-testing/tiny-random-bert-sharded\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint(\"success\")\n        "

										_UpperCAmelCase       :		str 		=    "\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
										_UpperCAmelCase       :		List[Any] 		=    [sys.executable, "-c", "\n".join([load, run]	)]

										# should succeed
										_UpperCAmelCase       :		Optional[int] 		=    self.get_env()
										_UpperCAmelCase       :		Optional[Any] 		=    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
										_UpperCAmelCase       :		Any 		=    [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
										_UpperCAmelCase       :		List[Any] 		=    "1"
										_UpperCAmelCase       :		Optional[int] 		=    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        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		Optional[Any] 		=    "\nfrom transformers import pipeline\n        "
										_UpperCAmelCase       :		Union[str, Any] 		=    "\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

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







										'''simple docstring'''


										_UpperCAmelCase       :		int 		=    "\nfrom transformers import AutoModel\n        "
										_UpperCAmelCase       :		List[str] 		=    "\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
										_UpperCAmelCase       :		Optional[Any] 		=    [sys.executable, "-c", "\n".join([load, run]	)]

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





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE_        =      {
    'configuration_nllb_moe': [
        'NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'NllbMoeConfig',
    ]
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE_        =      [
					    'NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'NllbMoeForConditionalGeneration',
					    'NllbMoeModel',
					    'NllbMoePreTrainedModel',
					    'NllbMoeTop2Router',
					    'NllbMoeSparseMLP',
					]


if TYPE_CHECKING:
					from .configuration_nllb_moe import (
					    NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    NllbMoeConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_nllb_moe import (
										    NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
										    NllbMoeForConditionalGeneration,
										    NllbMoeModel,
										    NllbMoePreTrainedModel,
										    NllbMoeSparseMLP,
										    NllbMoeTopaRouter,
										)


else:
					import sys

					SCREAMING_SNAKE_CASE_        =      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


