o
    0i9                  &   @   s  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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d0d	f ded1ee ejd2d	f d3ee ejd4d	f d5eee  ejd6d	f d7eee	 ejd8d	f d9eee! ejd:d	f ddf"d;dZ"ejdd<gd				dcd!eeejd"d	f d ed%eee ejd=d	f dededdfd>d?Z#	dddeddfd@dAZ$ejddBgde$ ejdCdDdEe$ ej%edFdG ejdHgd		ded edededdfdIdJZ&ejdKgd														dfd 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dLd	f dMeee ejdNd	f d/eee ejd0d	f d1eee  ejd2d	f d3eee  ejd4d	f d5eee  ejd6d	f d7eee	 ejd8d	f d9eee! ejd:d	f deddf dOdPZ'ejdQgd			dgd ededRee(ejdSdTd	f deddf
dUdVZ)ejdWgd		ded edededdfdXdYZ*ejdZgd			dgd eded[ee(ejd\d]d	f deddf
d^d_Z+ejd`gd		ded edededdfdadbZ,dS )hz2CLI 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   a/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/cli/inference_endpoints.py_print_endpoint,   s   r   z	list | lsz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	frameworkZinstanceTypevendorregion)getstr
isinstancedict)r)   r*   r+   r,   r-   r   r   r   row_fnA   s   zls.<locals>.row_fnr.   )NAMEZMODELZSTATUSZTASKZ	FRAMEWORKZINSTANCEZVENDORZREGION)itemsr!   r"   Zid_keyheadersr:   )r   Zlist_inference_endpointsr   r   r   Exitresponsestatus_coder9   r7   r   listr   )	r    r!   r"   r#   apiZ	endpointserrorresultsr:   r   r   r   ls0   s$   

rE   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').r5   zTThe cloud region in which the Inference Endpoint will be created (e.g. 'us-east-1').r4   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').rG   zVThe minimum number of replicas (instances) to keep running for the Inference Endpoint.rH   zRThe maximum number of replicas (instances) to scale to for the Inference Endpoint.rI   zFThe duration in minutes before an inactive endpoint is scaled to zero.rJ   z!The metric reference for scaling.rK   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   rM   rN   rO   r5   r4   r    r2   r#   rG   rH   rJ   rK   rI   Nr   )r   Zcreate_inference_endpointr   )r.   rL   r3   rM   rN   rO   r5   r4   r    r2   r#   rG   rH   rI   rJ   rK   rB   r   r   r   r   rF   [   sF   
V
	
zChf endpoints catalog deploy --repo meta-llama/Llama-3.2-1B-InstructzPThe hardware accelerator to be used for inference (e.g. 'cpu', 'gpu', 'neuron').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 )z4Deploy an Inference Endpoint from the Model Catalog.r$   )Zrepo_idr.   rM   r    r#   zDeployment failed: r%   N)	r   Z&create_inference_endpoint_from_catalogr   r   r   r>   r?   r@   r   )rL   r.   rM   r    r#   rB   r   rC   r   r   r   deploy_from_catalog   s   

rP   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   Zlist_inference_catalogr   r   r   r>   r?   r@   r   r   )r#   rB   rQ   rC   r   r   r   list_catalog   s   
rR   zhf endpoints catalog lszlist-catalogT)r.   Z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   Zget_inference_endpointr   r   r   r>   r?   r@   r   r.   r    r#   rB   r   rC   r   r   r   describe      
rW   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   rY   r2   rM   rN   rO   rG   rH   rI   rJ   rK   r#   zUpdate failed: r%   N)	r   Zupdate_inference_endpointr   r   r   r>   r?   r@   r   )r.   r    rL   rM   rN   rO   r3   rY   r2   rG   rH   rI   rJ   rK   r#   rB   r   rC   r   r   r   update  s2   
O
rZ   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$   rU   zDelete failed: Nz	Deleted 'z'.)	r   promptr   r>   r   Zdelete_inference_endpointr   r?   r@   )r.   r    r[   r#   ZconfirmationrB   rC   r   r   r   deletey  s   

r]   zhf endpoints pause my-endpointc              
   C   rT   )zPause an Inference Endpoint.r$   rU   zPause failed: r%   N)	r   Zpause_inference_endpointr   r   r   r>   r?   r@   r   rV   r   r   r   pause  rX   r^   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#   Z
running_okzResume failed: r%   N)	r   Zresume_inference_endpointr   r   r   r>   r?   r@   r   )r.   r    r_   r#   rB   r   rC   r   r   r   resume  s   

r`   z&hf endpoints scale-to-zero my-endpointc              
   C   rT   )z$Scale an Inference Endpoint to zero.r$   rU   zScale To Zero failed: r%   N)	r   Z scale_to_zero_inference_endpointr   r   r   r>   r?   r@   r   rV   r   r   r   scale_to_zero  rX   ra   )NNNN)N)NN)NNNNNNNNNNNNNN)NFN)-__doc__r   typingr   r   r   r   Z$huggingface_hub._inference_endpointsr   r   Zhuggingface_hub.errorsr   Z
_cli_utilsr	   r
   r   r   r   r   r   Zie_cliZcatalog_appr7   ZArgumentZNameArgOptionZNameOptZNamespaceOptr   commandtablerE   intfloatrF   rP   rR   Z	add_typerrW   rZ   boolr]   r^   r`   ra   r   r   r   r   <module>   s"   $



*. &-.45;AGMSl#
	!'-39?EKLg
