
    ITh8              	       B   % S 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.J/r/  \0r1\\0\
4   r2\\3\4   r4\\4\2\14   r5SS1r6\Rn                  " \85      r9\ " S S5      5       r:\ " S S5      5       r;S r<S r=S r>\S\5S\\4   4S j5       r?\S\S   S\\S      4S j5       r?\S\\5   S\\\4   SS4   4S j5       r?S\5S\04S jr@S\0SS4S  jrAS\3S\4S! jrBS\3S\4S" jrCS\3SS4S# jrDS$\\3\4   S\4S% jrES&\\3   S'\FS\\\0   \\*   4   4S( jrGS&\\3   S'\FS\\\0   \\*   4   4S) jrHS*\3S'\FS\\\0\*4      4S+ jrIS,\\3   S\\)   4S- jrJS,\\3   S\\)   4S. jrKS*\3S\\)   4S/ jrLS0S1S$S2S\\3   4S3 jrM0 rN\\\0   \\0   4   \OS4'   S5\\0   S6\\0   SS4S7 jrPS5\\0   S\\0   4S8 jrQS9\S\4S: jrRS;\\0   S<\\0   S\ 4S= jrSg)>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Imageztext-to-imagezimage-to-imagec                   z    \ rS rSr% \\S'   \\S'   \\   \S'   \\\\\	4      \S'   \\
   \S'   \\\4   \S'   Srg	)
RequestParametersH   urltaskmodeljsondataheaders N)__name__
__module____qualname____firstlineno__str__annotations__r   r   r   r   ContentTr   __static_attributes__r0       Y/var/www/auris/envauris/lib/python3.13/site-packages/huggingface_hub/inference/_common.pyr(   r(   H   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)	ModelStatusS   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<   S   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   t   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              #   *  #    [        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U v   g! , (       d  f       g= f7f)	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)
isinstancer5   
startswithloggerdebugr   getrN   r   existsFileNotFoundErroropen)rN   fs     r:   rS   rS      s      '3j))W-?-?	-J-JLL4WI>?-##G,444w-~~#$WI .O O  '4  ,WI67\\$1G   	  s   C-D/D4D
DD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   rW   bytesreadbase64	b64encodedecode)rN   r.   data_as_bytess      r:   _b64_encoderg      sH    		!T *4 7 7TYY[.557 
"	!	!s   AA!!
A/encoded_imager&   c                     [        5       nUR                  " [        R                  " [        R
                  " U 5      5      5      $ )z/Parse a base64-encoded string into a PIL Image.)rM   r^   ioBytesIOrc   	b64decode)rh   r&   s     r:   _b64_to_imagerm      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-   loadsre   rR   s    r:   _bytes_to_listrq           ::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.
ro   rR   s    r:   _bytes_to_dictrt      rr   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   r^   rj   rk   )rN   r&   s     r:   _bytes_to_imagerv      s$    
 E::bjj)**r9   responsec                 \    [        U [        5      (       a  [        R                  " U 5      $ U $ rQ   )rW   ra   r-   rp   )rw   s    r:   _as_dictry      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rz   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-8zdata:z/nerror
error_type)rX   stripr   re   r-   rp   lstriprstripr[   _parse_text_generation_errorr"   parse_obj_as_instancetokentext)r   r{   payloadjson_payloadr   s        r:   r~   r~     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   )  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   6  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   )rX   r   r   re   r-   rp   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 7frQ   )rN   r   close)r   rw   r   s      r:   _async_yield_fromr   X  s;     &.. #l  ""#.
,,.s+   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  s    '225"=DDEWXr9   c                 .    [         R                  U / 5      $ rQ   )r   r[   )r,   s    r:   '_get_unsupported_text_generation_kwargsr   w  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   )getattrrw   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   )TrB   rc   rj   r-   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   utilsr   r   r   r   _generated.typesr!   r"   rF   r#   r$   	PIL.Imager&   r5   UrlTPathTra   BinaryTr7   TASKS_EXPECTING_IMAGES	getLoggerr1   rY   r(   r<   rG   rJ   rM   rS   rg   rm   rq   rt   rv   ry   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
 %%& *+;< 			8	$      @	 
""G" 
"
 
(T](GDM"( 
(
 Xh/ Ihw>OQUW[>[4\  >8 8c 8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 0 GI #T(3-c*B%C HY8C= YVZ[^V_ Ydh Y>8C= >T#Y >I ( 6	 	8C= 	Uh 	r9   