Ë
    Q^(hÿ8  ã            	       ó  — U d Z ddlZddlZddlZddlZddl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mZmZmZmZmZmZmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z" d	d
l#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* erddl+m,Z,m-Z- ddl.m/Z/ e0Z1ee0e
f   Z2ee3ef   Z4ee4e2e1f   Z5ddhZ6 ejn                  e8«      Z9e G d„ d«      «       Z:e G d„ d«      «       Z;d„ Z<d„ Z=d„ Z>ede5dee4   fd„«       Z?eded   deed      fd„«       Z?edee5   deee4   ddf   fd„«       Z?de5de0fd„Z@de0ddfd „ZAde3defd!„ZBde3defd"„ZCde3ddfd#„ZDd$ee3ef   defd%„ZEd&ee3   d'eFdeee0   ee*   f   fd(„ZGd&ee3   d'eFdeee0   ee*   f   fd)„ZHd*e3d'eFdeee0e*f      fd+„ZId,ee3   dee)   fd-„ZJd,ee3   dee)   fd.„ZKd*e3dee)   fd/„ZLd0d1d$d2dee3   fd3„ZMi ZNeee0   ee0   f   eOd4<   d5ee0   d6ee0   ddfd7„ZPd5ee0   dee0   fd8„ZQd9edefd:„ZRd;ee0   d<ee0   de fd=„ZSy)>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                   ór   — e Zd ZU eed<   eed<   ee   ed<   eeeeef      ed<   ee	   ed<   eee
f   ed<   y)ÚRequestParametersÚurlÚtaskÚmodelÚjsonÚdataÚheadersN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__r   r   r   r   ÚContentTr   © ó    ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/huggingface_hub/inference/_common.pyr(   r(   H   sE   … à	ƒHØ
ƒIØ�C‰=ÓØ
�5˜˜d D˜Ñ)Ñ
*Ó*Ø
�8Ñ
ÓØ�#�s�(‰^Ôr6   r(   c                   ó:   — e Zd ZU dZeed<   eed<   eed<   eed<   y)ÚModelStatusas  
    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Ú	frameworkN)r/   r0   r1   Ú__doc__Úboolr3   r2   r   r5   r6   r7   r9   r9   S   s   … ñð, ƒLØƒJØÓØ„Nr6   r9   c                  ó8   — t        «       st        d«      ‚dd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ÚImportErrorÚaiohttp)rB   s    r7   Ú_import_aiohttprC   t   s   € äÔ!ÜÐiÓjÐjÛà€Nr6   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   rA   Únumpy)rE   s    r7   Ú_import_numpyrF   }   s   € äÔÜÐcÓdÐdÛà€Lr6   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   rA   ÚPILr&   r%   s    r7   Ú_import_pil_imagerI   †   s%   € äÔ Üð`ó
ð 	
õ à€Lr6   ÚcontentÚreturnc                  ó   — y ©Nr5   ©rJ   s    r7   Ú_open_as_binaryrO   •   s   € ð "r6   c                  ó   — y rM   r5   rN   s    r7   rO   rO   ›   s   € ð %(r6   c              #   óî  K  — t        | t        «      rŠ| j                  d«      s| j                  d«      r>t        j	                  d| › �«       t        «       j                  | «      j                  –— yt        | «      } | j                  «       st        d| › d�«      ‚t        | t        «      r7t        j	                  d| › �«       | j                  d«      5 }|–— ddd«       y| –— y# 1 sw Y   yxY w­w)	zÂOpen `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)Ú
isinstancer2   Ú
startswithÚloggerÚdebugr   ÚgetrJ   r   ÚexistsÚFileNotFoundErrorÚopen)rJ   Úfs     r7   rO   rO   ¡   sã   è ø€ ô �'œ3ÔØ×Ñ˜jÔ)¨W×-?Ñ-?À	Ô-JÜ�L‰LÐ4°W°IÐ>Ô?Ü“-×#Ñ# GÓ,×4Ñ4Ò4ØÜ�w“-ˆØ�~‰~ÔÜ#Ø$ W Ið .Oð Oóð ô �'œ4Ô Ü�‰Ð,¨W¨IÐ6Ô7Ø�\‰\˜$Óð 	 1ØŠG÷	ð 	ð ‹÷		ð 	üs   ‚CC5ÃC)ÃC5Ã)C2Ã.C5c                 óÎ   — t        | «      5 }t        |t        «      r|n|j                  «       }t	        j
                  |«      j                  «       cddd«       S # 1 sw Y   yxY w)z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.N)rO   rS   ÚbytesÚreadÚbase64Ú	b64encodeÚdecode)rJ   r-   Údata_as_bytess      r7   Ú_b64_encoderc   Á   sN   € ä	˜Ó	!ð 8 TÜ *¨4´Ô 7™¸T¿Y¹Y»[ˆÜ×Ñ Ó.×5Ñ5Ó7÷8÷ 8ò 8ús   ŒAAÁA$Úencoded_imager&   c                 ó†   — t        «       } |j                  t        j                  t	        j
                  | «      «      «      S )z/Parse a base64-encoded string into a PIL Image.)rI   rZ   ÚioÚBytesIOr_   Ú	b64decode)rd   r&   s     r7   Ú_b64_to_imageri   È   s/   € äÓ€EØˆ5�:‰:”b—j‘j¤×!1Ñ!1°-Ó!@ÓAÓBÐBr6   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,   Úloadsra   rN   s    r7   Ú_bytes_to_listrm   Î   ó   € ô �:‰:�g—n‘nÓ&Ó'Ð'r6   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.
    rk   rN   s    r7   Ú_bytes_to_dictrp   Ù   rn   r6   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.
    )rI   rZ   rf   rg   )rJ   r&   s     r7   Ú_bytes_to_imagerr   ä   s&   € ô
 Ó€EØˆ5�:‰:”b—j‘j Ó)Ó*Ð*r6   Úresponsec                 óP   — t        | t        «      rt        j                  | «      S | S rM   )rS   r]   r,   rl   )rs   s    r7   Ú_as_dictru   í   s   € Ü#-¨h¼Ô#>Œ4�:‰:�hÓÐLÀHÐLr6   Úbytes_output_as_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©rv   rw   Úbyte_payloadÚoutputs       r7   Ú _stream_text_generation_responser   ÷   sH   è ø€ ð
 .ò ˆð	Ü:¸<ÈÓQˆ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`.Nry   r|   s       r7   Ú&_async_stream_text_generation_responser�     sS   è ø€ ð
 4÷ ð ˆlð	Ü:¸<ÈÓQˆFð ÑØŒLðùô ò 	Úð	úñ 4ù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«      }t	        j
                  |j                  d«      j                  d«      «      }|j                  d«      �t        |d   |j                  d«      «      ‚t        j                  |«      }|s|j                  j                  S |S ©	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8zdata:z/nÚerrorÚ
error_type)rT   Ústripr{   ra   r,   rl   ÚlstripÚrstriprW   Ú_parse_text_generation_errorr"   Úparse_obj_as_instanceÚtokenÚtext)r}   rw   ÚpayloadÚjson_payloadr~   s        r7   rz   rz     sÀ   € ð ×"Ñ" 8Ô,Øà×ÑÓ˜Ò.ÜÐ5Ó6Ð6ð ×!Ñ! 'Ó*€GÜ—:‘:˜gŸn™n¨WÓ5×<Ñ<¸TÓBÓC€Lð ×Ñ˜Ó Ð,Ü*¨<¸Ñ+@À,×BRÑBRÐS_ÓB`ÓaÐaô (×=Ñ=¸lÓK€FÙ$+ˆ6�<‰<×ÑÐ7°Ð7r6   Úbytes_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�   Úitemr~   s      r7   Ú _stream_chat_completion_responser•   )  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      r7   Ú&_async_stream_chat_completion_responser—   6  sQ   è ø€ ð "÷ ð ˆdð	Ü:¸4Ó@ˆFð ÑØŒLðùô ò 	Úð	úñ "ùs4   ‚7…5‰#Š5�7�%š7�7£5¥	2®7±2²7c                 ór  — | j                  d«      sy | j                  «       dk(  rt        d«      ‚| j                  d«      }t	        j
                  |j                  d«      j                  d«      «      }|j                  d«      �t        |d   |j                  d«      «      ‚t        j                  |«      S rƒ   )rT   r†   r{   ra   r,   rl   r‡   rˆ   rW   r‰   r!   rŠ   )r}   r�   rŽ   s      r7   r’   r’   C  sª   € ð ×"Ñ" 8Ô,Øà×ÑÓ˜Ò.ÜÐ5Ó6Ð6ð ×!Ñ! 'Ó*€GÜ—:‘:˜gŸn™n¨WÓ5×<Ñ<¸TÓBÓC€Lð ×Ñ˜Ó Ð,Ü*¨<¸Ñ+@À,×BRÑBRÐS_ÓB`ÓaÐaô &×;Ñ;¸LÓIÐIr6   Úclientr$   r#   c                ó–   K  — |j                   2 3 d {  –—† }|j                  «       ­–— Œ7 Œ6 | j                  «       ƒ d {  –—†7   y ­wrM   )rJ   r†   Úclose)r™   rs   r}   s      r7   Ú_async_yield_fromrœ   X  sB   è ø€ Ø&×.Ñ.÷ #ð #ˆlØ× Ñ Ó"Ô"ð#øÐ.à
�,‰,‹.×Òùs+   ‚A	�.“,”.—A	¬.®A	ÁAÁA	Ú#_UNSUPPORTED_TEXT_GENERATION_KWARGSr+   Úunsupported_kwargsc                 óN   — t         j                  | g «      j                  |«       y rM   )r�   Ú
setdefaultÚextend)r+   rž   s     r7   Ú'_set_unsupported_text_generation_kwargsr¢   s  s   € Ü'×2Ñ2°5¸"Ó=×DÑDÐEWÕXr6   c                 ó.   — t         j                  | g «      S rM   )r�   rW   )r+   s    r7   Ú'_get_unsupported_text_generation_kwargsr¤   w  s   € Ü.×2Ñ2°5¸"Ó=Ð=r6   Ú
http_errorc                 óÞ   — 	 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.
    Úresponse_error_payloadNr„   r…   )Úgetattrrs   r,   rW   Ú	Exceptionr‰   )r¥   r�   r„   r…   Ú	exceptions        r7   Ú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     r7   r‰   r‰   �  sZ   € Ø�\Ò!Ü˜uÓ%Ð%ØÐ,Ò,Ü(¨Ó/Ð/Ø�\Ò!Ü˜uÓ%Ð%Ø�\Ò!Ü˜uÓ%Ð%Ü˜ÓÐr6   )Tr>   r_   rf   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"   rB   r#   r$   Ú	PIL.Imager&   r2   ÚUrlTÚPathTr]   ÚBinaryTr4   ÚTASKS_EXPECTING_IMAGESÚ	getLoggerr/   rU   r(   r9   rC   rF   rI   rO   rc   ri   rm   rp   rr   ru   r?   r   r�   rz   r•   r—   r’   rœ   r�   r3   r¢   r¤   r«   r‰   r5   r6   r7   ú<module>rÀ      s¹  ðò Lã Û 	Û Û Ý %Ý !Ý ÷÷ ÷ ÷ õ" ÷÷ ÷ _Ó ^ß Tñ ß5Ýð €Øˆc�4ˆiÑ€Ø
��x�Ñ
 €Ø�˜% Ð%Ñ&€ð *Ð+;Ð<Ð à	ˆ×	Ñ	˜8Ó	$€ð ÷ð ó ðð ÷ð ó ðò@òò	ð 
ð"Øð"à�GÑò"ó 
ð"ð
 
ð(Ø�T‰]ð(à�G˜D‘MÑ"ò(ó 
ð(ð
 ð˜X hÑ/ð °I¸hÀwÑ>OÐQUÐW[Ð>[Ñ4\ò ó ðð>8˜ð 8 có 8ðC ð C¨ó Cð(˜Eð ( dó (ð(˜Eð ( dó (ð+˜Uð + wó +ðM�u˜U D˜[Ñ)ð M¨dó MðØ# E™?ðØ59ðà
ˆ8�C‰=˜(Ð#=Ñ>Ð>Ñ?óðØ(¨Ñ/ðØ:>ðà
ˆ=˜Ñ˜}Ð-GÑHÐHÑIóð8Øð8Ø"&ð8àˆe�CÐ3Ð3Ñ4Ñ5ó8ð,
Ø˜%‘ð
àÐ(Ñ)ó
ð
Ø˜uÑ%ð
àÐ-Ñ.ó
ðJØðJàÐ(Ñ)óJð* Oð Ð?Oð ÐTaÐbgÑThó ð0 GIÐ # T¨(°3©-¸¸c¹Ð*BÑ%CÓ HðY°8¸C±=ð YÐVZÐ[^ÑV_ð YÐdhó Yð>°8¸C±=ð >ÀTÈ#ÁYó >ð¨Ið ¸(ó ð6	¨°©ð 	À8ÈCÁ=ð 	ÐUhô 	r6   