

import subprocess
import sys

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







class 					__lowercase  (						_SCREAMING_SNAKE_CASE				):





							"""simple docstring"""

							@require_torch
							def       snake_case			(					self						)		->       str:
														# 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
														A      :					str      		=					'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

														A      :					Union[str, Any]      		=					'''
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")
        '''

														A      :					Dict      		=					'''
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
														A      :					int      		=					'''hf-internal-testing/tiny-random-bert'''
														BertConfig.from_pretrained(__UpperCAmelCase						)
														BertModel.from_pretrained(__UpperCAmelCase						)
														BertTokenizer.from_pretrained(__UpperCAmelCase						)
														pipeline(task='''fill-mask'''    ,						model=__UpperCAmelCase						)

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

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




							@require_torch
							def       snake_case			(					self						)		->       List[Any]:
														# python one-liner segments
														# this must be loaded before socket.socket is monkey-patched
														A      :					Tuple      		=					'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

														A      :					Any      		=					'''
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")
        '''

														A      :					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
														A      :					Any      		=					'''hf-internal-testing/tiny-random-bert'''
														BertConfig.from_pretrained(__UpperCAmelCase						)
														BertModel.from_pretrained(__UpperCAmelCase						)
														BertTokenizer.from_pretrained(__UpperCAmelCase						)
														pipeline(task='''fill-mask'''    ,						model=__UpperCAmelCase						)

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

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




							@require_torch
							def       snake_case			(					self						)		->       str:
														# 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
														A      :					str      		=					'''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

														A      :					Union[str, Any]      		=					'''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

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

														# should succeed
														A      :					Union[str, Any]      		=					self.get_env()
														A      :					int      		=					subprocess.run(__UpperCAmelCase    ,						env=__UpperCAmelCase    ,						check=__UpperCAmelCase    ,						capture_output=__UpperCAmelCase						)
														self.assertEqual(result.returncode    ,						0    ,						result.stderr						)
														self.assertIn('''success'''    ,						result.stdout.decode()						)

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




							@require_torch
							def       snake_case			(					self						)		->       Optional[int]:
														A      :					int      		=					'''
from transformers import pipeline
        '''
														A      :					Any      		=					'''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

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

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









import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar







class 					__lowercase  (						unittest.TestCase				):





							"""simple docstring"""

							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Dict      		=					logging.get_logger()

														# the current default level is logging.WARNING
														A      :					List[Any]      		=					logging.get_verbosity()

														logging.set_verbosity_error()
														self.assertEqual(logger.getEffectiveLevel()    ,						logging.get_verbosity()						)

														logging.set_verbosity_warning()
														self.assertEqual(logger.getEffectiveLevel()    ,						logging.get_verbosity()						)

														logging.set_verbosity_info()
														self.assertEqual(logger.getEffectiveLevel()    ,						logging.get_verbosity()						)

														logging.set_verbosity_debug()
														self.assertEqual(logger.getEffectiveLevel()    ,						logging.get_verbosity()						)

														# restore to the original level
														logging.set_verbosity(__UpperCAmelCase						)




							def       snake_case			(					self						)		->       str:
														A      :					Any      		=					logging.get_verbosity()

														A      :					Optional[Any]      		=					logging.get_logger('''transformers.models.bart.tokenization_bart'''						)
														A      :					Tuple      		=					'''Testing 1, 2, 3'''

														# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
														if level_origin <= logging.WARNING:
																					with CaptureLogger(__UpperCAmelCase						) as cl:
																												logger.warning(__UpperCAmelCase						)
																					self.assertEqual(cl.out    ,						msg + '''\n'''						)

														# this is setting the level for all of `transformers.*` loggers
														logging.set_verbosity_error()

														# should not be able to log warnings
														with CaptureLogger(__UpperCAmelCase						) as cl:
																					logger.warning(__UpperCAmelCase						)
														self.assertEqual(cl.out    ,						''''''						)

														# should be able to log warnings again
														logging.set_verbosity_warning()
														with CaptureLogger(__UpperCAmelCase						) as cl:
																					logger.warning(__UpperCAmelCase						)
														self.assertEqual(cl.out    ,						msg + '''\n'''						)

														# restore to the original level
														logging.set_verbosity(__UpperCAmelCase						)




							@mockenv(TRANSFORMERS_VERBOSITY='''error'''						)
							def       snake_case			(					self						)		->       Optional[int]:
														# reset for the env var to take effect, next time some logger call is made
														transformers.utils.logging._reset_library_root_logger()
														# this action activates the env var
														A      :					int      		=					logging.get_logger('''transformers.models.bart.tokenization_bart'''						)

														A      :					Any      		=					os.getenv('''TRANSFORMERS_VERBOSITY'''    ,						__UpperCAmelCase						)
														A      :					List[str]      		=					logging.log_levels[env_level_str]

														A      :					Optional[int]      		=					logging.get_verbosity()
														self.assertEqual(
														    __UpperCAmelCase    ,						__UpperCAmelCase    ,						f'TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}'    ,						)

														# restore to the original level
														A      :					str      		=					''''''
														transformers.utils.logging._reset_library_root_logger()




							@mockenv(TRANSFORMERS_VERBOSITY='''super-error'''						)
							def       snake_case			(					self						)		->       Optional[int]:
														# reset for the env var to take effect, next time some logger call is made
														transformers.utils.logging._reset_library_root_logger()
														A      :					str      		=					logging.logging.getLogger()
														with CaptureLogger(__UpperCAmelCase						) as cl:
																					# this action activates the env var
																					logging.get_logger('''transformers.models.bart.tokenization_bart'''						)
														self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error'''    ,						cl.out						)

														# no need to restore as nothing was changed






							def       snake_case			(					self						)		->       Optional[int]:
														# testing `logger.warning_advice()`
														transformers.utils.logging._reset_library_root_logger()

														A      :					Union[str, Any]      		=					logging.get_logger('''transformers.models.bart.tokenization_bart'''						)
														A      :					Optional[int]      		=					'''Testing 1, 2, 3'''

														with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1'''						):
																					# nothing should be logged as env var disables this method
																					with CaptureLogger(__UpperCAmelCase						) as cl:
																												logger.warning_advice(__UpperCAmelCase						)
																					self.assertEqual(cl.out    ,						''''''						)

														with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=''''''						):
																					# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
																					with CaptureLogger(__UpperCAmelCase						) as cl:
																												logger.warning_advice(__UpperCAmelCase						)
																					self.assertEqual(cl.out    ,						msg + '''\n'''						)






def        snake_case__		(    ):
							disable_progress_bar()
							assert are_progress_bars_disabled()

							enable_progress_bar()
							assert not are_progress_bars_disabled()






