
    9i0                        d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZ  ej"                  e      Z e ee      j*                        Zd	d
ddZ G d de      Z eej2                        e_        ej2                  j                   8ej2                  j                   j5                  ddd      ej2                  _         yy)z9
 Processing saving/loading class for common processors.
    N)Path)OptionalUnion   )custom_object_save)PreTrainedTokenizerBase)PushToHubMixin	copy_funcdirect_transformers_importloggingr   FeatureExtractionMixinImageProcessingMixin)AutoTokenizerAutoFeatureExtractorAutoImageProcessorc                       e Zd ZdZddgZdZdZdZd Zd Z	dde
fdZe	 	 	 	 	 dd	eeej                   f   d
eeeej                   f      de
de
deeee
f      defd       Zedd       Zed        Zed        Zy)ProcessorMixinza
    This is a mixin used to provide saving/loading functionality for all processor classes.
    feature_extractor	tokenizerNc           
         |D ]  }|| j                   vst        d| d       t        || j                         D ]  \  }}||v rt        d| d      |||<    t        |      t        | j                         k7  rJt	        dt        | j                          ddj                  | j                          dt        |       d      |j                         D ]  \  }}t        | | d	      }t        j                  ||      }t        |t              rt        d
 |D              }nt        t        |      }t        ||      s(t	        dt        |      j                   d| d| d      t        | ||        y )NzUnexpected keyword argument .z!Got multiple values for argument zThis processor requires z arguments: z, z. Got z arguments instead._classc              3   B   K   | ]  }|t        t        |        y wNgetattrtransformers_module.0ns     g/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/transformers/processing_utils.py	<genexpr>z*ProcessorMixin.__init__.<locals>.<genexpr>M   s     $j\]\iW-@!%D$js   zReceived a z for argument z, but a z was expected.)
attributes	TypeErrorziplen
ValueErrorjoinitemsr   AUTO_TO_BASE_CLASS_MAPPINGget
isinstancetupler   type__name__setattr)selfargskwargskeyargattribute_name
class_nameproper_classs           r!   __init__zProcessorMixin.__init__6   s    	GC$//)">se1 EFF	G $'tT__#= 	-C'"CNCSST UVV),~&		- v;#doo..*3t+?*@TYYW[WfWfMgLhhnt9+02  $*<<> 	/NC .)9'@AJ377
JOJ*e,$$jj$jj&':JGc<0 !$s)"4"4!5^NCSS[\f[gguv  D.#.	/    c                     | j                   D cg c]  }d| dt        t        | |              }}dj                  |      }| j                  j
                   d| S c c}w )Nz- z: 
z:
)r#   reprr   r(   	__class__r/   )r1   nameattributes_reprs      r!   __repr__zProcessorMixin.__repr__X   sf    PTP_P_`RvRWT4-@(A'BC``))O4..))*#o->?? as   "Apush_to_hubc           	      :   |j                  dd      }|<t        j                  dt               |j	                  dd      t        d      ||d<   t        j                  |d       |rr|j                  dd      }|j                  d	|j                  t        j                  j                        d
         } | j                  |fi |}| j                  |      }| j                  \| j                  D cg c]  }t        | |       }	}|	D 
cg c]   }
t!        |
t"              r|
j$                  n|
" }}
t'        | ||       | j                  D ]P  }t        | |      }t)        |d      r%|j+                  | j,                  j.                         |j1                  |       R | j                  ;| j                  D ],  }t        | |      }t!        |t"              s |j$                  d= . |r&| j3                  ||j	                  d             yyc c}w c c}
w )a  
        Saves the attributes of this processor (feature extractor, tokenizer...) in the specified directory so that it
        can be reloaded using the [`~ProcessorMixin.from_pretrained`] method.

        <Tip>

        This class method is simply calling [`~feature_extraction_utils.FeatureExtractionMixin.save_pretrained`] and
        [`~tokenization_utils_base.PreTrainedTokenizerBase.save_pretrained`]. Please refer to the docstrings of the
        methods above for more information.

        </Tip>

        Args:
            save_directory (`str` or `os.PathLike`):
                Directory where the feature extractor JSON file and the tokenizer files will be saved (directory will
                be created if it does not exist).
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
                repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
                namespace).
            kwargs (`Dict[str, Any]`, *optional*):
                Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        use_auth_tokenNrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.tokenV`token` and `use_auth_token` are both specified. Please set only the argument `token`.T)exist_okcommit_messagerepo_id)config_set_processor_classauto_map)rI   rF   )popwarningswarnFutureWarningr+   r'   osmakedirssplitpathsep_create_repo_get_files_timestamps_auto_classr#   r   r,   r   init_kwargsr   hasattrrM   r>   r/   save_pretrained_upload_modified_files)r1   save_directoryrB   r3   rD   rI   rJ   files_timestampsr6   attrsaconfigs	attributes                r!   r]   zProcessorMixin.save_pretrained]   s   0  $4d;%MM E zz'4(4 l  -F7O
NT2#ZZ(8$?NjjN,@,@,Mb,QRG'd'':6:G#99.I 'IMY~WT>2YEYafg\]A7N)OUVVgGgt^GD"oo 	6Nn5I y"89..t~~/F/FG%%n5	6 '"&// :#D.9	i)@A!--j9:
 '' -jj) (  ' Zgs   6H%Hpretrained_model_name_or_path	cache_dirforce_downloadlocal_files_onlyrF   revisionc                     ||d<   ||d<   ||d<   ||d<   |j                  dd      }|)t        j                  dt               |t	        d      |}|||d	<    | j
                  |fi |}	 | |	 S )
a  
        Instantiate a processor associated with a pretrained model.

        <Tip>

        This class method is simply calling the feature extractor
        [`~feature_extraction_utils.FeatureExtractionMixin.from_pretrained`], image processor
        [`~image_processing_utils.ImageProcessingMixin`] and the tokenizer
        [`~tokenization_utils_base.PreTrainedTokenizer.from_pretrained`] methods. Please refer to the docstrings of the
        methods above for more information.

        </Tip>

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                This can be either:

                - a string, the *model id* of a pretrained feature_extractor hosted inside a model repo on
                  huggingface.co. Valid model ids can be located at the root-level, like `bert-base-uncased`, or
                  namespaced under a user or organization name, like `dbmdz/bert-base-german-cased`.
                - a path to a *directory* containing a feature extractor file saved using the
                  [`~SequenceFeatureExtractor.save_pretrained`] method, e.g., `./my_model_directory/`.
                - a path or url to a saved feature extractor JSON *file*, e.g.,
                  `./my_model_directory/preprocessor_config.json`.
            **kwargs
                Additional keyword arguments passed along to both
                [`~feature_extraction_utils.FeatureExtractionMixin.from_pretrained`] and
                [`~tokenization_utils_base.PreTrainedTokenizer.from_pretrained`].
        rf   rg   rh   ri   rD   NrE   rG   rF   )rO   rP   rQ   rR   r'   _get_arguments_from_pretrained)
clsre   rf   rg   rh   rF   ri   r3   rD   r2   s
             r!   from_pretrainedzProcessorMixin.from_pretrained   s    P ({#1 %5!"%z$4d;%MM E   l  #E#F7O1s112OZSYZDzr:   c                     t        |t              s|j                  }ddlmc m} t        ||      st        | d      || _        y)a  
        Register this class with a given auto class. This should only be used for custom feature extractors as the ones
        in the library are already mapped with `AutoProcessor`.

        <Tip warning={true}>

        This API is experimental and may have some slight breaking changes in the next releases.

        </Tip>

        Args:
            auto_class (`str` or `type`, *optional*, defaults to `"AutoProcessor"`):
                The auto class to register this new feature extractor with.
        r   Nz is not a valid auto class.)	r,   strr/   transformers.models.automodelsautor\   r'   rZ   )rl   
auto_classauto_modules      r!   register_for_auto_classz&ProcessorMixin.register_for_auto_class   sC      *c*#,,J66{J/
|+FGHH$r:   c                 <   g }| j                   D ]  }t        | | d      }t        |t              r7t        d |D              }|j	                  dd      }|r|d   |d   }n|d   }nt        t
        |      }|j                   |j                  |fi |        |S )Nr   c              3   D   K   | ]  }|t        t        |      nd   y wr   r   r   s     r!   r"   z@ProcessorMixin._get_arguments_from_pretrained.<locals>.<genexpr>  s#     o_`1=(;Q ?VZ Zos    use_fastTr   r   )r#   r   r,   r-   r+   r   appendrm   )	rl   re   r3   r2   r6   r7   classesrx   attribute_classs	            r!   rk   z-ProcessorMixin._get_arguments_from_pretrained  s    !nn 	bN (8&?@J*e,odnoo!::j$7
 6&-ajO&-ajO")*=z"JKK7778U`Y_`a	b r:   c                 N    t        | | j                  d         }t        |dd       S )Nr   model_input_names)r   r#   )r1   first_attributes     r!   r}   z ProcessorMixin.model_input_names  s'    !$(:;(;TBBr:   )F)NFFNmain)AutoProcessor)r/   
__module____qualname____doc__r#   feature_extractor_classtokenizer_classrZ   r9   rA   boolr]   classmethodr   ro   rS   PathLiker   rm   ru   rk   propertyr}    r:   r!   r   r   *   s    &{3J"OK /D@
I4 IV  8<$!&,0<',S"++-='>< E#r{{"234< 	<
 < c4i()< < <| % %2  " C Cr:   r   	processorr   zprocessor files)objectobject_classobject_files)r   rS   rP   pathlibr   typingr   r   dynamic_module_utilsr   tokenization_utils_baser   utilsr	   r
   r   r   
get_loggerr/   logger__file__parentr   r*   r   rB   formatr   r:   r!   <module>r      s    
   " 4 < Q Q 
		H	% 1h1F1FG  /40 lC^ lC^ '~'A'AB %%1)7)C)C)K)K)R)RGX *S *N& 2r:   