o
    )j",                     @   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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-Z0G d+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cpuZgpuN)__name__
__module____qualname__CPUGPU r   r   V/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/hub/deploy.pyr          r   c                   @   s   e Zd ZdZdS )VendorZeasN)r   r   r   EASr   r   r   r   r       s    r    c                   @   r   )	EASRegionz
cn-beijingzcn-hangzhouN)r   r   r   ZbeijingZ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__ZtinyZsmallZmediumZ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   
r.   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	validatorr)   min_replicaN)r   r   r   r&   r   r   ger)   int__annotations__r.   r4   r   r   r   r   r/   ?   s   
 r/   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.
    Zinstance_typeZscalingr1   acceleratorN)r   r   r   r&   strr7   r/   r   r   r   r   in_r   r9   r   r   r   r   r8   L   s   
 r8   c                   @   s   e Zd ZdS )ServiceProviderParametersN)r   r   r   r   r   r   r   r<   \   s    r<   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_groupr1   vendor)r   r   r   r&   r:   r7   rA   r   r   r    r!   r   r;   rB   r   r   r   r   r=   a   s   
 	r=   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'
    r?   r@   Nr>   rA   r1   rB   )r   r   r   r&   r:   r7   r>   rA   r   r    r!   r   r;   rB   r   r   r   r   rC   t   s   
 	rC   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&   r:   r7   r8   r<   r   r   r   r   rD      s   
 	rD   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   )attrr-   r   r   r   <lambda>   s    z1AttrsToQueryString.to_query_str.<locals>.<lambda>)filterzprovider=%s)r   rI   jsondumpsprinturllibparse
quote_plus)selfZ	self_dictZjson_strsafe_strZquery_paramr   r   r   to_query_str   s   

zAttrsToQueryString.to_query_strN)r   r   r   r&   rV   r   r   r   r   rJ      s    rJ   c                   @   s.   e Zd ZU eed< dZeed< dZeed< dS )ListServiceParametersrI   r   skipd   limitN)r   r   r   r<   r7   rX   r6   rZ   r   r   r   r   rW      s   
 rW   c                   @      e Zd ZU eed< dS )GetServiceParametersrI   Nr   r   r   r<   r7   r   r   r   r   r\         
 r\   c                   @   r[   )DeleteServiceParametersrI   Nr]   r   r   r   r   r_      r^   r_   c                
   @   s   e Zd ZdZddee f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).
    Ntokenc                 C   s8   |d ur|nt  | _dt i| _t j|dd| _d S )Nz
user-agentT)Zaccess_tokenZcookies_required)r   endpointr
   Zget_user_agentheadersr	   Zget_cookiescookies)rT   rb   ra   r   r   r   __init__   s
   zServiceDeployer.__init__rF   rG   rE   rH   rI   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.)rE   rF   rG   rH   rI   z/api/v1/deployer/endpoint)rN   rd   rc   Zcreate_serviceN)rB   r    r!   r   rD   rb   r   requestspostrd   rc   r   loggerstatus_coder   OKMULTIPLE_CHOICESr   rN   r   r   r   r   )rT   rF   rG   rE   rH   rI   Z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.
        rI   !%s/api/v1/deployer/endpoint/%s?%srd   rc   Zget_serviceN)r\   rb   rV   rf   getrd   rc   r   rh   ri   r   rj   r   rN   r   r   r   r   rT   rE   rI   paramsrl   rn   ro   r   r   r   ru      s   
zServiceDeployer.getc                 C   rq   )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.
        rr   rs   rt   Zdelete_serviceN)r_   rb   rV   rf   deleterd   rc   r   rh   ri   r   rj   r   rN   r   r   r   r   rv   r   r   r   rx     s   
zServiceDeployer.deleter   rY   rX   rZ   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
        )rI   rX   rZ   z%s/api/v1/deployer/endpoint?%srt   Zlist_service_instancesN)rW   rb   rV   rf   ru   rd   rc   r   rh   ri   r   rj   r   rN   r   r   r   r   )rT   rI   rX   rZ   rw   rl   rn   ro   r   r   r   list/  s   zServiceDeployer.list)NN)r   rY   )r   r   r   r&   r   r:   re   r8   r<   rp   ru   rx   r6   ry   r   r   r   r   r`      s&    
. r`   )2rQ   abcr   httpr   typingr   rN   rf   attrsr   r   r   r   Zmodelscope.hub.apir	   r
   Zmodelscope.hub.constantsr   r   Zmodelscope.hub.errorsr   r   r   r   r   r   Zmodelscope.hub.utils.utilsr   Zmodelscope.utils.loggerr   rh   objectr   r    r"   r$   r(   r.   r/   r8   r<   r=   rC   rD   rJ   rW   r\   r_   r`   r   r   r   r   <module>   sN     		