o
    rqi%                     @   s   d dl Z d dlZd dlmZm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 d	d
lmZ G dd deZejddG dd deZdS )    N)ABCabstractmethod)CallableDictOptional)check_local_model_is_latest)snapshot_download)TRAINERS)Config)Invoke
ThirdParty   )	LogBufferc                   @   s`   e Zd ZdZddedee fddZ		dddZe	d	d
 Z
e	dedeeef fddZdS )BaseTrainerz Base class for trainer which can not be instantiated.

    BaseTrainer defines necessary interface
    and provide default implementation for basic initialization
    such as parsing config file and parsing commandline args.
    Ncfg_filearg_parse_fnc                 C   sL   t || _|r| j|| _nd| _t | _t | _t	dt
 | _dS )z Trainer basic init, should be called in derived class

        Args:
            cfg_file: Path to configuration file.
            arg_parse_fn: Same as ``parse_fn`` in :obj:`Config.to_args`.
        Nz%Y%m%d_%H%M%S)r
   	from_filecfgto_argsargsr   Z
log_bufferZvisualization_buffertimestrftime	localtime	timestamp)selfr   r    r   _/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/modelscope/trainers/base.py__init__   s   zBaseTrainer.__init__c                 C   sf   t j|r#t j|r|nt j|}t|tjtjt	j|id |S t
||tjtjt	j|id}|S )aH   Get local model directory or download model if necessary.

        Args:
            model (str): model id or path to local model directory.
            model_revision  (str, optional): model version number.
            third_party (str, optional): in which third party library
                this function is called.
        )
user_agent)revisionr   )ospathexistsisdirdirnamer   r   ZKEYZLOCAL_TRAINERr   r   ZTRAINER)r   modelZmodel_revisionZthird_partyZmodel_cache_dirr   r   r   get_or_download_model_dir(   s*   
z%BaseTrainer.get_or_download_model_dirc                 O      dS ) Train (and evaluate) process

        Train process should be implemented for specific task or
        model, related parameters have been initialized in
        ``BaseTrainer.__init__`` and should be used in this function
        Nr   )r   r   kwargsr   r   r   trainG   s   zBaseTrainer.traincheckpoint_pathreturnc                 O   r'   ) Evaluation process

        Evaluation process should be implemented for specific task or
        model, related parameters have been initialized in
        ``BaseTrainer.__init__`` and should be used in this function
        Nr   )r   r+   r   r)   r   r   r   evaluateQ   s   	zBaseTrainer.evaluateN)NN)__name__
__module____qualname____doc__strr   r   r   r&   r   r*   r   floatr.   r   r   r   r   r      s    

	
r   dummy)module_namec                       sH   e Zd Zdef fddZdd Z	ddedeeef fd	d
Z  Z	S )DummyTrainerr   c                    s   t  | dS )zY Dummy Trainer.

        Args:
            cfg_file: Path to configuration file.
        N)superr   )r   r   r   r)   	__class__r   r   r   `   s   zDummyTrainer.__init__c                 O   s   | j j}td|  dS )r(   z
train cfg N)r   r*   print)r   r   r)   r   r   r   r   r*   h   s   zDummyTrainer.trainNr+   r,   c                 O   s(   | j j}td|  td|  dS )r-   z	eval cfg zcheckpoint_path N)r   Z
evaluationr<   )r   r+   r   r)   r   r   r   r   r.   r   s   
zDummyTrainer.evaluater/   )
r0   r1   r2   r4   r   r*   r   r5   r.   __classcell__r   r   r:   r   r8   ]   s    
r8   )r    r   abcr   r   typingr   r   r   Zmodelscope.hub.check_modelr   Z modelscope.hub.snapshot_downloadr   Zmodelscope.trainers.builderr	   Zmodelscope.utils.configr
   Zmodelscope.utils.constantr   r   Zutils.log_bufferr   r   Zregister_moduler8   r   r   r   r   <module>   s   
M