a
    h>                     @   s  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 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/m0Z0 e1Z2ee1ef Z3ee4ef Z5ee5e3e2df Z6ddhZ7e8e9Z:e	G dd dZ;e	G dd dZ<dd Z=dd Z>dd Z?ee6ee5 dddZ@eed eed  dddZ@eee6 eee5 ddf dd dZ@e6e1dd!d"ZAe6e1e1d#d$d%ZBe1dd&d'd(ZCe4edd)d*ZDe4edd+d,ZEe4ddd-d.ZFee4ef ed/d0d1ZGee4 eHeee1 ee+ f d2d3d4ZIee4 eHeee1 ee+ f d2d5d6ZJe4eHeee1e+f  d7d8d9ZKee4 ee* d:d;d<ZLee4 ee* d:d=d>ZMe4ee* d?d@dAZNdBdCee4 dDdEdFZOi ZPeee1 ee1 f eQdG< ee1 ee1 ddHdIdJZRee1 ee1 dKdLdMZSeedNdOdPZTee1 ee1 e!dQdRdSZUdS )TzEContains 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Imager%   z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   ]/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/inference/_common.pyr&   I   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   T   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_aiohttpu   s    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~   s    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   PILr%   r$   r3   r3   r4   _import_pil_image   s    r?   )contentreturnc                 C   s   d S Nr3   r@   r3   r3   r4   _open_as_binary   s    rD   c                 C   s   d S rB   r3   rC   r3   r3   r4   rD      s    c                 c   s  t | trb| ds| drBtd|   t | jV  dS t| } | 	 sbt
d|  dt | trtd|   | d}|V  W d   n1 s0    Y  dS t rd	d
lm} t | |jrtd t }| j|| jpdd | V  dS | V  dS )zOpen `content` as a binary file, either from a URL, a local path, raw bytes, or a PIL Image.

    Do nothing if `content` is None.

    TODO: handle base64 as input
    https://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 rbr   r$   zConverting PIL Image to bytesPNG)format)
isinstancer0   
startswithloggerdebugr   getr@   r   existsFileNotFoundErroropenr   r>   r%   ioBytesIOsaverI   getvalue)r@   fr%   bufferr3   r3   r4   rD      s0    	


$

c                 C   sP   t | 4}t|tr|n| }t| W  d   S 1 sB0    Y  dS )z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.N)rD   rJ   bytesreadbase64	b64encodedecode)r@   r+   Zdata_as_bytesr3   r3   r4   _b64_encode   s    
r]   )r@   default_mime_typerA   c                 C   s   t | tr"| ds| dr"| S d }t | ttfrHtj| ddd }n2t rzddlm} t | |jrzd| j	ppd
  }|p|}t| }d	| d
| S )NrE   rF   F)strictr   r$   zimage/rH   data:z;base64,)rJ   r0   rK   r   	mimetypes
guess_typer   r>   r%   rI   lowerr]   )r@   r^   Z	mime_typer%   Zencoded_datar3   r3   r4   _as_url   s    rd   )encoded_imagerA   c                 C   s   t  }|tt| S )z/Parse a base64-encoded string into a PIL Image.)r?   rQ   rR   rS   rZ   	b64decode)re   r%   r3   r3   r4   _b64_to_image   s    rg   c                 C   s   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*   loadsr\   rC   r3   r3   r4   _bytes_to_list   s    rj   c                 C   s   t |  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.
    rh   rC   r3   r3   r4   _bytes_to_dict   s    rk   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?   rQ   rR   rS   )r@   r%   r3   r3   r4   _bytes_to_image  s    rl   )responserA   c                 C   s   t | trt| S | S rB   )rJ   rX   r*   ri   )rm   r3   r3   r4   _as_dict  s    rn   )bytes_output_as_linesdetailsrA   c              	   c   sD   | D ]:}zt ||}W n ty.   Y  q@Y n0 |dur|V  qdS )z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_outputStopIterationro   rp   byte_payloadoutputr3   r3   r4    _stream_text_generation_response  s    rw   c              	   C  sN   | 2 zB3 dH W }zt ||}W n ty6   Y  qJY n0 |dur|V  q6 dS )z/Used in `AsyncInferenceClient.text_generation`.Nrq   rt   r3   r3   r4   &_async_stream_text_generation_response%  s    rx   )ru   rp   rA   c                 C   s~   |  dsd S |  dkr"td| d}t|dd}|dd urdt	|d |dt
|}|sz|jjS |S 	Ns   data:s   data: [DONE]z[DONE] signal received.zutf-8r`   z/nerror
error_type)rK   striprs   r\   r*   ri   lstriprstriprN   _parse_text_generation_errorr!   parse_obj_as_instancetokentext)ru   rp   payloadjson_payloadrv   r3   r3   r4   rr   3  s    


rr   )bytes_linesrA   c              	   c   sB   | D ]8}zt |}W n ty,   Y  q>Y n0 |dur|V  qdS )zFUsed in `InferenceClient.chat_completion` if model is served with TGI.N%_format_chat_completion_stream_outputrs   r   itemrv   r3   r3   r4    _stream_chat_completion_responseI  s    r   c              	   C  sL   | 2 z@3 dH W }zt |}W n ty4   Y  qHY n0 |dur|V  q6 dS )z/Used in `AsyncInferenceClient.chat_completion`.Nr   r   r3   r3   r4   &_async_stream_chat_completion_responseV  s    r   )ru   rA   c                 C   sn   |  dsd S |  dkr"td| d}t|dd}|dd urdt	|d |dt
|S ry   )rK   r|   rs   r\   r*   ri   r}   r~   rN   r   r    r   )ru   r   r   r3   r3   r4   r   c  s    

r   r#   r"   )clientrm   rA   c                 C  sH   z2|j 2 z3 d H W }| V  q6 W |  I d H  n|  I d H  0 d S rB   )r@   r|   close)r   rm   ru   r3   r3   r4   _async_yield_fromx  s    r   #_UNSUPPORTED_TEXT_GENERATION_KWARGS)r)   unsupported_kwargsrA   c                 C   s   t | g | d S rB   )r   
setdefaultextend)r)   r   r3   r3   r4   '_set_unsupported_text_generation_kwargs  s    r   )r)   rA   c                 C   s   t | g S rB   )r   rN   )r)   r3   r3   r4   '_get_unsupported_text_generation_kwargs  s    r   )
http_errorrA   c                 C   sf   z.t | ddp| j }|d}|d}W n tyD   | Y n0 |dur^t||}|| | dS )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_payloadNrz   r{   )getattrrm   r*   rN   	Exceptionr   )r   r   rz   r{   	exceptionr3   r3   r4   raise_text_generation_error  s    



r   )rz   r{   rA   c                 C   sH   |dkrt | S |dkr t| S |dkr0t| S |dkr@t| S t| S )NZ
generationZincomplete_generationZ
overloadedZ
validation)r   r   r   r   r   )rz   r{   r3   r3   r4   r     s    r   )Vr7   rZ   rR   r*   loggingra   
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PathTrX   ZBinaryTr2   ZTASKS_EXPECTING_IMAGES	getLoggerr-   rL   r&   r5   r;   r=   r?   rD   r]   rd   rg   rj   rk   rl   rn   r8   rw   rx   rr   r   r   r   r   r   r1   r   r   r   r   r3   r3   r3   r4   <module>   s   @ 	

 		
$*	