Ë
    -Œ:j—:  ã                   óâ  — U d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZmZmZmZ ddlZddlmZmZmZmZmZmZmZ dd	lmZm Z m!Z! d
dl"m#Z#m$Z$ erddl%m&Z& e'Z(ee'ef   Z)ee*ee)e(de+e,f   Z-ddhZ. ej^                  e0«      Z1e
 G d„ d«      «       Z2 G d„ de*«      Z3d„ Z4d„ Z5ede-de3fd„«       Z6eded   ded   fd„«       Z6de-dz  de3dz  fd„Z6de-de'fd„Z7de-de'de'fd„Z8de'ddfd„Z9de*de:fd „Z;de*de<fd!„Z=de*ddfd"„Z>d#e*e<z  de<fd$„Z?d%ee'   d&e@dee'   ee$   z  fd'„ZAd%ee'   d&e@dee'   ee$   z  fd(„ZBd)e'd&e@de'e$z  dz  fd*„ZCd+ee'   dee#   fd,„ZDd+ee'   dee#   fd-„ZEd)e'de#dz  fd.„ZFd/ejŽ                  d#ej�                  dee'   fd0„ZIi ZJe<e'dz  e:e'   f   eKd1<   d2e'dz  d3e:e'   ddfd4„ZLd2e'dz  de:e'   fd5„ZMd6edefd7„ZNd8e'dz  d9e'dz  defd:„ZOy);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                   ór   — e Zd ZU eed<   eed<   edz  ed<   eez  ez  dz  ed<   edz  ed<   eeef   ed<   y)ÚRequestParametersÚurlÚtaskNÚmodelÚjsonÚdataÚheaders)	Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__ÚdictÚlistÚbytesr   © ó    úc/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/inference/_common.pyr   r   8   sB   … à	ƒHØ
ƒIØ�‰:ÓØ
�‰*�tÑ
˜dÑ
"Ó"Ø
�$‰,ÓØ�#�s�(‰^Ôr/   r   c                   óF   ‡ — e Zd ZU dZedz  ed<   ddededz  fˆ fd„Zˆ xZS )Ú	MimeBytesa&  
    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         ‰| �  | |«      }||_        t        |t        «      r|€|j                  |_        |S ©N)ÚsuperÚ__new__r3   Ú
isinstancer2   )Úclsr$   r3   ÚobjÚ	__class__s       €r0   r7   zMimeBytes.__new__S   s9   ø€ Ü‰g‰o˜c 4Ó(ˆØ!ˆŒÜ�dœIÔ&¨9Ð+<Ø ŸN™NˆCŒMØˆ
r/   r5   )	r&   r'   r(   Ú__doc__r)   r*   r-   r7   Ú__classcell__)r;   s   @r0   r2   r2   B   s0   ø… ñð �T‰zÓñ˜5ð ¨S°4©Z÷ ñ r/   r2   c                  ó8   — t        «       st        d«      ‚ddl} | S )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)r@   s    r0   Ú_import_numpyrA   ^   s   € äÔÜÐcÓdÐdÛà€Lr/   c                  ó<   — t        «       st        d«      ‚ddlm}  | S )z,Make sure `PIL` is installed on the machine.z¸Please 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   r?   ÚPILr   r   s    r0   Ú_import_pil_imagerD   g   s%   € äÔ Üð`ó
ð 	
õ à€Lr/   ÚcontentÚreturnc                  ó   — y r5   r.   ©rE   s    r0   Ú_open_as_mime_bytesrI   v   s   € Ø9<r/   c                  ó   — y r5   r.   rH   s    r0   rI   rI   z   s   € ØBEr/   c                 ó†  — | €yt        | t        «      rt        | «      S t        | t        t        f«      rt        t        | «      «      S t        | d«      r†t        j                  d«       | j                  «       }t        | d«      r+t        j                  t        | j                  «      «      d   nd}t        |t        «      rt        d«      ‚t        ||¬«      S t        | t        «      rÉ| j                  d«      s| j                  d	«      r}t        j                  d
| › �«       t        «       j!                  | «      }|j"                  j!                  d«      }|€t        j                  | «      d   }t        |j$                  |¬«      S t'        | «      } | j)                  «       st+        d| › d�«      ‚t        | t&        «      rIt        j                  d| › �«       t        | j-                  «       t        j                  | «      d   ¬«      S t/        «       r”ddlm} t        | |j2                  «      rxt        j                  d«       t5        j6                  «       }| j8                  xs d}| j;                  ||¬«       t        |j=                  «       d|j?                  «       › �¬«      S t        dtA        | «      › d�«      ‚)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)r3   ú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.)!r8   r-   r2   Ú	bytearrayÚ
memoryviewÚhasattrÚloggerÚdebugrL   Ú	mimetypesÚ
guess_typer)   rM   Ú	TypeErrorÚ
startswithr   Úgetr%   rE   r   ÚexistsÚFileNotFoundErrorÚ
read_bytesr   rC   r   ÚioÚBytesIOrQ   ÚsaveÚgetvalueÚlowerÚtype)rE   r$   r3   Úresponser   ÚbufferrQ   s          r0   rI   rI   ~   sW  € ð €Øô �'œ5Ô!Ü˜Ó!Ð!ô �'œI¤zÐ2Ô3Üœ˜w›Ó(Ð(ô ˆw˜ÔÜ�‰Ð4Ô5Ø�|‰|‹~ˆÜBIÈ'ÐSYÔBZ”I×(Ñ(¬¨W¯\©\Ó):Ó;¸AÒ>Ð`dˆ	Ü�dœCÔ ÜÐQÓRÐRÜ˜¨Ô3Ð3ô �'œ3ÔØ×Ñ˜jÔ)¨W×-?Ñ-?À	Ô-JÜ�L‰LÐ4°W°IÐ>Ô?Ü"“}×(Ñ(¨Ó1ˆHØ ×(Ñ(×,Ñ,¨^Ó<ˆIØÐ Ü%×0Ñ0°Ó9¸!Ñ<�	Ü˜X×-Ñ-¸ÔCÐCä�w“-ˆØ�~‰~ÔÜ#Ø$ W Ið .Oð Oóð ô �'œ4Ô Ü�‰Ð,¨W¨IÐ6Ô7Ü˜×+Ñ+Ó-¼×9MÑ9MÈgÓ9VÐWXÑ9YÔZÐZô ÔÝä�g˜uŸ{™{Ô+Ü�L‰LÐ8Ô9Ü—Z‘Z“\ˆFØ—^‘^Ò, uˆFØ�L‰L˜¨ˆLÔ/Ü˜VŸ_™_Ó.¸FÀ6Ç<Á<Ã>ÐBRÐ:SÔTÐTô Ø
$¤T¨'£] Oð 4uð 	uóð r/   c                 ó^   — t        | «      }t        j                  |«      j                  «       S )z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.)rI   Úbase64Ú	b64encodeÚdecode)rE   Ú	raw_bytess     r0   Ú_b64_encoderl   À   s&   € ä# GÓ,€IÜ×Ñ˜IÓ&×-Ñ-Ó/Ð/r/   Údefault_mime_typec                 óÔ   — t        | t        «      r| j                  d«      r| S t        | «      }|j                  xs |}t        j                  |«      j                  «       }d|› d|› �S )N)rO   rN   údata:ro   z;base64,)r8   r)   rZ   rI   r3   rh   ri   rj   )rE   rm   rk   r3   Úencoded_datas        r0   Ú_as_urlrq   Æ   sn   € Ü�'œ3Ô G×$6Ñ$6Ð7WÔ$XØˆô $ GÓ,€Ið ×#Ñ#Ò8Ð'8€Iô ×#Ñ# IÓ.×5Ñ5Ó7€Lð �9�+˜X l ^Ð4Ð4r/   Úencoded_imagec                 ó†   — t        «       } |j                  t        j                  t	        j
                  | «      «      «      S )z/Parse a base64-encoded string into a PIL Image.)rD   Úopenr_   r`   rh   Ú	b64decode)rr   r   s     r0   Ú_b64_to_imagerv   ×   s/   € äÓ€EØˆ5�:‰:”b—j‘j¤×!1Ñ!1°-Ó!@ÓAÓBÐBr/   c                 óH   — t        j                  | j                  «       «      S )ac  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#   Úloadsrj   rH   s    r0   Ú_bytes_to_listrz   Ý   ó   € ô �:‰:�g—n‘nÓ&Ó'Ð'r/   c                 óH   — t        j                  | j                  «       «      S )ac  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.
    rx   rH   s    r0   Ú_bytes_to_dictr}   è   r{   r/   c                 ó`   — t        «       } |j                  t        j                  | «      «      S )z§Parse 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.
    )rD   rt   r_   r`   )rE   r   s     r0   Ú_bytes_to_imager   ó   s&   € ô
 Ó€EØˆ5�:‰:”b—j‘j Ó)Ó*Ð*r/   re   c                 óP   — t        | t        «      rt        j                  | «      S | S r5   )r8   r-   r#   ry   )re   s    r0   Ú_as_dictr�   ü   s   € Ü#-¨h¼Ô#>Œ4�:‰:�hÓÐLÀHÐLr/   Úoutput_linesÚdetailsc              #   ób   K  — | D ]  }	 t        ||«      }|€Œ|–— Œ y# t        $ r Y  yw xY w­w)z*Used in `InferenceClient.text_generation`.N©Ú%_format_text_generation_stream_outputÚStopIteration©r‚   rƒ   ÚlineÚoutputs       r0   Ú _stream_text_generation_responser‹     sH   è ø€ ð
 ò ˆð	Ü:¸4ÀÓIˆFð ÑØ‹Lñøô ò 	Úð	üs    ‚/‰•/˜/Ÿ	,¨/«,¬/c                ót   K  — | 2 3 d{  –—† }	 t        ||«      }|€Œ|­–— Œ7 Œ# t        $ r Y  yw xY w6 y­w)z/Used in `AsyncInferenceClient.text_generation`.Nr…   rˆ   s       r0   Ú&_async_stream_text_generation_responser�     sS   è ø€ ð
 #÷ ð ˆdð	Ü:¸4ÀÓIˆFð ÑØŒLðùô ò 	Úð	úñ #ùs4   ‚8…6‰$Š6�8�&›8ž8¤6¦	3¯8²3³8r‰   c                 óˆ  — | j                  d«      sy | j                  «       dk(  rt        d«      ‚| j                  d«      j	                  d«      }t        j                  |«      }|j                  d«      �t        |d   |j                  d«      «      ‚t        j                  |«      }|s|j                  j                  S |S )Nro   údata: [DONE]ú[DONE] signal received.z/nÚerrorÚ
error_type)rZ   Ústripr‡   ÚlstripÚrstripr#   ry   r[   Ú_parse_text_generation_errorr   Úparse_obj_as_instanceÚtokenÚtext)r‰   rƒ   ÚpayloadÚjson_payloadrŠ   s        r0   r†   r†     s®   € Ø�?‰?˜7Ô#Øà‡z�zƒ|�~Ò%ÜÐ5Ó6Ð6ð �k‰k˜'Ó"×)Ñ)¨$Ó/€GÜ—:‘:˜gÓ&€Lð ×Ñ˜Ó Ð,Ü*¨<¸Ñ+@À,×BRÑBRÐS_ÓB`ÓaÐaô (×=Ñ=¸lÓK€FÙ$+ˆ6�<‰<×ÑÐ7°Ð7r/   Úlinesc              #   ó`   K  — | D ]  }	 t        |«      }|€Œ|–— Œ y# t        $ r Y  yw xY w­w)zFUsed in `InferenceClient.chat_completion` if model is served with TGI.N©Ú%_format_chat_completion_stream_outputr‡   ©rœ   r‰   rŠ   s      r0   Ú _stream_chat_completion_responser¡   3  sF   è ø€ ð ò ˆð	Ü:¸4Ó@ˆFð ÑØ‹Lñøô ò 	Úð	üs    ‚.‰”.—.ž	+§.ª+«.c                ór   K  — | 2 3 d{  –—† }	 t        |«      }|€Œ|­–— Œ7 Œ# t        $ r Y  yw xY w6 y­w)z/Used in `AsyncInferenceClient.chat_completion`.Nrž   r    s      r0   Ú&_async_stream_chat_completion_responser£   @  sQ   è ø€ ð ÷ ð ˆdð	Ü:¸4Ó@ˆFð ÑØŒLðùô ò 	Úð	úñ ùs4   ‚7…5‰#Š5�7�%š7�7£5¥	2®7±2²7c                 óN  — | j                  d«      sy | j                  «       dk(  rt        d«      ‚t        j                  | j                  d«      j                  «       «      }|j                  d«      �t        |d   |j                  d«      «      ‚t        j                  |«      S )Nro   r�   r�   r‘   r’   )
rZ   r“   r‡   r#   ry   r”   r[   r–   r   r—   )r‰   r›   s     r0   rŸ   rŸ   M  s“   € ð �?‰?˜7Ô#Øà‡z�zƒ|�~Ò%ÜÐ5Ó6Ð6ô —:‘:˜dŸk™k¨'Ó2×8Ñ8Ó:Ó;€Lð ×Ñ˜Ó Ð,Ü*¨<¸Ñ+@À,×BRÑBRÐS_ÓB`ÓaÐaô &×;Ñ;¸LÓIÐIr/   Úclientc                ól   K  — |j                  «       2 3 d {  –—† }|j                  «       ­–— Œ7 Œ6 y ­wr5   )Úaiter_linesr“   )r¥   re   r‰   s      r0   Ú_async_yield_fromr¨   a  s3   è ø€ Ø×*Ñ*Ó,÷ ð ˆdØ�j‰j‹lÔðøÑ,ùs   ‚4“2—0˜2›4°2²4Ú#_UNSUPPORTED_TEXT_GENERATION_KWARGSr"   Úunsupported_kwargsc                 óN   — t         j                  | g «      j                  |«       y r5   )r©   Ú
setdefaultÚextend)r"   rª   s     r0   Ú'_set_unsupported_text_generation_kwargsr®   {  s   € Ü'×2Ñ2°5¸"Ó=×DÑDÐEWÕXr/   c                 ó.   — t         j                  | g «      S r5   )r©   r[   )r"   s    r0   Ú'_get_unsupported_text_generation_kwargsr°     s   € Ü.×2Ñ2°5¸"Ó=Ð=r/   Ú
http_errorc                 óú   — | j                   €| ‚	 t        | dd«      xs | j                   j                  «       }|j                  d«      }|j                  d«      }|�t        ||«      }|| ‚| ‚# t        $ r | ‚w xY w)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’   )re   Úgetattrr#   r[   Ú	Exceptionr–   )r±   rš   r‘   r’   Ú	exceptions        r0   Úraise_text_generation_errorr·   Š  s•   € ð ×ÑÐ"ØÐðä˜*Ð&>ÀÓEÒcÈ×I\ÑI\×IaÑIaÓIcˆØ—‘˜GÓ$ˆØ—[‘[ Ó.ˆ
ð
 ÐÜ0°¸
ÓCˆ	Ø˜ZÐ'ð Ðøô ò ØÐðús   �AA. Á.A:r‘   r’   c                 ó˜   — |dk(  rt        | «      S |dk(  rt        | «      S |dk(  rt        | «      S |dk(  rt        | «      S t	        | «      S )NÚ
generationÚincomplete_generationÚ
overloadedÚ
validation)r   r   r   r   r   )r‘   r’   s     r0   r–   r–   §  sZ   € Ø�\Ò!Ü˜uÓ%Ð%ØÐ,Ò,Ü(¨Ó/Ð/Ø�\Ò!Ü˜uÓ%Ð%Ø�\Ò!Ü˜uÓ%Ð%Ü˜ÓÐr/   )Pr<   rh   r_   r#   ÚloggingrW   Ú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ÚPathTr-   rR   rS   ÚContentTÚTASKS_EXPECTING_IMAGESÚ	getLoggerr&   rU   r   r2   rA   rD   rI   rl   rq   rv   r,   rz   r+   r}   r   r�   Úboolr‹   r�   r†   r¡   r£   rŸ   ÚAsyncClientÚResponser¨   r©   r*   r®   r°   r·   r–   r.   r/   r0   ú<module>rÏ      si  ðò Lã Û 	Û Û Û ß 3Ý !Ý ß S× SÑ Sã ÷÷ ñ ÷ IÑ Hß Tñ Ýð €Øˆc�4ˆiÑ€Ø�˜ %¨¨w¸	À:ÐMÑN€ð *Ð+;Ð<Ð à	ˆ×	Ñ	˜8Ó	$€ð ÷ð ó ðô�ô ò8ò	ð 
Ø < Ð <¨iÒ <ó 
Ø <ð 
Ø E ¨¡Ð E°7¸4±=Ò Eó 
Ø Eð? ¨D¡ð ?°YÀÑ5Eó ?ðD0˜ð 0 có 0ð5�Xð 5°#ð 5¸#ó 5ð"C ð C¨ó Cð(˜Eð ( dó (ð(˜Eð ( dó (ð+˜Uð + wó +ðM�u˜t‘|ð M¨ó MðØ˜3‘-ðØ*.ðàˆc�]�XÐ8Ñ9Ñ9óðØ Ñ$ðØ/3ðà�3Ñ˜-Ð(BÑCÑCóð8°ð 8¸dð 8ÀsÐMgÑGgÐjnÑGnó 8ð(
Ø�C‰=ð
àÐ(Ñ)ó
ð
Ø˜Ñð
àÐ-Ñ.ó
ðJØ
ðJà $Ñ&óJð( E×$5Ñ$5ð ÀÇÁð ÐTaÐbeÑTfó ð. DFÐ # T¨#°©*°d¸3±iÐ*?Ñ%@Ó EðY°3¸±:ð YÐSWÐX[ÑS\ð YÐaeó Yð>°3¸±:ð >À$ÀsÁ)ó >ð¨Nð ¸xó ð:	¨¨d©
ð 	ÀÀdÁ
ð 	ÐObô 	r/   