
    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jv                  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	 dDd'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.ZOd/e7d,eLdeee4e.f      fd0ZPd1ee7   dee-   fd2ZQd1ee7   dee-   fd3ZRd/e7dee-   fd4ZSd5d6d7d8dee7   fd9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d@ZXdAee4   dBee4   de fdCZYy)EzEContains 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<   y)ModelStatusa  
    This Dataclass represents 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        V/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/inference/_common.pyr+   r+   Q   s    , LJNr8   r+   c                  8    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    !ijjNr8   c                  8    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    cddLr8   c                  <    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      s%     `
 	
 Lr8   _RECOMMENDED_MODELSreturnc            	         t         st               j                  t         dt	                     } t        |        | j                         j                         D ci c]  \  }}|t        |d          c}}a t         S c c}}w )Nz
/api/tasks)headerswidgetModels)	rD   r   getr   r   r   jsonitems_first_or_none)responsetaskdetailss      r9   _fetch_recommended_modelsrP      s|    "=$$z%<FVFX$YH%OW}}OdOdOf
OfmdGD.!899Of
 
s   B rK   c                 4    	 | d   xs d S # t         $ r Y y w xY w)Nr   )
IndexError)rK   s    r9   rL   rL      s(    Qx4 s    	contentc                      y Nr7   rS   s    r9   _open_as_binaryrW      s     "r8   c                      y rU   r7   rV   s    r9   rW   rW      s     %(r8   c              #     K   t        | t              r| j                  d      s| j                  d      r>t        j	                  d|         t               j                  |       j                   yt        |       } | j                         st        d|  d      t        | t              r7t        j	                  d|         | j                  d      5 }| ddd       y|  y# 1 sw Y   yxY ww)	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   rI   rS   r   existsFileNotFoundErroropen)rS   fs     r9   rW   rW      s      '3j)W-?-?	-JLL4WI>?-##G,444w-~~#$WI .O O  '4 ,WI67\\$1G   	  s   CC5C)C5)C2.C5c                     t        |       5 }t        |t              r|n|j                         }t	        j
                  |      j                         cddd       S # 1 sw Y   yxY w)z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.N)rW   r[   bytesreadbase64	b64encodedecode)rS   datadata_as_bytess      r9   _b64_encoderk      sE    		!T *4 7TYY[.557 
"	!	!s   AAA$encoded_imager)   c                     t               } |j                  t        j                  t	        j
                  |                   S )z/Parse a base64-encoded string into a PIL Image.)rC   ra   ioBytesIOrf   	b64decode)rl   r)   s     r9   _b64_to_imagerq      s/    E5::bjj!1!1-!@ABBr8   c                 H    t        j                  | 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.
    rJ   loadsrh   rV   s    r9   _bytes_to_listru           ::gnn&''r8   c                 H    t        j                  | 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.
    rs   rV   s    r9   _bytes_to_dictrx      rv   r8   c                 `    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   ra   rn   ro   )rS   r)   s     r9   _bytes_to_imagerz      s&    
 E5::bjj)**r8   inputs
parametersexpect_binaryc                 h   |i }|j                         D ci c]  \  }}|	|| }}}t        |      dkD  }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 c c}}w )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.
    r   z5Expected binary inputs or a local path or a URL. Got ri   zUnexpected binary inputs. Got r{   r|   rJ   )rK   lenr[   rd   r   r6   
ValueErrorrk   )r{   r|   r}   kvhas_parameters	is_binaryrJ   s           r9   _prepare_payloadr     s     
#-#3#3#5G#541a!Q$#5JG_q(N6E4=1IYz&#/FPQWPXYZZ^Y9&BCCD$V,X  X'\D>1 Hs
   
B.B.bytes_output_as_linesrO   c              #   b   K   | D ]  }	 t        ||      }||  y# t        $ r Y  yw xY ww)z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_outputStopIterationr   rO   byte_payloadoutputs       r9    _stream_text_generation_responser   /  sC     
 .	:<QF L .  		s    ///	,/,/c                t   K   | 2 3 d{   }	 t        ||      }|| 7 # t        $ r Y  yw xY w6 yw)z/Used in `AsyncInferenceClient.text_generation`.Nr   r   s       r9   &_async_stream_text_generation_responser   =  sN     
 4 l	:<QF L  		 4s4   86$68&886	3838r   c                    | j                  d      sy | j                         dk(  rt        d      | j                  d      }t	        j
                  |j                  d      j                  d            }|j                  d      t        |d   |j                  d            t        j                  |      }|s|j                  j                  S |S 	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8zdata:z/nerror
error_type)r\   stripr   rh   rJ   rt   lstriprstriprI   _parse_text_generation_errorr%   parse_obj_as_instancetokentext)r   rO   payloadjson_payloadr   s        r9   r   r   K  s     ""8,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa (==lKF$+6<<77r8   bytes_linesc              #   `   K   | D ]  }	 t        |      }||  y# t        $ r Y  yw xY ww)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  sA      	:4@F L   		s    ...	+.+.c                r   K   | 2 3 d{   }	 t        |      }|| 7 # t        $ r Y  yw xY w6 yw)z/Used in `AsyncInferenceClient.chat_completion`.Nr   r   s      r9   &_async_stream_chat_completion_responser   n  sL      " d	:4@F L  		 "s4   75#57%775	2727c                 r   | j                  d      sy | j                         dk(  rt        d      | j                  d      }t	        j
                  |j                  d      j                  d            }|j                  d      t        |d   |j                  d            t        j                  |      S r   )r\   r   r   rh   rJ   rt   r   r   rI   r   r$   r   )r   r   r   s      r9   r   r   {  s     ""8,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa &;;LIIr8   clientr'   rM   r&   c                   K   |j                   2 3 d {   }|j                          7 6 | j                          d {  7   y wrU   )rS   r   close)r   rM   r   s      r9   _async_yield_fromr     s=     &.. #l  ""#.
,,.s+   A	.,.A	.A	AA	#_UNSUPPORTED_TEXT_GENERATION_KWARGSmodelunsupported_kwargsc                 N    t         j                  | g       j                  |       y rU   )r   
setdefaultextend)r   r   s     r9   '_set_unsupported_text_generation_kwargsr     s    '225"=DDEWXr8   c                 .    t         j                  | g       S rU   )r   rI   )r   s    r9   '_get_unsupported_text_generation_kwargsr     s    .225"==r8   
http_errorc                     	 t        | dd      xs | j                  j                         }|j                  d      }|j                  d      }|t        ||      }|| | # t        $ r | w xY w)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   )getattrrM   rJ   rI   	Exceptionr   )r   r   r   r   	exceptions        r9   raise_text_generation_errorr     s    *&>EcI\I\IaIaIcG$[[.

 0
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     sZ    \!u%%,,(//\!u%%\!u%%r8   )F)Zr3   rf   rn   rJ   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PathTrd   BinaryTContentTTASKS_EXPECTING_IMAGES	getLoggerr0   r]   r+   r=   r@   rC   rD   r5   rP   rL   rW   rk   rq   ru   rx   rz   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r8   r9   <module>r      s7   L  	   % !    "   !  U 5 c4i
x
 %%& *+;< 			8	$   @	 ;? Xd3#567 >4Xc](:#; $s)   
""G" 
"
 
(T](GDM"( 
(
 Xh/ Ihw>OQUW[>[4\  >8 8c 8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,
%
()

u%
-.
JJ()J*O ?O TabgTh 0 GI #T(3-c*B%C HY8C= YVZ[^V_ Ydh Y>8C= >T#Y >I ( 6	 	8C= 	Uh 	r8   