o
    )j"                     @   s|  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mZmZ d dl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eddfdee!e
f de!de!dee!e"df dee! dee! dee! fddZ#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)Path)ListOptionalUnionHubApi)ModelVisibility)DEFAULT_REPOSITORY_REVISION)
get_logger   )max_workerspath_or_fileobjpath_in_reporepo_idtokenrevisioncommit_messagecommit_descriptionc                 C   s   t j| sdS ddlm} | }|| |sd}|r"|d | }t N}	ddlm	}
 |
|	||d}|r>t j
|	|}n|	}t j|dd	 t j| r_t j
|t j| }t| | ntj| |dd
 || W d   dS 1 sww   Y  dS )zPush files to model hub incrementally

    This function if used for patch_hub, user is not recommended to call this.
    This function will be merged to push_to_hub in later sprints.
    Nr   r	   zUpdating files
)
Repository)r   T)exist_ok)dirs_exist_ok)ospathexistsZ
modelscoper
   logintempfileTemporaryDirectoryZmodelscope.hub.repositoryr   joinmakedirsisfilebasenameshutilcopyfilecopytreepush)r   r   r   r   r   r   r   r
   apiZtemp_cache_dirr   repoZ
sub_folder	dest_file r,   [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/hub/push_to_hub.py_push_files_to_hub   s0   


"r.   T 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)Z
visibilityZchinese_namer   tagZoriginal_model_idignore_file_patternr   zNo commit messagez!Successfully upload the model to z with message: Tz#Error happens when uploading model z: F)
r
   r   Z
push_modelr   ZPUBLICZPRIVATEloggerinfo	Exceptionerror)	repo_name
output_dirr   privater   r0   source_repor1   r   r)   er,   r,   r-   _api_push_to_hubD   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)
r   environgetr   isdirlistdirr2   r3   ranger;   )r6   r7   r   r8   retryr   r0   r9   r1   r   ir,   r,   r-   push_to_hubg   s(   
rM   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@   rA   rB   rC   rD   rE   )r   rF   rG   r   rH   rI   r2   r3   	_executorZsubmitr;   )	r6   r7   r   r8   r   r0   r9   r1   r   r,   r,   r-   push_to_hub_async   s    rO   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_dirr7   r,   )valuerG   r2   r3   poprM   r   r   r   r%   rmtreer4   r5   )qbitemrQ   r7   r:   r,   r,   r-   submit_task   s&   


rX   c                   @   s   e Zd ZdZdZdS )UploadStrategycancelwaitN)__name__
__module____qualname__rZ   r[   r,   r,   r,   r-   rY      s    rY   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!rV   F)targetargsrP   z:Another uploading is running, this uploading with message r   z will be canceled.)len_managerr   _queuesQueuer   _flagsr   rX   start_tasksrG   putrR   rY   rZ   r2   r5   )
queue_nameZstrategykwargsprocessqueueflagr,   r,   r-   push_to_hub_in_queue   s8   
rn   c                 C   s8   t | d }|d u rd S |  t|  t|  d S )N)rg   rS   r!   rc   re   )ri   rk   r,   r,   r-   wait_for_done   s   
ro   ),concurrent.futuresZ
concurrentr   r%   r   multiprocessingr   r   r   pathlibr   typingr   r   r   jsonZmodelscope.hub.apir
   Zmodelscope.hub.constantsr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   r2   ZfuturesProcessPoolExecutorrN   dictrc   re   rg   rb   strboolr.   r;   rM   rO   rX   rY   rZ   rn   ro   r,   r,   r,   r-   <module>   s   

,
%
/
(