o
    rqik                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZmZ d dlmZ e Zdd	 ZG d
d deZdS )    )ArgumentParser_SubParsersAction)
CLICommand)HubApi)LicensesModelVisibility
VisibilityVisibilityMap)	AigcModel)REPO_TYPE_MODELREPO_TYPE_SUPPORT)
get_loggerc                 C   s   t | S )z> Function which will be called for a specific sub parser.
    )	CreateCMDargs r   \/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/modelscope/cli/create.pysubparser_func   s   r   c                   @   sL   e Zd ZdZdZdefddZedefddZd	d
 Z	dd Z
dd ZdS )r   zS
    Command for creating a new repository, supporting both model and dataset.
    creater   c                 C   s
   || _ d S )Nr   )selfr   r   r   r   __init__   s   
zCreateCMD.__init__parsersc                 C   sR  |  tj}|jdtdd |jdtd dd |jdttdd	 |jd
tjtj	tj
gtjdd	 |jdtd dd |jdtt tjdd |jdtd dd |dd}|jdddd |jdtdd |jdtdd |jdtdd |jdtd d |jd!td"d#d |jd$td%d&d |jd'td(d)d |jd*td%d+d |jtd, d S )-Nrepo_idz8The ID of the repo to create (e.g. `username/repo-name`))typehelpz--tokenzA User Access Token generated from https://modelscope.cn/my/myaccesstoken to authenticate the user. If not provided, the CLI will use the local credentials if available.)r   defaultr   z--repo_typezIType of the repo to create (e.g. `dataset`, `model`). Default to `model`.)choicesr   r   z--visibilityz6Visibility of the repo to create. Default to `public`.z--chinese_namez6Optional, Chinese name of the repo. Default to `None`.z	--licensez?Optional, License of the repo. Default to `Apache License 2.0`.)r   r   r   r   z
--endpointz9Optional, The modelscope server address. Default to None.zAIGC Model Creationz;Arguments for creating an AIGC model. Use --aigc to enable.z--aigc
store_truezEnable AIGC model creation.)actionr   z--from_jsonztPath to a JSON file containing AIGC model configuration. If used, all other parameters except --repo_id are ignored.z--model_pathz!Path to the model file or folder.z--aigc_typez4AIGC type. Recommended: 'Checkpoint', 'LoRA', 'VAE'.z--base_model_typezBase model type, e.g., SD_XL.z
--revisionzv1.0z#Model revision. Defaults to 'v1.0'.z--base_model_id zBase model ID from ModelScope.z--descriptionzThis is an AIGC model.zModel description.z--path_in_repoz$Path in the repository to upload to.)func)
add_parserr   nameadd_argumentstrr   r   r   PUBLICZINTERNALZPRIVATEr   Zto_listZ	APACHE_V2add_argument_groupset_defaultsr   )r   parserZ
aigc_groupr   r   r   define_args   s   zCreateCMD.define_argsc                 C   s4   | j jr| j jtkrtd|   d S |   d S )Nz:AIGC models can only be created when repo_type is "model".)r   Zaigc	repo_typer   
ValueError_create_aigc_model_create_regular_repo)r   r   r   r   execute   s   zCreateCMD.executec                 C   sJ   t | jjd}|j| jj| jj| jj| jj| jj| jj	dd| jjd	 d S )NendpointT)	r   token
visibilityr*   chinese_namelicenseexist_okZcreate_default_configr0   )
r   r   r0   Zcreate_repor   r1   r2   r*   r3   r4   )r   apir   r   r   r-      s   
zCreateCMD._create_regular_repoc              
   C   s0  t | jjd}| jj}| jjr!td| jj  t| jj}n/td t	| jj
| jj| jjgs7tdt| jj
| jj| jj| jj| jj| jj| jjd}dd t D }|| jjtj}z|j|| jj|| jj| jj|d}td	|  W dS  ty } ztd
|  W Y d}~dS d}~ww )zExecute the command.r/   z$Creating AIGC model from JSON file: z2Creating AIGC model from command line arguments...z`Error: --model_path, --aigc_type, and --base_model_type are required when not using --from_json.)
model_path	aigc_typebase_model_typerevisiondescriptionbase_model_idpath_in_repoc                 S   s   i | ]\}}||qS r   r   ).0kvr   r   r   
<dictcomp>   s    z0CreateCMD._create_aigc_model.<locals>.<dictcomp>)model_idr1   r2   r4   r3   
aigc_modelz!Successfully created AIGC model: zError creating AIGC model: N)r   r   r0   r   	from_jsonloggerinfor
   Zfrom_json_fileallr7   r8   r9   r+   r:   r;   r<   r=   r	   itemsgetr2   r   r%   Zcreate_modelr1   r4   r3   print	Exception)r   r6   rB   rC   Zreverse_visibility_mapZvisibility_idxZ	model_urler   r   r   r,      sT   

zCreateCMD._create_aigc_modelN)__name__
__module____qualname____doc__r"   r   r   staticmethodr)   r.   r-   r,   r   r   r   r   r      s    `
r   N)argparser   r   Zmodelscope.cli.baser   Zmodelscope.hub.apir   Zmodelscope.hub.constantsr   r   r   r	   Zmodelscope.hub.utils.aigcr
   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   rE   r   r   r   r   r   r   <module>   s   