o
    ߥiH                     @   s   d dl Z d dlZd dl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 d dlmZ d dlmZ d dlmZ ee jd	ZejejeZejed
Zdd ZG dd de	ZdS )    N)ArgumentParser)Template)
CLICommand)HubApi)snapshot_download)get_endpoint)
get_logger)	log_leveltemplatec                 C   s   t | S )z> Function which will be called for a specific sub parser.
    )ModelCardCMD)args r   L/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/cli/modelcard.pysubparser_func   s   r   c                   @   sT   e Zd ZdZdd ZedefddZdd Zd	d
 Z	dddZ
dd Zdd ZdS )r   	modelcardc                 C   sf   || _ t | _|jr| j|j d| j jvr"tj| j j	| j jn| j j| _tjt
 | j| _d S )N/)r   r   apiaccess_tokenloginmodel_idospathjoingroup_idr   url)selfr   r   r   r   __init__   s   zModelCardCMD.__init__parsersc                 C   s   | j tjdgd}|jddtddd |jdd	td
g ddd |jddtddd |jddtd
dd |jddtddd |jddtddd |jddtd d!d |jd"d#td$d%d |jd&d'td(d)d |jd*d+td(d,d |jtd- d(S ).z= define args for create or upload modelcard command.
        model)aliasesz-tkz--access_tokenFz%the certification of visit ModelScope)typerequiredhelpz-actz--actionT)createuploaddownloadz,the action of api ModelScope[create, upload])r    r!   choicesr"   z-gidz
--group_iddamoz&the group name of ModelScope, eg, damo)r    defaultr"   z-midz
--model_idzthe model name of ModelScopez-visz--visibility   z>the visibility of ModelScope[PRIVATE: 1, INTERNAL:3, PUBLIC:5]z-licz	--licensezApache License 2.0zithe license of visit ModelScope[Apache License 2.0|GPL-2.0|GPL-3.0|LGPL-2.1|LGPL-3.0|AFL-3.0|ECL-2.0|MIT]z-chz--chinese_nameu   这是我的第一个模型zthe chinese name of ModelScopez-mdz--model_dir.z#the model_dir of configuration.jsonz-vtz--version_tagNzthe tag of uploaded modelz-viz--version_infozthe info of uploaded model)func)
add_parserr   nameadd_argumentstrintset_defaultsr   )r   parserr   r   r   define_args(   s   zModelCardCMD.define_argsc              
      s   ddl m m fddtD }| jj|vrtd|  fddt D }| jj|vr6td| z	| j	| j
 W n, tyk } z tdt|  | jj| j
| jj| jj| jjd	 W Y d }~nd }~ww |   d S )
Nr   LicensesModelVisibilityc                        g | ]}| d st |qS __
startswithgetattr.0attr)r6   r   r   
<listcomp>p       z-ModelCardCMD.create_model.<locals>.<listcomp>zThe access_token must in %s!c                    r7   r8   r:   r=   )r5   r   r   r@   v   rA   zThe license must in %s!z>>> %s)r   
visibilitylicensechinese_name)modelscope.hub.constantsr5   r6   dirr   rB   
ValueErrorrC   r   	get_modelr   	Exceptionloggerinfor    create_modelrD   pprint)r   visibilitieslicenseser   r4   r   rL   n   s0   

zModelCardCMD.create_modelc                 C   s   | j | jS )N)r   get_model_urlr   r   r   r   r   rQ      s   zModelCardCMD.get_model_url
readme.tplc           	      C   s
  ddl m} | jjr*| jjr*t j}||| jd}|	| jj| jj t
| nUtj| jjd}tj|skttjt|!}t| }t|d}||j| jd |  W d    n1 sfw   Y  | jj| j| jj| jj| jj| jjd |   d S )Nr   )
Repository)
clone_fromz	README.mdw)r   )r   	model_dirrB   rC   rD   )modelscope.hub.repositoryrT   r   version_tagversion_infotempfileTemporaryDirectoryr-   r   tag_and_pushshutilrmtreer   r   r   rW   existsopentemplate_pathr   readwrite
substitutecloser   
push_modelrB   rC   rD   rM   )	r   tpl_dirrT   	clone_dirrepocfg_filetpl_file_pathtplfr   r   r   rg      s2   



zModelCardCMD.push_modelc                 C   s6   t d|    t d| j  t d| j  d S )Nz3>>> Clone the model_git < %s >, commit and push it.z+>>> Open the url < %s >, check and read it.z3>>> Visit the model_id < %s >, download and run it.)rJ   rK   rQ   r   r   rR   r   r   r   rM      s   
zModelCardCMD.pprintc                 C   s`   | j jdkr|   d S | j jdkr|   d S | j jdkr,t| j| j j| j jd d S td)Nr#   r$   r%   )	cache_dirrevisionz3The parameter of action must be in [create, upload])	r   actionrL   rg   r   r   rW   rY   rG   rR   r   r   r   execute   s   
zModelCardCMD.executeN)rS   )__name__
__module____qualname__r-   r   staticmethodr   r3   rL   rQ   rg   rM   rr   r   r   r   r   r      s    
E
r   )loggingr   r^   r[   argparser   stringr   modelscope.cli.baser   modelscope.hub.apir    modelscope.hub.snapshot_downloadr   modelscope.hub.utils.utilsr   modelscope.utils.loggerr   WARNINGrJ   r   dirnameabspath__file__current_pathr   rb   r   r   r   r   r   r   <module>   s    