o
    ,wi9                  	   @   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e4e2e1df 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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   ^/home/ubuntu/sommelier/.venv/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'.
    loadedstatecompute_type	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   r<   numpy)r@   r3   r3   r4   _import_numpy}   r?   rA   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   r<   PILr%   r$   r3   r3   r4   _import_pil_image   s   rC   contentreturnc                 C      d S Nr3   rD   r3   r3   r4   _open_as_binary      rI   c                 C   rF   rG   r3   rH   r3   r3   r4   rI      rJ   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 t| d	rytd
 t }| j|dd |d |V  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 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savezConverting PIL Image to bytesPNG)formatr   )
isinstancer0   
startswithloggerdebugr   getrD   r   existsFileNotFoundErroropenhasattrioBytesIOrL   seek)rD   fbufferr3   r3   r4   rI      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)rI   rO   bytesreadbase64	b64encodedecode)rD   r+   data_as_bytesr3   r3   r4   _b64_encode   s   
$rc   encoded_imagec                 C   s   t  }|tt| S )z/Parse a base64-encoded string into a PIL Image.)rC   rV   rX   rY   r_   	b64decode)rd   r%   r3   r3   r4   _b64_to_image   s   rf   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*   loadsra   rH   r3   r3   r4   _bytes_to_list      rj   c                 C   rg   )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   rH   r3   r3   r4   _bytes_to_dict   rk   rl   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.
    )rC   rV   rX   rY   )rD   r%   r3   r3   r4   _bytes_to_image   s   rm   responsec                 C   s   t | tr
t| S | S rG   )rO   r]   r*   ri   )rn   r3   r3   r4   _as_dict   s   ro   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rp   rq   byte_payloadoutputr3   r3   r4    _stream_text_generation_response   s   rx   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`.Nrr   ru   r3   r3   r4   &_async_stream_text_generation_response
  s   ry   rv   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)rP   striprt   ra   r*   ri   lstriprstriprS   _parse_text_generation_errorr!   parse_obj_as_instancetokentext)rv   rq   payloadjson_payloadrw   r3   r3   r4   rs     s   


rs   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_outputrt   r   itemrw   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_response;  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 rz   )rP   r}   rt   ra   r*   ri   r~   r   rS   r   r    r   )rv   r   r   r3   r3   r4   r   H  s   


r   clientr#   r"   c                 C  s4   |j 2 z3 d H W }| V  q6 |  I d H  d S rG   )rD   r}   close)r   rn   rv   r3   r3   r4   _async_yield_from]  s
   r   #_UNSUPPORTED_TEXT_GENERATION_KWARGSr)   unsupported_kwargsc                 C   s   t | g | d S rG   )r   
setdefaultextend)r)   r   r3   r3   r4   '_set_unsupported_text_generation_kwargsx  s   r   c                 C   s   t | g S rG   )r   rS   )r)   r3   r3   r4   '_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|   )getattrrn   r*   rS   	Exceptionr   )r   r   r{   r|   	exceptionr3   r3   r4   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|   r3   r3   r4   r     s   r   )Tr:   r_   rX   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!   r=   r"   r#   	PIL.Imager%   r0   UrlTPathTr]   BinaryTr2   TASKS_EXPECTING_IMAGES	getLoggerr-   rQ   r&   r5   r>   rA   rC   rI   rc   rf   rj   rl   rm   ro   r;   rx   ry   rs   r   r   r   r   r   r1   r   r   r   r   r3   r3   r3   r4   <module>   s   @ 	

 		
&$	






"