




"""simple docstring"""







import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
a      =							{'LayoutLMv2Config', 'LayoutLMv3Config'}






@is_pipeline_test
class       SCREAMING_SNAKE_CASE__					(					unittest.TestCase					):
						_a    						=						MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
						_a    						=						TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

						if model_mapping is not None:
												_a    						=						{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
						if tf_model_mapping is not None:
												_a    						=						{
												    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
												}


						def   __lowercase (							self				:			Optional[Any]						,						lowerCAmelCase				:			Any						,						lowerCAmelCase				:			Any						,						lowerCAmelCase				:			Optional[int]					):
										lowerCAmelCase  =				ZeroShotClassificationPipeline(
										    model=lowerCAmelCase						,						tokenizer=lowerCAmelCase						,						candidate_labels=["""polics""", """health"""]					)
										return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]


						def   __lowercase (							self				:			Tuple						,						lowerCAmelCase				:			Optional[Any]						,						lowerCAmelCase				:			str					):
										lowerCAmelCase  =				classifier("""Who are you voting for in 2020?"""						,						candidate_labels="""politics"""					)
										self.assertEqual(lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					)]}					)

										# No kwarg
										lowerCAmelCase  =				classifier("""Who are you voting for in 2020?"""						,						["""politics"""]					)
										self.assertEqual(lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					)]}					)

										lowerCAmelCase  =				classifier("""Who are you voting for in 2020?"""						,						candidate_labels=["""politics"""]					)
										self.assertEqual(lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					)]}					)

										lowerCAmelCase  =				classifier("""Who are you voting for in 2020?"""						,						candidate_labels="""politics, public health"""					)
										self.assertEqual(
										    lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)]}					)
										self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]					)					)						,						1.0					)

										lowerCAmelCase  =				classifier("""Who are you voting for in 2020?"""						,						candidate_labels=["""politics""", """public health"""]					)
										self.assertEqual(
										    lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)]}					)
										self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]					)					)						,						1.0					)

										lowerCAmelCase  =				classifier(
										    """Who are you voting for in 2020?"""						,						candidate_labels="""politics"""						,						hypothesis_template="""This text is about {}"""					)
										self.assertEqual(lowerCAmelCase						,						{"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					)]}					)

										# https://github.com/huggingface/transformers/issues/13846
										lowerCAmelCase  =				classifier(["""I am happy"""]						,						["""positive""", """negative"""]					)
										self.assertEqual(
										    lowerCAmelCase						,						[
										        {"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)]}
										        for i in range(1					)
										    ]						,						)
										lowerCAmelCase  =				classifier(["""I am happy""", """I am sad"""]						,						["""positive""", """negative"""]					)
										self.assertEqual(
										    lowerCAmelCase						,						[
										        {"""sequence""": ANY(lowerCAmelCase					), """labels""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)], """scores""": [ANY(lowerCAmelCase					), ANY(lowerCAmelCase					)]}
										        for i in range(2					)
										    ]						,						)

										with self.assertRaises(lowerCAmelCase					):
														classifier(""""""						,						candidate_labels="""politics"""					)

										with self.assertRaises(lowerCAmelCase					):
														classifier(lowerCAmelCase						,						candidate_labels="""politics"""					)

										with self.assertRaises(lowerCAmelCase					):
														classifier("""Who are you voting for in 2020?"""						,						candidate_labels=""""""					)

										with self.assertRaises(lowerCAmelCase					):
														classifier("""Who are you voting for in 2020?"""						,						candidate_labels=lowerCAmelCase					)

										with self.assertRaises(lowerCAmelCase					):
														classifier(
														    """Who are you voting for in 2020?"""						,						candidate_labels="""politics"""						,						hypothesis_template="""Not formatting template"""						,						)

										with self.assertRaises(lowerCAmelCase					):
														classifier(
														    """Who are you voting for in 2020?"""						,						candidate_labels="""politics"""						,						hypothesis_template=lowerCAmelCase						,						)

										self.run_entailment_id(lowerCAmelCase					)


						def   __lowercase (							self				:			List[Any]						,						lowerCAmelCase				:			Pipeline					):
										lowerCAmelCase  =				zero_shot_classifier.model.config
										lowerCAmelCase  =				config.labelaid
										lowerCAmelCase  =				zero_shot_classifier.entailment_id

										lowerCAmelCase  =				{"""LABEL_0""": 0, """LABEL_1""": 1, """LABEL_2""": 2}
										self.assertEqual(zero_shot_classifier.entailment_id						,						-1					)

										lowerCAmelCase  =				{"""entailment""": 0, """neutral""": 1, """contradiction""": 2}
										self.assertEqual(zero_shot_classifier.entailment_id						,						0					)

										lowerCAmelCase  =				{"""ENTAIL""": 0, """NON-ENTAIL""": 1}
										self.assertEqual(zero_shot_classifier.entailment_id						,						0					)

										lowerCAmelCase  =				{"""ENTAIL""": 2, """NEUTRAL""": 1, """CONTR""": 0}
										self.assertEqual(zero_shot_classifier.entailment_id						,						2					)

										lowerCAmelCase  =				original_labelaid
										self.assertEqual(lowerCAmelCase						,						zero_shot_classifier.entailment_id					)


						@require_torch
						def   __lowercase (							self				:			str					):
										lowerCAmelCase  =				pipeline(
										    """zero-shot-classification"""						,						model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""						,						framework="""pt"""						,						)
										# There was a regression in 4.10 for this
										# Adding a test so we don't make the mistake again.
										# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
										zero_shot_classifier(
										    """Who are you voting for in 2020?""" * 100						,						candidate_labels=["""politics""", """public health""", """science"""]					)


						@require_torch
						def   __lowercase (							self				:			Dict					):
										lowerCAmelCase  =				pipeline(
										    """zero-shot-classification"""						,						model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""						,						framework="""pt"""						,						)
										lowerCAmelCase  =				zero_shot_classifier(
										    """Who are you voting for in 2020?"""						,						candidate_labels=["""politics""", """public health""", """science"""]					)

										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": """Who are you voting for in 2020?""",
										        """labels""": ["""science""", """public health""", """politics"""],
										        """scores""": [0.333, 0.333, 0.333],
										    }						,						)


						@require_tf
						def   __lowercase (							self				:			int					):
										lowerCAmelCase  =				pipeline(
										    """zero-shot-classification"""						,						model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""						,						framework="""tf"""						,						)
										lowerCAmelCase  =				zero_shot_classifier(
										    """Who are you voting for in 2020?"""						,						candidate_labels=["""politics""", """public health""", """science"""]					)

										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": """Who are you voting for in 2020?""",
										        """labels""": ["""science""", """public health""", """politics"""],
										        """scores""": [0.333, 0.333, 0.333],
										    }						,						)


						@slow
						@require_torch
						def   __lowercase (							self				:			Optional[Any]					):
										lowerCAmelCase  =				pipeline("""zero-shot-classification"""						,						model="""roberta-large-mnli"""						,						framework="""pt"""					)
										lowerCAmelCase  =				zero_shot_classifier(
										    """Who are you voting for in 2020?"""						,						candidate_labels=["""politics""", """public health""", """science"""]					)

										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": """Who are you voting for in 2020?""",
										        """labels""": ["""politics""", """public health""", """science"""],
										        """scores""": [0.976, 0.015, 0.009],
										    }						,						)
										lowerCAmelCase  =				zero_shot_classifier(
										    """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
										    """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
										    """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
										    """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
										    """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
										    """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
										    """ English-to-German translation task, improving over the existing best results, including ensembles by"""
										    """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
										    """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
										    """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
										    """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
										    """ large and limited training data."""						,						candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]						,						multi_label=lowerCAmelCase						,						)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": (
										            """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
										            """ networks in an encoder-decoder configuration. The best performing models also connect the"""
										            """ encoder and decoder through an attention mechanism. We propose a new simple network"""
										            """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
										            """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
										            """ superior in quality while being more parallelizable and requiring significantly less time to"""
										            """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
										            """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
										            """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
										            """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
										            """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
										            """ other tasks by applying it successfully to English constituency parsing both with large and"""
										            """ limited training data."""
										        ),
										        """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
										        """scores""": [0.817, 0.713, 0.018, 0.018],
										    }						,						)




						@slow
						@require_tf
						def   __lowercase (							self				:			str					):
										lowerCAmelCase  =				pipeline("""zero-shot-classification"""						,						model="""roberta-large-mnli"""						,						framework="""tf"""					)
										lowerCAmelCase  =				zero_shot_classifier(
										    """Who are you voting for in 2020?"""						,						candidate_labels=["""politics""", """public health""", """science"""]					)

										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": """Who are you voting for in 2020?""",
										        """labels""": ["""politics""", """public health""", """science"""],
										        """scores""": [0.976, 0.015, 0.009],
										    }						,						)
										lowerCAmelCase  =				zero_shot_classifier(
										    """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
										    """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
										    """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
										    """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
										    """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
										    """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
										    """ English-to-German translation task, improving over the existing best results, including ensembles by"""
										    """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
										    """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
										    """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
										    """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
										    """ large and limited training data."""						,						candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]						,						multi_label=lowerCAmelCase						,						)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase					)						,						{
										        """sequence""": (
										            """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
										            """ networks in an encoder-decoder configuration. The best performing models also connect the"""
										            """ encoder and decoder through an attention mechanism. We propose a new simple network"""
										            """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
										            """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
										            """ superior in quality while being more parallelizable and requiring significantly less time to"""
										            """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
										            """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
										            """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
										            """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
										            """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
										            """ other tasks by applying it successfully to English constituency parsing both with large and"""
										            """ limited training data."""
										        ),
										        """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
										        """scores""": [0.817, 0.713, 0.018, 0.018],
										    }						,						)











"""simple docstring"""







import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
a      =							'src/transformers'
a      =							'docs/source/en'
a      =							'.'





def 							lowercase					(snake_case__   : int      ,       snake_case__   : Optional[int]      ,       snake_case__   : Tuple )     ->  Any:


				'''simple docstring'''






				with open(snake_case__      ,       """r"""      ,       encoding="""utf-8"""      ,       newline="""\n""" ) as f:
								lowerCAmelCase  =				f.readlines()
				# Find the start prompt.
				lowerCAmelCase  =				0
				while not lines[start_index].startswith(snake_case__ ):
								start_index += 1
				start_index += 1

				lowerCAmelCase  =				start_index
				while not lines[end_index].startswith(snake_case__ ):
								end_index += 1
				end_index -= 1

				while len(lines[start_index] ) <= 1:
								start_index += 1
				while len(lines[end_index] ) <= 1:
								end_index -= 1
				end_index += 1
				return "".join(lines[start_index:end_index] ), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
a      =							'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
a      =							re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
a      =							re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
a      =							re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the transformers module imported is the one in the repo.
a      =							direct_transformers_import(TRANSFORMERS_PATH)





def 							lowercase					(snake_case__   : int )     ->  Any:


				'''simple docstring'''






				lowerCAmelCase  =				re.finditer(""".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)"""      ,       snake_case__ )
				return [m.group(0 ) for m in matches]





def 							lowercase					(snake_case__   : str      ,       snake_case__   : Tuple )     ->  int:


				'''simple docstring'''






				lowerCAmelCase  =				2 if text == """✅""" or text == """❌""" else len(snake_case__ )
				lowerCAmelCase  =				(width - text_length) // 2
				lowerCAmelCase  =				width - text_length - left_indent
				return " " * left_indent + text + " " * right_indent





def 							lowercase					()     ->  str:


				'''simple docstring'''






				lowerCAmelCase  =				transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
				lowerCAmelCase  =				{
				    name: config_maping_names[code]
				    for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
				    if code in config_maping_names
				}
				lowerCAmelCase  =				{name: config.replace("""Config"""      ,       """""" ) for name, config in model_name_to_config.items()}

				# Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
				lowerCAmelCase  =				collections.defaultdict(snake_case__ )
				lowerCAmelCase  =				collections.defaultdict(snake_case__ )
				lowerCAmelCase  =				collections.defaultdict(snake_case__ )
				lowerCAmelCase  =				collections.defaultdict(snake_case__ )
				lowerCAmelCase  =				collections.defaultdict(snake_case__ )

				# Let's lookup through all transformers object (once).
				for attr_name in dir(snake_case__ ):
								lowerCAmelCase  =				None
								if attr_name.endswith("""Tokenizer""" ):
												lowerCAmelCase  =				slow_tokenizers
												lowerCAmelCase  =				attr_name[:-9]
								elif attr_name.endswith("""TokenizerFast""" ):
												lowerCAmelCase  =				fast_tokenizers
												lowerCAmelCase  =				attr_name[:-13]
								elif _re_tf_models.match(snake_case__ ) is not None:
												lowerCAmelCase  =				tf_models
												lowerCAmelCase  =				_re_tf_models.match(snake_case__ ).groups()[0]
								elif _re_flax_models.match(snake_case__ ) is not None:
												lowerCAmelCase  =				flax_models
												lowerCAmelCase  =				_re_flax_models.match(snake_case__ ).groups()[0]
								elif _re_pt_models.match(snake_case__ ) is not None:
												lowerCAmelCase  =				pt_models
												lowerCAmelCase  =				_re_pt_models.match(snake_case__ ).groups()[0]

								if lookup_dict is not None:
												while len(snake_case__ ) > 0:
																if attr_name in model_name_to_prefix.values():
																				lowerCAmelCase  =				True
																				break
																# Try again after removing the last word in the name
																lowerCAmelCase  =				"""""".join(camel_case_split(snake_case__ )[:-1] )

    # Let's build that table!
				lowerCAmelCase  =				list(model_name_to_config.keys() )
				model_names.sort(key=str.lower )
				lowerCAmelCase  =				["""Model""", """Tokenizer slow""", """Tokenizer fast""", """PyTorch support""", """TensorFlow support""", """Flax Support"""]
				# We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
				lowerCAmelCase  =				[len(snake_case__ ) + 2 for c in columns]
				lowerCAmelCase  =				max([len(snake_case__ ) for name in model_names] ) + 2

				# Build the table per se
				lowerCAmelCase  =				"""|""" + """|""".join([_center_text(snake_case__      ,       snake_case__ ) for c, w in zip(snake_case__      ,       snake_case__ )] ) + """|\n"""
				# Use ":-----:" format to center-aligned table cell texts
				table += "|" + "|".join([""":""" + """-""" * (w - 2) + """:""" for w in widths] ) + "|\n"

				lowerCAmelCase  =				{True: """✅""", False: """❌"""}
				for name in model_names:
								lowerCAmelCase  =				model_name_to_prefix[name]
								lowerCAmelCase  =				[
								    name,
								    check[slow_tokenizers[prefix]],
								    check[fast_tokenizers[prefix]],
								    check[pt_models[prefix]],
								    check[tf_models[prefix]],
								    check[flax_models[prefix]],
								]
								table += "|" + "|".join([_center_text(snake_case__      ,       snake_case__ ) for l, w in zip(snake_case__      ,       snake_case__ )] ) + "|\n"
				return table





def 							lowercase					(snake_case__   : Dict=False )     ->  Any:


				'''simple docstring'''






				lowerCAmelCase ,						lowerCAmelCase ,						lowerCAmelCase ,						lowerCAmelCase  =				_find_text_in_file(
				    filename=os.path.join(snake_case__      ,       """index.md""" )      ,       start_prompt="""<!--This table is updated automatically from the auto modules"""      ,       end_prompt="""<!-- End table-->"""      ,       )
				lowerCAmelCase  =				get_model_table_from_auto_modules()

				if current_table != new_table:
								if overwrite:
												with open(os.path.join(snake_case__      ,       """index.md""" )      ,       """w"""      ,       encoding="""utf-8"""      ,       newline="""\n""" ) as f:
																f.writelines(lines[:start_index] + [new_table] + lines[end_index:] )
								else:
												raise ValueError(
												    """The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this.""" )


if __name__ == "__main__":
		a      =							argparse.ArgumentParser()
		parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
		a      =							parser.parse_args()

		check_model_table(args.fix_and_overwrite)





