

import subprocess
import sys

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

class __UpperCamelCase     (				_a		):






					'''simple docstring'''



					@require_torch
					def    _UpperCAmelCase    (				self						):
						# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
						# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
						# while running an external program

						# python one-liner segments

						# this must be loaded before socket.socket is monkey-patched
						UpperCAmelCase__:	Dict									= "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

						UpperCAmelCase__:	Union[str, Any]									= "\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 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__:	str									= "hf-internal-testing/tiny-random-bert"
						BertConfig.from_pretrained(lowerCamelCase__						)
						BertModel.from_pretrained(lowerCamelCase__						)
						BertTokenizer.from_pretrained(lowerCamelCase__						)
						pipeline(task="fill-mask"				,      model=lowerCamelCase__						)

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

						# should succeed
						UpperCAmelCase__:	List[str]									= self.get_env()
						# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
						UpperCAmelCase__:	Any									= "1"
						UpperCAmelCase__:	Optional[Any]									= subprocess.run(lowerCamelCase__				,      env=lowerCamelCase__				,      check=lowerCamelCase__				,      capture_output=lowerCamelCase__						)
						self.assertEqual(result.returncode				,      0				,      result.stderr						)
						self.assertIn("success"				,      result.stdout.decode()						)





					@require_torch
					def    _UpperCAmelCase    (				self						):
						# python one-liner segments
						# this must be loaded before socket.socket is monkey-patched
						UpperCAmelCase__:	Union[str, Any]									= "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

						UpperCAmelCase__:	Union[str, Any]									= "\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__:	Optional[Any]									= "\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__:	List[Any]									= "hf-internal-testing/tiny-random-bert"
						BertConfig.from_pretrained(lowerCamelCase__						)
						BertModel.from_pretrained(lowerCamelCase__						)
						BertTokenizer.from_pretrained(lowerCamelCase__						)
						pipeline(task="fill-mask"				,      model=lowerCamelCase__						)

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

						# should succeed
						UpperCAmelCase__:	Dict									= self.get_env()
						UpperCAmelCase__:	Optional[int]									= subprocess.run(lowerCamelCase__				,      env=lowerCamelCase__				,      check=lowerCamelCase__				,      capture_output=lowerCamelCase__						)
						self.assertEqual(result.returncode				,      0				,      result.stderr						)
						self.assertIn("success"				,      result.stdout.decode()						)





					@require_torch
					def    _UpperCAmelCase    (				self						):
						# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
						# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
						# while running an external program

						# python one-liner segments

						# this must be loaded before socket.socket is monkey-patched
						UpperCAmelCase__:	Union[str, Any]									= "\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

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

						UpperCAmelCase__:	Optional[int]									= "\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__:	Union[str, Any]									= [sys.executable, "-c", "\n".join([load, run]						)]

						# should succeed
						UpperCAmelCase__:	Optional[int]									= self.get_env()
						UpperCAmelCase__:	List[Any]									= subprocess.run(lowerCamelCase__				,      env=lowerCamelCase__				,      check=lowerCamelCase__				,      capture_output=lowerCamelCase__						)
						self.assertEqual(result.returncode				,      0				,      result.stderr						)
						self.assertIn("success"				,      result.stdout.decode()						)

						# next emulate no network
						UpperCAmelCase__:	Dict									= [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__:	List[str]									= subprocess.run(lowerCamelCase__				,      env=lowerCamelCase__				,      check=lowerCamelCase__				,      capture_output=lowerCamelCase__						)
						self.assertEqual(result.returncode				,      0				,      result.stderr						)
						self.assertIn("success"				,      result.stdout.decode()						)





					@require_torch
					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	int									= "\nfrom transformers import pipeline\n        "
						UpperCAmelCase__:	Dict									= "\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

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

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


_lowerCAmelCase  :      int			 ="""
# Installazione di Transformers
! pip install transformers datasets
# Per installare dalla fonte invece dell'ultima versione rilasciata, commenta il comando sopra e
# rimuovi la modalità commento al comando seguente.
# ! pip install git+https://github.com/huggingface/transformers.git
"""

_lowerCAmelCase  :      List[str]			 =[{"""type""": """code""", """content""": INSTALL_CONTENT}]
_lowerCAmelCase  :      int			 ={
    """{processor_class}""": """FakeProcessorClass""",
    """{model_class}""": """FakeModelClass""",
    """{object_class}""": """FakeObjectClass""",
}