o
    g@;                  	   @   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m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rddl.m/Z/m0Z0 ddl1m2Z2 e3Z4ee3ef Z5ee6ef Z7ee7e5e4f Z8ddhZ9e:e;Z<eG dd dZ=G dd deZ>eG dd dZ?dd Z@dd ZAdd ZBede8dee7 fd d!ZCeded deed  fd"d!ZCe	dee8 deee7 ddf fd#d!ZCde8de3fd$d%ZDd&e3dd'fd(d)ZEde6defd*d+ZFde6defd,d-ZGde6dd'fd.d/ZHd0ee6ef defd1d2ZId3ee6 d4eJdeee3 ee- f fd5d6ZKd3ee6 d4eJdeee3 ee- f fd7d8ZLd9e6d4eJdeee3e-f  fd:d;ZMd<ee6 dee, fd=d>ZNd<ee6 dee, fd?d@ZOd9e6dee, fdAdBZPdCdDd0dEdee6 fdFdGZQi ZReee3 ee3 f eSdH< dIee3 dJee3 ddfdKdLZTdIee3 dee3 fdMdNZUdOedefdPdQZVdRee3 dSee3 de#fdTdUZWdS )VzEContains utilities used by both the sync and async inference clients.    N)ABCabstractmethod)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    r5   r5   X/var/www/visachat/venv/lib/python3.10/site-packages/huggingface_hub/inference/_common.pyr(   N   s   
 r(   c                   @   sz   e Zd ZdZedddedeeef dedee dee d	eeeef  d
e	fddZ
edeeef d
efddZdS )TaskProviderHelperzCProtocol defining the interface for task-specific provider helpers.N)extra_payloadinputs
parametersr.   r+   api_keyr8   returnc                C      d S Nr5   )selfr9   r:   r.   r+   r;   r8   r5   r5   r6   prepare_request[   s   
z"TaskProviderHelper.prepare_requestresponsec                 C   r=   r>   r5   )r?   rA   r5   r5   r6   get_responsef   s   zTaskProviderHelper.get_response)r/   r0   r1   __doc__r   r   r   r2   r   r(   r@   r   bytesrB   r5   r5   r5   r6   r7   X   s*    	
	
 r7   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )ModelStatusa  
    This Dataclass represents the model status in the Hugging Face Inference API.

    Args:
        loaded (`bool`):
            If the model is currently loaded into Hugging Face's InferenceAPI. 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   rC   boolr3   r2   r   r5   r5   r5   r6   rE   k   s   
 rE   c                  C   s   t  stddd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ImportErroraiohttp)rL   r5   r5   r6   _import_aiohttp      rM   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   rK   numpy)rO   r5   r5   r6   _import_numpy   rN   rP   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    rK   PILr'   r&   r5   r5   r6   _import_pil_image   s   rR   contentr<   c                 C   r=   r>   r5   rS   r5   r5   r6   _open_as_binary      rU   c                 C   r=   r>   r5   rT   r5   r5   r6   rU      rV   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)
isinstancer2   
startswithloggerdebugr   getrS   r   existsFileNotFoundErroropen)rS   fr5   r5   r6   rU      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)rU   rX   rD   readbase64	b64encodedecode)rS   r-   data_as_bytesr5   r5   r6   _b64_encode   s   
$rf   encoded_imager'   c                 C   s   t  }|tt| S )z/Parse a base64-encoded string into a PIL Image.)rR   r_   ioBytesIOrb   	b64decode)rg   r'   r5   r5   r6   _b64_to_image   s   rk   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,   loadsrd   rT   r5   r5   r6   _bytes_to_list      ro   c                 C   rl   )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.
    rm   rT   r5   r5   r6   _bytes_to_dict   rp   rq   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.
    )rR   r_   rh   ri   )rS   r'   r5   r5   r6   _bytes_to_image   s   rr   rA   c                 C   s   t | tr
t| S | S r>   )rX   rD   r,   rn   )rA   r5   r5   r6   _as_dict  s   rs   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rt   ru   byte_payloadoutputr5   r5   r6    _stream_text_generation_response  s   r|   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`.Nrv   ry   r5   r5   r6   &_async_stream_text_generation_response  s   r}   rz   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)rY   striprx   rd   r,   rn   lstriprstripr\   _parse_text_generation_errorr#   parse_obj_as_instancetokentext)rz   ru   payloadjson_payloadr{   r5   r5   r6   rw   +  s   


rw   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_outputrx   r   itemr{   r5   r5   r6    _stream_chat_completion_responseA  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   r5   r5   r6   &_async_stream_chat_completion_responseN  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 r~   )rY   r   rx   rd   r,   rn   r   r   r\   r   r"   r   )rz   r   r   r5   r5   r6   r   [  s   


r   clientr%   r$   c                 C  s4   |j 2 z3 d H W }| V  q6 |  I d H  d S r>   )rS   r   close)r   rA   rz   r5   r5   r6   _async_yield_fromp  s
   r   #_UNSUPPORTED_TEXT_GENERATION_KWARGSr+   unsupported_kwargsc                 C   s   t | g | d S r>   )r   
setdefaultextend)r+   r   r5   r5   r6   '_set_unsupported_text_generation_kwargs  s   r   c                 C   s   t | g S r>   )r   r\   )r+   r5   r5   r6   '_get_unsupported_text_generation_kwargs  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.
    response_error_payloadNr   r   )getattrrA   r,   r\   	Exceptionr   )r   r   r   r   	exceptionr5   r5   r6   raise_text_generation_error  s   


r   r   r   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 )N
generationincomplete_generation
overloaded
validation)r   r   r   r   r   )r   r   r5   r5   r6   r     s   r   )XrC   rb   rh   r,   loggingabcr   r   
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#   rL   r$   r%   	PIL.Imager'   r2   UrlTPathTrD   BinaryTr4   TASKS_EXPECTING_IMAGES	getLoggerr/   rZ   r(   r7   rE   rM   rP   rR   rU   rf   rk   ro   rq   rr   rs   rJ   r|   r}   rw   r   r   r   r   r   r3   r   r   r   r   r5   r5   r5   r6   <module>   s   @ 	
	 		
&	






"