





"""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						: Optional[int]     	=			"""src/transformers"""
a						: str     	=			"""docs/source/en"""
a						: Optional[int]     	=			"""."""






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Optional[int]    ,       _lowercase						:    Optional[Any]    ,       _lowercase						:    Optional[int]					) ->List[str]:






	'''simple docstring'''






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

	a      :		List[str]	=				start_index
	while not lines[end_index].startswith(lowerCAmelCase_					):
		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						: Optional[Any]     	=			"""Model|Encoder|Decoder|ForConditionalGeneration"""
# Regexes that match TF/Flax/PT model names.
a						: Optional[int]     	=			re.compile(R'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')
a						: Dict     	=			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						: Optional[Any]     	=			re.compile(R'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')


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






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Any					) ->Optional[int]:






	'''simple docstring'''






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






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    List[Any]    ,       _lowercase						:    Optional[Any]					) ->Optional[int]:






	'''simple docstring'''






	a      :		Tuple	=				2 if text == '''✅''' or text == '''❌''' else len(lowerCAmelCase_					)
	a      :		int	=				(width - text_length) // 2
	a      :		Union[str, Any]	=				width - text_length - left_indent
	return " " * left_indent + text + " " * right_indent






def       _SCREAMING_SNAKE_CASE		(	) ->str:






	'''simple docstring'''






	a      :		Any	=				transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
	a      :		Dict	=				{
	    name: config_maping_names[code]
	    for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
	    if code in config_maping_names
	}
	a      :		List[Any]	=				{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.
	a      :		Tuple	=				collections.defaultdict(lowerCAmelCase_					)
	a      :		List[str]	=				collections.defaultdict(lowerCAmelCase_					)
	a      :		Optional[Any]	=				collections.defaultdict(lowerCAmelCase_					)
	a      :		Optional[int]	=				collections.defaultdict(lowerCAmelCase_					)
	a      :		List[str]	=				collections.defaultdict(lowerCAmelCase_					)

	# Let's lookup through all transformers object (once).
	for attr_name in dir(lowerCAmelCase_					):
		a      :		Optional[int]	=				None
		if attr_name.endswith("Tokenizer"					):
			a      :		Union[str, Any]	=				slow_tokenizers
			a      :		List[str]	=				attr_name[:-9]
		elif attr_name.endswith("TokenizerFast"					):
			a      :		int	=				fast_tokenizers
			a      :		Union[str, Any]	=				attr_name[:-13]
		elif _re_tf_models.match(lowerCAmelCase_					) is not None:
			a      :		Tuple	=				tf_models
			a      :		str	=				_re_tf_models.match(lowerCAmelCase_					).groups()[0]
		elif _re_flax_models.match(lowerCAmelCase_					) is not None:
			a      :		Tuple	=				flax_models
			a      :		Union[str, Any]	=				_re_flax_models.match(lowerCAmelCase_					).groups()[0]
		elif _re_pt_models.match(lowerCAmelCase_					) is not None:
			a      :		Any	=				pt_models
			a      :		List[Any]	=				_re_pt_models.match(lowerCAmelCase_					).groups()[0]

		if lookup_dict is not None:
			while len(lowerCAmelCase_					) > 0:
				if attr_name in model_name_to_prefix.values():
					a      :		Union[str, Any]	=				True
					break
				# Try again after removing the last word in the name
				a      :		Any	=				''''''.join(camel_case_split(lowerCAmelCase_					)[:-1]					)

    # Let's build that table!
	a      :		int	=				list(model_name_to_config.keys()					)
	model_names.sort(key=str.lower					)
	a      :		Tuple	=				['''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).
	a      :		Union[str, Any]	=				[len(lowerCAmelCase_					) + 2 for c in columns]
	a      :		Optional[Any]	=				max([len(lowerCAmelCase_					) for name in model_names]					) + 2

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

	a      :		List[str]	=				{True: '''✅''', False: '''❌'''}
	for name in model_names:
		a      :		List[Any]	=				model_name_to_prefix[name]
		a      :		Union[str, Any]	=				[
		    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(lowerCAmelCase_    ,       lowerCAmelCase_					) for l, w in zip(lowerCAmelCase_    ,       lowerCAmelCase_					)]					) + "|\n"
	return table






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    int=False					) ->Tuple:






	'''simple docstring'''






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

	if current_table != new_table:
		if overwrite:
			with open(os.path.join(lowerCAmelCase_    ,       "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						: List[Any]     	=			argparse.ArgumentParser()
		parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''')
		a						: Tuple     	=			parser.parse_args()

		check_model_table(args.fix_and_overwrite)






import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
						from PIL import Image
else:







						class  snake_case__:







												'''simple docstring'''




												@staticmethod
												def   lowercase_       ( *__lowercase      ,  **__lowercase							)    ->			Union[str, Any]:
																pass







@is_pipeline_test
@require_vision
@require_torch
class  snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__					:     Union[str, Any]    =							MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase							)    ->			List[str]:
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline(
										    '''zero-shot-object-detection'''      ,  model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

										lowerCAmelCase_       :					str			 = [
										    {
										        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										        '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										    }
										]
										return object_detector, examples




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase							)    ->			str:
										lowerCAmelCase_       :					Tuple			 = object_detector(examples[0]      ,  threshold=0.0							)

										lowerCAmelCase_       :					Dict			 = len(__lowercase							)
										self.assertGreater(__lowercase      ,  0							)
										self.assertEqual(
										    __lowercase      ,  [
										        {
										            '''score''': ANY(__lowercase							),
										            '''label''': ANY(__lowercase							),
										            '''box''': {'''xmin''': ANY(__lowercase							), '''ymin''': ANY(__lowercase							), '''xmax''': ANY(__lowercase							), '''ymax''': ANY(__lowercase							)},
										        }
										        for i in range(__lowercase							)
										    ]      ,  )




						@require_tf
						@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
						def   lowercase_       ( self							)    ->			List[str]:
										pass




						@require_torch
						def   lowercase_       ( self							)    ->			int:
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline(
										    '''zero-shot-object-detection'''      ,  model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

										lowerCAmelCase_       :					Union[str, Any]			 = object_detector(
										    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  threshold=0.64      ,  )

										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										        {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
										        {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										    ]      ,  )

										lowerCAmelCase_       :					Union[str, Any]			 = object_detector(
										    [
										        {
										            '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        }
										    ]      ,  threshold=0.64      ,  )

										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        [
										            {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										            {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
										            {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										        ]
										    ]      ,  )




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Union[str, Any]:
										lowerCAmelCase_       :					Any			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Dict			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										        {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										        {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										        {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										    ]      ,  )

										lowerCAmelCase_       :					Tuple			 = object_detector(
										    [
										        {
										            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        },
										        {
										            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        },
										    ]      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        [
										            {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										            {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										            {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										            {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										            {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										        ],
										        [
										            {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										            {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										            {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										            {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										            {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										        ],
										    ]      ,  )




						@require_tf
						@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
						def   lowercase_       ( self							)    ->			List[str]:
										pass




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Optional[int]:
										lowerCAmelCase_       :					Any			 = 0.2
										lowerCAmelCase_       :					List[Any]			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Optional[Any]			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  threshold=__lowercase      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										        {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										    ]      ,  )




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Optional[int]:
										lowerCAmelCase_       :					Dict			 = 2
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Optional[Any]			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  top_k=__lowercase      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										    ]      ,  )