"""simple docstring"""




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch
class 		__A							(				SCREAMING_SNAKE_CASE_	):






       @require_torch
       def 					__A						(	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
         _lowerCAmelCase		: int						    =  """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

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

         # should succeed
         _lowerCAmelCase		: Union[str, Any]						    =  self.get_env()
         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         _lowerCAmelCase		: Dict						    =  """1"""
         _lowerCAmelCase		: 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 					__A						(	self						):
         # python one-liner segments
         # this must be loaded before socket.socket is monkey-patched
         _lowerCAmelCase		: Dict						    =  """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

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

         # should succeed
         _lowerCAmelCase		: List[Any]						    =  self.get_env()
         _lowerCAmelCase		: List[str]						    =  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 					__A						(	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
         _lowerCAmelCase		: str						    =  """
from transformers import BertConfig, BertModel, BertTokenizer
        """

         _lowerCAmelCase		: int						    =  """
mname = \"hf-internal-testing/tiny-random-bert-sharded\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print(\"success\")
        """

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

         # should succeed
         _lowerCAmelCase		: Any						    =  self.get_env()
         _lowerCAmelCase		: int						    =  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		: Optional[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
         _lowerCAmelCase		: Any						    =  """1"""
         _lowerCAmelCase		: List[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 					__A						(	self						):
         _lowerCAmelCase		: Optional[Any]						    =  """
from transformers import pipeline
        """
         _lowerCAmelCase		: List[Any]						    =  """
mname = \"hf-internal-testing/tiny-random-bert\"
pipe = pipeline(model=mname)
        """

         _lowerCAmelCase		: List[Any]						    =  """
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")
socket.socket = offline_socket
        """
         _lowerCAmelCase		: List[Any]						    =  self.get_env()
         _lowerCAmelCase		: Union[str, Any]						    =  """1"""
         _lowerCAmelCase		: Tuple						    =  [sys.executable, """-c""", """\n""".join([load, mock, run]						)]
         _lowerCAmelCase		: Tuple						    =  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 					__A						(	self						):
         _lowerCAmelCase		: List[str]						    =  """
from transformers import AutoModel
        """
         _lowerCAmelCase		: Optional[Any]						    =  """
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		: Tuple						    =  [sys.executable, """-c""", """\n""".join([load, run]						)]

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

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_a   :	int 					=     {
    'configuration_altclip': [
        'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'AltCLIPConfig',
        'AltCLIPTextConfig',
        'AltCLIPVisionConfig',
    ],
    'processing_altclip': ['AltCLIPProcessor'],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _a   :	Dict 					=     [
         'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
         'AltCLIPPreTrainedModel',
         'AltCLIPModel',
         'AltCLIPTextModel',
         'AltCLIPVisionModel',
     ]


if TYPE_CHECKING:
     from .configuration_altclip import (
         ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
         AltCLIPConfig,
         AltCLIPTextConfig,
         AltCLIPVisionConfig,
     )
     from .processing_altclip import AltCLIPProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_altclip import (
              ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
              AltCLIPModel,
              AltCLIPPreTrainedModel,
              AltCLIPTextModel,
              AltCLIPVisionModel,
          )


else:
     import sys

     _a   :	List[Any] 					=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




