o
    ߥi^,                     @   s  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 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 deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$dd Z%eG dd deZ&eG dd deZ'eG dd deZ(eG dd de(Z)eG dd  d e(Z*eG d!d" d"eZ+G d#d$ d$eZ,eG d%d& d&e,Z-eG d'd( d(e,Z.eG d)d* d*e,Z/G d+d, d,eZ0dS )-    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                   @      e Zd ZdZdZdS )AcceleratorcpugpuN)__name__
__module____qualname__CPUGPU r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/hub/deploy.pyr          r   c                   @   s   e Zd ZdZdS )VendoreasN)r   r   r   EASr   r   r   r   r       s    r    c                   @   r   )	EASRegionz
cn-beijingzcn-hangzhouN)r   r   r   beijinghangzhour   r   r   r   r#   !   r   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'   &       r'   c                   @   r&   )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   r   r/   /   r.   r/   c                 C   s    || j krtd|| j f d S )NzG'min_replica' value: %s has to be smaller than 'max_replica' value: %s!)max_replica
ValueError)instance	attributevaluer   r   r   min_smaller_than_max8   s   
r5   c                   @   sJ   e Zd ZU dZededdZee	d< edede
gdZee	d< dS )ServiceScalingConfigzResource scaling config
       Currently we ignore max_replica
    Args:
        max_replica: maximum replica
        min_replica: minimum replica
       default	validatorr0   min_replicaN)r   r   r   r)   r   r   ger0   int__annotations__r5   r;   r   r   r   r   r6   ?   s   
 r6   c                   @   sF   e Zd ZU dZeed< eed< eej	e
ej	ejgd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scalingr8   acceleratorN)r   r   r   r)   strr>   r6   r   r   r   r   in_r   rB   r   r   r   r   r?   L   s   
 r?   c                   @   s   e Zd ZdS )ServiceProviderParametersN)r   r   r   r   r   r   r   rE   \   s    rE   c                   @   sZ   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e	j
gd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_groupr8   vendor)r   r   r   r)   rC   r>   rJ   r   r   r    r"   r   rD   rK   r   r   r   r   rF   a   s   
 	rF   c                   @   sZ   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e	j
gd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'
    rH   rI   NrG   rJ   r8   rK   )r   r   r   r)   rC   r>   rG   rJ   r   r    r"   r   rD   rK   r   r   r   r   rL   t   s   
 	rL   c                   @   s:   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)   rC   r>   r?   rE   r   r   r   r   rM      s   
 	rM   c                   @   s   e Zd ZdZdd ZdS )AttrsToQueryStringz7Convert the attrs class to json string.

    Args:
    c                 C   sD   t | jdd d}t|}t| tj|}t| d| }|S )Nc                 S   s   |d uS Nr   )attrr4   r   r   r   <lambda>   s    z1AttrsToQueryString.to_query_str.<locals>.<lambda>)filterzprovider=%s)r   rR   jsondumpsprinturllibparse
quote_plus)self	self_dictjson_strsafe_strquery_paramr   r   r   to_query_str   s   

zAttrsToQueryString.to_query_strN)r   r   r   r)   rc   r   r   r   r   rS      s    rS   c                   @   s.   e Zd ZU eed< dZeed< dZeed< dS )ListServiceParametersrR   r   skipd   limitN)r   r   r   rE   r>   re   r=   rg   r   r   r   r   rd      s   
 rd   c                   @      e Zd ZU eed< dS )GetServiceParametersrR   Nr   r   r   rE   r>   r   r   r   r   ri         
 ri   c                   @   rh   )DeleteServiceParametersrR   Nrj   r   r   r   r   rl      rk   rl   c                
   @   s   e Zd ZdZdddZdedededed	ef
d
dZded	efddZ	ded	efddZ
		dd	edee dee fddZdS )ServiceDeployerzEFacilitate model deployment on to supported service provider(s).
    Nc                 C   sB   |d ur|nt  | _dt i| _t | _| jd u rtdd S )Nz
user-agentz5Token does not exist, please login with HubApi first.)r   endpointr	   get_user_agentheadersget_cookiescookiesr   )r^   rn   r   r   r   __init__   s   

zServiceDeployer.__init__rO   rP   rN   rQ   rR   c                 C   s   |j tjkrtd|j  t|||||d}| j d}t|}tj||| j	| j
d}	t|	t| j	d |	jtjkrU|	jtjk rUt|	 rM|	 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.)rN   rO   rP   rQ   rR   z/api/v1/deployer/endpoint)rX   rr   rp   create_serviceN)rK   r    r"   r   rM   rn   r   requestspostrr   rp   r   loggerstatus_coder   OKMULTIPLE_CHOICESr   rX   r
   r   r   r   )r^   rO   rP   rN   rQ   rR   create_paramspathbodyrdatar   r   r   create   s4   zServiceDeployer.createc                 C      t |d}d| j|| f }tj|| j| jd}t|t| jd |j	t
jkr=t| r5| 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.
        rR   !%s/api/v1/deployer/endpoint/%s?%srr   rp   get_serviceN)ri   rn   rc   ru   getrr   rp   r   rw   rx   r   ry   r   rX   r
   r   r   r   r^   rN   rR   paramsr|   r~   r   r   r   r   r      s   
zServiceDeployer.getc                 C   r   )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   delete_serviceN)rl   rn   rc   ru   deleterr   rp   r   rw   rx   r   ry   r   rX   r
   r   r   r   r   r   r   r   r     s   
zServiceDeployer.deleter   rf   re   rg   c                 C   s   t |||d}d| j| f }tj|| j| jd}t|t| jd |j	t
jkr>t| r6| 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
        )rR   re   rg   z%s/api/v1/deployer/endpoint?%sr   list_service_instancesN)rd   rn   rc   ru   r   rr   rp   r   rw   rx   r   ry   r   rX   r
   r   r   r   )r^   rR   re   rg   r   r|   r~   r   r   r   r   list1  s   zServiceDeployer.listrT   )r   rf   )r   r   r   r)   rs   rC   r?   rE   r   r   r   r   r=   r   r   r   r   r   rm      s&    
	
. rm   )1r[   abcr   httpr   typingr   rX   ru   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   rw   objectr   r    r#   r'   r/   r5   r6   r?   rE   rF   rL   rM   rS   rd   ri   rl   rm   r   r   r   r   <module>   sN     		