o
    )j!                     @   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 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)resolve_endpoint)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   V/var/www/html/Deteccion_Ine/venv/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   s  |  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dd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d0td1d2d |jd3td)d4d |jtd5 d S )6N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
--exist_ok
store_trueFzHIf True, do not raise error when repo already exists. Defaults to False.)actionr   r   z
--endpointzModelScope 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.zAIGC Model Creationz;Arguments for creating an AIGC model. Use --aigc to enable.z--aigczEnable AIGC model creation.)r   r   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.z--model_sourceZUSER_UPLOADz_Source of the AIGC model. `USER_UPLOAD`, `TRAINED_FROM_MODELSCOPE` or `TRAINED_FROM_ALIYUN_FC`.z--base_model_sub_typez)Base model sub type, e.g., Qwen_Edit_2509)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   sR   t | jj}t|d}|j| jj| jj| jj| jj| jj	| jj
| jjd|d	 d S )NendpointT)	r   token
visibilityr+   chinese_namelicenseexist_okZcreate_default_configr1   )r   r   r1   r   Zcreate_repor   r2   r3   r+   r4   r5   r6   )r   r1   apir   r   r   r.      s   

zCreateCMD._create_regular_repoc           	      C   sD  t | jj}t|d}| jj}| jjr%td| jj  t	| jj}n5td t
| jj| jj| jjgs;tdt| jj| jj| 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.r0   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tagdescriptionbase_model_idpath_in_repomodel_sourcebase_model_sub_typec                 S   s   i | ]\}}||qS r   r   ).0kvr   r   r   
<dictcomp>   s    z0CreateCMD._create_aigc_model.<locals>.<dictcomp>)model_idr2   r3   r5   r4   
aigc_modelz!Successfully created AIGC model: zError creating AIGC model: N)!r   r   r1   r   r   	from_jsonloggerinfor
   Zfrom_json_fileallr8   r9   r:   r,   revisionr<   r=   r>   r?   r@   r	   itemsgetr3   r   r&   Zcreate_modelr2   r5   r4   print	Exception)	r   r1   r7   rE   rF   Zreverse_visibility_mapZvisibility_idxZ	model_urler   r   r   r-      sZ   


zCreateCMD._create_aigc_modelN)__name__
__module____qualname____doc__r#   r   r   staticmethodr*   r/   r.   r-   r   r   r   r   r      s    w
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.hub.utils.utilsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   rH   r   r   r   r   r   r   <module>   s   