
    uj!                         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	 Z G 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                      t          |           S )z> Function which will be called for a specific sub parser.
    )	CreateCMDargss    e/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/cli/create.pysubparser_funcr      s     T??    c                   P    e Zd ZdZdZdefdZedefd            Zd Z	d Z
d	 Zd
S )r   zS
    Command for creating a new repository, supporting both model and dataset.
    creater   c                     || _         d S )Nr   )selfr   s     r   __init__zCreateCMD.__init__   s    			r   parsersc                    |                      t          j                  }|                    dt          d           |                    dt          d d           |                    dt
          t          d	           |                    d
t          j        t          j	        t          j
        gt          j        d	           |                    dt          d d           |                    dt          t          j                    t          j        d           |                    dddd           |                    dt          d d           |                    dd          }|                    ddd           |                    dt          d           |                    dt          d            |                    d!t          d"           |                    d#t          d$           |                    d%t          d&d'           |                    d(t          d)d*           |                    d+t          d,d-           |                    d.t          d)d/           |                    d0t          d1d2           |                    d3t          d)d4           |                    t           5           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_source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	   PUBLICINTERNALPRIVATEr   to_list	APACHE_V2add_argument_groupset_defaultsr   )r   parser
aigc_groups      r   define_argszCreateCMD.define_args    s    ")!3!3IN!C!CK 	 	M 	M 	M 	T 	 	
 	
 	
 	%#W 	 	
 	
 	
 	!:#6
8J %I 	 	
 	
 	
 	I	 	 	
 	
 	
 	$&&&M 	 	
 	
 	
 	V 	 	
 	
 	
 	:	 	 		
 		
 		
 ..!IK K
 	\0M 	  	O 	O 	OJ 	  	K 	K 	K
 	+N 	  	P 	P 	PG 	  	I 	I 	I 	0 	  	2 	2 	2 	6	 	  	8 	8 	8
 	1	 	  	3 	3 	3
 	,%	 	  	' 	' 	'
 	7	 	  	9 	9 	9
 	!m 	  	
 	
 	
 	#<	 	  	> 	> 	> 	00000r   c                     | 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   aigc	repo_typer   
ValueError_create_aigc_model_create_regular_repo)r   s    r   executezCreateCMD.execute   se    9> 	(y"o55 P   ##%%%%%%%'''''r   c                     t          | j        j                  }t          |          }|                    | j        j        | j        j        | j        j        | j        j        | j        j	        | j        j
        | j        j        d|	  	         d S )NendpointT)	r   token
visibilityr8   chinese_namelicenseexist_okcreate_default_configr?   )r   r   r?   r   create_repor   r@   rA   r8   rB   rC   rD   )r   r?   apis      r   r;   zCreateCMD._create_regular_repo   s     $DI$677h''' 	I%)/y+i)/I%Y'"& 	 
	
 
	
 
	
 
	
 
	
r   c                 $   t          | j        j                  }t          |          }| j        j        }| j        j        rFt                              d| j        j                    t          j	        | j        j                  }nt                              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 t+          j                    D             }|                    | j        j        t2          j                  }	 |                    || j        j        || j        j        | j        j        |          }t?          d|            d
S # t@          $ r}t?          d	|            Y d
}~d
S d
}~ww xY 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tagdescriptionbase_model_idpath_in_repomodel_sourcebase_model_sub_typec                     i | ]\  }}||	S  rS   ).0kvs      r   
<dictcomp>z0CreateCMD._create_aigc_model.<locals>.<dictcomp>   s    !I!I!I41a!Q!I!I!Ir   )model_idr@   rA   rC   rB   
aigc_modelz!Successfully created AIGC model: zError creating AIGC model: N)!r   r   r?   r   r   	from_jsonloggerinfor   from_json_fileallrI   rJ   rK   r9   revisionrM   rN   rO   rP   rQ   r
   itemsgetrA   r   r,   create_modelr@   rC   rB   print	Exception)	r   r?   rG   rX   rY   reverse_visibility_mapvisibility_idx	model_urles	            r   r:   zCreateCMD._create_aigc_model   s!   #DI$677h'''9$9 	KK 19.1 1 2 2 2"1$)2EFFJJ KKLMMMI($)*=I-   $ !#$ $ $
 #9/)- $	 9I& I1"i5!Y3!Y3$(I$A
 
 
J "J!I=3F3H3H!I!I!I488I /"8: :
	5((!io)	)!Y3% ) ' 'I AiAABBBBB 	5 	5 	5333444444444	5s   AG( (
H2H

HN)__name__
__module____qualname____doc__r)   r   r   staticmethodr5   r<   r;   r:   rS   r   r   r   r      s          D.     u1. u1 u1 u1 \u1n( ( (
 
 
&25 25 25 25 25r   r   N)argparser   r   modelscope.cli.baser   modelscope.hub.apir   modelscope.hub.constantsr   r   r	   r
   modelscope.hub.utils.aigcr   modelscope.hub.utils.utilsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r[   r   r   rS   r   r   <module>rv      s#   6 6 6 6 6 6 6 6 * * * * * * % % % % % %5 5 5 5 5 5 5 5 5 5 5 5 / / / / / / 7 7 7 7 7 7 H H H H H H H H . . . . . .	  Q5 Q5 Q5 Q5 Q5
 Q5 Q5 Q5 Q5 Q5r   