o
    rq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   _/home/app/PaddleOCR-VL/.venv_paddleocr/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   Z	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   apiZaccess_tokenloginmodel_idospathjoinZ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_idZ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)r2   r   r   
<listcomp>p       z-ModelCardCMD.create_model.<locals>.<listcomp>zThe access_token must in %s!c                    r3   r4   r6   r9   )r1   r   r   r<   v   r=   zThe license must in %s!z>>> %s)r   
visibilitylicensechinese_name)Zmodelscope.hub.constantsr1   r2   dirr   r>   
ValueErrorr?   r   Z	get_modelr   	Exceptionloggerinfor   create_modelr@   pprint)r   ZvisibilitiesZlicenseser   r0   r   rF   n   s0   

zModelCardCMD.create_modelc                 C   s   | j | jS )N)r   get_model_urlr   r   r   r   r   rI      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)Z
clone_fromz	README.mdw)r   )r   	model_dirr>   r?   r@   )Zmodelscope.hub.repositoryrL   r   version_tagversion_infotempfileTemporaryDirectoryr)   r   Ztag_and_pushshutilrmtreer   r   r   rN   existsopentemplate_pathr   readwrite
substitutecloser   
push_modelr>   r?   r@   rG   )	r   Ztpl_dirrL   Z	clone_dirrepoZcfg_fileZtpl_file_pathZtplfr   r   r   r\      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.)rD   rE   rI   r   r   rJ   r   r   r   rG      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   actionrF   r\   r   r   rN   rO   rB   rJ   r   r   r   execute   s   
zModelCardCMD.executeN)rK   )__name__
__module____qualname__r)   r   staticmethodr   r/   rF   rI   r\   rG   rb   r   r   r   r   r      s    
E
r   )loggingr   rS   rQ   argparser   stringr   Zmodelscope.cli.baser   Zmodelscope.hub.apir   Z modelscope.hub.snapshot_downloadr   Zmodelscope.hub.utils.utilsr   Zmodelscope.utils.loggerr   WARNINGrD   r   dirnameabspath__file__current_pathr   rW   r   r   r   r   r   r   <module>   s    