
    uj",                        d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
mZmZmZ d dlmZmZ d dlmZmZ d dlmZmZmZmZmZmZ d d	lmZ d d
lmZ  e            Z G d de           Z! G d de           Z" G d de           Z# G d de           Z$ G d de           Z%d Z&e G d de                       Z'e G d de                       Z(e G d de                      Z)e G d de)                      Z*e G d de)                      Z+e G d  d!e                       Z, G d" d#e          Z-e G d$ d%e-                      Z.e G d& d'e-                      Z/e G d( d)e-                      Z0 G d* d+e           Z1dS ),    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                       e Zd ZdZdZdS )AcceleratorcpugpuN)__name__
__module____qualname__CPUGPU     e/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/hub/deploy.pyr   r      s        
C
CCCr    r   c                       e Zd ZdZdS )VendoreasN)r   r   r   EASr   r    r!   r#   r#      s        
CCCr    r#   c                       e Zd ZdZdZdS )	EASRegionz
cn-beijingzcn-hangzhouN)r   r   r   beijinghangzhour   r    r!   r'   r'   !   s        GHHHr    r'   c                   "    e Zd ZdZdZdZdZdZdS )EASCpuInstanceTypezTEAS Cpu Instance Type, ref(https://help.aliyun.com/document_detail/144261.html)
    zecs.c6.2xlargezecs.c6.4xlargezecs.c6.6xlargezecs.c6.8xlargeNr   r   r   __doc__tinysmallmediumlarger   r    r!   r+   r+   &   s,         DEFEEEr    r+   c                   "    e Zd ZdZdZdZdZdZdS )EASGpuInstanceTypezTEAS 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.3xlargeNr,   r   r    r!   r3   r3   /   s,         "D"E&F$EEEr    r3   c                 R    || j         k    rt          d|d| j         d          d S )Nz'min_replica' value: z- has to be smaller than 'max_replica' value: !)max_replica
ValueError)instance	attributevalues      r!   min_smaller_than_maxr;   8   sC    x###juuh***,- - 	- $#r    c                       e Zd ZU dZ ed ej        d                    Zee	d<    ed ej        d          e
g          Zee	d<   dS )ServiceScalingConfigzResource scaling config
       Currently we ignore max_replica
    Args:
        max_replica: maximum replica
        min_replica: minimum replica
       default	validatorr6   min_replicaN)r   r   r   r-   r   r	   ger6   int__annotations__r;   rB   r   r    r!   r=   r=   ?   s           uQ-*-2B2BCCCKCCCumjmA..0DEG G GK G G G G Gr    r=   c                       e Zd ZU dZeed<   eed<    eej	         e
j        ej	        ej        g                    Zeed<   dS )ServiceResourceConfigzEas Resource request.

    Args:
        accelerator: the accelerator(cpu|gpu)
        instance_type: the instance type.
        scaling: The instance scaling config.
    instance_typescalingr?   acceleratorN)r   r   r   r-   strrE   r=   r   r   r   r	   in_r   rJ   r   r    r!   rG   rG   L   s           !!!!u *.+/;?!CDDF F FK F F F F Fr    rG   c                       e Zd ZdS )ServiceProviderParametersN)r   r   r   r   r    r!   rN   rN   \   s        Dr    rN   c                       e Zd ZU dZeed<   eed<   eed<   dZee         ed<    ee	j
         ej        e	j
        g                    Zeed<   dS )	EASDeployParametersaA  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_groupr?   vendor)r   r   r   r-   rK   rE   rT   r   r   r#   r%   r	   rL   rU   r   r    r!   rP   rP   a   s           KKK$(NHSM(((%
njnfj\&B&BD D DFC D D D D Dr    rP   c                       e Zd ZU dZeed<   eed<   dZeed<   dZeed<    ee	j
         ej        e	j
        g                    Zeed<   dS )	EASListParametersa@  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'
    rR   rS   NrQ   rT   r?   rU   )r   r   r   r-   rK   rE   rQ   rT   r   r#   r%   r	   rL   rU   r   r    r!   rW   rW   t   s           FCNC%
njnfj\&B&BD D DFC D D D D Dr    rW   c                   F    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   dS )DeployServiceParametersa  Deploy 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providerN)r   r   r   r-   rK   rE   rG   rN   r   r    r!   rY   rY      sT           MMMMMM####''''''r    rY   c                       e Zd ZdZd ZdS )AttrsToQueryStringz7Convert the attrs class to json string.

    Args:
    c                     t          | j        d           }t          j        |          }t	          |           t
          j                            |          }t	          |           d|z  }|S )Nc                 
    |d uS )Nr   )attrr:   s     r!   <lambda>z1AttrsToQueryString.to_query_str.<locals>.<lambda>   s    e46G r    )filterzprovider=%s)r   r^   jsondumpsprinturllibparse
quote_plus)self	self_dictjson_strsafe_strquery_params        r!   to_query_strzAttrsToQueryString.to_query_str   sn    M"G"GI I I	:i((h<**844h#h.r    N)r   r   r   r-   rq   r   r    r!   r`   r`      s-         
    r    r`   c                   6    e Zd ZU eed<   dZeed<   dZeed<   dS )ListServiceParametersr^   r   skipd   limitN)r   r   r   rN   rE   rt   rD   rv   r   r    r!   rs   rs      s;         ''''D#MMME3r    rs   c                       e Zd ZU eed<   dS )GetServiceParametersr^   Nr   r   r   rN   rE   r   r    r!   rx   rx               ''''''r    rx   c                       e Zd ZU eed<   dS )DeleteServiceParametersr^   Nry   r   r    r!   r|   r|      rz   r    r|   c            
           e Zd ZdZddee         fdZdedededed	ef
d
Z	ded	efdZ
ded	efdZ	 	 dd	edee         dee         fdZdS )ServiceDeployerzEFacilitate model deployment on to supported service provider(s).
    Ntokenc                     ||nt                      | _        dt          j                    i| _        t                                          |d          | _        d S )Nz
user-agentT)access_tokencookies_required)r   endpointr   get_user_agentheadersr
   get_cookiescookies)rl   r   r   s      r!   __init__zServiceDeployer.__init__   sU    $,$8lnn$&6&E&G&GHxx++ , 7 7r    r[   r\   rZ   r]   r^   c                    |j         t          j        k    rt          d|j         z            t	          |||||          }| j         d}t          |          }t          j        ||| j	        | j
                  }	t          |	t          | j	        d           |	j        t          j        k    r|	j        t          j        k     rnt#          |	                                          r!|	                                t&                   }
|
S t)          |	                                t*                             t-          |	           dS )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.)rZ   r[   r\   r]   r^   z/api/v1/deployer/endpoint)rf   r   r   create_serviceN)rU   r#   r%   r   rY   r   r   requestspostr   r   r   loggerstatus_coder   OKMULTIPLE_CHOICESr   rf   r   r   r   r   )rl   r[   r\   rZ   r]   r^   create_paramspathbodyrdatas              r!   createzServiceDeployer.create   s/   * ?fj((!C"# # # 0'   -:::m$$MtT\4<I I IQ6FGGG=JM))amj>Y.Y.YQVVXX Ivvxx 78"16688,F#GHHH!!$$$tr    c                    t          |          }| j        d|d|                                }t          j        || j        | j                  }t          |t          | j        d           |j	        t          j        k    rnt          |                                          r!|                                t                   }|S t          |                                t                              t#          |           dS )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.
        r^   /api/v1/deployer/endpoint/?r   r   get_serviceN)rx   r   rq   r   getr   r   r   r   r   r   r   r   rf   r   r   r   r   rl   rZ   r^   paramsr   r   r   s          r!   r   zServiceDeployer.get   s     &x888MMM===&*=*=*?*?*?ALt|T\JJJQmDDD=JM))QVVXX Ivvxx 78"16688,F#GHHH!!$$$tr    c                    t          |          }| j        d|d|                                }t          j        || j        | j                  }t          |t          | j        d           |j	        t          j        k    rnt          |                                          r!|                                t                   }|S t          |                                t                              t#          |           dS )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   rq   r   deleter   r   r   r   r   r   r   r   rf   r   r   r   r   r   s          r!   r   zServiceDeployer.delete  s     )(;;;MMM===&*=*=*?*?*?AOD$,MMMQ6FGGG=JM))QVVXX Ivvxx 78"16688,F#GHHH!!$$$tr    r   ru   rt   rv   c                    t          |||          }| j        d|                                }t          j        || j        | j                  }t          |t          | j        d           |j	        t          j        k    rnt          |                                          r!|                                t                   }|S t          |                                t                              t#          |           dS )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
        )r^   rt   rv   z/api/v1/deployer/endpoint?r   list_service_instancesN)rs   r   rq   r   r   r   r   r   r   r   r   r   r   rf   r   r   r   r   )rl   r^   rt   rv   r   r   r   r   s           r!   listzServiceDeployer.list/  s    & 'D7 7 737===393F3F3H3H3HJLt|T\JJJQ6NOOO=JM))QVVXX Ivvxx 78"16688,F#GHHH!!$$$tr    )NN)r   ru   )r   r   r   r-   r   rK   r   rG   rN   r   r   r   rD   r   r   r    r!   r~   r~      s        7 7Xc] 7 7 7 7,s ,c ,# ,.,2, , , ,\ 0I    :C 3L    @ $%$'! !0!C=! SM! ! ! ! ! !r    r~   )2ri   abcr   httpr   typingr   rf   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'   r+   r3   r;   r=   rG   rN   rP   rW   rY   r`   rs   rx   r|   r~   r   r    r!   <module>r      s                        3 3 3 3 3 3 3 3 3 3 3 3 7 7 7 7 7 7 7 7B B B B B B B B: : : : : : : : : : : : : : : : 4 3 3 3 3 3 . . . . . . 
    &   
    V          
       % % % % % % % %- - - 	G 	G 	G 	G 	G6 	G 	G 	G F F F F FF F F F 	 	 	 	 	 	 	 	 D D D D D3 D D D$ D D D D D1 D D D$ ( ( ( ( (f ( ( ("       "     .    ( ( ( ( (- ( ( ( ( ( ( ( (0 ( ( (U U U U Uf U U U U Ur    