
    ,h>              	       b   % S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SSKJr  SSKJrJrJ r J!r!J"r"J#r#  S	S
K$J%r%J&r&J'r'J(r(  SSK)J*r*J+r+  \(       a  SSK,J-r-J.r.  SSK/J0r0  \1r2\\1\4   r3\\4\4   r5\\5\3\2S4   r6SS1r7\Rp                  " \95      r:\	 " S S5      5       r;\	 " S S5      5       r<S r=S r>S r?\S\6S\\5   4S j5       r@\S\S   S\\S      4S j5       r@\S\\6   S\\\5   SS4   4S j5       r@S\6S\14S jrAS\6S\1S\14S  jrBS!\1SS4S" jrCS\4S\4S# jrDS\4S\4S$ jrES\4SS4S% jrFS&\\4\4   S\4S' jrGS(\\4   S)\HS\\\1   \\+   4   4S* jrIS(\\4   S)\HS\\\1   \\+   4   4S+ jrJS,\4S)\HS\\\1\+4      4S- jrKS.\\4   S\\*   4S/ jrLS.\\4   S\\*   4S0 jrMS,\4S\\*   4S1 jrNS2S3S&S4S\\4   4S5 jrO0 rP\\\1   \\1   4   \QS6'   S7\\1   S8\\1   SS4S9 jrRS7\\1   S\\1   4S: jrSS;\S\4S< jrTS=\\1   S>\\1   S\!4S? jrUg)@zEContains 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   )get_sessionis_aiohttp_availableis_numpy_availableis_pillow_available   )ChatCompletionStreamOutputTextGenerationStreamOutput)ClientResponseClientSessionImager&   ztext-to-imagezimage-to-imagec                   z    \ rS rSr% \\S'   \\S'   \\   \S'   \\\\\	4      \S'   \\
   \S'   \\\4   \S'   Srg	)
RequestParametersI   urltaskmodeljsondataheaders N)__name__
__module____qualname____firstlineno__str__annotations__r   r   r   r   ContentTr   __static_attributes__r0       Y/var/www/html/shao/venv/lib/python3.13/site-packages/huggingface_hub/inference/_common.pyr(   r(   I   sE    	H
IC=
5dD)
**
8
#s(^r9   r(   c                   B    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   Sr	g)	ModelStatusT   a'  
This Dataclass represents the model status in the HF Inference API.

Args:
    loaded (`bool`):
        If the model is currently loaded into HF's Inference API. 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	frameworkr0   N)
r1   r2   r3   r4   __doc__boolr6   r5   r   r8   r0   r9   r:   r<   r<   T   s    , LJNr9   r<   c                  B    [        5       (       d  [        S5      eSS Kn U $ )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ImportErroraiohttp)rF   s    r:   _import_aiohttprG   u   s    !!ijjNr9   c                  B    [        5       (       d  [        S5      eSSKn U $ )z.Make sure `numpy` is installed on the machine.zGPlease install numpy to use deal with embeddings (`pip install numpy`).r   N)r   rE   numpy)rI   s    r:   _import_numpyrJ   ~   s    cddLr9   c                  F    [        5       (       d  [        S5      eSSKJn   U $ )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   rE   PILr&   r%   s    r:   _import_pil_imagerM      s(      `
 	
 Lr9   contentreturnc                     g Nr0   rN   s    r:   _open_as_binaryrS      s     "r9   c                     g rQ   r0   rR   s    r:   rS   rS      s     %(r9   c              #   F  #    [        U [        5      (       a  U R                  S5      (       d  U R                  S5      (       a>  [        R	                  SU  35        [        5       R                  U 5      R                  v   g[        U 5      n U R                  5       (       d  [        SU  S35      e[        U [        5      (       a7  [        R	                  SU  35        U R                  S5       nUv   SSS5        g[        5       (       a  S	S
KJn  [        XR                  5      (       a_  [        R	                  S5        [        R                   " 5       nU R#                  X0R$                  =(       d    SS9  UR'                  5       v   gU v   g! , (       d  f       g= f7f)zOpen `content` as a binary file, either from a URL, a local path, raw bytes, or a PIL Image.

Do nothing if `content` is None.

TODO: handle base64 as input
https://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 rbr   r%   zConverting PIL Image to bytesPNG)format)
isinstancer5   
startswithloggerdebugr   getrN   r   existsFileNotFoundErroropenr   rL   r&   ioBytesIOsaverZ   getvalue)rN   fr&   buffers       r:   rS   rS      sG     '3j))W-?-?	-J-JLL4WI>?-##G,444w-~~#$WI .O O  '4  ,WI67\\$1G   g{{++LL89ZZ\FLL(?%L@//## M!  s   C-F!/F4BF!
FF!c                     [        U 5       n[        U[        5      (       a  UOUR                  5       n[        R
                  " U5      R                  5       sSSS5        $ ! , (       d  f       g= f)z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.N)rS   r[   bytesreadbase64	b64encodedecode)rN   r.   data_as_bytess      r:   _b64_encoderp      sH    		!T *4 7 7TYY[.557 
"	!	!s   AA!!
A/default_mime_typec                    [        U [        5      (       a.  U R                  S5      (       d  U R                  S5      (       a  U $ S n[        U [        [        45      (       a  [        R
                  " U SS9S   nOU[        5       (       aF  SSKJn  [        XR                  5      (       a&  SU R                  =(       d    SR                  5        3nU=(       d    Un[        U 5      nS	U S
U 3$ )NrV   rW   F)strictr   r%   zimage/rY   data:z;base64,)r[   r5   r\   r   	mimetypes
guess_typer   rL   r&   rZ   lowerrp   )rN   rq   	mime_typer&   encoded_datas        r:   _as_urlrz      s    '3W%7%7
%C%CwGYGYZcGdGd I'C;''((?B				g{{++ '.."9E!@!@!B CDI..Iw'L9+Xl^44r9   encoded_imagec                     [        5       nUR                  " [        R                  " [        R
                  " U 5      5      5      $ )z/Parse a base64-encoded string into a PIL Image.)rM   rb   rc   rd   rl   	b64decode)r{   r&   s     r:   _b64_to_imager~      s-    E::bjj!1!1-!@ABBr9   c                 J    [         R                  " U R                  5       5      $ )aS  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.
r-   loadsrn   rR   s    r:   _bytes_to_listr           ::gnn&''r9   c                 J    [         R                  " U R                  5       5      $ )aS  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.
r   rR   s    r:   _bytes_to_dictr      r   r9   c                 b    [        5       nUR                  " [        R                  " U 5      5      $ )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.
)rM   rb   rc   rd   )rN   r&   s     r:   _bytes_to_imager     s$    
 E::bjj)**r9   responsec                 \    [        U [        5      (       a  [        R                  " U 5      $ U $ rQ   )r[   rj   r-   r   )r   s    r:   _as_dictr     s"    #-h#>#>4::hLHLr9   bytes_output_as_linesdetailsc              #   j   #    U  H  n [        X!5      nUc  M  Uv   M     g! [         a       gf = f7f)z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_outputStopIterationr   r   byte_payloadoutputs       r:    _stream_text_generation_responser     s@     
 .	:<QF L .  		    3"3	3
0303c                z   #    U   Sh  vN n [        X!5      nUc  M  U7v   M!   N! [         a       gf = f
 g7f)z/Used in `AsyncInferenceClient.text_generation`.Nr   r   s       r:   &_async_stream_text_generation_responser   %  sK     
 4 l	:<QF L  		 44   ;9&9;(;;9
6;6;r   c                    U R                  S5      (       d  g U R                  5       S:X  a  [        S5      eU R                  S5      n[        R
                  " UR                  S5      R                  S5      5      nUR                  S5      b  [        US   UR                  S5      5      e[        R                  " U5      nU(       d  UR                  R                  $ U$ 	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8rt   z/nerror
error_type)r\   stripr   rn   r-   r   lstriprstripr_   _parse_text_generation_errorr"   parse_obj_as_instancetokentext)r   r   payloadjson_payloadr   s        r:   r   r   3  s     ""8,,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa (==lKF$+6<<77r9   bytes_linesc              #   j   #    U  H  n [        U5      nUc  M  Uv   M     g! [         a       gf = f7f)zFUsed in `InferenceClient.chat_completion` if model is served with TGI.N%_format_chat_completion_stream_outputr   r   itemr   s      r:    _stream_chat_completion_responser   I  s@      	:4@F L   		r   c                z   #    U   Sh  vN n [        U5      nUc  M  U7v   M!   N! [         a       gf = f
 g7f)z/Used in `AsyncInferenceClient.chat_completion`.Nr   r   s      r:   &_async_stream_chat_completion_responser   V  sK      " d	:4@F L  		 "r   c                    U R                  S5      (       d  g U R                  5       S:X  a  [        S5      eU R                  S5      n[        R
                  " UR                  S5      R                  S5      5      nUR                  S5      b  [        US   UR                  S5      5      e[        R                  " U5      $ r   )r\   r   r   rn   r-   r   r   r   r_   r   r!   r   )r   r   r   s      r:   r   r   c  s     ""8,,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa &;;LIIr9   clientr$   r#   c                   #     UR                     S h  vN nUR                  5       7v   M   N
 U R                  5       I S h  vN    g ! U R                  5       I S h  vN    f = f7frQ   )rN   r   close)r   r   r   s      r:   _async_yield_fromr   x  sQ     "*"2"2 	',$$&&	'"2 llnfllnsM   A)A 0.0A 0A A)AA)A&A" A&&A)#_UNSUPPORTED_TEXT_GENERATION_KWARGSr,   unsupported_kwargsc                 N    [         R                  U / 5      R                  U5        g rQ   )r   
setdefaultextend)r,   r   s     r:   '_set_unsupported_text_generation_kwargsr     s    '225"=DDEWXr9   c                 .    [         R                  U / 5      $ rQ   )r   r_   )r,   s    r:   '_get_unsupported_text_generation_kwargsr     s    .225"==r9   
http_errorc                      [        U SS5      =(       d    U R                  R                  5       nUR                  S5      nUR                  S5      nUb  [        X#5      nX@eU e! [         a    U ef = f)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   )getattrr   r-   r_   	Exceptionr   )r   r   r   r   	exceptions        r:   raise_text_generation_errorr     s}    *&>EcI\I\IaIaIcG$[[.

 0C	'   s   AA$ $A1r   r   c                     US:X  a  [        U 5      $ US:X  a  [        U 5      $ US:X  a  [        U 5      $ US:X  a  [        U 5      $ [	        U 5      $ )N
generationincomplete_generation
overloaded
validation)r   r   r   r   r   )r   r   s     r:   r   r     sZ    \!u%%,,(//\!u%%\!u%%r9   )VrB   rl   rc   r-   loggingru   
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   utilsr   r   r   r   _generated.typesr!   r"   rF   r#   r$   	PIL.Imager&   r5   UrlTPathTrj   BinaryTr7   TASKS_EXPECTING_IMAGES	getLoggerr1   r]   r(   r<   rG   rJ   rM   rS   rp   rz   r~   r   r   r   r   rC   r   r   r   r   r   r   r   r   r6   r   r   r   r   r0   r9   r:   <module>r      s   L  	    % !    "   _ ^ T 5 c4i
x
 %w./ *+;< 			8	$      @	 
""G" 
"
 
(T](GDM"( 
(
 'Xh/ 'Ihw>OQUW[>[4\ ' 'T8 8c 85X 5# 5# 5(C C C(E (d ((E (d (+U +w +MuUD[) Md M#E?59
8C=(#=>>?(/:>
=}-GHHI88"&8eC33458,
%
()

u%
-.
JJ()J*O ?O TabgTh 6 GI #T(3-c*B%C HY8C= YVZ[^V_ Ydh Y>8C= >T#Y >I ( 6	 	8C= 	Uh 	r9   