
     `ifM                        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 d dl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e         f                  dee         dedeg ef         dee"         de j#        fdZ$ G d de          Z%dS )    N)ArgumentDefaultsHelpFormatterArgumentParserArgumentTypeError)Iterable)copy)Enum)isclassPath)AnyCallableLiteralNewTypeOptional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    m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-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'   r4   s    @r$   make_choice_type_functionr6   1   s(     @?w???M22222r&   )aliaseshelpdefaultdefault_factorymetadatar7   r8   r9   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.
    Nr7   r8   )r;   r9   r:   r+   )dataclassesfield)r7   r8   r9   r:   r;   kwargss         r$   HfArgr@   @   sR    H %khQ`kkdjkkkr&   c            	           e Zd ZU dZee         ed<   dde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.

    Args:
        dataclass_types (`DataClassType` or `Iterable[DataClassType]`, *optional*):
            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.
    dataclass_typesNc                 $   |g }nt          |t                    s|g}d|vr
t          |d<    t                      j        di | t          j        |          r|g}t          |          | _        | j        D ]}| 	                    |           d S )Nformatter_classr+   )
r    r   r   super__init__r=   is_dataclasslistrC   _add_dataclass_arguments)selfrC   r?   dtype	__class__s       r$   rG   zHfArgumentParser.__init__   s    " OOOX66 	0./O 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 defaultr7   
__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)r-   xs     r$   
<listcomp>z;HfArgumentParser._parse_dataclass_field.<locals>.<listcomp>   s    $A$A$AQW$A$A$Ar&   typer9   TrequiredF?nargsconst+z--no_z--no-store_false)actiondest) nameappendreplacer;   r   r    rZ   r,   RuntimeErrorpopgetattrr   hasattrtypesrS   __args__len
ValueErrorr!   r   
issubclassr   r6   r9   r=   MISSINGr   r%   r	   rI   r:   add_argument)rN   r>   long_optionsr?   r7   origin_typebool_kwargsr9   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&   rL   c                 R   t          |d          r|                     |j                  }n| }	 t          |          }n!# t          $ r t          d| d          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))ri   add_argument_grouprv   r   	NameErrorrf   r=   fieldsinitrc   rZ   rt   )rK   rL   rN   
type_hintsr>   s        r$   rJ   z)HfArgumentParser._add_dataclass_arguments   s    5011 	,,U-GHHFFF	*8*?*?JJ 	 	 	6e 6 6 6  	 !'.. 	7 	7E: #EJ/EJ''6666		7 	7s	   ? AFTr(   .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/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.argsrd   )rZ   ra   )argsrQ   Nc                 ,    g | ]}t          |          S r+   r
   )r-   ps     r$   rY   z@HfArgumentParser.parse_args_into_dataclasses.<locals>.<listcomp>H  s    &L&L&L1tAww&L&L&Lr&   rU   c                 *    h | ]}|j         	|j        S r+   rz   rc   r-   r   s     r$   	<setcomp>z?HfArgumentParser.parse_args_into_dataclasses.<locals>.<setcomp>U  !    HHHqHAFHHHr&   c                 $    i | ]\  }}|v 	||S r+   r+   r-   kr#   keyss      r$   r/   z@HfArgumentParser.parse_args_into_dataclasses.<locals>.<dictcomp>V  s$    LLLtq!!t))a)))r&   z?Some specified arguments are not used by the HfArgumentParser: r+   )rl   sysargvrd   r   with_suffixr   rp   r,   parse_known_argsvarsr2   lstripextendexists	read_textsplitrC   r=   ry   itemsdelattr__dict__rm   )rK   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outputsrL   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>y  r   r&   c                 $    i | ]\  }}|v 	||S r+   r+   r   s      r$   r/   z/HfArgumentParser.parse_dict.<locals>.<dictcomp>z  s$    AAAtq!qDyyayyyr&   z0Some keys are not used by the HfArgumentParser: r+   )setr   rC   r=   ry   r   difference_updaterd   rm   sortedtuple)	rK   r}   r   unused_keysr   rL   r   r   r   s	           @r$   
parse_dictzHfArgumentParser.parse_dictf  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   )rK   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   )rK   r   r   r   s       r$   parse_yaml_filez HfArgumentParser.parse_yaml_file  sB    & //$.i1J1J1L1L"M"M`p/qqW~~r&   r1   )NFTNN)F)__name__
__module____qualname____doc__r   r   __annotations__r   r   rG   staticmethodr   r=   Fieldrt   rJ   r   r   r   dictr,   r   r!   r   osPathLiker   r   __classcell__)rM   s   @r$   rB   rB   o   s          m,,,,1 1}h}F]7]1^(_ 1 1 1 1 1 1" g~ gk>O g g g \gR7m 7 7 7 7. !&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&   rB   )&r=   r   r   r   rj   argparser   r   r   collections.abcr   r   enumr   inspectr	   pathlibr   typingr   r   r   r   r   r   r   r   r   r   r%   rI   r,   r6   ro   r   r   r@   rB   r+   r&   r$   <module>r      s.        				 



  U U U U U U U U U U $ $ $ $ $ $                         S S S S S S S S S S S S S S S S S S  GK%%	--

 

 

3t 3#0D 3 3 3 3" 04&)4)<#,l ,l ,leCcN+,,l 3-,l 	,l
 b#g&,l tn,l ,l ,l ,l ,l^    ~     r&   