
import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
lowerCamelCase_						     =					'''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
lowerCamelCase_						     =					importlib.util.spec_from_file_location(
    '''transformers''',
    os.path.join(PATH_TO_TRANSFORMERS, '''__init__.py'''),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
lowerCamelCase_						     =					spec.loader.load_module()

lowerCamelCase_						     =					transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
lowerCamelCase_						     =					re.compile('''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


lowerCamelCase_						     =					{
    '''CLIPConfigMixin''',
    '''DecisionTransformerConfigMixin''',
    '''EncoderDecoderConfigMixin''',
    '''RagConfigMixin''',
    '''SpeechEncoderDecoderConfigMixin''',
    '''VisionEncoderDecoderConfigMixin''',
    '''VisionTextDualEncoderConfigMixin''',
}






def 						__magic_name__			(   ):



					'''simple docstring'''




					UpperCamelCase__				=    []

					for config_class in list(CONFIG_MAPPING.values()						):
										UpperCamelCase__				=    False

										# source code of `config_class`
										UpperCamelCase__				=    inspect.getsource(__a						)
										UpperCamelCase__				=    _re_checkpoint.findall(__a						)

										for checkpoint in checkpoints:
															# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
															# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
															UpperCamelCase__		,	UpperCamelCase__				=    checkpoint

															# verify the checkpoint name corresponds to the checkpoint link
															UpperCamelCase__				=    f"https://huggingface.co/{ckpt_name}"
															if ckpt_link == ckpt_link_from_name:
																				UpperCamelCase__				=    True
																				break

										UpperCamelCase__				=    config_class.__name__
										if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
															configs_without_checkpoint.append(__a						)

					if len(__a						) > 0:
										UpperCamelCase__				=    """\n""".join(sorted(__a						)						)
										raise ValueError(f"The following configurations don't contain any valid checkpoint:\n{message}"						)


if __name__ == "__main__":
				check_config_docstrings_have_checkpoints()







import argparse
from collections import defaultdict

import yaml


lowerCamelCase_						     =					'''docs/source/en/_toctree.yml'''






def 						__magic_name__			(   __a      :	Union[str, Any]						):



     '''simple docstring'''




     UpperCamelCase__				=    defaultdict(__a						)
     for doc in model_doc:
          counts[doc["local"]] += 1
     UpperCamelCase__				=    [key for key, value in counts.items() if value > 1]

     UpperCamelCase__				=    []
     for duplicate_key in duplicates:
          UpperCamelCase__				=    list({doc["""title"""] for doc in model_doc if doc["""local"""] == duplicate_key}						)
          if len(__a						) > 1:
               raise ValueError(
                   f"{duplicate_key} is present several times in the documentation table of content at "
                   """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """
                   """others."""						)
          # Only add this once
          new_doc.append({"""local""": duplicate_key, """title""": titles[0]}						)

     # Add none duplicate-keys
     new_doc.extend([doc for doc in model_doc if counts[doc["""local"""]] == 1]						)

     # Sort
     return sorted(__a	,		key=lambda __a						: s["title"].lower()						)






def 						__magic_name__			(   __a      :	int=False						):



     '''simple docstring'''




     with open(__a	,		encoding="""utf-8"""						) as f:
          UpperCamelCase__				=    yaml.safe_load(f.read()						)

     # Get to the API doc
     UpperCamelCase__				=    0
     while content[api_idx]["title"] != "API":
          api_idx += 1
     UpperCamelCase__				=    content[api_idx]["""sections"""]

     # Then to the model doc
     UpperCamelCase__				=    0
     while api_doc[model_idx]["title"] != "Models":
          model_idx += 1

     UpperCamelCase__				=    api_doc[model_idx]["""sections"""]

     UpperCamelCase__				=    [(idx, section) for idx, section in enumerate(__a						) if """sections""" in section]
     UpperCamelCase__				=    False
     for idx, modality_doc in modalities_docs:
          UpperCamelCase__				=    modality_doc["""sections"""]
          UpperCamelCase__				=    clean_model_doc_toc(__a						)

          if old_modality_doc != new_modality_doc:
               UpperCamelCase__				=    True
               if overwrite:
                    UpperCamelCase__				=    new_modality_doc

     if diff:
          if overwrite:
               UpperCamelCase__				=    model_doc
               UpperCamelCase__				=    api_doc
               with open(__a	,		"""w"""	,		encoding="""utf-8"""						) as f:
                    f.write(yaml.dump(__a	,		allow_unicode=__a						)						)
          else:
               raise ValueError(
                   """The model doc part of the table of content is not properly sorted, run `make style` to fix this."""						)


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

    check_model_doc(args.fix_and_overwrite)





