
    9i^,                     z   S SK r S SKJr  S SKJr  S SKJr  S SKrS SKrS SK	J
r
JrJrJr  S SKJr  S SKJrJr  S SKJrJrJrJrJrJr  S S	KJr  S S
KJr  \" 5       r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$S r%\ " S S\5      5       r&\ " S S\5      5       r'\ " S S\5      5       r(\ " S S\(5      5       r)\ " S S\(5      5       r*\ " S  S!\5      5       r+ " S" S#\5      r,\ " S$ S%\,5      5       r-\ " S& S'\,5      5       r.\ " S( S)\,5      5       r/ " S* S+\5      r0g),    N)ABC)
HTTPStatus)Optional)asdictdefinefield
validators)ModelScopeConfig)API_RESPONSE_FIELD_DATAAPI_RESPONSE_FIELD_MESSAGE)NotLoginExceptionNotSupportErrorRequestErrorhandle_http_responseis_okraise_for_http_status)get_endpoint)
get_loggerc                       \ rS rSrSrSrSrg)Accelerator   cpugpu N)__name__
__module____qualname____firstlineno__CPUGPU__static_attributes__r       U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/hub/deploy.pyr   r      s    
C
Cr"   r   c                       \ rS rSrSrSrg)Vendor   easr   N)r   r   r   r   EASr!   r   r"   r#   r%   r%      s    
Cr"   r%   c                       \ rS rSrSrSrSrg)	EASRegion!   z
cn-beijingzcn-hangzhour   N)r   r   r   r   beijinghangzhour!   r   r"   r#   r*   r*   !   s    GHr"   r*   c                   (    \ rS rSrSrSrSrSrSrSr	g)	EASCpuInstanceType&   zTEAS Cpu Instance Type, ref(https://help.aliyun.com/document_detail/144261.html)
    zecs.c6.2xlargezecs.c6.4xlargezecs.c6.6xlargezecs.c6.8xlarger   N
r   r   r   r   __doc__tinysmallmediumlarger!   r   r"   r#   r/   r/   &   s    DEFEr"   r/   c                   (    \ rS rSrSrSrSrSrSrSr	g)	EASGpuInstanceType/   zTEAS Gpu Instance Type, ref(https://help.aliyun.com/document_detail/144261.html)
    zecs.gn5-c28g1.7xlargezecs.gn5-c8g1.4xlargezecs.gn6i-c24g1.12xlargezecs.gn6e-c12g1.3xlarger   Nr1   r   r"   r#   r8   r8   /   s    "D"E&F$Er"   r8   c                 ^    X R                   :  a  [        SU< SU R                   < S35      eg )Nz'min_replica' value: z- has to be smaller than 'max_replica' value: !)max_replica
ValueError)instance	attributevalues      r#   min_smaller_than_maxrA   8   s0    ###h**,- 	- $r"   c                       \ rS rSr% Sr\" S\R                  " S5      S9r\	\
S'   \" S\R                  " S5      \/S9r\	\
S'   Srg)	ServiceScalingConfig?   zResource scaling config
   Currently we ignore max_replica
Args:
    max_replica: maximum replica
    min_replica: minimum replica
   default	validatorr<   min_replicar   N)r   r   r   r   r2   r   r	   ger<   int__annotations__rA   rI   r!   r   r"   r#   rC   rC   ?   sJ     Q*--2BCKCjmmA.0DEGK Gr"   rC   c                       \ rS rSr% Sr\\S'   \\S'   \" \	R                  \R                  " \	R                  \	R                  /5      S9r\\S'   Srg)	ServiceResourceConfigL   zEas Resource request.

Args:
    accelerator: the accelerator(cpu|gpu)
    instance_type: the instance type.
    scaling: The instance scaling config.
instance_typescalingrF   acceleratorr   N)r   r   r   r   r2   strrL   rC   r   r   r   r	   in_r    rR   r!   r   r"   r#   rN   rN   L   sH     !!..+//;??!CDFK Fr"   rN   c                       \ rS rSrSrg)ServiceProviderParameters\   r   N)r   r   r   r   r!   r   r"   r#   rV   rV   \   s    r"   rV   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   Sr\\   \S'   \	" \
R                  \R                  " \
R                  /5      S9r\\S	'   S
rg)EASDeployParametersa   a%  Parameters for EAS Deployment.

Args:
    resource_group: the resource group to deploy, current default.
    region: The eas instance region(eg: cn-hangzhou).
    access_key_id: The eas account access key id.
    access_key_secret: The eas account access key secret.
    vendor: must be 'eas'
regionaccess_key_idaccess_key_secretNresource_grouprF   vendorr   )r   r   r   r   r2   rS   rL   r^   r   r   r%   r(   r	   rT   r_   r!   r   r"   r#   rY   rY   a   sS     K$(NHSM(

jnnfjj\&BDFC Dr"   rY   c                       \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr\\S'   \	" \
R                  \R                  " \
R                  /5      S9r\\S	'   S
rg)EASListParameterst   a$  EAS instance list parameters.

Args:
    resource_group: the resource group to deploy, current default.
    region: The eas instance region(eg: cn-hangzhou).
    access_key_id: The eas account access key id.
    access_key_secret: The eas account access key secret.
    vendor: must be 'eas'
r\   r]   Nr[   r^   rF   r_   r   )r   r   r   r   r2   rS   rL   r[   r^   r   r%   r(   r	   rT   r_   r!   r   r"   r#   ra   ra   t   sT     FCNC

jnnfjj\&BDFC Dr"   ra   c                   L    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   Sr	g	)
DeployServiceParameters   zDeploy service parameters

Args:
    instance_name: the name of the service.
    model_id: the modelscope model_id
    revision: the modelscope model revision
    resource: the resource requirement.
    provider: the cloud service provider.
instance_namemodel_idrevisionresourceproviderr   N)
r   r   r   r   r2   rS   rL   rN   rV   r!   r   r"   r#   rd   rd      s%     MM##''r"   rd   c                       \ rS rSrSrS rSrg)AttrsToQueryString   z/Convert the attrs class to json string.

Args:
c                     [        U R                  S S9n[        R                  " U5      n[	        U5        [
        R                  R                  U5      n[	        U5        SU-  nU$ )Nc                 
    US L$ Nr   )attrr@   s     r#   <lambda>1AttrsToQueryString.to_query_str.<locals>.<lambda>   s	    e46Gr"   )filterzprovider=%s)r   rj   jsondumpsprinturllibparse
quote_plus)self	self_dictjson_strsafe_strquery_params        r#   to_query_strAttrsToQueryString.to_query_str   sW    MM"GI	::i(h<<**84h#h.r"   r   N)r   r   r   r   r2   r   r!   r   r"   r#   rl   rl      s    
r"   rl   c                   <    \ rS rSr% \\S'   Sr\\S'   Sr\\S'   Sr	g)	ListServiceParameters   rj   r   skipd   limitr   N)
r   r   r   r   rV   rL   r   rK   r   r!   r   r"   r#   r   r      s    ''D#ME3r"   r   c                        \ rS rSr% \\S'   Srg)GetServiceParameters   rj   r   Nr   r   r   r   rV   rL   r!   r   r"   r#   r   r          ''r"   r   c                        \ rS rSr% \\S'   Srg)DeleteServiceParameters   rj   r   Nr   r   r"   r#   r   r      r   r"   r   c            
           \ rS rSrSrSS jrS\S\S\S\S	\4
S
 jr	S\S	\4S jr
S\S	\4S jr  SS	\S\\   S\\   4S jjrSrg)ServiceDeployer   zEFacilitate model deployment on to supported service provider(s).
    Nc                     Ub  UO	[        5       U l        S[        R                  " 5       0U l        [        R
                  " 5       U l        U R                  c  [        S5      eg )Nz
user-agentz5Token does not exist, please login with HubApi first.)r   endpointr
   get_user_agentheadersget_cookiescookiesr   )r{   r   s     r#   __init__ServiceDeployer.__init__   sY    $,$8ln$&6&E&E&GH'335<<#GI I  r"   rg   rh   rf   ri   rj   c                    UR                   [        R                  :w  a  [        SUR                   -  5      e[	        UUUUUS9nU R
                   S3n[        U5      n[        R                  " XxU R                  U R                  S9n	[        U	[        U R                  S5        U	R                  [        R                  :  au  U	R                  [        R                   :  aW  [#        U	R%                  5       5      (       a  U	R%                  5       [&           n
U
$ [)        U	R%                  5       [*           5      e[-        U	5        g)a  Deploy model to cloud, current we only support PAI EAS, this is an async API ,
and the deployment could take a while to finish remotely. Please check deploy instance
status separately via checking the status.

Args:
    model_id (str): The deployed model id
    revision (str): The model revision
    instance_name (str): The deployed model instance name.
    resource (ServiceResourceConfig): The service resource information.
    provider (ServiceProviderParameters): The service provider parameter

Raises:
    NotSupportError: Not supported platform.
    RequestError: The server return error.

Returns:
    ServiceInstanceInfo: The information of the deployed service instance.
z1Not support vendor: %s ,only support EAS current.)rf   rg   rh   ri   rj   z/api/v1/deployer/endpoint)ru   r   r   create_serviceN)r_   r%   r(   r   rd   r   r   requestspostr   r   r   loggerstatus_coder   OKMULTIPLE_CHOICESr   ru   r   r   r   r   )r{   rg   rh   rf   ri   rj   create_paramspathbodyrdatas              r#   createServiceDeployer.create   s   * ??fjj(!C"# # 0' -- 9:m$MMT\\4<<IQ6FG==JMM)ammj>Y>Y.YQVVXvvx 78"1668,F#GHH!!$r"   c                    [        US9nU R                  < SU< SUR                  5       < 3n[        R                  " X@R
                  U R                  S9n[        U[        U R
                  S5        UR                  [        R                  :X  aW  [        UR                  5       5      (       a  UR                  5       [           nU$ [        UR                  5       [            5      e[#        U5        g)a  Query the specified instance information.

Args:
    instance_name (str): The deployed instance name.
    provider (ServiceProviderParameters): The cloud provider information, for eas
        need region(eg: ch-hangzhou), access_key_id and access_key_secret.

Raises:
    RequestError: The request is failed from server.

Returns:
    Dict: The information of the requested service instance.
rj   /api/v1/deployer/endpoint/?r   r   get_serviceN)r   r   r   r   getr   r   r   r   r   r   r   r   ru   r   r   r   r   r{   rf   rj   paramsr   r   r   s          r#   r   ServiceDeployer.get   s     &x8MM=&*=*=*?ALL||T\\JQmD==JMM)QVVXvvx 78"1668,F#GHH!!$r"   c                    [        US9nU R                  < SU< SUR                  5       < 3n[        R                  " X@R
                  U R                  S9n[        U[        U R
                  S5        UR                  [        R                  :X  aW  [        UR                  5       5      (       a  UR                  5       [           nU$ [        UR                  5       [            5      e[#        U5        g)a  Delete deployed model, this api send delete command and return, it will take
some to delete, please check through the cloud console.

Args:
    instance_name (str): The instance name you want to delete.
    provider (ServiceProviderParameters): The cloud provider information, for eas
        need region(eg: ch-hangzhou), access_key_id and access_key_secret.

Raises:
    RequestError: The request is failed.

Returns:
    Dict: The deleted instance information.
r   r   r   r   delete_serviceN)r   r   r   r   deleter   r   r   r   r   r   r   r   ru   r   r   r   r   r   s          r#   r   ServiceDeployer.delete  s     )(;MM=&*=*=*?AOOD,,MQ6FG==JMM)QVVXvvx 78"1668,F#GHH!!$r"   r   r   c                    [        XUS9nU R                  < SUR                  5       < 3n[        R                  " XPR
                  U R                  S9n[        U[        U R
                  S5        UR                  [        R                  :X  aW  [        UR                  5       5      (       a  UR                  5       [           nU$ [        UR                  5       [            5      e[#        U5        g)a  List deployed model instances.

Args:
    provider (ServiceProviderParameters): The cloud service provider parameter,
        for eas, need access_key_id and access_key_secret.
    skip (int, optional): start of the list, current not support.
    limit (int, optional): maximum number of instances return, current not support

Raises:
    RequestError: The request is failed from server.

Returns:
    List: List of instance information
)rj   r   r   z/api/v1/deployer/endpoint?r   list_service_instancesN)r   r   r   r   r   r   r   r   r   r   r   r   r   ru   r   r   r   r   )r{   rj   r   r   r   r   r   r   s           r#   listServiceDeployer.list1  s    & '737==393F3F3HJLL||T\\JQ6NO==JMM)QVVXvvx 78"1668,F#GHH!!$r"   )r   r   r   rp   )r   r   )r   r   r   r   r2   r   rS   rN   rV   r   r   r   r   rK   r   r!   r   r"   r#   r   r      s    I,s ,c ,# ,.,2,\ 0I :C 3L @ $%$'!0!C=! SM! !r"   r   )1rx   abcr   httpr   typingr   ru   r   attrsr   r   r   r	   modelscope.hub.apir
   modelscope.hub.constantsr   r   modelscope.hub.errorsr   r   r   r   r   r   modelscope.hub.utils.utilsr   modelscope.utils.loggerr   r   objectr   r%   r*   r/   r8   rA   rC   rN   rV   rY   ra   rd   rl   r   r   r   r   r   r"   r#   <module>r      s         3 3 /B: : 4 . 
& 
V  
 % %- 	G6 	G 	G FF F F 	 	 	 D3 D D$ D1 D D$ (f ( (" " .   (- ( ( (0 ( (Wf Wr"   