




"""simple docstring"""




import subprocess
import sys

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






class 					__magic_name__							( UpperCAmelCase__       ):






				'''simple docstring'''





				@require_torch
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							# 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       =      """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

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

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





							"""simple docstring"""
							# python one-liner segments
							# this must be loaded before socket.socket is monkey-patched
							lowerCamelCase       =      """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

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

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      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      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							# 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       =      """
from transformers import BertConfig, BertModel, BertTokenizer
        """

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

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

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      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       =      [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       =      """1"""
							lowerCamelCase       =      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      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      """
from transformers import pipeline
        """
							lowerCamelCase       =      """
mname = \"hf-internal-testing/tiny-random-bert\"
pipe = pipeline(model=mname)
        """

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





							"""simple docstring"""
							lowerCamelCase       =      """
from transformers import AutoModel
        """
							lowerCamelCase       =      """
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       =      [sys.executable, """-c""", """\n""".join([load, run]				)]

							# should succeed
							lowerCamelCase       =      self.get_env()
							lowerCamelCase       =      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       =      """1"""
							lowerCamelCase       =      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"""




# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.



def 					a__				(       snake_case__       )		->      Dict:
			lowerCamelCase       =      [False] * len(snake_case__       )
			lowerCamelCase       =      [-1] * len(snake_case__       )

			def dfs(snake_case__ ,				snake_case__       ):
						lowerCamelCase       =      True
						lowerCamelCase       =      c
						for u in graph[v]:
									if not visited[u]:
												dfs(snake_case__ ,				1 - c       )

			for i in range(len(snake_case__       )       ):
						if not visited[i]:
									dfs(snake_case__ ,				0       )

			for i in range(len(snake_case__       )       ):
						for j in graph[i]:
									if color[i] == color[j]:
												return False

			return True


# Adjacency list of graph
lowerCAmelCase							:    str  							=							{0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []}


print(check_bipartite_dfs(graph))



