CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
hf_argparser.cpython-310.pyc185 linesDownload Raw Back to __pycache__
1o

2.�YifM�
@sFddlZddlZddlZddlZddlZddlmZmZmZddl	m3Z4ddlmZddlm
Z
ddlmZddlmZddlmZmZmZmZmZmZmZddlZed	e�Zed5e�Zdd�Zd
edeegeffdd�Z ddej!ej!dd�deeeeefdeededegefdee"dej#fdd�Z$Gdd�de�Z%dS)�N)�ArgumentDefaultsHelpFormatter�ArgumentParser�ArgumentTypeError)�Iterable)�copy)�Enum)�isclass��Path)�Any�Callable�Literal�NewType�Optional�Union�get_type_hints�	DataClass�
DataClassTypecCs>t|t�r|S|��dvrdS|��dvrdStd|�d���)N)�yes�true�t�y�1T)�no�false�f�n�0FzTruthy value expected: got zJ but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).)�6isinstance�bool�lowerr)�v�r"��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/hf_argparser.py�string_to_bool$s78�r$�choices�returncsdd�|D���fdd�S)aN9    Creates a mapping function from each choices string representation to the actual value. Used to support multiple10    value types for a single argument.11 12    Args:13        choices (list): List of choices.14 15    Returns:16        Callable[[str], Any]: Mapping function from string representation to actual value for each choice.17    cSsi|]}t|�|�qSr")�str)�.0�choicer"r"r#�18<dictcomp><sz-make_choice_type_function.<locals>.<dictcomp>cs��||�S�N)�get)�arg�Z
str_to_choicer"r#�<lambda>=sz+make_choice_type_function.<locals>.<lambda>r")r%r"r.r#�make_choice_type_function1sr0)�aliases�help�default�default_factory�metadatar1r2r3r4r5cKsD|duri}|dur||d<|dur||d<tjd|||d�|��S)a�Argument helper enabling a concise syntax to create dataclass fields for parsing with `HfArgumentParser`.19 20    Example comparing the use of `HfArg` and `dataclasses.field`:21    ```22    @dataclass23    class Args:24        regular_arg: str = dataclasses.field(default="Huggingface", metadata={"aliases": ["--example", "-e"], "help": "This syntax could be better!"})25        hf_arg: str = HfArg(default="Huggingface", aliases=["--example", "-e"], help="What a nice syntax!")26    ```27 28    Args:29        aliases (Union[str, list[str]], optional):30            Single string or list of strings of aliases to pass on to argparse, e.g. `aliases=["--example", "-e"]`.31            Defaults to None.32        help (str, optional): Help string to pass on to argparse that can be displayed with --help. Defaults to None.33        default (Any, optional):34            Default value for the argument. If not default or default_factory is specified, the argument is required.35            Defaults to dataclasses.MISSING.36        default_factory (Callable[[], Any], optional):37            The default_factory is a 0-argument function called to initialize a field's value. It is useful to provide38            default values for mutable types, e.g. lists: `default_factory=list`. Mutually exclusive with `default=`.39            Defaults to dataclasses.MISSING.40        metadata (dict, optional): Further metadata to pass on to `dataclasses.field`. Defaults to None.41 42    Returns:43        Field: A `dataclasses.Field` with the desired properties.44    Nr1r2)r5r3r4r")�dataclasses�field)r1r2r3r4r5�kwargsr"r"r#�HfArg@s$r9c	seZdZUdZeeed<ddeeeeeff�fdd�
Z	e45dedej
fdd	��Zd46efdd�Z		
			ddeedffdd�Zddeeefdedeedffdd�Z	
ddeeejfdedeedffdd�Z	
ddeeejfdedeedffdd�Z�ZS) �HfArgumentParsera47    This subclass of `argparse.ArgumentParser` uses type hints on dataclasses to generate arguments.48 49    The class is designed to play well with the native argparse. In particular, you can add more (non-dataclass backed)50    arguments to the parser after initialization and you'll get the output back after parsing as an additional51    namespace. Optional: To create sub argument groups use the `_argument_group_name` attribute in the dataclass.52 53    Args:54        dataclass_types (`DataClassType` or `Iterable[DataClassType]`, *optional*):55            Dataclass type, or list of dataclass types for which we will "fill" instances with the parsed args.56        kwargs (`dict[str, Any]`, *optional*):57            Passed to `argparse.ArgumentParser()` in the regular way.58    �dataclass_typesNcst|durg}nt|t�s|g}d|vrt|d<t�jdi|��t�|�r(|g}t|�|_|jD]}|�	|�q0dS)NZformatter_classr")59rrr�super�__init__r6�is_dataclass�listr;�_add_dataclass_arguments)�selfr;r8�dtype��	__class__r"r#r=�s60616263�zHfArgumentParser.__init__�parserr7cCs�d|j��g}d|jvr|�d|j�dd����|j��}t|jt�r(td��|�	dg�}t|t�r6|g}t64|jd|j�}|tusMtt
d�r�t|t
j�r�t|jjvrlt|jj�dksctd�|jjvrltd	|j�d65���td�|jjvr�|jjdtur�|jjdn|jjd|_t66|jd|j�}n$t|jjvr�td|jjd�r�|jjdn|jjd|_t67|jd|j�}i}|tus�t|jt�r�t|jt�r�|tur�|jj|d
<n68dd�|jD�|d
<t|d
�|d<|jtjur�|j|d<n�d|d<n�|jtu�s|jttk�r8t|�}t|d<|jtu�s|jdu�r7|jtju�r7|jtju�r(dn|j}||d<d|d<d|d<nUt|��rit|t��ri|jjd|d<d|d<|jtju�r]|��|d<n0|jtju�rhd|d<n$|j|d<|jtju�r{|j|d<n|jtju�r�|��|d<nd|d<|jg|�|�Ri|��|jdu�r�|jtu�s�|jttk�r�d|d<|jd|j��d|j�dd���fd|jd�|��dSdSdS)Nz--�_�-zpUnresolved type detected, which should have been done with the help of `typing.get_type_hints` method by defaultr1�69__origin__�	UnionType�z�Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because the argument parser only supports one type per argument. Problem encountered in field 'z'.�rr%cSsg|]}|j�qSr")�value)r(�xr"r"r#�70<listcomp>�sz;HfArgumentParser._parse_dataclass_field.<locals>.<listcomp>�typer3T�requiredF�?�nargs�const�+z--no_z--no-�store_false)�action�dest) �name�append�replacer5rrrOr'�RuntimeError�pop�getattrr�hasattr�typesrI�__args__�len�71ValueErrorrr
�72issubclassrr0r3r6�MISSINGrr$rr?r4�add_argument)rEr7Zlong_optionsr8r1Zorigin_typeZbool_kwargsr3r"r"r#�_parse_dataclass_field�s�7374�75 ��*(� 76&��77(78��79��z'HfArgumentParser._parse_dataclass_fieldrBcCs|t|d�r|�|j�}n|}zt|�}Wnty#td|�d���wt�|�D]}|js/q)||j	|_80|�||�q)dS)N�_argument_group_namezType resolution failed for z�. Try declaring the class in global scope or removing line of `from __future__ import annotations` which opts in Postponed Evaluation of Annotations (PEP 563))r^�add_argument_grouprgr�	NameErrorr[r6�fields�initrXrOrf)rArBrEZ81type_hintsr7r"r"r#r@�s 8283���z)HfArgumentParser._add_dataclass_argumentsFTr&.cs�|s|s|r{ttj�r{g}|r|�t|��n|r+ttj�r+|�ttjd��d��|rWt�}|j|tdd�|j	|d�\}}t84|��|�d�d�}	|	rW|�
dd	�|	D��g}85|D]}|��ri|86|����7}87q[|durr|88|n|89tjd90d�}|j	|d�\}}
g}|jD]/}dd�t�|�D���fd
d�t91|���D�}�D]}t||�q�|di|��}|�|�q�t|j�dkr�|�|�|r�g|�|
�RS|
r�td|
����g|�RS)a%92        Parse command-line args into instances of the specified dataclass types.93 94        This relies on argparse's `ArgumentParser.parse_known_args`. See the doc at:95        docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args96 97        Args:98            args:99                List of strings to parse. The default is taken from sys.argv. (same as argparse.ArgumentParser)100            return_remaining_strings:101                If true, also return a list of remaining argument strings.102            look_for_args_file:103                If true, will look for a ".args" file with the same base name as the entry point script for this104                process, and will append its potential content to the command line args.105            args_filename:106                If not None, will uses this file instead of the ".args" file specified in the previous argument.107            args_file_flag:108                If not None, will look for a file in the command-line args specified with this flag. The flag can be109                specified multiple times and precedence is determined by the order (last one wins).110 111        Returns:112            Tuple consisting of:113 114                - the dataclass instances in the same order as they were passed to the initializer.abspath115                - if applicable, an additional namespace for more (non-dataclass backed) arguments added to the parser116                  after initialization.117                - The potential list of remaining argument strings. (same as argparse.ArgumentParser.parse_known_args)118        rz.argsrY)rOrV)�argsrGNcSsg|]}t|��qSr"r	)r(�pr"r"r#rNHsz@HfArgumentParser.parse_args_into_dataclasses.<locals>.<listcomp>rKcS�h|]}|jr|j�qSr"�rkrX�r(rr"r"r#�	<setcomp>U�z?HfArgumentParser.parse_args_into_dataclasses.<locals>.<setcomp>c�i|]\}}|�vr||�qSr"r"�r(�kr!��keysr"r#r*V�z@HfArgumentParser.parse_args_into_dataclasses.<locals>.<dictcomp>z?Some specified arguments are not used by the HfArgumentParser: r")ra�sys�argvrYr119�with_suffixrrer'Zparse_known_args�varsr,�lstrip�extend�exists�	read_text�splitr;r6rj�items�delattr�__dict__rb)rArlZreturn_remaining_stringsZlook_for_args_fileZ
args_filenameZargs_file_flagZ120args_filesZargs_file_parser�cfgZcmd_args_file_pathsZ	file_argsZ	args_file�	namespaceZremaining_args�outputsrB�inputsru�objr"rvr#�parse_args_into_dataclassessF%�"121122123z,HfArgumentParser.parse_args_into_dataclassesrl�allow_extra_keyscs�t|���}g}|jD]*}dd�t�|�D���fdd�|��D�}|�|���|di|��}|�|�q|sC|rCtdt	|�����t124|�S)a<125        Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass126        types.127 128        Args:129            args (`dict`):130                dict containing config values131            allow_extra_keys (`bool`, *optional*, defaults to `False`):132                Defaults to False. If False, will raise an exception if the dict contains keys that are not parsed.133 134        Returns:135            Tuple consisting of:136 137                - the dataclass instances in the same order as they were passed to the initializer.138        cSrnr"rorpr"r"r#rqyrrz.HfArgumentParser.parse_dict.<locals>.<setcomp>crsr"r"rtrvr"r#r*zrxz/HfArgumentParser.parse_dict.<locals>.<dictcomp>z0Some keys are not used by the HfArgumentParser: Nr")�setrwr;r6rjr��difference_updaterYrb�sorted�tuple)rArlr�Zunused_keysr�rBr�r�r"rvr#�139parse_dictfs140zHfArgumentParser.parse_dict�	json_filecCsTtt|�dd��}t�|���}Wd�n1swY|j||d�}t|�S)at141        Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the142        dataclass types.143 144        Args:145            json_file (`str` or `os.PathLike`):146                File name of the json file to parse147            allow_extra_keys (`bool`, *optional*, defaults to `False`):148                Defaults to False. If False, will raise an exception if the json file contains keys that are not149                parsed.150 151        Returns:152            Tuple consisting of:153 154                - the dataclass instances in the same order as they were passed to the initializer.155        zutf-8)�encodingN�r�)�openr156�json�loads�readr�r�)rAr�r�Zopen_json_file�datar�r"r"r#�parse_json_file�s157�z HfArgumentParser.parse_json_file�	yaml_filecCs$|jt�t|����|d�}t|�S)at158        Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the159        dataclass types.160 161        Args:162            yaml_file (`str` or `os.PathLike`):163                File name of the yaml file to parse164            allow_extra_keys (`bool`, *optional*, defaults to `False`):165                Defaults to False. If False, will raise an exception if the json file contains keys that are not166                parsed.167 168        Returns:169            Tuple consisting of:170 171                - the dataclass instances in the same order as they were passed to the initializer.172        r�)r��yamlZ	safe_loadr173r�r�)rAr�r�r�r"r"r#�parse_yaml_file�sz HfArgumentParser.parse_yaml_filer+)NFTNN)F)�__name__�174__module__�__qualname__�__doc__rr�__annotations__rrr=�staticmethodrr6�Fieldrfr@r�rr��dictr'rrr��os�PathLiker�r��
__classcell__r"r"rCr#r:os@175$i�176177�(V���178179����180�r:)&r6r�r�ryr_�argparserrr�collections.abcrr�enumr�inspectr�pathlibr181�typingrrr
rrrrr�rrr$r?r'r0rdr�r�r9r:r"r"r"r#�<module>sH$182183
����184��185�/