Ë
    çÍ: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 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 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, e-Z.ee-e	f   Z/ee0ee/e.de1e2f   Z3ddhZ4 ejj                  e6«      Z7e G d„ d«      «       Z8 G d„ de0«      Z9d„ Z:d„ Z;d„ Z<ede3de9fd„«       Z=eded   ded   fd„«       Z=dee3   dee9   fd„Z=de3de-fd„Z>de3de-de-fd„Z?d e-ddfd!„Z@de0defd"„ZAde0defd#„ZBde0ddfd$„ZCd%ee0ef   defd&„ZDd'ee0   d(eEdeee-   ee'   f   fd)„ZFd'ee0   d(eEdeee-   ee'   f   fd*„ZGd+e0d(eEdeee-e'f      fd,„ZHd-ee0   dee&   fd.„ZId-ee0   dee&   fd/„ZJd+e0dee&   fd0„ZKd1d2d%d3dee0   fd4„ZLi ZMeee-   ee-   f   eNd5<   d6ee-   d7ee-   ddfd8„ZOd6ee-   dee-   fd9„ZPd:edefd;„ZQd<ee-   d=ee-   defd>„ZRy)?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                   ó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   Úbytesr   © ó    úv/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/huggingface_hub/inference/_common.pyr%   r%   E   sD   … à	ƒHØ
ƒIØ�C‰=ÓØ
�5˜˜d D˜Ñ)Ñ
*Ó*Ø
�5‰/ÓØ�#�s�(‰^Ôr3   r%   c                   óF   ‡ — e Zd ZU dZee   ed<   ddedee   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'
    ```
    Ú	mime_typer*   c                 ó|   •— t         ‰| �  | |«      }||_        t        |t        «      r|€|j                  |_        |S ©N)ÚsuperÚ__new__r7   Ú
isinstancer6   )Úclsr*   r7   ÚobjÚ	__class__s       €r4   r;   zMimeBytes.__new__`   s9   ø€ Ü‰g‰o˜c 4Ó(ˆØ!ˆŒÜ�dœIÔ&¨9Ð+<Ø ŸN™NˆCŒMØˆ
r3   r9   )
r,   r-   r.   Ú__doc__r   r/   r0   r1   r;   Ú__classcell__)r?   s   @r4   r6   r6   O   s0   ø… ñð ˜‰}Óñ˜5ð ¨X°c©]÷ ñ r3   r6   c                  ó8   — t        «       st        d«      ‚dd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ÚImportErrorÚaiohttp)rD   s    r4   Ú_import_aiohttprE   k   s   € äÔ!ÜÐiÓjÐjÛà€Nr3   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   rC   Únumpy)rG   s    r4   Ú_import_numpyrH   t   s   € äÔÜÐcÓdÐdÛà€Lr3   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   rC   ÚPILr#   r"   s    r4   Ú_import_pil_imagerK   }   s%   € äÔ Üð`ó
ð 	
õ à€Lr3   ÚcontentÚreturnc                  ó   — y r9   r2   ©rL   s    r4   Ú_open_as_mime_bytesrP   Œ   s   € Ø9<r3   c                  ó   — y r9   r2   rO   s    r4   rP   rP   �   s   € ØBEr3   c                 ót  — | €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                  | 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)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<   r1   r6   Ú	bytearrayÚ
memoryviewÚhasattrÚloggerÚdebugrS   Ú	mimetypesÚ
guess_typerT   r/   Ú	TypeErrorÚ
startswithr   Úgetr+   rL   r   ÚexistsÚFileNotFoundErrorÚ
read_bytesr   rJ   r#   ÚioÚBytesIOrX   ÚsaveÚgetvalueÚlowerÚtype)rL   r*   r7   Úresponser#   ÚbufferrX   s          r4   rP   rP   ”   sQ  € ð €Øô �'œ5Ô!Ü˜Ó!Ð!ô �'œI¤zÐ2Ô3Üœ˜w›Ó(Ð(ô ˆw˜ÔÜ�‰Ð4Ô5Ø�|‰|‹~ˆÜ=DÀWÈfÔ=U”I×(Ñ(¨¯©Ó6°qÒ9Ð[_ˆ	Ü�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óð r3   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.)rP   Úbase64Ú	b64encodeÚdecode)rL   Ú	raw_bytess     r4   Ú_b64_encoders   Ö   s&   € ä# GÓ,€IÜ×Ñ˜IÓ&×-Ñ-Ó/Ð/r3   Údefault_mime_typec                 óÔ   — t        | t        «      r| j                  d«      r| S t        | «      }|j                  xs |}t        j                  |«      j                  «       }d|› d|› �S )N)rV   rU   údata:rv   z;base64,)r<   r/   ra   rP   r7   ro   rp   rq   )rL   rt   rr   r7   Úencoded_datas        r4   Ú_as_urlrx   Ü   sn   € Ü�'œ3Ô G×$6Ñ$6Ð7WÔ$XØˆô $ GÓ,€Ið ×#Ñ#Ò8Ð'8€Iô ×#Ñ# IÓ.×5Ñ5Ó7€Lð �9�+˜X l ^Ð4Ð4r3   Úencoded_imagec                 ó†   — t        «       } |j                  t        j                  t	        j
                  | «      «      «      S )z/Parse a base64-encoded string into a PIL Image.)rK   Úopenrf   rg   ro   Ú	b64decode)ry   r#   s     r4   Ú_b64_to_imager}   í   s/   € äÓ€EØˆ5�:‰:”b—j‘j¤×!1Ñ!1°-Ó!@ÓAÓBÐBr3   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)   Úloadsrq   rO   s    r4   Ú_bytes_to_listr�   ó   ó   € ô �:‰:�g—n‘nÓ&Ó'Ð'r3   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.
    r   rO   s    r4   Ú_bytes_to_dictr„   þ   r‚   r3   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.
    )rK   r{   rf   rg   )rL   r#   s     r4   Ú_bytes_to_imager†   	  s&   € ô
 Ó€EØˆ5�:‰:”b—j‘j Ó)Ó*Ð*r3   rl   c                 óP   — t        | t        «      rt        j                  | «      S | S r9   )r<   r1   r)   r€   )rl   s    r4   Ú_as_dictrˆ     s   € Ü#-¨h¼Ô#>Œ4�:‰:�hÓÐLÀHÐLr3   Ú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©r‰   rŠ   Úbyte_payloadÚoutputs       r4   Ú _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`.NrŒ   r�   s       r4   Ú&_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-8rv   z/nÚerrorÚ
error_type)ra   ÚstriprŽ   rq   r)   r€   ÚlstripÚrstriprb   Ú_parse_text_generation_errorr   Úparse_obj_as_instanceÚtokenÚtext)r�   rŠ   ÚpayloadÚjson_payloadr‘   s        r4   r�   r�   5  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°Ð7r3   Ú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      r4   Ú _stream_chat_completion_responser¨   K  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      r4   Ú&_async_stream_chat_completion_responserª   X  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–   )ra   r™   rŽ   rq   r)   r€   rš   r›   rb   rœ   r   r�   )r�   r    r¡   s      r4   r¥   r¥   e  sª   € ð ×"Ñ" 8Ô,Øà×ÑÓ˜Ò.ÜÐ5Ó6Ð6ð ×!Ñ! 'Ó*€GÜ—:‘:˜gŸn™n¨WÓ5×<Ñ<¸TÓBÓC€Lð ×Ñ˜Ó Ð,Ü*¨<¸Ñ+@À,×BRÑBRÐS_ÓB`ÓaÐaô &×;Ñ;¸LÓIÐIr3   Úclientr!   r    c                óÖ   K  — 	 |j                   2 3 d {  –—† }|j                  «       ­–— Œ7 Œ6 	 | j                  «       ƒ d {  –—†7   y # | j                  «       ƒ d {  –—†7   w xY w­wr9   )rL   r™   Úclose)r¬   rl   r�   s      r4   Ú_async_yield_fromr¯   z  sX   è ø€ ðØ"*×"2Ñ"2÷ 	'ð 	'�,Ø×$Ñ$Ó&Ô&ð	'øÑ"2ð �l‰l‹n×Òøˆf�l‰l‹n×ÒüsM   ‚A)„A �/”-•/˜A ­/¯A ±A)ÁAÁA)ÁA&ÁA"Á A&Á&A)Ú#_UNSUPPORTED_TEXT_GENERATION_KWARGSr(   Úunsupported_kwargsc                 óN   — t         j                  | g «      j                  |«       y r9   )r°   Ú
setdefaultÚextend)r(   r±   s     r4   Ú'_set_unsupported_text_generation_kwargsrµ   ˜  s   € Ü'×2Ñ2°5¸"Ó=×DÑDÐEWÕXr3   c                 ó.   — t         j                  | g «      S r9   )r°   rb   )r(   s    r4   Ú'_get_unsupported_text_generation_kwargsr·   œ  s   € Ü.×2Ñ2°5¸"Ó=Ð=r3   Ú
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˜   )Úgetattrrl   r)   rb   Ú	Exceptionrœ   )r¸   r    r—   r˜   Ú	exceptions        r4   Ú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     r4   rœ   rœ   Â  sZ   € Ø�\Ò!Ü˜uÓ%Ð%ØÐ,Ò,Ü(¨Ó/Ð/Ø�\Ò!Ü˜uÓ%Ð%Ø�\Ò!Ü˜uÓ%Ð%Ü˜ÓÐr3   )Sr@   ro   rf   r)   Úloggingr^   Ú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   rD   r    r!   Ú	PIL.Imager#   r/   ÚUrlTÚPathTr1   rY   rZ   ÚContentTÚTASKS_EXPECTING_IMAGESÚ	getLoggerr,   r\   r%   r6   rE   rH   rK   rP   rs   rx   r}   r�   r„   r†   rˆ   Úboolr’   r”   r�   r¨   rª   r¥   r¯   r°   r0   rµ   r·   r¾   rœ   r2   r3   r4   ú<module>rÓ      s…  ðò Lã Û 	Û Û Û Ý !Ý ÷÷ ÷ ó õ ÷÷ ÷ _Ó ^ß Tñ ß5Ýð €Øˆc�4ˆiÑ€Ø�˜ %¨¨w¸	À:ÐMÑN€ð *Ð+;Ð<Ð à	ˆ×	Ñ	˜8Ó	$€ð ÷ð ó ðô�ô ò8òò	ð 
Ø < Ð <¨iÒ <ó 
Ø <ð 
Ø E ¨¡Ð E°7¸4±=Ò Eó 
Ø Eð? ¨(Ñ!3ð ?¸ÀÑ8Kó ?ðD0˜ð 0 có 0ð5�Xð 5°#ð 5¸#ó 5ð"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ó ð6 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ô 	r3   