
    gO                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	m	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZ d dlZ ede          Z ed	e          Zd
 Zde dee!gef         fdZ"dde j#        e j#        dddee!ee!         f         de!dedeg ef         de$de j%        fdZ& G d de          Z'dS )    N)ArgumentDefaultsHelpFormatterArgumentParserArgumentTypeError)copy)Enum)isclassPath)AnyCallableDictIterableListLiteralNewTypeOptionalTupleUnionget_type_hints	DataClassDataClassTypec                     t          | t                    r| S |                                 dv rdS |                                 dv rdS t          d|  d          )N)yestruety1T)nofalsefn0FzTruthy value expected: got zJ but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).)
isinstanceboollowerr   )vs    U/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/hf_argparser.pystring_to_boolr(   #   sk    !T wwyy222t	
4	4	4uw!www
 
 	
    choicesreturnc                 &    d | D             fdS )aN  
    Creates a mapping function from each choices string representation to the actual value. Used to support multiple
    value types for a single argument.

    Args:
        choices (list): List of choices.

    Returns:
        Callable[[str], Any]: Mapping function from string representation to actual value for each choice.
    c                 .    i | ]}t          |          |S  )str).0choices     r'   
<dictcomp>z-make_choice_type_function.<locals>.<dictcomp>;   s     ???VS[[&???r)   c                 0                         | |           S )N)get)argstr_to_choices    r'   <lambda>z+make_choice_type_function.<locals>.<lambda><   s    }((c22 r)   r.   )r*   r6   s    @r'   make_choice_type_functionr8   0   s(     @?w???M22222r)   )aliaseshelpdefaultdefault_factorymetadatar9   r:   r;   r<   r=   c                 P    |i }| | |d<   |||d<   t          j        d|||d|S )a  Argument helper enabling a concise syntax to create dataclass fields for parsing with `HfArgumentParser`.

    Example comparing the use of `HfArg` and `dataclasses.field`:
    ```
    @dataclass
    class Args:
        regular_arg: str = dataclasses.field(default="Huggingface", metadata={"aliases": ["--example", "-e"], "help": "This syntax could be better!"})
        hf_arg: str = HfArg(default="Huggingface", aliases=["--example", "-e"], help="What a nice syntax!")
    ```

    Args:
        aliases (Union[str, List[str]], optional):
            Single string or list of strings of aliases to pass on to argparse, e.g. `aliases=["--example", "-e"]`.
            Defaults to None.
        help (str, optional): Help string to pass on to argparse that can be displayed with --help. Defaults to None.
        default (Any, optional):
            Default value for the argument. If not default or default_factory is specified, the argument is required.
            Defaults to dataclasses.MISSING.
        default_factory (Callable[[], Any], optional):
            The default_factory is a 0-argument function called to initialize a field's value. It is useful to provide
            default values for mutable types, e.g. lists: `default_factory=list`. Mutually exclusive with `default=`.
            Defaults to dataclasses.MISSING.
        metadata (dict, optional): Further metadata to pass on to `dataclasses.field`. Defaults to None.

    Returns:
        Field: A `dataclasses.Field` with the desired properties.
    Nr9   r:   )r=   r;   r<   r.   )dataclassesfield)r9   r:   r;   r<   r=   kwargss         r'   HfArgrB   ?   sR    H %khQ`kkdjkkkr)   c            	       t    e Zd ZU dZee         ed<   deeee         f         f fdZe	de
dej        fd            ZdefdZ	 	 	 	 	 ddeedf         fdZddeeef         dedeedf         fdZ	 ddeeej        f         dedeedf         fdZ	 ddeeej        f         dedeedf         fdZ xZS )HfArgumentParsera  
    This subclass of `argparse.ArgumentParser` uses type hints on dataclasses to generate arguments.

    The class is designed to play well with the native argparse. In particular, you can add more (non-dataclass backed)
    arguments to the parser after initialization and you'll get the output back after parsing as an additional
    namespace. Optional: To create sub argument groups use the `_argument_group_name` attribute in the dataclass.
    dataclass_typesc                     d|vr
t           |d<    t                      j        di | t          j        |          r|g}t          |          | _        | j        D ]}|                     |           dS )a%  
        Args:
            dataclass_types:
                Dataclass type, or list of dataclass types for which we will "fill" instances with the parsed args.
            kwargs (`Dict[str, Any]`, *optional*):
                Passed to `argparse.ArgumentParser()` in the regular way.
        formatter_classNr.   )r   super__init__r?   is_dataclasslistrE   _add_dataclass_arguments)selfrE   rA   dtype	__class__s       r'   rI   zHfArgumentParser.__init__y   s     F**(EF$%""6"""#O44 	0./O#O44) 	1 	1E))%0000	1 	1r)   parserr@   c                 
   d|j          g}d|j         v r1|                    d|j                             dd                      |j                                        }t          |j        t                    rt          d          |	                    dg           }t          |t                    r|g}t          |j        d|j                  }|t          u s1t          t          d          rnt          |t          j                  rSt          |j        j        vrPt!          |j        j                  dk    st          d           |j        j        vrt#          d	|j          d
          t          d           |j        j        vr^|j        j        d         t          u r|j        j        d         n|j        j        d         |_        t          |j        d|j                  }nwt$          |j        j        vrdt          d |j        j        d                   r|j        j        d         n|j        j        d         |_        t          |j        d|j                  }i }|t&          u s4t          |j        t                    rt)          |j        t*                    rk|t&          u r|j        j        |d<   nd |j        D             |d<   t-          |d                   |d<   |j        t0          j        ur|j        |d<   nwd|d<   np|j        t$          u s|j        t4          t$                   k    rmt	          |          }t6          |d<   |j        t$          u s|j        >|j        t0          j        ur+|j        t0          j        u rdn|j        }||d<   d|d<   d|d<   nt9          |          rst)          |t:                    r^|j        j        d         |d<   d|d<   |j        t0          j        ur|                                |d<   nq|j        t0          j        u rd|d<   nX|j        |d<   |j        t0          j        ur|j        |d<   n0|j        t0          j        ur|                                |d<   nd|d<    | j        g ||R i | |j        du rk|j        t$          u s|j        t4          t$                   k    rDd|d<    | j        d|j          d|j                             dd           fd|j         d| d S d S d S )Nz--_-zpUnresolved type detected, which should have been done with the help of `typing.get_type_hints` method by defaultr9   
__origin__	UnionType   zOnly `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'.   r   r*   c                     g | ]	}|j         
S r.   )value)r0   xs     r'   
<listcomp>z;HfArgumentParser._parse_dataclass_field.<locals>.<listcomp>   s    $A$A$AQW$A$A$Ar)   typer;   TrequiredF?nargsconst+z--no_z--no-store_false)actiondest) nameappendreplacer=   r   r#   r\   r/   RuntimeErrorpopgetattrr   hasattrtypesrU   __args__len
ValueErrorr$   r   
issubclassr   r8   r;   r?   MISSINGr   r(   r   rK   r<   add_argument)rP   r@   long_optionsrA   r9   origin_typebool_kwargsr;   s           r'   _parse_dataclass_fieldz'HfArgumentParser._parse_dataclass_field   s1    *UZ))*%* CUZ%7%7S%A%A C CDDD$$&& ej#&& 	<  
 **Y++gs## 	 iGej,
CC%GE;$?$?J{\a\kDlDl%*---EJ'((A--d5:CV1V1V E6;jE E E  
 Dzz!4447<z7J17MQT7T7TUZ033Z_ZdZmnoZp
%ej,
KKUZ000 /9uz?RST?U.V.VrEJ'**\a\f\opq\r 
 &ej,
KK '!!jT&B&B!zRWR\^bGcGc!g%%$)J$7y!!$A$Aej$A$A$Ay!6vi7HIIF6N}K$777$)My!!%)z""Z45:$#?#? v,,K ,F6NzT!!em&?EMYdYlDlDl#(=K4G#G#G%%U]$+y!"%w"&w[!! 	*jd&C&C 	*"Z03F6N!F7O$K,???$)$9$9$;$;y!!+"555%)z""ZF6N}K$777$)My!!&k.AAA$)$9$9$;$;y!!%)z">\>G>>>v>>> =D  ejD&8&8EJ(SW.<X<X%*K	"F$
$$6
**34466 %Z	 
      ! <X<Xr)   rN   c           	      b   t          |d          r|                     |j                  }n| }	 t          |          }n# t          $ r t          d| d          t          $ r}t          j        d d         dk     rbdt          |          v rQd
                    t          t          t          j        d d                             }t          d| d	| d
          | d }~ww xY wt          j        |          D ]2}|j        s
||j                 |_        |                     ||           3d S )N_argument_group_namezType 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)rV   )   
   z!unsupported operand type(s) for |.ry   z on Python a6  . Try removing line of `from __future__ import annotations` which opts in union types as `X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To support Python versions that lower than 3.10, you need to use `typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of `X | None`.)rk   add_argument_grouprx   r   	NameErrorrh   	TypeErrorsysversion_infor/   joinmapr?   fieldsinitre   r\   rv   )rM   rN   rP   
type_hintsexpython_versionr@   s          r'   rL   z)HfArgumentParser._add_dataclass_arguments   s   5011 	,,U-GHHFFF	*8*?*?JJ 	 	 	6e 6 6 6  
  	 	 	#g--2UY\]_Y`Y`2`2`!$#c33CBQB3G*H*H!I!I""% " "N " " "   	 !'.. 	7 	7E: #EJ/EJ''6666		7 	7s   ? &C%%A;C  C%NFTr+   .c                 4   |s|s|rt          t          j                  rg }|r#|                    t	          |                     n`|r^t          t          j                  rE|                    t	          t          j        d                                       d                     |rt                      }|                    |t          d           |	                    |          \  }}t          |                              |                    d          d          }	|	r|                    d |	D                        g }
|D ]?}|                                r)|
|                                                                z  }
@||
|z   n|
t          j        d	d         z   }| 	                    |          \  }}g }| j        D ]}d
 t%          j        |          D             fdt          |                                          D             }D ]}t+          ||            |di |}|                    |           t          |j                  dk    r|                    |           |rg ||R S |rt/          d|           g |R S )a'  
        Parse command-line args into instances of the specified dataclass types.

        This relies on argparse's `ArgumentParser.parse_known_args`. See the doc at:
        docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args

        Args:
            args:
                List of strings to parse. The default is taken from sys.argv. (same as argparse.ArgumentParser)
            return_remaining_strings:
                If true, also return a list of remaining argument strings.
            look_for_args_file:
                If true, will look for a ".args" file with the same base name as the entry point script for this
                process, and will append its potential content to the command line args.
            args_filename:
                If not None, will uses this file instead of the ".args" file specified in the previous argument.
            args_file_flag:
                If not None, will look for a file in the command-line args specified with this flag. The flag can be
                specified multiple times and precedence is determined by the order (last one wins).

        Returns:
            Tuple consisting of:

                - the dataclass instances in the same order as they were passed to the initializer.abspath
                - if applicable, an additional namespace for more (non-dataclass backed) arguments added to the parser
                  after initialization.
                - The potential list of remaining argument strings. (same as argparse.ArgumentParser.parse_known_args)
        r   z.argsrf   )r\   rc   )argsrS   Nc                 ,    g | ]}t          |          S r.   r	   )r0   ps     r'   r[   z@HfArgumentParser.parse_args_into_dataclasses.<locals>.<listcomp>O  s    &L&L&L1tAww&L&L&Lr)   rW   c                 *    h | ]}|j         	|j        S r.   r   re   r0   r    s     r'   	<setcomp>z?HfArgumentParser.parse_args_into_dataclasses.<locals>.<setcomp>\  !    HHHqHAFHHHr)   c                 $    i | ]\  }}|v 	||S r.   r.   r0   kr&   keyss      r'   r2   z@HfArgumentParser.parse_args_into_dataclasses.<locals>.<dictcomp>]  s$    LLLtq!!t))a)))r)   z?Some specified arguments are not used by the HfArgumentParser: r.   )rn   r   argvrf   r
   with_suffixr   rr   r/   parse_known_argsvarsr4   lstripextendexists	read_textsplitrE   r?   r   itemsdelattr__dict__ro   )rM   r   return_remaining_stringslook_for_args_fileargs_filenameargs_file_flag
args_filesargs_file_parsercfgcmd_args_file_paths	file_args	args_file	namespaceremaining_argsoutputsrN   inputsr   objr   s                      @r'   parse_args_into_dataclassesz,HfArgumentParser.parse_args_into_dataclasses  s   J  	V] 	V/A 	Vc#(mm 	VJ J!!$}"5"56666# JCH J!!$sx{"3"3"?"?"H"HIII  
N#1#3#3  --n3x-XXX -==4=HH	T&*3iimmN4I4I#4N4NPT&U&U#& N%%&L&L8K&L&L&LMMMI' ? ?	##%% ?!4!4!6!6!<!<!>!>>I (,'79t##YRSRTRT=UD$($9$9t$9$D$D!	>) 	  	 EHHK$6u$=$=HHHDLLLLtI'<'<'>'>LLLF & &	1%%%%%//&//CNN3y!""Q&&NN9%%%# 	-W-n--- u !scq!s!stttW;;r)   r   allow_extra_keysc                    t          |                                          }g }| j        D ]}d t          j        |          D             fd|                                D             }|                    |                                            |di |}|                    |           |s!|rt          dt          |                     t          |          S )a<  
        Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass
        types.

        Args:
            args (`dict`):
                dict containing config values
            allow_extra_keys (`bool`, *optional*, defaults to `False`):
                Defaults to False. If False, will raise an exception if the dict contains keys that are not parsed.

        Returns:
            Tuple consisting of:

                - the dataclass instances in the same order as they were passed to the initializer.
        c                 *    h | ]}|j         	|j        S r.   r   r   s     r'   r   z.HfArgumentParser.parse_dict.<locals>.<setcomp>  r   r)   c                 $    i | ]\  }}|v 	||S r.   r.   r   s      r'   r2   z/HfArgumentParser.parse_dict.<locals>.<dictcomp>  s$    AAAtq!qDyyayyyr)   z0Some keys are not used by the HfArgumentParser: r.   )setr   rE   r?   r   r   difference_updaterf   ro   sortedtuple)	rM   r   r   unused_keysr   rN   r   r   r   s	           @r'   
parse_dictzHfArgumentParser.parse_dictm  s      $))++&&) 	  	 EHHK$6u$=$=HHHDAAAAtzz||AAAF))&++--888%//&//CNN3 	gK 	gePVWbPcPceefffW~~r)   	json_filec                    t          t          |          d          5 }t          j        |                                          }ddd           n# 1 swxY w Y   |                     ||          }t          |          S )at  
        Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the
        dataclass types.

        Args:
            json_file (`str` or `os.PathLike`):
                File name of the json file to parse
            allow_extra_keys (`bool`, *optional*, defaults to `False`):
                Defaults to False. If False, will raise an exception if the json file contains keys that are not
                parsed.

        Returns:
            Tuple consisting of:

                - the dataclass instances in the same order as they were passed to the initializer.
        zutf-8)encodingNr   )openr
   jsonloadsreadr   r   )rM   r   r   open_json_filedatar   s         r'   parse_json_filez HfArgumentParser.parse_json_file  s    & $y//G444 	5:n113344D	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5//$9I/JJW~~s   'AAA	yaml_filec                     |                      t          j        t          |                                                    |          }t          |          S )at  
        Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the
        dataclass types.

        Args:
            yaml_file (`str` or `os.PathLike`):
                File name of the yaml file to parse
            allow_extra_keys (`bool`, *optional*, defaults to `False`):
                Defaults to False. If False, will raise an exception if the json file contains keys that are not
                parsed.

        Returns:
            Tuple consisting of:

                - the dataclass instances in the same order as they were passed to the initializer.
        r   )r   yaml	safe_loadr
   r   r   )rM   r   r   r   s       r'   parse_yaml_filez HfArgumentParser.parse_yaml_file  sB    & //$.i1J1J1L1L"M"M`p/qqW~~r)   )NFTNN)F)__name__
__module____qualname____doc__r   r   __annotations__r   rI   staticmethodr   r?   Fieldrv   rL   r   r   r   r   r/   r   r$   r   osPathLiker   r   __classcell__)rO   s   @r'   rD   rD   n   s          m,,,,1mXm=T.T(U 1 1 1 1 1 1$ g~ gk>O g g g \gR 7m  7  7  7  7H !&T T 
y#~	T T T Tl tCH~  RWXacfXfRg    : LQ sBK/0DH	y#~	   2 LQ sBK/0DH	y#~	       r)   rD   )(r?   r   r   r   rl   argparser   r   r   r   enumr   inspectr   pathlibr
   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   rK   r/   r8   rq   dictr   rB   rD   r.   r)   r'   <module>r      s'        				 



  U U U U U U U U U U                         p p p p p p p p p p p p p p p p p p p p p p p p p p  GK%%	--

 

 

3t 3#0D 3 3 3 3" &*&)4)<,l ,l ,l3S	>",l ,l 	,l
 b#g&,l ,l ,l ,l ,l ,l^G G G G G~ G G G G Gr)   