o
    i8                  &   @   sn  d Z ddlZddlmZmZmZ ddlZddlmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZ edd	Zed
d	Zeeejdd	f Zeee ejdd	f Zeee ejdd	f ZdeddfddZejddgddejddfdededededdf
ddZejddgddddddddddded eeejd!d	f d"eeejd#d	f d$eeejd%d	f d&eeejd'd	f d(eeejd)d	f d*eeejd+d	f d,eeejd-d	f ded.eee ejd/d	f ded0ee ejd1d	f d2ee ejd3d	f d4eee  ejd5d	f d6eee	 ejd7d	f d8eee! ejd9d	f ddf"d:dZ"ejdd;gd			dad eeejd!d	f dedededdf
d<d=Z#	dbdeddfd>d?Z$ejdd@gde$ ejdAdBdCe$ ej%edDdE ejdFgd		dcdedededdfdGdHZ&ejdIgd														dddeded eee ejd!d	f d$eee ejd%d	f d&eee ejd'd	f d(eee ejd)d	f d"eee ejdJd	f dKeee ejdLd	f d.eee ejd/d	f d0eee  ejd1d	f d2eee  ejd3d	f d4eee  ejd5d	f d6eee	 ejd7d	f d8eee! ejd9d	f deddf dMdNZ'ejdOgd			dedededPee(ejdQdRd	f deddf
dSdTZ)ejdUgd		dcdedededdfdVdWZ*ejdXgd			dedededYee(ejdZd[d	f deddf
d\d]Z+ejd^gd		dcdedededdfd_d`Z,dS )fz2CLI commands for Hugging Face Inference Endpoints.    N)	AnnotatedAnyOptional)InferenceEndpointInferenceEndpointScalingMetric)HfHubHTTPError   )	FormatOptOutputFormatQuietOptTokenOpt
get_hf_apiprint_list_outputtyper_factoryz(Manage Hugging Face Inference Endpoints.)helpz.Interact with the Inference Endpoints catalog.zEndpoint name.z_The namespace associated with the Inference Endpoint. Defaults to the current user's namespace.endpointreturnc                 C   s   t tj| jddd d S )N   Tindent	sort_keys)typerechojsondumpsraw)r    r   i/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/huggingface_hub/cli/inference_endpoints.py_print_endpoint,   s   r   zhf endpoints lsz"hf endpoints ls --namespace my-org)examplesF	namespaceformatquiettokenc           	   
   C   s   t |d}z	|j| |d}W n ty+ } ztd|  tj|jjd|d}~ww dd |D }dtt	t
f d	tt	 fd
d}t|||dg d|d dS )z6Lists all Inference Endpoints for the given namespace.r#   )r    r#   zListing failed: codeNc                 S   s   g | ]}|j qS r   )r   ).0r   r   r   r   
<listcomp>?   s    zls.<locals>.<listcomp>itemr   c                 S   s
  |  di }|  di }|  di }|  di }t|  ddtt|tr+| ddndtt|tr9| ddndtt|trG| d	dndtt|trU| d
dndtt|trc| ddndtt|trq| ddndtt|tr| ddgS dgS )Nstatusmodelcomputeprovidername 
repositorystatetask	frameworkinstanceTypevendorregion)getstr
isinstancedict)r)   r*   r+   r,   r-   r   r   r   row_fnA   s   zls.<locals>.row_fnr.   )NAMEMODELSTATUSTASK	FRAMEWORKINSTANCEVENDORREGION)itemsr!   r"   id_keyheadersr;   )r   list_inference_endpointsr   r   r   Exitresponsestatus_coder:   r8   r   listr   )	r    r!   r"   r#   api	endpointserrorresultsr;   r   r   r   ls0   s$   

rP   deployzChf endpoints deploy my-endpoint --repo gpt2 --framework pytorch ...)r.   r   )r    r2   r#   min_replicamax_replicascale_to_zero_timeoutscaling_metricscaling_thresholdr.   repozeThe name of the model repository associated with the Inference Endpoint (e.g. 'openai/gpt-oss-120b').r3   z@The machine learning framework used for the model (e.g. 'vllm').acceleratorz?The hardware accelerator to be used for inference (e.g. 'cpu').instance_sizezNThe size or type of the instance to be used for hosting the model (e.g. 'x4').instance_typezYThe cloud instance type where the Inference Endpoint will be deployed (e.g. 'intel-icl').r6   zTThe cloud region in which the Inference Endpoint will be created (e.g. 'us-east-1').r5   zVThe cloud provider or vendor where the Inference Endpoint will be hosted (e.g. 'aws').r2   zCThe task on which to deploy the model (e.g. 'text-classification').rR   zVThe minimum number of replicas (instances) to keep running for the Inference Endpoint.rS   zRThe maximum number of replicas (instances) to scale to for the Inference Endpoint.rT   zFThe duration in minutes before an inactive endpoint is scaled to zero.rU   z!The metric reference for scaling.rV   zeThe scaling metric threshold used to trigger a scale up. Ignored when scaling metric is not provided.c                C   s   t |
d}|jdi d| d|d|d|d|d|d|d	|d
|d|	d|
d|d|d|d|d|}t| dS )z3Deploy an Inference Endpoint from a Hub repository.r$   r.   r0   r3   rX   rY   rZ   r6   r5   r    r2   r#   rR   rS   rU   rV   rT   Nr   )r   create_inference_endpointr   )r.   rW   r3   rX   rY   rZ   r6   r5   r    r2   r#   rR   rS   rT   rU   rV   rL   r   r   r   r   rQ   [   sF   
V
	
zChf endpoints catalog deploy --repo meta-llama/Llama-3.2-1B-Instructc              
   C   sh   t |d}z|j| |||d}W n ty- } ztd|  tj|jjd|d}~ww t| dS )z4Deploy an Inference Endpoint from the Model Catalog.r$   )repo_idr.   r    r#   zDeployment failed: r%   N)	r   &create_inference_endpoint_from_catalogr   r   r   rH   rI   rJ   r   )rW   r.   r    r#   rL   r   rN   r   r   r   deploy_from_catalog   s   

r^   c              
   C   st   t | d}z|j| d}W n ty* } ztd|  tj|jjd|d}~ww ttj	d|iddd dS )	zList available Catalog models.r$   zCatalog fetch failed: r%   Nmodelsr   Tr   )
r   list_inference_catalogr   r   r   rH   rI   rJ   r   r   )r#   rL   r_   rN   r   r   r   list_catalog   s   
ra   zhf endpoints catalog lszlist-catalogT)r.   hiddencatalog)r.   z!hf endpoints describe my-endpointc              
   C   f   t |d}z
|j| ||d}W n ty, } ztd|  tj|jjd|d}~ww t| dS )z+Get information about an existing endpoint.r$   r.   r    r#   zFetch failed: r%   N)	r   get_inference_endpointr   r   r   rH   rI   rJ   r   r.   r    r#   rL   r   rN   r   r   r   describe      
rh   z/hf endpoints update my-endpoint --min-replica 2zBThe machine learning framework used for the model (e.g. 'custom').revisionzrThe specific model revision to deploy on the Inference Endpoint (e.g. '6c0e6080953db56375760c0471a8c5f2929baf11').c                 C   s~   t |d}z|j| |||||||||	|
||||d}W n ty8 } ztd|  tj|jjd|d}~ww t| dS )zUpdate an existing endpoint.r$   )r.   r    r0   r3   rj   r2   rX   rY   rZ   rR   rS   rT   rU   rV   r#   zUpdate failed: r%   N)	r   update_inference_endpointr   r   r   rH   rI   rJ   r   )r.   r    rW   rX   rY   rZ   r3   rj   r2   rR   rS   rT   rU   rV   r#   rL   r   rN   r   r   r   update
  s2   
O
rl   zhf endpoints delete my-endpointyesz--yeszSkip confirmation prompts.c              
   C   s   |st d|  d}|| krt d t jddt|d}z
|j| ||d W n tyF } zt d|  t j|jjd|d	}~ww t d
|  d d	S )z)Delete an Inference Endpoint permanently.zDelete endpoint 'z'? Type the name to confirm.zAborted.r   r%   r$   re   zDelete failed: Nz	Deleted 'z'.)	r   promptr   rH   r   delete_inference_endpointr   rI   rJ   )r.   r    rm   r#   confirmationrL   rN   r   r   r   deleter  s   

rq   zhf endpoints pause my-endpointc              
   C   rd   )zPause an Inference Endpoint.r$   re   zPause failed: r%   N)	r   pause_inference_endpointr   r   r   rH   rI   rJ   r   rg   r   r   r   pause  ri   rs   zhf endpoints resume my-endpointfail_if_already_runningz--fail-if-already-runningzWIf `True`, the method will raise an error if the Inference Endpoint is already running.c              
   C   sj   t |d}z|j| ||| d}W n ty. } ztd|  tj|jjd|d}~ww t| dS )zResume an Inference Endpoint.r$   )r.   r    r#   
running_okzResume failed: r%   N)	r   resume_inference_endpointr   r   r   rH   rI   rJ   r   )r.   r    rt   r#   rL   r   rN   r   r   r   resume  s   

rw   z&hf endpoints scale-to-zero my-endpointc              
   C   rd   )z$Scale an Inference Endpoint to zero.r$   re   zScale To Zero failed: r%   N)	r    scale_to_zero_inference_endpointr   r   r   rH   rI   rJ   r   rg   r   r   r   scale_to_zero  ri   ry   )NNN)N)NN)NNNNNNNNNNNNNN)NFN)-__doc__r   typingr   r   r   r   $huggingface_hub._inference_endpointsr   r   huggingface_hub.errorsr   
_cli_utilsr	   r
   r   r   r   r   r   ie_clicatalog_appr8   ArgumentNameArgOptionNameOptNamespaceOptr   commandtablerP   intfloatrQ   r^   ra   	add_typerrh   rl   boolrq   rs   rw   ry   r   r   r   r   <module>   s   $



*. &-.45;AGMSl	

	!'-39?EKLg
