o
    ߥih                     @   s   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 e ZejjddZe Ze Ze Zdad	d
dd
defddZdd	dd
dd
defddZdd	d
dd
defddZdd ZG dd dZejfddZdd Z dS )    N)ManagerProcessValue)HubApi)ModelVisibility)DEFAULT_REPOSITORY_REVISION)
get_logger   )max_workersT c	                 C   s   z.t  }	|	| |	j| ||stjntj| |||||d	 |p d}td|  d|  W dS  tyO }
 zt	d|  d| d|
  W Y d }
~
dS d }
~
ww )	N)
visibilitychinese_namecommit_messagetagoriginal_model_idignore_file_patternrevisionzNo commit messagez!Successfully upload the model to z with message: Tz#Error happens when uploading model z: F)
r   login
push_modelr   PUBLICPRIVATEloggerinfo	Exceptionerror)	repo_name
output_dirtokenprivater   r   source_repor   r   apie r"   N/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/hub/push_to_hub.py_api_push_to_hub   s8   	
r$      c
                 C   s   |du r
t jd}|du rt jd}| dusJ |dus"J dt j|s*J dt |v sAdt |v sAdt |v sAJ td| d	|  d
|  t|D ]}
t	| ||||||||		rd dS qSdS )a  
    Args:
        repo_name: The repo name for the modelhub repo
        output_dir: The local output_dir for the checkpoint
        token: The user api token, function will check the `MODELSCOPE_API_TOKEN` variable if this argument is None
        private: If is a private repo, default True
        retry: Retry times if something error in uploading, default 3
        commit_message: The commit message
        tag: The tag of this commit
        source_repo: The source repo (model id) which this model comes from
        ignore_file_pattern: The file pattern to be ignored in uploading.
        revision: The branch to commit to
    Returns:
        The boolean value to represent whether the model is uploaded.
    NMODELSCOPE_API_TOKENUPLOAD_IGNORE_FILE_PATTERNUEither pass in a token or to set `MODELSCOPE_API_TOKEN` in the environment variables.configuration.jsonconfiguration.yamlconfiguration.yml
Uploading  to  with message TF)
osenvirongetpathisdirlistdirr   r   ranger$   )r   r   r   r   retryr   r   r   r   r   ir"   r"   r#   push_to_hub9   s(   
r8   c	           	      C   s   |du r
t jd}|du rt jd}| dusJ |dus"J dt j|s*J dt |v sAdt |v sAdt |v sAJ td| d	|  d
|  t	t
| ||||||||
S )az  
    Args:
        repo_name: The repo name for the modelhub repo
        output_dir: The local output_dir for the checkpoint
        token: The user api token, function will check the `MODELSCOPE_API_TOKEN` variable if this argument is None
        private: If is a private repo, default True
        commit_message: The commit message
        tag: The tag of this commit
        source_repo: The source repo (model id) which this model comes from
        ignore_file_pattern: The file pattern to be ignored in uploading
        revision: The branch to commit to
    Returns:
        A handler to check the result and the status
    Nr&   r'   r(   r)   r*   r+   r,   r-   r.   )r/   r0   r1   r2   r3   r4   r   r   	_executorsubmitr$   )	r   r   r   r   r   r   r   r   r   r"   r"   r#   push_to_hub_asyncf   s    r;   c              
   C   s   	 d|_ |  }t| d|_ |ddsO|dd}|d}ztdi | |r6tj|r6t	
| W n tyN } zt| W Y d }~nd }~ww d S q)NTFdone
delete_dirr   r"   )valuer1   r   r   popr8   r/   r2   existsshutilrmtreer   r   )qbitemr=   r   r!   r"   r"   r#   submit_task   s&   


rF   c                   @   s   e Zd ZdZdZdS )UploadStrategycancelwaitN)__name__
__module____qualname__rH   rI   r"   r"   r"   r#   rG      s    rG   c                 K   s   | d ur
t | dksJ dtd u rt a| tvr:t t| < tddt| < ttt|  t|  fd}|	  |t
| < t|  }t|  }|ddrO|| d S |jre|tjkretd|d d	 d S || d S )
Nr   z"Please specify a valid queue name!rD   F)targetargsr<   z:Another uploading is running, this uploading with message r   z will be canceled.)len_managerr   _queuesQueuer   _flagsr   rF   start_tasksr1   putr>   rG   rH   r   r   )
queue_namestrategykwargsprocessqueueflagr"   r"   r#   push_to_hub_in_queue   s8   
r]   c                 C   s8   t | d }|d u rd S |  t|  t|  d S )N)rU   r?   joinrQ   rS   )rW   rZ   r"   r"   r#   wait_for_done   s   
r_   )!concurrent.futures
concurrentr/   rA   multiprocessingr   r   r   modelscope.hub.apir   modelscope.hub.constantsr   modelscope.utils.constantr   modelscope.utils.loggerr   r   futuresProcessPoolExecutorr9   dictrQ   rS   rU   rP   r$   r8   r;   rF   rG   rH   r]   r_   r"   r"   r"   r#   <module>   sT   
%
/
(