
    ujt-                         d dl 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mZ d dlmZmZ d d	lmZmZ d d
lmZ d dlmZ  ee j                  Zd Z G d de          ZdS )    N)ArgumentParser)
CLICommand)concurrent_download)HubApi)DEFAULT_MAX_WORKERSDEFAULT_SKILLS_DIR)dataset_file_downloadmodel_file_download)dataset_snapshot_downloadsnapshot_download)convert_patternsresolve_endpoint)DEFAULT_DATASET_REVISION)
get_logger)	log_levelc                      t          |           S )z> Function which will be called for a specific sub parser.
    )DownloadCMDargss    g/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/cli/download.pysubparser_funcr      s     t    c                   :    e Zd ZdZd Zedefd            Zd ZdS )r   downloadc                     || _         d S )Nr   )selfr   s     r   __init__zDownloadCMD.__init__   s    			r   parsersc                    |                      t          j                  }|                                }|                    dt
          d           |                    dt
          d           |                    dt
          dd	           |                    d
t
          ddd           |                    dddgdd           |                    dt
          dd	           |                    dt
          dd	           |                    dt
          dd	           |                    dt
          dd	           |                    dt
          ddd           |                    dddt
          d !           |                    d"dt
          dd#$           |                    d%t
          dd&	           |                    d't          t          d(	           |                    t          )           dS )*z+ define args for download command.
        z--modelziThe id of the model to be downloaded. For download, the id of either a model or dataset must be provided.)typehelpz	--datasetzkThe id of the dataset to be downloaded. For download, the id of either a model or dataset must be provided.z--collectionNz2The ID of the collection to download (skills only))r    defaultr!   repo_id?zQOptional, ID of the repo to download, It can also be set by --model or --dataset.)r    nargsr"   r!   z--repo-typemodeldatasetz4Type of repo to download from (defaults to 'model').)choicesr"   r!   z--tokenz7Optional. Access token to download controlled entities.z
--revisionz%Revision of the entity (e.g., model).z--cache_dirz-Cache directory to save entity (e.g., model).z--local_dirzsFile will be downloaded to local location specified bylocal_dir, in this case, cache_dir parameter will be ignored.files*znSpecify relative path to the repository file(s) to download.(e.g 'tokenizer.json', 'onnx/decoder_model.onnx').)r    r"   r%   r!   z	--includezEGlob patterns to match files to download.Ignored if file is specified)r%   r"   r    r!   z	--excludezLGlob patterns to exclude from files to download.Ignored if file is specified)r%   r    r"   r!   z
--endpointaD  ModelScope server endpoint, e.g. modelscope.cn or modelscope.ai   Full URL like https://modelscope.cn is also accepted. Scheme (https://) is auto-completed if omitted. Falls back to env MODELSCOPE_DOMAIN, then defaults to https://www.modelscope.cn. When omitted, the CLI auto-detects the correct site (cn/intl) for download.z--max-workersz0The maximum number of workers to download files.)func)

add_parserr   nameadd_mutually_exclusive_groupadd_argumentstrintr   set_defaultsr   )r   parsergroups      r   define_argszDownloadCMD.define_args    s    ")!3!3K4D!E!E3355D 	 	E 	E 	E
 	D 	 	E 	E 	E
 	E	 	 	G 	G 	G
 	V 	 	
 	
 	
 	i(G	 	 	
 	
 	
 	J	 	 	L 	L 	L
 	8	 	 	: 	: 	:
 	@	 	 	B 	B 	B
 	L	 	 	M 	M 	M 	A 	 	B 	B 	B 	+ 	 	, 	, 	, 	+ 	 	, 	, 	, 	&	 	 
	' 
	' 
	' 	'C	 	 	E 	E 	E 	00000r   c                   	
 | j         j        s| j         j        r[| j         j        N| j         j        r+| j         j                            d| j         j                   n| j         j        g| j         _        nv| j         j        j| j         j        dk    r| j         j        | j         _        nC| j         j        dk    r| j         j        | j         _        nt          d| j         j        z            | j         j        s'| j         j        s| j         j        st          d          | j         j	        rt          | j         j	                  	nd 	d }| j         j        0t          	                              | j         j                  }| j         j        rt          | j         j                  dk    rZt          | j         j        | j         j        d         | j         j        | j         j        | j         j        || j         j        		           nt          | j         j                  dk    r_t'          | j         j        | j         j        | j         j        | j         j        | j         j        | j         j        || j         j        	
	  	         nt'          | j         j        | j         j        | j         j        | j         j        t+          | j         j                  t+          | j         j                  | j         j        || j         j        	
  
         t1          d| j         j         d           d S | j         j        r| j         j        r| j         j        nt2          }t          | j         j                  dk    rPt5          | j         j        | j         j        d         | j         j        | j         j        ||| j         j        		           nt          | j         j                  dk    rUt7          | j         j        || j         j        | j         j        | j         j        | j         j        || j         j        	
	  	         nyt7          | j         j        || j         j        | j         j        t+          | j         j                  t+          | j         j                  | j         j        || j         j        	
  
         t1          d| j         j         d           d S | j         j        rt          	| j         j                  | j         j        pt8          
                    | j         j        d	          }|                    di                               dg           }t>                               d| j         j         dt          |           d           |st1          d| j         j                    d S g }|D ]]}|                    d          r|                    d          st>          !                    d|           H|"                    |           ^|st1          d| j         j                    d S t1          dt          |           d           	
fd}tG          ||| j         j        d           d S d S ) Nr   r&   r'   zNot support repo-type: %sz*Model, dataset, or collection must be set.)endpoint)access_token   )	cache_dir	local_dirrevisioncookiestokenr7   )r<   r:   r;   allow_file_patternmax_workersr=   r>   r7   )	r<   r:   r;   r?   ignore_file_patternr@   r=   r>   r7   z$
Successfully Downloaded from model z.
z&
Successfully Downloaded from dataset )r7   r>   skill)	repo_typer7   CollectionElementsCollectionElementVoListzCollection z has z
 elements.z'No skill elements found in collection: ElementPathElementNamez)Skipping malformed collection element: %sz-No valid skill elements found in collection: zFound z' skill(s) in collection, downloading...c                     | d         }| d         }| d| }	                      |          }||d fS # t          $ r}|d t          |          fcY d }~S d }~ww xY w)NrF   rG   /)skill_idr;   r7   )download_skill	Exceptionr0   )	elementelement_pathelement_namerJ   	skill_direapir7   r;   s	         r   _download_one_skillz0DownloadCMD.execute.<locals>._download_one_skill  s    &}5&}5*;;\;;4 # 2 2!)"+!) !3 !+ !+I %i66  4 4 4$dCFF33333334s   7 
AAAA)r@   	item_name)$r   r&   r'   r#   r)   insertrC   rL   
collectionr7   r   r>   r   get_cookieslenr
   r:   r;   r<   r   r@   r   includeexcludeprintr   r	   r   r   get_collectiongetloggerinfowarningappendr   )r   r=   dataset_revisiondataelementsvalid_elementselemrS   rR   r7   r;   s           @@@r   executezDownloadCMD.execute   s   9? 	;di/ 	;y ,9? :IO**1di.?@@@@'+y'8&9DIOy ,9&'11&*i&7DIOOY(I55(,	(9DI%%#$?&*i&9%: ; ; ;y 	Jty'8 	JAU 	JHIII9 	'	(:;;HHH9?&(+++Coo49?oCCG9? @	49?##q((#IOIOA&"i1"i1!Y/#)/%' ' ' ' ' IO% %'() )!IO!Y/"i1"i1'+y $	 5#)/%	' 	' 	' 	' 	' "IO!Y/"i1"i1'7	8I'J'J(89J(K(K $	 5#)/%
' 
' 
' 
' N$)/NNNOOOOOY \	:>):L$jDI$6$6Rj49?##q((%I%IOA&"i1"i1-#)/%' ' ' ' ' IO% %'() ))I%-"i1"i1'+y $	 5#)/%	' 	' 	' 	' 	' *I%-"i1"i1'7	8I'J'J(89J(K(K $	 5#)/%
' 
' 
' 
' P$):KPPP     Y! 5	($)/BBBC	+A/AI%%	$( & L LDxx 4 "$ $$'C(A2$F$F  KKRdi2RRXRRR     0-0 0 1 1 1  N  , ,xx.. dhh}6M6M NN#N#') ) )%%d++++!  0-0 0 1 1 1 $3~.. $ $ $ % % %4 4 4 4 4 4 4  # I1!	# # # # # # Dr   N)	__name__
__module____qualname__r-   r   staticmethodr   r5   rg    r   r   r   r      si        D   ]1^ ]1 ]1 ]1 \]1~[ [ [ [ [r   r   )loggingargparser   modelscope.cli.baser   modelscope.cli.utilsr   modelscope.hub.apir   modelscope.hub.constantsr   r   modelscope.hub.file_downloadr	   r
    modelscope.hub.snapshot_downloadr   r   modelscope.hub.utils.utilsr   r   modelscope.utils.constantr   modelscope.utils.loggerr   WARNINGr^   r   r   rl   r   r   <module>ry      sc    # # # # # # * * * * * * 4 4 4 4 4 4 % % % % % % L L L L L L L L? ? ? ? ? ? ? ?A A A A A A A A I I I I I I I I > > > > > > . . . . . .	go	.	.	.  A A A A A* A A A A Ar   