
    RX9i=              	          % 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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,  \-r.\\-\	4   r/\\0\\/\.S\1\24   r3SS1r4\Rj                  " \65      r7\ " S S5      5       r8 " S S\05      r9S r:S r;S r<\S\3S\94S j5       r=\S\S   S\S   4S j5       r=S\\3   S\\9   4S jr=S\3S\-4S jr>S\3S\-S\-4S jr?S \-SS4S! jr@S\0S\4S" jrAS\0S\4S# jrBS\0SS4S$ jrCS%\\0\4   S\4S& jrDS'\\0   S(\ES\\\-   \\'   4   4S) jrFS'\\0   S(\ES\\\-   \\'   4   4S* jrGS+\0S(\ES\\\-\'4      4S, jrHS-\\0   S\\&   4S. jrIS-\\0   S\\&   4S/ jrJS+\0S\\&   4S0 jrKS1S2S%S3S\\0   4S4 jrL0 rM\\\-   \\-   4   \NS5'   S6\\-   S7\\-   SS4S8 jrOS6\\-   S\\-   4S9 jrPS:\S\4S; jrQS<\\-   S=\\-   S\4S> jrRg)?zEContains utilities used by both the sync and async inference clients.    N)	dataclass)Path)TYPE_CHECKINGAnyAsyncIterableBinaryIODict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	)
RequestParametersE   urltaskmodeljsondataheaders N)__name__
__module____qualname____firstlineno__str__annotations__r   r   r	   r   bytesr   __static_attributes__r-       a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/huggingface_hub/inference/_common.pyr%   r%   E   sD    	H
IC=
5dD)
**
5/#s(^r6   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$ )	MimeBytesO   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__r;   
isinstancer9   )clsr+   r;   obj	__class__s       r7   r?   MimeBytes.__new__`   s8    goc(!dI&&9+< NNCM
r6   r-   r=   )r.   r/   r0   r1   __doc__r   r2   r3   r4   r?   r5   __classcell__)rC   s   @r7   r9   r9   O   s0     }5 Xc]  r6   r9   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)rI   s    r7   _import_aiohttprJ   k   s    !!ijjNr6   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   rH   numpy)rL   s    r7   _import_numpyrM   t   s    cddLr6   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   rH   PILr#   r"   s    r7   _import_pil_imagerP   }   s(      `
 	
 Lr6   contentreturnc                     g r=   r-   rQ   s    r7   _open_as_mime_bytesrU      s    9<r6   c                     g r=   r-   rT   s    r7   rU   rU      s    BEr6   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      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)r;   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@   r4   r9   	bytearray
memoryviewhasattrloggerdebugrX   	mimetypes
guess_typerY   r2   	TypeError
startswithr   getr,   rQ   r   existsFileNotFoundError
read_bytesr   rO   r#   ioBytesIOr]   savegetvaluelowertype)rQ   r+   r;   responser#   bufferr]   s          r7   rU   rU      so     '5!!!! 'Iz233w(( w45||~=DWf=U=UI((6q9[_	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 r6   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.)rU   base64	b64encodedecode)rQ   	raw_bytess     r7   _b64_encoderx      s&    #G,II&--//r6   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)r[   rZ   data:r{   z;base64,)r@   r2   rf   rU   r;   rt   ru   rv   )rQ   ry   rw   r;   encoded_datas        r7   _as_urlr}      st    '3G$6$67W$X$X $G,I ##8'8I ##I.557L 9+Xl^44r6   encoded_imagec                     [        5       nUR                  " [        R                  " [        R
                  " U 5      5      5      $ )z/Parse a base64-encoded string into a PIL Image.)rP   openrk   rl   rt   	b64decode)r~   r#   s     r7   _b64_to_imager      s-    E::bjj!1!1-!@ABBr6   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*   loadsrv   rT   s    r7   _bytes_to_listr           ::gnn&''r6   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   rT   s    r7   _bytes_to_dictr      r   r6   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.
)rP   r   rk   rl   )rQ   r#   s     r7   _bytes_to_imager   	  s$    
 E::bjj)**r6   rq   c                 \    [        U [        5      (       a  [        R                  " U 5      $ U $ r=   )r@   r4   r*   r   )rq   s    r7   _as_dictr     s"    #-h#>#>4::hLHLr6   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       r7    _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       r7   &_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-8r{   z/nerror
error_type)rf   stripr   rv   r*   r   lstriprstriprg   _parse_text_generation_errorr   parse_obj_as_instancetokentext)r   r   payloadjson_payloadr   s        r7   r   r   5  s     ""8,,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa (==lKF$+6<<77r6   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      r7    _stream_chat_completion_responser   K  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      r7   &_async_stream_chat_completion_responser   X  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   )rf   r   r   rv   r*   r   r   r   rg   r   r   r   )r   r   r   s      r7   r   r   e  s     ""8,,.566 !!'*G::gnnW5<<TBCL  ,*<+@,BRBRS_B`aa &;;LIIr6   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r=   )rQ   r   close)r   rq   r   s      r7   _async_yield_fromr   z  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 r=   )r   
setdefaultextend)r)   r   s     r7   '_set_unsupported_text_generation_kwargsr     s    '225"=DDEWXr6   c                 .    [         R                  U / 5      $ r=   )r   rg   )r)   s    r7   '_get_unsupported_text_generation_kwargsr     s    .225"==r6   
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   )getattrrq   r*   rg   	Exceptionr   )r   r   r   r   	exceptions        r7   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     r7   r   r     sZ    \!u%%,,(//\!u%%\!u%%r6   )SrE   rt   rk   r*   loggingrc   dataclassesr   pathlibr   typingr   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   rI   r    r!   	PIL.Imager#   r2   UrlTPathTr4   r^   r_   ContentTTASKS_EXPECTING_IMAGES	getLoggerr.   ra   r%   r9   rJ   rM   rP   rU   rx   r}   r   r   r   r   r   boolr   r   r   r   r   r   r   r   r3   r   r   r   r   r-   r6   r7   <module>r      s   L  	    !       _ ^ T 5 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#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 	r6   