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 __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
						__a		 =       '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

						__a		 =       '''
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		 =       '''
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		 =       '''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		 =       [sys.executable, '''-c''', '''\n'''.join([load, run, mock]   )]

						# should succeed
						__a		 =       self.get_env()
						# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
						__a		 =       '''1'''
						__a		 =       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 __SCREAMING_SNAKE_CASE							(						self   )			->   str:
						__a		 =       '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

						__a		 =       '''
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		 =       '''
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		 =       '''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		 =       [sys.executable, '''-c''', '''\n'''.join([load, run, mock]   )]

						# should succeed
						__a		 =       self.get_env()
						__a		 =       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 __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
						__a		 =       '''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

						__a		 =       '''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

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

						# should succeed
						__a		 =       self.get_env()
						__a		 =       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		 =       [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		 =       '''1'''
						__a		 =       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 __SCREAMING_SNAKE_CASE							(						self   )			->   Dict:
						__a		 =       '''
from transformers import pipeline
        '''
						__a		 =       '''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

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

						# should succeed
						__a		 =       self.get_env()
						__a		 =       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		 =       '''1'''
						__a		 =       subprocess.run(UpperCamelCase__				,						env=UpperCamelCase__				,						check=UpperCamelCase__				,						capture_output=UpperCamelCase__   )
						self.assertEqual(result.returncode				,						0				,						result.stderr   )
						self.assertIn('success'				,						result.stdout.decode()   )





def 							lowerCAmelCase(     __lowerCamelCase						):
					if not all(char in '01' for char in bin_string						):
										raise ValueError('Non-binary value was passed to the function'						)
					if not bin_string:
										raise ValueError('Empty string was passed to the function'						)
					__a		 =       ''
					while len(__lowerCamelCase						) % 3 != 0:
										__a		 =       '0' + bin_string
					__a		 =       [
					    bin_string[index : index + 3]
					    for index in range(len(__lowerCamelCase						)						)
					    if index % 3 == 0
					]
					for bin_group in bin_string_in_3_list:
										__a		 =       0
										for index, val in enumerate(__lowerCamelCase						):
															oct_val += int(2 ** (2 - index) * int(__lowerCamelCase						)						)
										oct_string += str(__lowerCamelCase						)
					return oct_string


if __name__ == "__main__":
	from doctest import testmod

	testmod()




