o
    h0                     @   s   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eZeeejZd	d
ddZG dd deZeeje_ejj durdejj jddddej_ dS dS )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                   @   s   e Zd ZdZddgZdZdZdZdd Zdd Z	dd
e
fd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dZedddZedd Zedd ZdS ) ProcessorMixinza
    This is a mixin used to provide saving/loading functionality for all processor classes.
    feature_extractor	tokenizerNc              	   O   s,  |D ]}|| j vrtd| dqt|| j D ]\}}||v r(td| d|||< qt|t| j krMtdt| j  dd| j  dt| d| D ]B\}}t| | d	}t	||}t
|trrtd
d |D }ntt|}t
||stdt|j d| d| dt| || qQd S )NzUnexpected keyword argument .z!Got multiple values for argument zThis processor requires z arguments: z, z. Got z arguments instead._classc                 s   s"    | ]}|d urt t|V  qd S Ngetattrtransformers_module.0n r   S/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/processing_utils.py	<genexpr>M   s     z*ProcessorMixin.__init__.<locals>.<genexpr>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_classr   r   r   __init__6   s4   




zProcessorMixin.__init__c                    s0    fdd j D }d|} jj d| S )Nc              	      s&   g | ]}d | dt t | qS )z- z: )reprr   )r   namer.   r   r   
<listcomp>Y   s   & z+ProcessorMixin.__repr__.<locals>.<listcomp>
z:
)r    r%   	__class__r,   )r.   attributes_reprr   r9   r   __repr__X   s   
zProcessorMixin.__repr__Fpush_to_hubc                    s^  | dd}|dur tdt |dddurtd||d< tj|dd |rJ| dd}| d	|tj	j
d
 } j|fi |} |} jdurg fdd jD }dd |D }	t ||	d  jD ]}
t |
}t|dr}| jj || qj jdur jD ]}
t |
}t|tr|jd= q|r j|||||dd dS dS )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_idc                    s   g | ]}t  |qS r   )r   )r   r3   r9   r   r   r:      s    z2ProcessorMixin.save_pretrained.<locals>.<listcomp>c                 S   s    g | ]}t |tr|jn|qS r   )r)   r   init_kwargs)r   ar   r   r   r:      s     )config_set_processor_classauto_map)rE   rB   )popwarningswarnFutureWarningr(   r$   osmakedirssplitpathsep_create_repo_get_files_timestamps_auto_classr    r   r   hasattrrK   r<   r,   save_pretrainedr)   r   rH   _upload_modified_files)r.   save_directoryr?   r0   r@   rE   rF   files_timestampsattrsconfigsr3   	attributer   r9   r   rZ   ]   sR   









zProcessorMixin.save_pretrainedmainpretrained_model_name_or_path	cache_dirforce_downloadlocal_files_onlyrB   revisionc           
      K   s~   ||d< ||d< ||d< ||d< | dd}|dur*tdt |dur(td|}|dur2||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`].
        rc   rd   re   rf   r@   NrA   rC   rB   )rM   rN   rO   rP   r$   _get_arguments_from_pretrained)
clsrb   rc   rd   re   rB   rf   r0   r@   r/   r   r   r   from_pretrained   s&   (zProcessorMixin.from_pretrainedAutoProcessorc                 C   sD   t |ts|j}ddlm  m} t||st| d|| _dS )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autorY   r$   rX   )rh   
auto_classauto_moduler   r   r   register_for_auto_class   s   


z&ProcessorMixin.register_for_auto_classc           	      K   s   g }| j D ]A}t| | d}t|tr5tdd |D }|dd}|r0|d d ur0|d }n
|d }ntt|}||j|fi | q|S )Nr   c                 s   s&    | ]}|d urt t|nd V  qd S r   r   r   r   r   r   r     s   $ z@ProcessorMixin._get_arguments_from_pretrained.<locals>.<genexpr>use_fastTr   r   )r    r   r)   r*   r(   r   appendri   )	rh   rb   r0   r/   r3   r4   classesrr   attribute_classr   r   r   rg     s   




z-ProcessorMixin._get_arguments_from_pretrainedc                 C   s   t | | jd }t |dd S )Nr   model_input_names)r   r    )r.   first_attributer   r   r   rv     s   z ProcessorMixin.model_input_names)F)NFFNra   )rj   )r,   
__module____qualname____doc__r    feature_extractor_classtokenizer_classrX   r6   r>   boolrZ   classmethodr   rk   rQ   PathLiker   ri   rq   rg   propertyrv   r   r   r   r   r   *   sD    "K>
r   	processorrj   zprocessor files)objectobject_classobject_files)rz   rQ   rN   pathlibr   typingr   r   dynamic_module_utilsr   tokenization_utils_baser   utilsr   r	   r
   r   
get_loggerr,   logger__file__parentr   r'   r   r?   formatr   r   r   r   <module>   s,   
 p