
    Y j:                        % 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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  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-  S\3S-  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#\*\<-  S\<4S$ jr?S%\\'   S&\@S\\'   \\$   -  4S' jrAS%\\'   S&\@S\\'   \\$   -  4S( jrBS)\'S&\@S\'\$-  S-  4S* jrCS+\\'   S\\#   4S, jrDS+\\'   S\\#   4S- jrES)\'S\#S-  4S. jrFS/\R                  S#\R                  S\\'   4S0 jrI0 rJ\<\'S-  \:\'   4   \KS1'   S2\'S-  S3\:\'   SS4S4 jrLS2\'S-  S\:\'   4S5 jrMS6\S\4S7 jrNS8\'S-  S9\'S-  S\4S: jrOg);zEContains utilities used by both the sync and async inference clients.    N)AsyncIterableIterable)	dataclass)Path)TYPE_CHECKINGAnyBinaryIOLiteralNoReturn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-  \S'   \\-  \-  S-  \S'   \S-  \S'   \\\	4   \S'   S	r
g)
RequestParameters8   urltaskNmodeljsondataheaders )__name__
__module____qualname____firstlineno__str__annotations__dictlistbytesr   __static_attributes__r'       r/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/huggingface_hub/inference/_common.pyr   r   8   sB    	H
I:
*t
d
""
$,#s(^r2   r   c                   T   ^  \ rS rSr% Sr\S-  \S'   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'
```
N	mime_typer%   c                    > [         TU ]  X5      nX#l        [        U[        5      (       a  Uc  UR                  Ul        U$ N)super__new__r7   
isinstancer5   )clsr%   r7   obj	__class__s       r3   r;   MimeBytes.__new__S   s8    goc(!dI&&9+< NNCM
r2   r'   r9   )r(   r)   r*   r+   __doc__r,   r-   r0   r;   r1   __classcell__)r?   s   @r3   r5   r5   B   s0     Tz5 S4Z  r2   r5   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)rE   s    r3   _import_numpyrF   ^   s    cddLr2   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   rD   PILr   r   s    r3   _import_pil_imagerI   g   s(      `
 	
 Lr2   contentreturnc                     g r9   r'   rJ   s    r3   _open_as_mime_bytesrN   v   s    9<r2   c                     g r9   r'   rM   s    r3   rN   rN   z   s    BEr2   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)r7   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<   r0   r5   	bytearray
memoryviewhasattrloggerdebugrQ   	mimetypes
guess_typer,   rR   	TypeError
startswithr   getr&   rJ   r   existsFileNotFoundError
read_bytesr   rH   r   ioBytesIOrV   savegetvaluelowertype)rJ   r%   r7   responser   bufferrV   s          r3   rN   rN   ~   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 r2   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.)rN   base64	b64encodedecode)rJ   	raw_bytess     r3   _b64_encoderq      s&    #G,II&--//r2   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)rT   rS   data:rt   z;base64,)r<   r,   r_   rN   r7   rm   rn   ro   )rJ   rr   rp   r7   encoded_datas        r3   _as_urlrv      st    '3G$6$67W$X$X $G,I ##8'8I ##I.557L 9+Xl^44r2   encoded_imagec                     [        5       nUR                  " [        R                  " [        R
                  " U 5      5      5      $ )z/Parse a base64-encoded string into a PIL Image.)rI   openrd   re   rm   	b64decode)rw   r   s     r3   _b64_to_imager{      s-    E::bjj!1!1-!@ABBr2   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$   loadsro   rM   s    r3   _bytes_to_listr           ::gnn&''r2   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}   rM   s    r3   _bytes_to_dictr      r   r2   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.
)rI   ry   rd   re   )rJ   r   s     r3   _bytes_to_imager      s$    
 E::bjj)**r2   rj   c                 \    [        U [        5      (       a  [        R                  " U 5      $ U $ r9   )r<   r0   r$   r~   )rj   s    r3   _as_dictr      s"    #-h#>#>4::hLHLr2   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       r3    _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       r3   &_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$ )Nrt   data: [DONE][DONE] signal received.z/nerror
error_type)r_   stripr   lstriprstripr$   r~   r`   _parse_text_generation_errorr   parse_obj_as_instancetokentext)r   r   payloadjson_payloadr   s        r3   r   r     s    ??7##zz|~%566 kk'"))$/G::g&L  ,*<+@,BRBRS_B`aa (==lKF$+6<<77r2   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      r3    _stream_chat_completion_responser   3  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      r3   &_async_stream_chat_completion_responser   @  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      $ )Nrt   r   r   r   r   )
r_   r   r   r$   r~   r   r`   r   r   r   )r   r   s     r3   r   r   M  s     ??7##zz|~%566 ::dkk'288:;L  ,*<+@,BRBRS_B`aa &;;LIIr2   clientc                n   #    UR                  5         S h  vN nUR                  5       7v   M   N
 g 7fr9   )aiter_linesr   )r   rj   r   s      r3   _async_yield_fromr   a  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 r9   )r   
setdefaultextend)r#   r   s     r3   '_set_unsupported_text_generation_kwargsr   {  s    '225"=DDEWXr2   c                 .    [         R                  U / 5      $ r9   )r   r`   )r#   s    r3   '_get_unsupported_text_generation_kwargsr     s    .225"==r2   
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   )rj   getattrr$   r`   	Exceptionr   )r   r   r   r   	exceptions        r3   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     r3   r   r     sZ    \!u%%,,(//\!u%%\!u%%r2   )PrA   rm   rd   r$   loggingr\   collections.abcr   r   dataclassesr   pathlibr   typingr   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PathTr0   rW   rX   ContentTTASKS_EXPECTING_IMAGES	getLoggerr(   rZ   r   r5   rF   rI   rN   rq   rv   r{   r/   r   r.   r   r   r   boolr   r   r   r   r   r   AsyncClientResponser   r   r-   r   r   r   r   r'   r2   r3   <module>r      sg   L  	    3 !  S S S    I H T  c4i%w	:MN *+;< 			8	$    8	 
 < <i < 
 < 
 E E74= E 
 E?D ?Y5E ?D0 0c 05X 5# 5# 5"C C C(E (d ((E (d (+U +w +Mut| M M3-*.c]X899$/33-(BCC8 8d 8sMgGgjnGn 8(
C=
()


-.
J
J$&J(E$5$5  TabeTf . DF #T#*d3i*?%@ EY3: YSWX[S\ Yae Y>3: >$s) >N x :	d
 	d
 	Ob 	r2   