
    Ƒi",                     ~   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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       r0 " S* S+\ 5      r1g),    N)ABC)
HTTPStatus)Optional)asdictdefinefield
validators)HubApi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/banglarbhumi/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$   r0   r0   &   s    DEFEr#   r0   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   Nr2   r   r#   r$   r9   r9   /   s    "D"E&F$Er#   r9   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_maxrB   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   r3   r   r	   ger=   int__annotations__rB   rJ   r"   r   r#   r$   rD   rD   ?   sJ     Q*--2BCKCjmmA.0DEGK Gr#   rD   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scalingrG   acceleratorr   N)r   r   r   r   r3   strrM   rD   r   r   r    r	   in_r!   rS   r"   r   r#   r$   rO   rO   L   sH     !!..+//;??!CDFK Fr#   rO   c                       \ rS rSrSrg)ServiceProviderParameters\   r   N)r   r   r   r   r"   r   r#   r$   rW   rW   \   s    r#   rW   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_grouprG   vendorr   )r   r   r   r   r3   rT   rM   r_   r   r   r&   r)   r	   rU   r`   r"   r   r#   r$   rZ   rZ   a   sS     K$(NHSM(

jnnfjj\&BDFC Dr#   rZ   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_   rG   r`   r   )r   r   r   r   r3   rT   rM   r\   r_   r   r&   r)   r	   rU   r`   r"   r   r#   r$   rb   rb   t   sT     FCNC

jnnfjj\&BDFC Dr#   rb   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   r3   rT   rM   rO   rW   r"   r   r#   r$   re   re      s%     MM##''r#   re   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   )attrrA   s     r$   <lambda>1AttrsToQueryString.to_query_str.<locals>.<lambda>   s	    e46Gr#   )filterzprovider=%s)r   rk   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   r3   r   r"   r   r#   r$   rm   rm      s    
r#   rm   c                   <    \ rS rSr% \\S'   Sr\\S'   Sr\\S'   Sr	g)	ListServiceParameters   rk   r   skipd   limitr   N)
r   r   r   r   rW   rM   r   rL   r   r"   r   r#   r$   r   r      s    ''D#ME3r#   r   c                        \ rS rSr% \\S'   Srg)GetServiceParameters   rk   r   Nr   r   r   r   rW   rM   r"   r   r#   r$   r   r          ''r#   r   c                        \ rS rSr% \\S'   Srg)DeleteServiceParameters   rk   r   Nr   r   r#   r$   r   r      r   r#   r   c            
           \ rS rSrSrSS\\   4S j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).
    Ntokenc                     Ub  UO	[        5       U l        S[        R                  " 5       0U l        [        5       R                  USS9U l        g )Nz
user-agentT)access_tokencookies_required)r   endpointr   get_user_agentheadersr
   get_cookiescookies)r{   r   r   s      r$   __init__ServiceDeployer.__init__   sF    $,$8ln$&6&E&E&GHx++ , 7r#   rh   ri   rg   rj   rk   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.)rg   rh   ri   rj   rk   z/api/v1/deployer/endpoint)ru   r   r   create_serviceN)r`   r&   r)   r   re   r   r   requestspostr   r   r   loggerstatus_coder   OKMULTIPLE_CHOICESr   ru   r   r   r   r   )r{   rh   ri   rg   rj   rk   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.
rk   /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{   rg   rk   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
)rk   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{   rk   r   r   r   r   r   r   s           r$   listServiceDeployer.list/  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   )NN)r   r   )r   r   r   r   r3   r   rT   r   rO   rW   r   r   r   rL   r   r"   r   r#   r$   r   r      s    7Xc] 7,s ,c ,# ,.,2,\ 0I :C 3L @ $%$'!0!C=! SM! !r#   r   )2rx   abcr   httpr   typingr   ru   r   attrsr   r   r   r	   modelscope.hub.apir
   r   modelscope.hub.constantsr   r   modelscope.hub.errorsr   r   r   r   r   r   modelscope.hub.utils.utilsr   modelscope.utils.loggerr   r   objectr   r&   r+   r0   r9   rB   rD   rO   rW   rZ   rb   re   rm   r   r   r   r   r   r#   r$   <module>r      s         3 3 7B: : 4 . 
& 
V  
 % %- 	G6 	G 	G FF F F 	 	 	 D3 D D$ D1 D D$ (f ( (" " .   (- ( ( (0 ( (Uf Ur#   