o
    vZh8                  	   @   s  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Z6e7e8Z9eG dd dZ:eG dd dZ;dd Z<dd Z=dd Z>ede5dee4 fddZ?eded deed  fddZ?edee5 deee4 ddf fd dZ?de5de0fd!d"Z@d#e0dd$fd%d&ZAde3defd'd(ZBde3defd)d*ZCde3dd$fd+d,ZDd-ee3ef defd.d/ZEd0ee3 d1eFdeee0 ee* f fd2d3ZGd0ee3 d1eFdeee0 ee* f fd4d5ZHd6e3d1eFdeee0e*f  fd7d8ZId9ee3 dee) fd:d;ZJd9ee3 dee) fd<d=ZKd6e3dee) fd>d?ZLd@dAd-dBdee3 fdCdDZMi ZNeee0 ee0 f eOdE< dFee0 dGee0 ddfdHdIZPdFee0 dee0 fdJdKZQdLedefdMdNZRdOee0 dPee0 de fdQdRZSdS )SzEContains 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                   @   s\   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< dS )RequestParametersurltaskmodeljsondataheadersN)__name__
__module____qualname__str__annotations__r   r   r
   r   ContentTr    r3   r3   P/var/www/auris/lib/python3.10/site-packages/huggingface_hub/inference/_common.pyr&   H   s   
 r&   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )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'.
    ZloadedstateZcompute_typeZ	frameworkN)r-   r.   r/   __doc__boolr1   r0   r
   r3   r3   r3   r4   r5   S   s   
 r5   c                  C   s   t  stddd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ImportErroraiohttp)r:   r3   r3   r4   _import_aiohttpt      r;   c                  C   s   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   r9   numpy)r=   r3   r3   r4   _import_numpy}   r<   r>   c                  C   s   t  stdddlm}  | S )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   r9   ZPILr%   r$   r3   r3   r4   _import_pil_image   s   r?   contentreturnc                 C      d S Nr3   r@   r3   r3   r4   _open_as_binary      rE   c                 C   rB   rC   r3   rD   r3   r3   r4   rE      rF   c                 c   s    t | tr2| ds| dr"td|   t | jV  dS t| } | 	 s2t
d|  dt | trZtd|   | d}|V  W d   dS 1 sSw   Y  dS | V  dS )	zOpen `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)
isinstancer0   
startswithloggerdebugr   getr@   r   existsFileNotFoundErroropen)r@   fr3   r3   r4   rE      s"   



"
c                 C   sP   t | }t|tr|n| }t| W  d   S 1 s!w   Y  dS )z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.N)rE   rH   bytesreadbase64	b64encodedecode)r@   r+   Zdata_as_bytesr3   r3   r4   _b64_encode   s   
$rV   encoded_imager%   c                 C   s   t  }|tt| S )z/Parse a base64-encoded string into a PIL Image.)r?   rO   ioBytesIOrS   	b64decode)rW   r%   r3   r3   r4   _b64_to_image   s   r[   c                 C      t |  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*   loadsrU   rD   r3   r3   r4   _bytes_to_list      r_   c                 C   r\   )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]   rD   r3   r3   r4   _bytes_to_dict   r`   ra   c                 C   s   t  }|t| S )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.
    )r?   rO   rX   rY   )r@   r%   r3   r3   r4   _bytes_to_image   s   rb   responsec                 C   s   t | tr
t| S | S rC   )rH   rQ   r*   r^   )rc   r3   r3   r4   _as_dict   s   rd   bytes_output_as_linesdetailsc              	   c   sD    | D ]}zt ||}W n ty   Y  dS w |dur|V  qdS )z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_outputStopIterationre   rf   byte_payloadoutputr3   r3   r4    _stream_text_generation_response   s   rm   c              	   C  sN   | 2 z 3 dH W }zt ||}W n ty   Y  dS w |dur#|V  q6 dS )z/Used in `AsyncInferenceClient.text_generation`.Nrg   rj   r3   r3   r4   &_async_stream_text_generation_response  s   rn   rk   c                 C   s~   |  dsd S |  dkrtd| d}t|dd}|dd ur2t	|d |dt
|}|s=|jjS |S 	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8zdata:z/nerror
error_type)rI   stripri   rU   r*   r^   lstriprstriprL   _parse_text_generation_errorr!   parse_obj_as_instancetokentext)rk   rf   payloadjson_payloadrl   r3   r3   r4   rh     s   


rh   bytes_linesc              	   c   sB    | D ]}zt |}W n ty   Y  dS w |dur|V  qdS )zFUsed in `InferenceClient.chat_completion` if model is served with TGI.N%_format_chat_completion_stream_outputri   r{   itemrl   r3   r3   r4    _stream_chat_completion_response)  s   r   c              	   C  sL   | 2 z3 dH W }zt |}W n ty   Y  dS w |dur"|V  q6 dS )z/Used in `AsyncInferenceClient.chat_completion`.Nr|   r~   r3   r3   r4   &_async_stream_chat_completion_response6  s   r   c                 C   sn   |  dsd S |  dkrtd| d}t|dd}|dd ur2t	|d |dt
|S ro   )rI   rr   ri   rU   r*   r^   rs   rt   rL   ru   r    rv   )rk   ry   rz   r3   r3   r4   r}   C  s   


r}   clientr#   r"   c                 C  s4   |j 2 z3 d H W }| V  q6 |  I d H  d S rC   )r@   rr   close)r   rc   rk   r3   r3   r4   _async_yield_fromX  s
   r   #_UNSUPPORTED_TEXT_GENERATION_KWARGSr)   unsupported_kwargsc                 C   s   t | g | d S rC   )r   
setdefaultextend)r)   r   r3   r3   r4   '_set_unsupported_text_generation_kwargss  s   r   c                 C   s   t | g S rC   )r   rL   )r)   r3   r3   r4   '_get_unsupported_text_generation_kwargsw  s   r   
http_errorc                 C   s^   zt | ddp| j }|d}|d}W n	 ty    | w |dur-t||}|| | )z
    Try to parse text-generation-inference error message and raise HTTPError in any case.

    Args:
        error (`HTTPError`):
            The HTTPError that have been raised.
    Zresponse_error_payloadNrp   rq   )getattrrc   r*   rL   	Exceptionru   )r   ry   rp   rq   	exceptionr3   r3   r4   raise_text_generation_error  s   


r   rp   rq   c                 C   sH   |dkrt | S |dkrt| S |dkrt| S |dkr t| S t| S )NZ
generationZincomplete_generationZ
overloadedZ
validation)r   r   r   r   r   )rp   rq   r3   r3   r4   ru     s   ru   )Tr7   rS   rX   r*   logging
contextlibr   dataclassesr   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   requestsr   Zhuggingface_hub.errorsr   r   r   r   r   r   utilsr   r   r   r   Z_generated.typesr    r!   r:   r"   r#   Z	PIL.Imager%   r0   ZUrlTZPathTrQ   ZBinaryTr2   ZTASKS_EXPECTING_IMAGES	getLoggerr-   rJ   r&   r5   r;   r>   r?   rE   rV   r[   r_   ra   rb   rd   r8   rm   rn   rh   r   r   r}   r   r   r1   r   r   r   ru   r3   r3   r3   r4   <module>   s   @ 	

 		
&	






"