o
    d~#gA                     @   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$ d	dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. erddl/m0Z0m1Z1 ddl2m3Z3 e4Z5ee4e
f Z6ee7ef Z8ee8e6e5f Z9ddhZ:e;e<Z=eG dd dZ>dd Z?dd Z@dd ZAdaBeee4ee4 f  eCd< dee4ee4 f fddZDdee dee fdd ZEed!e9dee8 fd"d#ZFed!ed deed  fd$d#ZFed!ee9 deee8 ddf fd%d#ZFd!e9de4fd&d'ZGd(e4dd)fd*d+ZHd!e7defd,d-ZId!e7defd.d/ZJd!e7dd)fd0d1ZK	2d\d3ee4ee4ef e9f d4eee4ef  d5eLdee4ef fd6d7ZMd8ee7 d9eLdeee4 ee. f fd:d;ZNd8ee7 d9eLdeee4 ee. f fd<d=ZOd>e7d9eLdeee4e.f  fd?d@ZPdAee7 dee- fdBdCZQdAee7 dee- fdDdEZRd>e7dee- fdFdGZSdHdIdJdKdee7 fdLdMZTi ZUeee4 ee4 f eCdN< dOee4 dPee4 ddfdQdRZVdOee4 dee4 fdSdTZWdUedefdVdWZXdXee4 dYee4 de fdZd[ZYdS )]zEContains 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   )ENDPOINT)build_hf_headersget_sessionhf_raise_for_statusis_aiohttp_availableis_numpy_availableis_pillow_available   )ChatCompletionStreamOutputTextGenerationStreamOutput)ClientResponseClientSessionImageztext-to-imagezimage-to-imagec                   @   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 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)__name__
__module____qualname____doc__bool__annotations__strr
    r5   r5   /var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/site-packages/huggingface_hub/inference/_common.pyr)   Q   s   
 r)   c                  C   s   t  stddd l} | S )NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r   )r   ImportErroraiohttp)r8   r5   r5   r6   _import_aiohttpr      r9   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    r7   numpy)r;   r5   r5   r6   _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!   r7   PILr(   r'   r5   r5   r6   _import_pil_image   s   r>   _RECOMMENDED_MODELSreturnc                  C   sB   t d u rt jt dt d} t|  dd |   D a t S )Nz
/api/tasks)headersc                 S   s   i | ]\}}|t |d  qS )widgetModels)_first_or_none).0taskdetailsr5   r5   r6   
<dictcomp>   s    z-_fetch_recommended_models.<locals>.<dictcomp>)r?   r   getr   r   r   jsonitems)responser5   r5   r6   _fetch_recommended_models   s   
rL   rJ   c                 C   s$   z| d pd W S  t y   Y d S w )Nr   )
IndexError)rJ   r5   r5   r6   rC      s
   rC   contentc                 C      d S Nr5   rN   r5   r5   r6   _open_as_binary      rR   c                 C   rO   rP   r5   rQ   r5   r5   r6   rR      rS   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)
isinstancer4   
startswithloggerdebugr   rH   rN   r   existsFileNotFoundErroropen)rN   fr5   r5   r6   rR      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 byes, an opened file, a path or a URL.N)rR   rU   bytesreadbase64	b64encodedecode)rN   datadata_as_bytesr5   r5   r6   _b64_encode   s   
$rd   encoded_imager(   c                 C   s   t  }|tt| S )z/Parse a base64-encoded string into a PIL Image.)r>   r[   ioBytesIOr_   	b64decode)re   r(   r5   r5   r6   _b64_to_image   s   ri   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.
    rI   loadsra   rQ   r5   r5   r6   _bytes_to_list      rm   c                 C   rj   )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.
    rk   rQ   r5   r5   r6   _bytes_to_dict   rn   ro   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>   r[   rf   rg   )rN   r(   r5   r5   r6   _bytes_to_image   s   rp   Finputs
parametersexpect_binaryc                 C   s   |du ri }dd |  D }t|dk}t| ttf}|r,|s,t| ts,td|  |r4|s4d| iS |s?|r?td|  i }|rJt| |d< n| |d< |rT||d	< d
|iS )a$  
    Used in `InferenceClient` and `AsyncInferenceClient` to prepare the payload for an API request, handling various input types and parameters.
    `expect_binary` is set to `True` when the inputs are a binary object or a local path or URL. This is the case for image and audio inputs.
    Nc                 S   s   i | ]\}}|d ur||qS rP   r5   )rD   kvr5   r5   r6   rG     s    z$_prepare_payload.<locals>.<dictcomp>r   z5Expected binary inputs or a local path or a URL. Got rb   zUnexpected binary inputs. Got rq   rr   rI   )rJ   lenrU   r]   r   r4   
ValueErrorrd   )rq   rr   rs   has_parameters	is_binaryrI   r5   r5   r6   _prepare_payload  s$   	rz   bytes_output_as_linesrF   c              	   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r{   rF   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`.Nr|   r   r5   r5   r6   &_async_stream_text_generation_response=  s   r   r   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)rV   stripr~   ra   rI   rl   lstriprstriprH   _parse_text_generation_errorr$   parse_obj_as_instancetokentext)r   rF   payloadjson_payloadr   r5   r5   r6   r}   K  s   


r}   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_outputr~   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   )rV   r   r~   ra   rI   rl   r   r   rH   r   r#   r   )r   r   r   r5   r5   r6   r   {  s   


r   clientr&   rK   r%   c                 C  s4   |j 2 z3 d H W }| V  q6 |  I d H  d S rP   )rN   r   close)r   rK   r   r5   r5   r6   _async_yield_from  s
   r   #_UNSUPPORTED_TEXT_GENERATION_KWARGSmodelunsupported_kwargsc                 C   s   t | g | d S rP   )r   
setdefaultextend)r   r   r5   r5   r6   '_set_unsupported_text_generation_kwargs  s   r   c                 C   s   t | g S rP   )r   rH   )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   )getattrrK   rI   rH   	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   )F)Zr1   r_   rf   rI   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   	constantsr   utilsr   r   r   r   r    r!   _generated.typesr#   r$   r8   r%   r&   	PIL.Imager(   r4   UrlTPathTr]   BinaryTContentTTASKS_EXPECTING_IMAGES	getLoggerr.   rW   r)   r9   r<   r>   r?   r3   rL   rC   rR   rd   ri   rm   ro   rp   r2   rz   r   r   r}   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   <module>   s   @ 	 
 		

&

)





"