






import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


__lowerCamelCase   :     Dict 		= get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
__lowerCamelCase   :     List[Any] 		= get_tests_dir("""fixtures/vocab.json""")
__lowerCamelCase   :     Optional[Any] 		= get_tests_dir("""fixtures""")



class 				SCREAMING_SNAKE_CASE__		(							unittest.TestCase		):





					"""simple docstring"""






					a_      =						["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]



					def 			_lowercase	(   self	:    int			):
						snake_case__					: List[str]  =    0



					def 			_lowercase	(   self	:    List[str]			):
						snake_case__					: Union[str, Any]  =    AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h"			)
						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    Optional[Any]			):



						with tempfile.TemporaryDirectory() as tmpdirname:
							snake_case__					: Dict  =    WavaVecaConfig()
							snake_case__					: Optional[int]  =    AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h"			)

							# save in new folder
							model_config.save_pretrained(a_			)
							processor.save_pretrained(a_			)

							snake_case__					: Optional[Any]  =    AutoProcessor.from_pretrained(a_			)

						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    str			):



						with tempfile.TemporaryDirectory() as tmpdirname:
							# copy relevant files
							copyfile(a_				,						os.path.join(a_				,						a_			)			)
							copyfile(a_				,						os.path.join(a_				,						"vocab.json"			)			)

							snake_case__					: int  =    AutoProcessor.from_pretrained(a_			)

						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    List[str]			):



						with tempfile.TemporaryDirectory() as tmpdirname:
							snake_case__					: Dict  =    WavaVecaFeatureExtractor()
							snake_case__					: Any  =    AutoTokenizer.from_pretrained("facebook/wav2vec2-base-960h"			)

							snake_case__					: List[Any]  =    WavaVecaProcessor(a_				,						a_			)

							# save in new folder
							processor.save_pretrained(a_			)

							# drop `processor_class` in tokenizer
							with open(os.path.join(a_				,						a_			)				,						"r"			) as f:
								snake_case__					: Optional[Any]  =    json.load(a_			)
								config_dict.pop("processor_class"			)

							with open(os.path.join(a_				,						a_			)				,						"w"			) as f:
								f.write(json.dumps(a_			)			)

							snake_case__					: List[str]  =    AutoProcessor.from_pretrained(a_			)

						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    Dict			):



						with tempfile.TemporaryDirectory() as tmpdirname:
							snake_case__					: Optional[int]  =    WavaVecaFeatureExtractor()
							snake_case__					: Optional[int]  =    AutoTokenizer.from_pretrained("facebook/wav2vec2-base-960h"			)

							snake_case__					: int  =    WavaVecaProcessor(a_				,						a_			)

							# save in new folder
							processor.save_pretrained(a_			)

							# drop `processor_class` in feature extractor
							with open(os.path.join(a_				,						a_			)				,						"r"			) as f:
								snake_case__					: List[str]  =    json.load(a_			)
								config_dict.pop("processor_class"			)

							with open(os.path.join(a_				,						a_			)				,						"w"			) as f:
								f.write(json.dumps(a_			)			)

							snake_case__					: str  =    AutoProcessor.from_pretrained(a_			)

						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    Optional[Any]			):



						with tempfile.TemporaryDirectory() as tmpdirname:
							snake_case__					: str  =    WavaVecaConfig(processor_class="Wav2Vec2Processor"			)
							model_config.save_pretrained(a_			)
							# copy relevant files
							copyfile(a_				,						os.path.join(a_				,						"vocab.json"			)			)
							# create emtpy sample processor
							with open(os.path.join(a_				,						a_			)				,						"w"			) as f:
								f.write("{}"			)

							snake_case__					: Optional[Any]  =    AutoProcessor.from_pretrained(a_			)

						self.assertIsInstance(a_				,						a_			)



					def 			_lowercase	(   self	:    Any			):



						with self.assertRaises(a_			):
							snake_case__					: Optional[Any]  =    AutoProcessor.from_pretrained("hf-internal-testing/test_dynamic_processor"			)
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(a_			):
							snake_case__					: Optional[Any]  =    AutoProcessor.from_pretrained(
							    "hf-internal-testing/test_dynamic_processor"				,						trust_remote_code=a_			)

						snake_case__					: Dict  =    AutoProcessor.from_pretrained("hf-internal-testing/test_dynamic_processor"				,						trust_remote_code=a_			)
						self.assertTrue(processor.special_attribute_present			)
						self.assertEqual(processor.__class__.__name__				,						"NewProcessor"			)

						snake_case__					: List[str]  =    processor.feature_extractor
						self.assertTrue(feature_extractor.special_attribute_present			)
						self.assertEqual(feature_extractor.__class__.__name__				,						"NewFeatureExtractor"			)

						snake_case__					: Tuple  =    processor.tokenizer
						self.assertTrue(tokenizer.special_attribute_present			)
						if is_tokenizers_available():
							self.assertEqual(tokenizer.__class__.__name__				,						"NewTokenizerFast"			)

							# Test we can also load the slow version
							snake_case__					: str  =    AutoProcessor.from_pretrained(
							    "hf-internal-testing/test_dynamic_processor"				,						trust_remote_code=a_				,						use_fast=a_			)
							snake_case__					: int  =    new_processor.tokenizer
							self.assertTrue(new_tokenizer.special_attribute_present			)
							self.assertEqual(new_tokenizer.__class__.__name__				,						"NewTokenizer"			)
						else:
							self.assertEqual(tokenizer.__class__.__name__				,						"NewTokenizer"			)



					def 			_lowercase	(   self	:    int			):



						try:
							AutoConfig.register("custom"				,						a_			)
							AutoFeatureExtractor.register(a_				,						a_			)
							AutoTokenizer.register(a_				,						slow_tokenizer_class=a_			)
							AutoProcessor.register(a_				,						a_			)
							# Trying to register something existing in the Transformers library will raise an error
							with self.assertRaises(a_			):
								AutoProcessor.register(a_				,						a_			)

							# Now that the config is registered, it can be used as any other config with the auto-API
							snake_case__					: Union[str, Any]  =    CustomFeatureExtractor.from_pretrained(a_			)

							with tempfile.TemporaryDirectory() as tmp_dir:
								snake_case__					: Tuple  =    os.path.join(a_				,						"vocab.txt"			)
								with open(a_				,						"w"				,						encoding="utf-8"			) as vocab_writer:
									vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]			)			)
								snake_case__					: List[str]  =    CustomTokenizer(a_			)

							snake_case__					: Optional[int]  =    CustomProcessor(a_				,						a_			)

							with tempfile.TemporaryDirectory() as tmp_dir:
								processor.save_pretrained(a_			)
								snake_case__					: Any  =    AutoProcessor.from_pretrained(a_			)
								self.assertIsInstance(a_				,						a_			)

						finally:
							if "custom" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
								del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
							if CustomConfig in TOKENIZER_MAPPING._extra_content:
								del TOKENIZER_MAPPING._extra_content[CustomConfig]
							if CustomConfig in PROCESSOR_MAPPING._extra_content:
								del PROCESSOR_MAPPING._extra_content[CustomConfig]



					def 			_lowercase	(   self	:    List[str]			):



						class 				SCREAMING_SNAKE_CASE__		(							_UpperCamelCase		):





											"""simple docstring"""






											a_      =						False



						class 				SCREAMING_SNAKE_CASE__		(							_UpperCamelCase		):





											"""simple docstring"""






											a_      =						False



						class 				SCREAMING_SNAKE_CASE__		(							_UpperCamelCase		):





											"""simple docstring"""






											a_      =						"AutoFeatureExtractor"
											a_      =						"AutoTokenizer"
											a_      =						False

						try:
							AutoConfig.register("custom"				,						a_			)
							AutoFeatureExtractor.register(a_				,						a_			)
							AutoTokenizer.register(a_				,						slow_tokenizer_class=a_			)
							AutoProcessor.register(a_				,						a_			)
							# If remote code is not set, the default is to use local classes.
							snake_case__					: int  =    AutoProcessor.from_pretrained("hf-internal-testing/test_dynamic_processor"			)
							self.assertEqual(processor.__class__.__name__				,						"NewProcessor"			)
							self.assertFalse(processor.special_attribute_present			)
							self.assertFalse(processor.feature_extractor.special_attribute_present			)
							self.assertFalse(processor.tokenizer.special_attribute_present			)

							# If remote code is disabled, we load the local ones.
							snake_case__					: Dict  =    AutoProcessor.from_pretrained(
							    "hf-internal-testing/test_dynamic_processor"				,						trust_remote_code=a_			)
							self.assertEqual(processor.__class__.__name__				,						"NewProcessor"			)
							self.assertFalse(processor.special_attribute_present			)
							self.assertFalse(processor.feature_extractor.special_attribute_present			)
							self.assertFalse(processor.tokenizer.special_attribute_present			)

							# If remote is enabled, we load from the Hub.
							snake_case__					: str  =    AutoProcessor.from_pretrained(
							    "hf-internal-testing/test_dynamic_processor"				,						trust_remote_code=a_			)
							self.assertEqual(processor.__class__.__name__				,						"NewProcessor"			)
							self.assertTrue(processor.special_attribute_present			)
							self.assertTrue(processor.feature_extractor.special_attribute_present			)
							self.assertTrue(processor.tokenizer.special_attribute_present			)

						finally:
							if "custom" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
								del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
							if CustomConfig in TOKENIZER_MAPPING._extra_content:
								del TOKENIZER_MAPPING._extra_content[CustomConfig]
							if CustomConfig in PROCESSOR_MAPPING._extra_content:
								del PROCESSOR_MAPPING._extra_content[CustomConfig]



					def 			_lowercase	(   self	:    Optional[int]			):
						snake_case__					: str  =    AutoProcessor.from_pretrained("hf-internal-testing/tiny-random-bert"			)
						self.assertEqual(processor.__class__.__name__				,						"BertTokenizerFast"			)






					def 			_lowercase	(   self	:    List[Any]			):
						snake_case__					: Optional[Any]  =    AutoProcessor.from_pretrained("hf-internal-testing/tiny-random-convnext"			)
						self.assertEqual(processor.__class__.__name__				,						"ConvNextImageProcessor"			)



@is_staging_test
class 				SCREAMING_SNAKE_CASE__		(							unittest.TestCase		):





					"""simple docstring"""






					a_      =						["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]



					@classmethod
					def 			_lowercase	(   cls	:    Union[str, Any]			):
						snake_case__					: Union[str, Any]  =    TOKEN
						HfFolder.save_token(a_			)



					@classmethod
					def 			_lowercase	(   cls	:    List[Any]			):



						try:
							delete_repo(token=cls._token				,						repo_id="test-processor"			)
						except HTTPError:
							pass

						try:
							delete_repo(token=cls._token				,						repo_id="valid_org/test-processor-org"			)
						except HTTPError:
							pass

						try:
							delete_repo(token=cls._token				,						repo_id="test-dynamic-processor"			)
						except HTTPError:
							pass



					def 			_lowercase	(   self	:    List[str]			):
						snake_case__					: int  =    WavaVecaProcessor.from_pretrained(a_			)
						with tempfile.TemporaryDirectory() as tmp_dir:
							processor.save_pretrained(
							    os.path.join(a_				,						"test-processor"			)				,						push_to_hub=a_				,						use_auth_token=self._token			)

							snake_case__					: Optional[Any]  =    WavaVecaProcessor.from_pretrained(f'''{USER}/test-processor'''			)
							for k, v in processor.feature_extractor.__dict__.items():
								self.assertEqual(a_				,						getattr(new_processor.feature_extractor				,						a_			)			)
							self.assertDictEqual(new_processor.tokenizer.get_vocab()				,						processor.tokenizer.get_vocab()			)



					def 			_lowercase	(   self	:    Dict			):
						snake_case__					: int  =    WavaVecaProcessor.from_pretrained(a_			)

						with tempfile.TemporaryDirectory() as tmp_dir:
							processor.save_pretrained(
							    os.path.join(a_				,						"test-processor-org"			)				,						push_to_hub=a_				,						use_auth_token=self._token				,						organization="valid_org"				,						)

							snake_case__					: Optional[Any]  =    WavaVecaProcessor.from_pretrained("valid_org/test-processor-org"			)
							for k, v in processor.feature_extractor.__dict__.items():
								self.assertEqual(a_				,						getattr(new_processor.feature_extractor				,						a_			)			)
							self.assertDictEqual(new_processor.tokenizer.get_vocab()				,						processor.tokenizer.get_vocab()			)






					def 			_lowercase	(   self	:    Optional[int]			):
						CustomFeatureExtractor.register_for_auto_class()
						CustomTokenizer.register_for_auto_class()
						CustomProcessor.register_for_auto_class()

						snake_case__					: Optional[Any]  =    CustomFeatureExtractor.from_pretrained(a_			)

						with tempfile.TemporaryDirectory() as tmp_dir:
							snake_case__					: Any  =    os.path.join(a_				,						"vocab.txt"			)
							with open(a_				,						"w"				,						encoding="utf-8"			) as vocab_writer:
								vocab_writer.write("".join([x + "\n" for x in self.vocab_tokens]			)			)
							snake_case__					: str  =    CustomTokenizer(a_			)

						snake_case__					: List[Any]  =    CustomProcessor(a_				,						a_			)

						with tempfile.TemporaryDirectory() as tmp_dir:
							create_repo(f'''{USER}/test-dynamic-processor'''				,						token=self._token			)
							snake_case__					: str  =    Repository(a_				,						clone_from=f'''{USER}/test-dynamic-processor'''				,						token=self._token			)
							processor.save_pretrained(a_			)

							# This has added the proper auto_map field to the feature extractor config
							self.assertDictEqual(
							    processor.feature_extractor.auto_map				,						{
							        "AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor",
							        "AutoProcessor": "custom_processing.CustomProcessor",
							    }				,						)

							# This has added the proper auto_map field to the tokenizer config
							with open(os.path.join(a_				,						"tokenizer_config.json"			)			) as f:
								snake_case__					: Dict  =    json.load(a_			)
							self.assertDictEqual(
							    tokenizer_config["auto_map"]				,						{
							        "AutoTokenizer": ["custom_tokenization.CustomTokenizer", None],
							        "AutoProcessor": "custom_processing.CustomProcessor",
							    }				,						)

							# The code has been copied from fixtures
							self.assertTrue(os.path.isfile(os.path.join(a_				,						"custom_feature_extraction.py"			)			)			)
							self.assertTrue(os.path.isfile(os.path.join(a_				,						"custom_tokenization.py"			)			)			)
							self.assertTrue(os.path.isfile(os.path.join(a_				,						"custom_processing.py"			)			)			)

							repo.push_to_hub()

						snake_case__					: Optional[int]  =    AutoProcessor.from_pretrained(f'''{USER}/test-dynamic-processor'''				,						trust_remote_code=a_			)
						# Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
						self.assertEqual(new_processor.__class__.__name__				,						"CustomProcessor"			)










"""simple docstring"""






__UpperCAmelCase						=			[
    [0, 16, 13, 0, 0, 0],
    [0, 0, 10, 12, 0, 0],
    [0, 4, 0, 0, 14, 0],
    [0, 0, 9, 0, 0, 20],
    [0, 0, 0, 7, 0, 4],
    [0, 0, 0, 0, 0, 0],
]






def      lowercase__ (					lowerCAmelCase__   :			Dict							,			lowerCAmelCase__   :			Optional[int]							,			lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			Tuple			)    ->			Dict:



			'''simple docstring'''
			# Return True if there is node that has not iterated.
			a__  :	int		 =			[False] * len(lowerCAmelCase__			)
			a__  :	Optional[Any]		 =			[s]
			a__  :	Optional[int]		 =			True

			while queue:
						a__  :	Any		 =			queue.pop(0			)
						for ind in range(len(graph[u]			)			):
									if visited[ind] is False and graph[u][ind] > 0:
												queue.append(lowerCAmelCase__			)
												a__  :	Any		 =			True
												a__  :	Union[str, Any]		 =			u

			return visited[t]






def      lowercase__ (					lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			List[Any]							,			lowerCAmelCase__   :			Optional[Any]			)    ->			Any:



			'''simple docstring'''
			a__  :	Any		 =			[-1] * (len(lowerCAmelCase__			))
			a__  :	Any		 =			0
			a__  :	List[str]		 =			[]
			a__  :	Tuple		 =			[i[:] for i in graph]  # Record original cut, copy.
			while bfs(lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__			):
						a__  :	Any		 =			float("Inf"			)
						a__  :	Optional[int]		 =			sink

						while s != source:
									# Find the minimum value in select path
									a__  :	Union[str, Any]		 =			min(lowerCAmelCase__							,			graph[parent[s]][s]			)
									a__  :	List[str]		 =			parent[s]

						max_flow += path_flow
						a__  :	List[str]		 =			sink

						while v != source:
									a__  :	int		 =			parent[v]
									graph[u][v] -= path_flow
									graph[v][u] += path_flow
									a__  :	List[str]		 =			parent[v]

			for i in range(len(lowerCAmelCase__			)			):
						for j in range(len(graph[0]			)			):
									if graph[i][j] == 0 and temp[i][j] > 0:
												res.append((i, j)			)

			return res


if __name__ == "__main__":
		print(mincut(test_graph, source=0, sink=5))