
    gA                        U d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddl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&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. erddl/m0Z0m1Z1 ddl2m3Z3 e4Z5ee4e
f         Z6ee7ef         Z8ee8e6e5f         Z9ddhZ: ej;        e<          Z=e G d d                      Z>d Z?d Z@d ZAdaBeee4ee4         f                  eCd<   dee4ee4         f         fdZDdee         dee         fdZEede9dee8         fd            ZFeded         deed                  fd            ZFedee9         deee8         ddf         fd            ZFde9de4fd ZGd!e4dd"fd#ZHde7defd$ZIde7defd%ZJde7dd"fd&ZK	 dEd(ee4ee4ef         e9f         d)eee4ef                  d*eLdee4ef         fd+ZMd,ee7         d-eLdeee4         ee.         f         fd.ZNd,ee7         d-eLdeee4         ee.         f         fd/ZOd0e7d-eLdeee4e.f                  fd1ZPd2ee7         dee-         fd3ZQd2ee7         dee-         fd4ZRd0e7dee-         fd5ZSd6d7d8d9dee7         fd:ZTi ZUeee4         ee4         f         eCd;<   d<ee4         d=ee4         ddfd>ZVd<ee4         dee4         fd?ZWd@edefdAZXdBee4         dCee4         de fdDZYdS )FzEContains utilities used by both the sync and async inference clients.    N)contextmanager)	dataclass)Path)TYPE_CHECKINGAnyAsyncIterableBinaryIOContextManagerDict	GeneratorIterableListLiteralNoReturnOptionalUnionoverload)	HTTPError)GenerationErrorIncompleteGenerationErrorOverloadedErrorTextGenerationErrorUnknownErrorValidationError   )ENDPOINT)build_hf_headersget_sessionhf_raise_for_statusis_aiohttp_availableis_numpy_availableis_pillow_available   )ChatCompletionStreamOutputTextGenerationStreamOutput)ClientResponseClientSessionImageztext-to-imagezimage-to-imagec                   <    e Zd ZU dZeed<   eed<   eed<   eed<   dS )ModelStatusa  
    This Dataclass represents the the model status in the Hugging Face Inference API.

    Args:
        loaded (`bool`):
            If the model is currently loaded into Hugging Face's InferenceAPI. Models
            are loaded on-demand, leading to the user's first request taking longer.
            If a model is loaded, you can be assured that it is in a healthy state.
        state (`str`):
            The current state of the model. This can be 'Loaded', 'Loadable', 'TooBig'.
            If a model's state is 'Loadable', it's not too big and has a supported
            backend. Loadable models are automatically loaded when the user first
            requests inference on the endpoint. This means it is transparent for the
            user to load a model, except that the first call takes longer to complete.
        compute_type (`Dict`):
            Information about the compute resource the model is using or will use, such as 'gpu' type and number of
            replicas.
        framework (`str`):
            The name of the framework that the model was built with, such as 'transformers'
            or 'text-generation-inference'.
    loadedstatecompute_type	frameworkN)__name__
__module____qualname____doc__bool__annotations__strr        ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/huggingface_hub/inference/_common.pyr+   r+   Q   sC          , LLLJJJNNNNNr8   r+   c                  H    t                      st          d          dd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r    ImportErroraiohttp)r<   s    r9   _import_aiohttpr=   r   s.    !! kijjjNNNNr8   c                  H    t                      st          d          ddl} | S )z.Make sure `numpy` is installed on the machine.zGPlease install numpy to use deal with embeddings (`pip install numpy`).r   N)r!   r;   numpy)r?   s    r9   _import_numpyr@   {   s.     ecdddLLLLr8   c                  L    t                      st          d          ddlm}  | S )z,Make sure `PIL` is installed on the machine.zPlease install Pillow to use deal with images (`pip install Pillow`). If you don't want the image to be post-processed, use `client.post(...)` and get the raw response from the server.r   r(   )r"   r;   PILr)   r(   s    r9   _import_pil_imagerC      sA       
`
 
 	
 Lr8   _RECOMMENDED_MODELSreturnc                  
   t           vt                                          t           dt	                                } t          |            d |                                                                 D             a t           S )Nz
/api/tasks)headersc                 @    i | ]\  }}|t          |d                    S )widgetModels)_first_or_none).0taskdetailss      r9   
<dictcomp>z-_fetch_recommended_models.<locals>.<dictcomp>   s8     
 
 
>KdGD.!899
 
 
r8   )rD   r   getr   r   r   jsonitems)responses    r9   _fetch_recommended_modelsrS      s}    "==$$%<%<%<FVFXFX$YYH%%%
 
OW}}OdOdOfOf
 
 
 r8   rQ   c                 :    	 | d         pd S # t           $ r Y d S w xY w)Nr   )
IndexError)rQ   s    r9   rJ   rJ      s8    Qx4   tts   	 
contentc                     d S Nr7   rV   s    r9   _open_as_binaryrZ      s	     "cr8   c                     d S rX   r7   rY   s    r9   rZ   rZ      s	     %(Cr8   c              #   V  K   t          | t                    r|                     d          s|                     d          rGt                              d|             t                                          |           j        V  dS t          |           } | 	                                st          d|  d          t          | t                    rQt                              d|             |                     d          5 }|V  ddd           dS # 1 swxY w Y   dS | V  dS )	zOpen `content` as a binary file, either from a URL, a local path, or raw bytes.

    Do nothing if `content` is None,

    TODO: handle a PIL.Image as input
    TODO: handle base64 as input
    zhttps://zhttp://zDownloading content from NzFile not found at z. If `data` is a string, it must either be a URL or a path to a local file. To pass raw content, please encode it as bytes first.zOpening content from rb)
isinstancer6   
startswithloggerdebugr   rO   rV   r   existsFileNotFoundErroropen)rV   fs     r9   rZ   rZ      s      '3 
j)) 	W-?-?	-J-J 	LL>W>>???--##G,,4444Fw--~~ 	#OW O O O   '4   6W66777\\$ 	1GGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   DDDc                     t          |           5 }t          |t                    r|n|                                }t	          j        |                                          cddd           S # 1 swxY w Y   dS )z[Encode a raw file (image, audio) into base64. Can be byes, an opened file, a path or a URL.N)rZ   r^   bytesreadbase64	b64encodedecode)rV   datadata_as_bytess      r9   _b64_encodern      s    		!	! 8T *4 7 7HTYY[[..55778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   AA..A25A2encoded_imager)   c                     t                      } |j        t          j        t	          j        |                               S )z/Parse a base64-encoded string into a PIL Image.)rC   rd   ioBytesIOri   	b64decode)ro   r)   s     r9   _b64_to_imagert      s4    E5:bj!1-!@!@AABBBr8   c                 N    t          j        |                                           S )ac  Parse bytes from a Response object into a Python list.

    Expects the response body to be JSON-encoded data.

    NOTE: This is exactly the same implementation as `_bytes_to_dict` and will not complain if the returned data is a
    dictionary. The only advantage of having both is to help the user (and mypy) understand what kind of data to expect.
    rP   loadsrk   rY   s    r9   _bytes_to_listrx           :gnn&&'''r8   c                 N    t          j        |                                           S )ac  Parse bytes from a Response object into a Python dictionary.

    Expects the response body to be JSON-encoded data.

    NOTE: This is exactly the same implementation as `_bytes_to_list` and will not complain if the returned data is a
    list. The only advantage of having both is to help the user (and mypy) understand what kind of data to expect.
    rv   rY   s    r9   _bytes_to_dictr{      ry   r8   c                 b    t                      } |j        t          j        |                     S )zParse bytes from a Response object into a PIL Image.

    Expects the response body to be raw bytes. To deal with b64 encoded images, use `_b64_to_image` instead.
    )rC   rd   rq   rr   )rV   r)   s     r9   _bytes_to_imager}      s+    
 E5:bj))***r8   Finputs
parametersexpect_binaryc                    |i }d |                                 D             }t          |          dk    }t          | t          t          f          }|r)|s't          | t
                    st          d|            |r|sd| iS |s|rt          d|            i }|rt          |           |d<   n| |d<   |r||d<   d	|iS )
a$  
    Used in `InferenceClient` and `AsyncInferenceClient` to prepare the payload for an API request, handling various input types and parameters.
    `expect_binary` is set to `True` when the inputs are a binary object or a local path or URL. This is the case for image and audio inputs.
    Nc                     i | ]
\  }}|||S rX   r7   )rK   kvs      r9   rN   z$_prepare_payload.<locals>.<dictcomp>  s    GGG41a!Qr8   r   z5Expected binary inputs or a local path or a URL. Got rl   zUnexpected binary inputs. Got r~   r   rP   )rQ   lenr^   rg   r   r6   
ValueErrorrn   )r~   r   r   has_parameters	is_binaryrP   s         r9   _prepare_payloadr     s    
GG:#3#3#5#5GGGJ__q(N6E4=11I [Y [z&#/F/F [YQWYYZZZ  ^   DY DB&BBCCCD  $V,,X  X ('\D>r8   bytes_output_as_linesrM   c              #   h   K   | D ],}	 t          ||          }n# t          $ r Y  dS w xY w||V  -dS )z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_outputStopIterationr   rM   byte_payloadoutputs       r9    _stream_text_generation_responser   /  sk      
 .  	:<QQFF 	 	 	EEE	LLL s   
((c                v   K   | 2 3 d{V }	 t          ||          }n# t          $ r Y  dS w xY w||W V  36 dS )z/Used in `AsyncInferenceClient.text_generation`.Nr   r   s       r9   &_async_stream_text_generation_responser   =  s      
 4       l	:<QQFF 	 	 	EEE	LLLL 433s   8
--r   c                    |                      d          sd S |                                 dk    rt          d          |                     d          }t	          j        |                    d                              d                    }|                    d          )t          |d         |                    d                    t          j        |          }|s|j        j        n|S 	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8zdata:z/nerror
error_type)r_   stripr   rk   rP   rw   lstriprstriprO   _parse_text_generation_errorr%   parse_obj_as_instancetokentext)r   rM   payloadjson_payloadr   s        r9   r   r   K  s     ""8,, t..5666 !!'**G:gnnW55<<TBBCCL   ,*<+@,BRBRS_B`B`aaa (=lKKF$+76<7r8   bytes_linesc              #   f   K   | D ]+}	 t          |          }n# t          $ r Y  dS w xY w||V  ,dS )zFUsed in `InferenceClient.chat_completion` if model is served with TGI.N%_format_chat_completion_stream_outputr   r   itemr   s      r9    _stream_chat_completion_responser   a  si         	:4@@FF 	 	 	EEE	LLL s   
''c                t   K   | 2 3 d{V }	 t          |          }n# t          $ r Y  dS w xY w||W V  26 dS )z/Used in `AsyncInferenceClient.chat_completion`.Nr   r   s      r9   &_async_stream_chat_completion_responser   n  s       "       d	:4@@FF 	 	 	EEE	LLLL "kks   7
,,c                    |                      d          sd S |                                 dk    rt          d          |                     d          }t	          j        |                    d                              d                    }|                    d          )t          |d         |                    d                    t          j        |          S r   )r_   r   r   rk   rP   rw   r   r   rO   r   r$   r   )r   r   r   s      r9   r   r   {  s     ""8,, t..5666 !!'**G:gnnW55<<TBBCCL   ,*<+@,BRBRS_B`B`aaa &;LIIIr8   clientr'   rR   r&   c                   K   |j         2 3 d {V }|                                W V  6 |                                  d {V  d S rX   )rV   r   close)r   rR   r   s      r9   _async_yield_fromr     sz      &. # # # # # # #l  """"""" /
,,..s   )#_UNSUPPORTED_TEXT_GENERATION_KWARGSmodelunsupported_kwargsc                 b    t                               | g                               |           d S rX   )r   
setdefaultextend)r   r   s     r9   '_set_unsupported_text_generation_kwargsr     s-    '225"==DDEWXXXXXr8   c                 8    t                               | g           S rX   )r   rO   )r   s    r9   '_get_unsupported_text_generation_kwargsr     s    .225"===r8   
http_errorc                     	 t          | dd          p| j                                        }|                    d          }|                    d          }n# t          $ r | w xY w|t          ||          }|| | )z
    Try to parse text-generation-inference error message and raise HTTPError in any case.

    Args:
        error (`HTTPError`):
            The HTTPError that have been raised.
    response_error_payloadNr   r   )getattrrR   rP   rO   	Exceptionr   )r   r   r   r   	exceptions        r9   raise_text_generation_errorr     s    *&>EEcI\IaIaIcIcG$$[[..

    0
CC	Z' s   AA A$r   r   c                     |dk    rt          |           S |dk    rt          |           S |dk    rt          |           S |dk    rt          |           S t	          |           S )N
generationincomplete_generation
overloaded
validation)r   r   r   r   r   )r   r   s     r9   r   r     su    \!!u%%%,,,(///\!!u%%%\!!u%%%r8   )F)Zr3   ri   rq   rP   logging
contextlibr   dataclassesr   pathlibr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   requestsr   huggingface_hub.errorsr   r   r   r   r   r   	constantsr   utilsr   r   r   r    r!   r"   _generated.typesr$   r%   r<   r&   r'   	PIL.Imager)   r6   UrlTPathTrg   BinaryTContentTTASKS_EXPECTING_IMAGES	getLoggerr0   r`   r+   r=   r@   rC   rD   r5   rS   rJ   rZ   rn   rt   rx   r{   r}   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r8   r9   <module>r      s   L K K  				   % % % % % % ! ! ! ! ! !                                     "                      !                          U T T T T T T T   55555555 c4i
x
 %%& *+;< 		8	$	$        @    	 	 	 ;? Xd3#567 > > >4Xc](:#;    $s)      
""G" " " 
"
 
(T](GDM"( ( ( 
(
 Xh/ Ihw>OQUW[>[4\    >8 8c 8 8 8 8C C C C C C(E (d ( ( ( ((E (d ( ( ( (+U +w + + + +  # ##tCH~x/0#c3h(# # 
#s(^	# # # #R#E?59
8C=(#=>>?   (/:>
=}-GHHI   88"&8eC33458 8 8 8,
%
()
 
 
 

u%
-.
 
 
 
JJ()J J J J*O ?O TabgTh    0 GI #T(3-c*B%C H H HY8C= YVZ[^V_ Ydh Y Y Y Y>8C= >T#Y > > > >I (    6	 	8C= 	Uh 	 	 	 	 	 	r8   