
    _i:              	       "   % 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JrJrJrJrJrJrJrJrJr  SSKrSSKJrJrJrJrJrJrJr  SSKJrJ r J!r!  S	S
K"J#r#J$r$  \(       a  SSK%J&r&  \'r(\\'\	4   r)\\*\\)\(S\+\,4   r-SS1r.\R^                  " \05      r1\ " S S5      5       r2 " S S\*5      r3S r4S r5\S\-S\34S j5       r6\S\S   S\S   4S j5       r6S\\-   S\\3   4S jr6S\-S\'4S jr7S\-S\'S\'4S jr8S\'SS4S jr9S\*S\:4S jr;S\*S\<4S  jr=S\*SS4S! jr>S"\\*\<4   S\<4S# jr?S$\\'   S%\@S\\\'   \\$   4   4S& jrAS$\\'   S%\@S\\\'   \\$   4   4S' jrBS(\'S%\@S\\\'\$4      4S) jrCS*\\'   S\\#   4S+ jrDS*\\'   S\\#   4S, jrES(\'S\\#   4S- jrFS.\R                  S"\R                  S\\'   4S/ jrI0 rJ\<\\'   \:\'   4   \KS0'   S1\\'   S2\:\'   SS4S3 jrLS1\\'   S\:\'   4S4 jrMS5\S\4S6 jrNS7\\'   S8\\'   S\4S9 jrOg):zEContains utilities used by both the sync and async inference clients.    N)	dataclass)Path)
TYPE_CHECKINGAnyAsyncIterableBinaryIOIterableLiteralNoReturnOptionalUnionoverload)GenerationErrorHfHubHTTPErrorIncompleteGenerationErrorOverloadedErrorTextGenerationErrorUnknownErrorValidationError   )get_sessionis_numpy_availableis_pillow_available   )ChatCompletionStreamOutputTextGenerationStreamOutput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	)
RequestParameters8   urltaskmodeljsondataheaders N)__name__
__module____qualname____firstlineno__str__annotations__r   r   dictlistbytesr   __static_attributes__r(       ]/var/www/html/land-ocr/venv/lib/python3.13/site-packages/huggingface_hub/inference/_common.pyr    r    8   sD    	H
IC=
5dD)
**
5/#s(^r3   r    c                   T   ^  \ rS rSr% Sr\\   \S'   SS\S\\   4U 4S jjjr	Sr
U =r$ )	MimeBytesB   z
A bytes object with a mime type.
To be returned by `_prepare_payload_open_as_mime_bytes` in subclasses.

Example:
```python
    >>> b = MimeBytes(b"hello", "text/plain")
    >>> isinstance(b, bytes)
    True
    >>> b.mime_type
    'text/plain'
```
	mime_typer&   c                    > [         TU ]  X5      nX#l        [        U[        5      (       a  Uc  UR                  Ul        U$ N)super__new__r8   
isinstancer6   )clsr&   r8   obj	__class__s       r4   r<   MimeBytes.__new__S   s8    goc(!dI&&9+< NNCM
r3   r(   r:   )r)   r*   r+   r,   __doc__r   r-   r.   r1   r<   r2   __classcell__)r@   s   @r4   r6   r6   B   s0     }5 Xc]  r3   r6   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   ImportErrornumpy)rF   s    r4   _import_numpyrG   ^   s    cddLr3   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    r4   _import_pil_imagerJ   g   s(      `
 	
 Lr3   contentreturnc                     g r:   r(   rK   s    r4   _open_as_mime_bytesrO   v   s    9<r3   c                     g r:   r(   rN   s    r4   rO   rO   z   s    BEr3   c                    U c  g[        U [        5      (       a  [        U 5      $ [        U [        [        45      (       a  [        [        U 5      5      $ [        U S5      (       a  [        R                  S5        U R                  5       n[        U S5      (       a,  [        R                  " [        U R                  5      5      S   OSn[        U[        5      (       a  [        S5      e[        XS9$ [        U [        5      (       a  U R                  S5      (       d  U R                  S	5      (       a|  [        R                  S
U  35        [        5       R!                  U 5      nUR"                  R!                  S5      nUc  [        R                  " U 5      S   n[        UR$                  US9$ ['        U 5      n U R)                  5       (       d  [+        SU  S35      e[        U [&        5      (       aG  [        R                  SU  35        [        U R-                  5       [        R                  " U 5      S   S9$ [/        5       (       a  SSKJn  [        XR2                  5      (       aw  [        R                  S5        [4        R6                  " 5       nU R8                  =(       d    SnU R;                  XVS9  [        UR=                  5       SUR?                  5        3S9$ [        S[A        U 5       S35      e)zOpen `content` as a binary file, either from a URL, a local path, raw bytes, or a PIL Image.

Do nothing if `content` is None.
NreadzReading content from BinaryIOnamer   z3Expected binary stream (bytes), but got text stream)r8   https://http://zDownloading content from zContent-Typez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 r   zConverting PIL Image to bytesPNG)formatzimage/zUnsupported content type: zl. Expected one of: bytes, bytearray, BinaryIO, memoryview, Path, str (URL or file path), or PIL.Image.Image.)!r=   r1   r6   	bytearray
memoryviewhasattrloggerdebugrR   	mimetypes
guess_typer-   rS   	TypeError
startswithr   getr'   rK   r   existsFileNotFoundError
read_bytesr   rI   r   ioBytesIOrW   savegetvaluelowertype)rK   r&   r8   responser   bufferrW   s          r4   rO   rO   ~   su     '5!!!! 'Iz233w(( w45||~BI'SYBZBZI((W\\):;A>`d	dC  QRR33 '3j))W-?-?	-J-JLL4WI>?"}((1H ((,,^<I %009!<	X--CCw-~~#$WI .O O  '4  ,WI67++-9M9Mg9VWX9YZZ g{{++LL89ZZ\F^^,uFLLL/V__.F6<<>BR:STT 
$T']O 4u 	u r3   c                 `    [        U 5      n[        R                  " U5      R                  5       $ )z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.)rO   base64	b64encodedecode)rK   	raw_bytess     r4   _b64_encoderr      s&    #G,II&--//r3   default_mime_typec                     [        U [        5      (       a  U R                  S5      (       a  U $ [        U 5      nUR                  =(       d    Un[
        R                  " U5      R                  5       nSU SU 3$ )N)rU   rT   data:ru   z;base64,)r=   r-   r`   rO   r8   rn   ro   rp   )rK   rs   rq   r8   encoded_datas        r4   _as_urlrw      st    '3G$6$67W$X$X $G,I ##8'8I ##I.557L 9+Xl^44r3   encoded_imagec                     [        5       nUR                  " [        R                  " [        R
                  " U 5      5      5      $ )z/Parse a base64-encoded string into a PIL Image.)rJ   openre   rf   rn   	b64decode)rx   r   s     r4   _b64_to_imager|      s-    E::bjj!1!1-!@ABBr3   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%   loadsrp   rN   s    r4   _bytes_to_listr           ::gnn&''r3   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~   rN   s    r4   _bytes_to_dictr      r   r3   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.
)rJ   rz   re   rf   )rK   r   s     r4   _bytes_to_imager      s$    
 E::bjj)**r3   rk   c                 \    [        U [        5      (       a  [        R                  " U 5      $ U $ r:   )r=   r1   r%   r   )rk   s    r4   _as_dictr      s"    #-h#>#>4::hLHLr3   output_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   lineoutputs       r4    _stream_text_generation_responser     s@     
 	:4IF 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       r4   &_async_stream_text_generation_responser     sK     
 # d	:4IF L  		 #4   ;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      R	                  S5      n[
        R                  " U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$ )Nru   data: [DONE][DONE] signal received.z/nerror
error_type)r`   stripr   lstriprstripr%   r   ra   _parse_text_generation_errorr   parse_obj_as_instancetokentext)r   r   payloadjson_payloadr   s        r4   r   r     s     ??7##zz|~%566 kk'"))$/G::g&L  ,*<+@,BRBRS_B`aa (==lKF$+6<<77r3   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   r   r   s      r4    _stream_chat_completion_responser   5  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      r4   &_async_stream_chat_completion_responser   B  sK       d	:4@F L  		 r   c                 `   U R                  S5      (       d  g U R                  5       S:X  a  [        S5      e[        R                  " U R                  S5      R                  5       5      nUR                  S5      b  [        US   UR                  S5      5      e[        R                  " U5      $ )Nru   r   r   r   r   )
r`   r   r   r%   r   r   ra   r   r   r   )r   r   s     r4   r   r   O  s     ??7##zz|~%566 ::dkk'288:;L  ,*<+@,BRBRS_B`aa &;;LIIr3   clientc                n   #    UR                  5         S h  vN nUR                  5       7v   M   N
 g 7fr:   )aiter_linesr   )r   rk   r   s      r4   _async_yield_fromr   c  s,     **, djjl,s   5313535#_UNSUPPORTED_TEXT_GENERATION_KWARGSr$   unsupported_kwargsc                 N    [         R                  U / 5      R                  U5        g r:   )r   
setdefaultextend)r$   r   s     r4   '_set_unsupported_text_generation_kwargsr   }  s    '225"=DDEWXr3   c                 .    [         R                  U / 5      $ r:   )r   ra   )r$   s    r4   '_get_unsupported_text_generation_kwargsr     s    .225"==r3   
http_errorc                    U R                   c  U e [        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.
Nresponse_error_payloadr   r   )rk   getattrr%   ra   	Exceptionr   )r   r   r   r   	exceptions        r4   raise_text_generation_errorr     s     "*&>EcI\I\IaIaIcG$[[.

 0C	'   s   AA3 3B 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     r4   r   r     sZ    \!u%%,,(//\!u%%\!u%%r3   )PrB   rn   re   r%   loggingr]   dataclassesr   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   httpxhuggingface_hub.errorsr   r   r   r   r   r   r   utilsr   r   r   _generated.typesr   r   	PIL.Imager   r-   UrlTPathTr1   rX   rY   ContentTTASKS_EXPECTING_IMAGES	getLoggerr)   r[   r    r6   rG   rJ   rO   rr   rw   r|   r0   r   r/   r   r   r   boolr   r   r   r   r   r   AsyncClientResponser   r   r.   r   r   r   r   r(   r3   r4   <module>r      s|   L  	    !  v v v    I H T  c4i%w	:MN *+;< 			8	$    8	 
 < <i < 
 < 
 E E74= E 
 E?(!3 ?8K ?D0 0c 05X 5# 5# 5"C C C(E (d ((E (d (+U +w +MuUD[) Md M3-*.
8C=(#=>>?$/3
=}-GHHI8
88eC33458,
C=
()


-.
J
J()J(E$5$5  TabeTf . GI #T(3-c*B%C HY8C= YVZ[^V_ Ydh Y>8C= >T#Y >N x :	 	8C= 	Uh 	r3   