a
    
d                     @   s2  d dl Z d dlZd dl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 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  d dl!m"Z"m#Z#m$Z$m%Z%m&Z& d d	l'm(Z(m)Z)m*Z* d d
l+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 erd dl2Z3d dl4m5Z5 e 6e7Z8G dd dZ9dS )    N)asdict)TYPE_CHECKINGAnyDictIterableListOptionalUnionoverload)	HTTPError)CaseInsensitiveDict)INFERENCE_ENDPOINT)ContentTInferenceTimeoutError_b64_encode_b64_to_image_bytes_to_dict_bytes_to_image_get_recommended_model_import_numpy_is_tgi_server_open_as_binary_set_as_non_tgi _stream_text_generation_response)TextGenerationParametersTextGenerationRequestTextGenerationResponseTextGenerationStreamResponseraise_text_generation_error)ClassificationOutputConversationalOutputImageSegmentationOutput)BadRequestErrorbuild_hf_headersget_sessionhf_raise_for_status)Literal)Imagec                   @   sB  e Zd ZdZdGee eeedf ee ee	eef  ee	eef  ddddZ
dd Zedddddd	eeee	ef  ee ee ee ed
 edddZedddddd	eeee	ef  ee ee ee ed ee dddZddddd
d	eeee	ef  ee ee ee eeeee f dddZddeee ee dddZddeee edddZdHdddeeee  eee  ee	eef  ee edddZddeee ddddZddeee ee dd d!Zddeee ee dd"d#ZdIddddddd$eee ee ee ee ee ee ee d%d&	d'd(Zddeee edd)d*Zddeee ee ee d+d,d-Z dddeee	eef  ee ed.d/d0Z!edddd
d1ddd
dddddddd
d2eed
 ed
 ee eeee ee eee eee  ee ee ee ee ee eed3d4d5Z"edddd
d1ddd
dddddddd
d2eed ed
 ee eeee ee eee eee  ee ee ee ee ee ee#d3d6d5Z"edddd
d1ddd
dddddddd
d2eed
 ed ee eeee ee eee eee  ee ee ee ee ee eee d3d7d5Z"edddd
d1ddd
dddddddd
d2eed ed ee eeee ee eee eee  ee ee ee ee ee eee$ d3d8d5Z"d
d
dd
d1ddd
dddddddd
d
d9eeeee eeee ee eee eee  ee ee ee ee ee eeeee#ee ee$ f d:d;d5Z"ddddddd$eee ee ee ee ee ee d%d<d=d>Z%ddeee edd?d@Z&ddeee ee ee dAdBdCZ'dJee ee edDdEdFZ(dS )KInferenceClientaZ  
    Initialize a new Inference Client.

    [`InferenceClient`] aims to provide a unified experience to perform inference. The client can be used
    seamlessly with either the (free) Inference API or self-hosted Inference Endpoints.

    Args:
        model (`str`, `optional`):
            The model to run inference with. Can be a model id hosted on the Hugging Face Hub, e.g. `bigcode/starcoder`
            or a URL to a deployed Inference Endpoint. Defaults to None, in which case a recommended model is
            automatically selected for the task.
        token (`str`, *optional*):
            Hugging Face token. Will default to the locally saved token. Pass `token=False` if you don't want to send
            your token to the server.
        timeout (`float`, `optional`):
            The maximum number of seconds to wait for a response from the server. Loading a new model in Inference
            API can take up to several minutes. Defaults to None, meaning it will loop until the server is available.
        headers (`Dict[str, str]`, `optional`):
            Additional headers to send to the server. By default only the authorization and user-agent headers are sent.
            Values in this dictionary will override the default values.
        cookies (`Dict[str, str]`, `optional`):
            Additional cookies to send to the server.
    N)modeltokentimeoutheaderscookiesreturnc                 C   s:   || _ tt|d| _|d ur*| j| || _|| _d S )N)r*   )r)   r   r#   r,   updater-   r+   )selfr)   r*   r+   r,   r-    r1   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/huggingface_hub/inference/_client.py__init__v   s    zInferenceClient.__init__c                 C   s    d| j r| j nd d| j dS )Nz<InferenceClient(model=' z', timeout=z)>)r)   r+   )r0   r1   r1   r2   __repr__   s    zInferenceClient.__repr__.)jsondatar)   taskstreamF)r6   r7   r)   r8   r9   r.   c                C   s   d S Nr1   r0   r6   r7   r)   r8   r9   r1   r1   r2   post   s    
zInferenceClient.postTc                C   s   d S r:   r1   r;   r1   r1   r2   r<      s    
c                C   s  |  ||}|dur&|dur&td t }| j}t|j}	z$t j|||	| j| j	| j|d}
W n4 t
y } ztd| |W Y d}~n
d}~0 0 W d   n1 s0    Y  zt|
 |r|
 n|
jW S  ty } z|jjdkrp|dur(t | |kr(td| d| j d|td	|  td
 |durdt| jt |  d
}W Y d}~q4 W Y d}~q4d}~0 0 q4dS )a	  
        Make a POST request to the inference server.

        Args:
            json (`Union[str, Dict, List]`, *optional*):
                The JSON data to send in the request body. Defaults to None.
            data (`Union[str, Path, bytes, BinaryIO]`, *optional*):
                The content to send in the request body. It can be raw bytes, a pointer to an opened file, a local file
                path, or a URL to an online resource (image, audio file,...). If both `json` and `data` are passed,
                `data` will take precedence. At least `json` or `data` must be provided. Defaults to None.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. Will override the model defined at the instance level. Defaults to None.
            task (`str`, *optional*):
                The task to perform on the inference. Used only to default to a recommended model if `model` is not
                provided. At least `model` or `task` must be provided. Defaults to None.
            stream (`bool`, *optional*):
                Whether to iterate over streaming APIs.

        Returns:
            bytes: The raw bytes returned by the server.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.
        Nz.Ignoring `json` as `data` is passed as binary.)r6   r7   r,   r-   r+   r9   zInference call timed out: i  z Model not loaded on the server: z/. Please retry with a higher timeout (current: z).z.Waiting for model to be loaded on the server:    )_resolve_urlwarningswarntimer+   r   r$   r<   r,   r-   TimeoutErrorr   r%   
iter_linescontentr   responsestatus_codeloggerinfosleepmax)r0   r6   r7   r)   r8   r9   urlt0r+   Zdata_as_binaryrE   errorr1   r1   r2   r<      sJ    %


	D

)r)   )audior)   r.   c                C   s   | j ||dd}t|S )a  
        Perform audio classification on the provided audio content.

        Args:
            audio (Union[str, Path, bytes, BinaryIO]):
                The audio content to classify. It can be raw audio bytes, a local audio file, or a URL pointing to an
                audio file.
            model (`str`, *optional*):
                The model to use for audio classification. Can be a model ID hosted on the Hugging Face Hub
                or a URL to a deployed Inference Endpoint. If not provided, the default recommended model for
                audio classification will be used.

        Returns:
            `List[Dict]`: The classification output containing the predicted label and its confidence.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.audio_classification("audio.flac")
        [{'score': 0.4976358711719513, 'label': 'hap'}, {'score': 0.3677836060523987, 'label': 'neu'},...]
        ```
        zaudio-classificationr7   r)   r8   r<   r   r0   rN   r)   rE   r1   r1   r2   audio_classification   s    #z$InferenceClient.audio_classificationc                C   s   | j ||dd}t|d S )a2  
        Perform automatic speech recognition (ASR or audio-to-text) on the given audio content.

        Args:
            audio (Union[str, Path, bytes, BinaryIO]):
                The content to transcribe. It can be raw audio bytes, local audio file, or a URL to an audio file.
            model (`str`, *optional*):
                The model to use for ASR. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. If not provided, the default recommended model for ASR will be used.

        Returns:
            str: The transcribed text.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.automatic_speech_recognition("hello_world.flac")
        "hello world"
        ```
        zautomatic-speech-recognitionrO   textrP   rQ   r1   r1   r2   automatic_speech_recognition  s    !z,InferenceClient.automatic_speech_recognition)
parametersr)   )rS   generated_responsespast_user_inputsrU   r)   r.   c                C   s\   dd|ii}|dur ||d d< |dur4||d d< |durD||d< | j ||dd}t|S )	a  
        Generate conversational responses based on the given input text (i.e. chat with the API).

        Args:
            text (`str`):
                The last input from the user in the conversation.
            generated_responses (`List[str]`, *optional*):
                A list of strings corresponding to the earlier replies from the model. Defaults to None.
            past_user_inputs (`List[str]`, *optional*):
                A list of strings corresponding to the earlier replies from the user. Should be the same length as
                `generated_responses`. Defaults to None.
            parameters (`Dict[str, Any]`, *optional*):
                Additional parameters for the conversational task. Defaults to None. For more details about the available
                parameters, please refer to [this page](https://huggingface.co/docs/api-inference/detailed_parameters#conversational-task)
            model (`str`, *optional*):
                The model to use for the conversational task. Can be a model ID hosted on the Hugging Face Hub or a URL to
                a deployed Inference Endpoint. If not provided, the default recommended conversational model will be used.
                Defaults to None.

        Returns:
            `Dict`: The generated conversational output.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> output = client.conversational("Hi, who are you?")
        >>> output
        {'generated_text': 'I am the one who knocks.', 'conversation': {'generated_responses': ['I am the one who knocks.'], 'past_user_inputs': ['Hi, who are you?']}, 'warnings': ['Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.']}
        >>> client.conversational(
        ...     "Wow, that's scary!",
        ...     generated_responses=output["conversation"]["generated_responses"],
        ...     past_user_inputs=output["conversation"]["past_user_inputs"],
        ... )
        ```
        inputsrS   NrV   rW   rU   conversationalr6   r)   r8   rP   )r0   rS   rV   rW   rU   r)   payloadrE   r1   r1   r2   rY   9  s    3zInferenceClient.conversationalz
np.ndarray)rS   r)   r.   c                C   s0   | j d|i|dd}t }|jt|d ddS )a   
        Generate embeddings for a given text.

        Args:
            text (`str`):
                The text to embed.
            model (`str`, *optional*):
                The model to use for the conversational task. Can be a model ID hosted on the Hugging Face Hub or a URL to
                a deployed Inference Endpoint. If not provided, the default recommended conversational model will be used.
                Defaults to None.

        Returns:
            `np.ndarray`: The embedding representing the input text as a float32 numpy array.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.feature_extraction("Hi, who are you?")
        array([[ 2.424802  ,  2.93384   ,  1.1750331 , ...,  1.240499, -0.13776633, -0.7889173 ],
        [-0.42943227, -0.6364878 , -1.693462  , ...,  0.41978157, -2.4336355 ,  0.6162071 ],
        ...,
        [ 0.28552425, -0.928395  , -1.2077185 , ...,  0.76810825, -2.1069427 ,  0.6236161 ]], dtype=float32)
        ```
        rX   feature-extractionrZ   r   float32)Zdtype)r<   r   arrayr   )r0   rS   r)   rE   npr1   r1   r2   feature_extractionv  s     z"InferenceClient.feature_extraction)imager)   r.   c                C   s   | j ||dd}t|S )a  
        Perform image classification on the given image using the specified model.

        Args:
            image (`Union[str, Path, bytes, BinaryIO]`):
                The image to classify. It can be raw bytes, an image file, or a URL to an online image.
            model (`str`, *optional*):
                The model to use for image classification. Can be a model ID hosted on the Hugging Face Hub or a URL to a
                deployed Inference Endpoint. If not provided, the default recommended model for image classification will be used.

        Returns:
            `List[Dict]`: a list of dictionaries containing the predicted label and associated probability.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.image_classification("https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Cute_dog.jpg/320px-Cute_dog.jpg")
        [{'score': 0.9779096841812134, 'label': 'Blenheim spaniel'}, ...]
        ```
        zimage-classificationrO   rP   r0   ra   r)   rE   r1   r1   r2   image_classification  s    !z$InferenceClient.image_classificationc                C   sf   | j ||dd}t|}t|tsHtdt| dt|dd  d|D ]}t|d |d< qL|S )	ap  
        Perform image segmentation on the given image using the specified model.

        <Tip warning={true}>

        You must have `PIL` installed if you want to work with images (`pip install Pillow`).

        </Tip>

        Args:
            image (`Union[str, Path, bytes, BinaryIO]`):
                The image to segment. It can be raw bytes, an image file, or a URL to an online image.
            model (`str`, *optional*):
                The model to use for image segmentation. Can be a model ID hosted on the Hugging Face Hub or a URL to a
                deployed Inference Endpoint. If not provided, the default recommended model for image segmentation will be used.

        Returns:
            `List[Dict]`: A list of dictionaries containing the segmented masks and associated attributes.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.image_segmentation("cat.jpg"):
        [{'score': 0.989008, 'label': 'LABEL_184', 'mask': <PIL.PngImagePlugin.PngImageFile image mode=L size=400x300 at 0x7FDD2B129CC0>}, ...]
        ```
        zimage-segmentationrO   z"Server output must be a list. Got z: N   z...mask)r<   r   
isinstancelist
ValueErrortypestrr   )r0   ra   r)   rE   outputitemr1   r1   r2   image_segmentation  s    )
&z"InferenceClient.image_segmentation)negative_promptheightwidthnum_inference_stepsguidance_scaler)   r'   )	ra   promptrn   ro   rp   rq   rr   r)   r.   c                K   s   ||||||d|	}
t dd |
 D r6|}d}n2d}dt|i}|
 D ]\}}|durN|||< qN| j|||dd}t|S )a;  
        Perform image-to-image translation using a specified model.

        <Tip warning={true}>

        You must have `PIL` installed if you want to work with images (`pip install Pillow`).

        </Tip>

        Args:
            image (`Union[str, Path, bytes, BinaryIO]`):
                The input image for translation. It can be raw bytes, an image file, or a URL to an online image.
            prompt (`str`, *optional*):
                The text prompt to guide the image generation.
            negative_prompt (`str`, *optional*):
                A negative prompt to guide the translation process.
            height (`int`, *optional*):
                The height in pixels of the generated image.
            width (`int`, *optional*):
                The width in pixels of the generated image.
            num_inference_steps (`int`, *optional*):
                The number of denoising steps. More denoising steps usually lead to a higher quality image at the
                expense of slower inference.
            guidance_scale (`float`, *optional*):
                Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
                usually at the expense of lower image quality.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `Image`: The translated image.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> image = client.image_to_image("cat.jpg", prompt="turn the cat into a tiger")
        >>> image.save("tiger.jpg")
        ```
        )rs   rn   ro   rp   rq   rr   c                 s   s   | ]}|d u V  qd S r:   r1   ).0Z	parameterr1   r1   r2   	<genexpr>6      z1InferenceClient.image_to_image.<locals>.<genexpr>NrX   zimage-to-image)r6   r7   r)   r8   )allvaluesr   itemsr<   r   )r0   ra   rs   rn   ro   rp   rq   rr   r)   kwargsrU   r7   r[   keyvaluerE   r1   r1   r2   image_to_image  s&    =	
zInferenceClient.image_to_imagec                C   s    | j ||dd}t|d d S )a  
        Takes an input image and return text.

        Models can have very different outputs depending on your use case (image captioning, optical character recognition
        (OCR), Pix2Struct, etc). Please have a look to the model card to learn more about a model's specificities.

        Args:
            image (`Union[str, Path, bytes, BinaryIO]`):
                The input image to caption. It can be raw bytes, an image file, or a URL to an online image..
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `str`: The generated text.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.image_to_text("cat.jpg")
        'a cat standing in a grassy field '
        >>> client.image_to_text("https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Cute_dog.jpg/320px-Cute_dog.jpg")
        'a dog laying on the grass next to a flower pot '
        ```
        zimage-to-textrO   r   generated_textrP   rb   r1   r1   r2   image_to_textE  s    !zInferenceClient.image_to_text)sentenceother_sentencesr)   r.   c                C   s"   | j d||di|dd}t|S )a  
        Compute the semantic similarity between a sentence and a list of other sentences by comparing their embeddings.

        Args:
            sentence (`str`):
                The main sentence to compare to others.
            other_sentences (`List[str]`):
                The list of sentences to compare to.
            model (`str`, *optional*):
                The model to use for the conversational task. Can be a model ID hosted on the Hugging Face Hub or a URL to
                a deployed Inference Endpoint. If not provided, the default recommended conversational model will be used.
                Defaults to None.

        Returns:
            `List[float]`: The embedding representing the input text.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.sentence_similarity(
        ...     "Machine learning is so easy.",
        ...     other_sentences=[
        ...         "Deep learning is so straightforward.",
        ...         "This is so difficult, like rocket science.",
        ...         "I can't believe how much I struggled with this.",
        ...     ],
        ... )
        [0.7785726189613342, 0.45876261591911316, 0.2906220555305481]
        ```
        rX   )Zsource_sentence	sentencessentence-similarityrZ   rP   )r0   r   r   r)   rE   r1   r1   r2   sentence_similarityi  s    (z#InferenceClient.sentence_similarity)rS   rU   r)   r.   c                C   s8   d|i}|dur||d< | j ||dd}t|d d S )a  
        Generate a summary of a given text using a specified model.

        Args:
            text (`str`):
                The input text to summarize.
            parameters (`Dict[str, Any]`, *optional*):
                Additional parameters for summarization. Check out this [page](https://huggingface.co/docs/api-inference/detailed_parameters#summarization-task)
                for more details.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `str`: The generated summary text.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()
        >>> client.summarization("The Eiffel tower...")
        'The Eiffel tower is one of the most famous landmarks in the world....'
        ```
        rX   NrU   summarizationrZ   r   Zsummary_textrP   )r0   rS   rU   r)   r[   rE   r1   r1   r2   r     s
    %zInferenceClient.summarization   )detailsr9   r)   	do_samplemax_new_tokensbest_ofrepetition_penaltyreturn_full_textseedstop_sequencestemperaturetop_ktop_ptruncate	typical_p	watermark)rs   r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r.   c                C   s   d S r:   r1   r0   rs   r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r1   r2   text_generation  s    zInferenceClient.text_generationc                C   s   d S r:   r1   r   r1   r1   r2   r     s    c                C   s   d S r:   r1   r   r1   r1   r2   r     s    c                C   s   d S r:   r1   r   r1   r1   r2   r     s    )r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   decoder_input_details)rs   r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r.   c                C   s  |r|st d d}t||||||	|
|dur2|ng |||||||d}t|||d}t|}t|sg }dD ](}|d | dur|| |d |= qnt|dkrt d	| d
t |rt dt d}|rt	dz| j
||d|d}W n tyx } zpt|tr\dt|v r\t| | j|||||||||	|
||||||||dW  Y d}~S t| W Y d}~n
d}~0 0 |rt||S t|d }|rtf i |S |d S )a"  
        Given a prompt, generate the following text.

        It is recommended to have Pydantic installed in order to get inputs validated. This is preferable as it allow
        early failures.

        API endpoint is supposed to run with the `text-generation-inference` backend (TGI). This backend is the
        go-to solution to run large language models at scale. However, for some smaller models (e.g. "gpt2") the
        default `transformers` + `api-inference` solution is still in use. Both approaches have very similar APIs, but
        not exactly the same. This method is compatible with both approaches but some parameters are only available for
        `text-generation-inference`. If some parameters are ignored, a warning message is triggered but the process
        continues correctly.

        To learn more about the TGI project, please refer to https://github.com/huggingface/text-generation-inference.

        Args:
            prompt (`str`):
                Input text.
            details (`bool`, *optional*):
                By default, text_generation returns a string. Pass `details=True` if you want a detailed output (tokens,
                probabilities, seed, finish reason, etc.). Only available for models running on with the
                `text-generation-inference` backend.
            stream (`bool`, *optional*):
                By default, text_generation returns the full generated text. Pass `stream=True` if you want a stream of
                tokens to be returned. Only available for models running on with the `text-generation-inference`
                backend.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.
            do_sample (`bool`):
                Activate logits sampling
            max_new_tokens (`int`):
                Maximum number of generated tokens
            best_of (`int`):
                Generate best_of sequences and return the one if the highest token logprobs
            repetition_penalty (`float`):
                The parameter for repetition penalty. 1.0 means no penalty. See [this
                paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.
            return_full_text (`bool`):
                Whether to prepend the prompt to the generated text
            seed (`int`):
                Random sampling seed
            stop_sequences (`List[str]`):
                Stop generating tokens if a member of `stop_sequences` is generated
            temperature (`float`):
                The value used to module the logits distribution.
            top_k (`int`):
                The number of highest probability vocabulary tokens to keep for top-k-filtering.
            top_p (`float`):
                If set to < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or
                higher are kept for generation.
            truncate (`int`):
                Truncate inputs tokens to the given size
            typical_p (`float`):
                Typical Decoding mass
                See [Typical Decoding for Natural Language Generation](https://arxiv.org/abs/2202.00666) for more information
            watermark (`bool`):
                Watermarking with [A Watermark for Large Language Models](https://arxiv.org/abs/2301.10226)
            decoder_input_details (`bool`):
                Return the decoder input token logprobs and ids. You must set `details=True` as well for it to be taken
                into account. Defaults to `False`.

        Returns:
            `Union[str, TextGenerationResponse, Iterable[str], Iterable[TextGenerationStreamResponse]]`:
            Generated text returned from the server:
            - if `stream=False` and `details=False`, the generated text is returned as a `str` (default)
            - if `stream=True` and `details=False`, the generated text is returned token by token as a `Iterable[str]`
            - if `stream=False` and `details=True`, the generated text is returned with more details as a [`~huggingface_hub.inference._text_generation.TextGenerationResponse`]
            - if `details=True` and `stream=True`, the generated text is returned token by token as a iterable of [`~huggingface_hub.inference._text_generation.TextGenerationStreamResponse`]

        Raises:
            `ValidationError`:
                If input values are not valid. No HTTP call is made to the server.
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()

        # Case 1: generate text
        >>> client.text_generation("The huggingface_hub library is ", max_new_tokens=12)
        '100% open source and built to be easy to use.'

        # Case 2: iterate over the generated tokens. Useful for large generation.
        >>> for token in client.text_generation("The huggingface_hub library is ", max_new_tokens=12, stream=True):
        ...     print(token)
        100
        %
        open
        source
        and
        built
        to
        be
        easy
        to
        use
        .

        # Case 3: get more details about the generation process.
        >>> client.text_generation("The huggingface_hub library is ", max_new_tokens=12, details=True)
        TextGenerationResponse(
            generated_text='100% open source and built to be easy to use.',
            details=Details(
                finish_reason=<FinishReason.Length: 'length'>,
                generated_tokens=12,
                seed=None,
                prefill=[
                    InputToken(id=487, text='The', logprob=None),
                    InputToken(id=53789, text=' hugging', logprob=-13.171875),
                    (...)
                    InputToken(id=204, text=' ', logprob=-7.0390625)
                ],
                tokens=[
                    Token(id=1425, text='100', logprob=-1.0175781, special=False),
                    Token(id=16, text='%', logprob=-0.0463562, special=False),
                    (...)
                    Token(id=25, text='.', logprob=-0.5703125, special=False)
                ],
                best_of_sequences=None
            )
        )

        # Case 4: iterate over the generated tokens with more details.
        # Last object is more complete, containing the full generated text and the finish reason.
        >>> for details in client.text_generation("The huggingface_hub library is ", max_new_tokens=12, details=True, stream=True):
        ...     print(details)
        ...
        TextGenerationStreamResponse(token=Token(id=1425, text='100', logprob=-1.0175781, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=16, text='%', logprob=-0.0463562, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=1314, text=' open', logprob=-1.3359375, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=3178, text=' source', logprob=-0.28100586, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=273, text=' and', logprob=-0.5961914, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=3426, text=' built', logprob=-1.9423828, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=271, text=' to', logprob=-1.4121094, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=314, text=' be', logprob=-1.5224609, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=1833, text=' easy', logprob=-2.1132812, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=271, text=' to', logprob=-0.08520508, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(id=745, text=' use', logprob=-0.39453125, special=False), generated_text=None, details=None)
        TextGenerationStreamResponse(token=Token(
            id=25,
            text='.',
            logprob=-0.5703125,
            special=False),
            generated_text='100% open source and built to be easy to use.',
            details=StreamDetails(finish_reason=<FinishReason.Length: 'length'>, generated_tokens=12, seed=None)
        )
        ```
        z`decoder_input_details=True` has been passed to the server but `details=False` is set meaning that the output from the server will be truncated.FN)r   r   r   r   r   r   r   stopr   r   r   r   r   r   r   )rX   r9   rU   )r   r   r   r   rU   r   zRAPI endpoint/model for text-generation is not served via TGI. Ignoring parameters .zAPI endpoint/model for text-generation is not served via TGI. Parameter `details=True` will be ignored meaning only the generated text will be returned.zAPI endpoint/model for text-generation is not served via TGI. Cannot return output as a stream. Please pass `stream=False` as input.ztext-generation)r6   r)   r8   r9   z6The following `model_kwargs` are not used by the model)rs   r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   )r?   r@   r   r   r   r   appendlenUserWarningrh   r<   r   rf   r"   rj   r   r   r   r   r   r   )r0   rs   r   r9   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   requestr[   Zignored_parametersr{   Zbytes_outputer7   r1   r1   r2   r   #  s     3

)rs   rn   ro   rp   rq   rr   r)   r.   c                K   sT   ||||||d|}	i }
|	  D ]\}}|dur"||
|< q"| j|
|dd}t|S )a  
        Generate an image based on a given text using a specified model.

        <Tip warning={true}>

        You must have `PIL` installed if you want to work with images (`pip install Pillow`).

        </Tip>

        Args:
            prompt (`str`):
                The prompt to generate an image from.
            negative_prompt (`str`, *optional*):
                An optional negative prompt for the image generation.
            height (`float`, *optional*):
                The height in pixels of the image to generate.
            width (`float`, *optional*):
                The width in pixels of the image to generate.
            num_inference_steps (`int`, *optional*):
                The number of denoising steps. More denoising steps usually lead to a higher quality image at the
                expense of slower inference.
            guidance_scale (`float`, *optional*):
                Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
                usually at the expense of lower image quality.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `Image`: The generated image.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()

        >>> image = client.text_to_image("An astronaut riding a horse on the moon.")
        >>> image.save("astronaut.png")

        >>> image = client.text_to_image(
        ...     "An astronaut riding a horse on the moon.",
        ...     negative_prompt="low resolution, blurry",
        ...     model="stabilityai/stable-diffusion-2-1",
        ... )
        >>> image.save("better_astronaut.png")
        ```
        )rX   rn   ro   rp   rq   rr   Nztext-to-imagerZ   )ry   r<   r   )r0   rs   rn   ro   rp   rq   rr   r)   rz   rU   r[   r{   r|   rE   r1   r1   r2   text_to_image2  s    B	
zInferenceClient.text_to_imagec                C   s   | j d|i|ddS )a   
        Synthesize an audio of a voice pronouncing a given text.

        Args:
            text (`str`):
                The text to synthesize.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `bytes`: The generated audio.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from pathlib import Path
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()

        >>> audio = client.text_to_speech("Hello world")
        >>> Path("hello_world.flac").write_bytes(audio)
        ```
        rX   ztext-to-speechrZ   )r<   )r0   rS   r)   r1   r1   r2   text_to_speech  s    zInferenceClient.text_to_speech)ra   labelsr)   r.   c                C   s@   t |dk rtd| jt|dd|id|dd}t|S )a  
        Provide input image and text labels to predict text labels for the image.

        Args:
            image (`Union[str, Path, bytes, BinaryIO]`):
                The input image to caption. It can be raw bytes, an image file, or a URL to an online image.
            labels (`List[str]`):
                List of string possible labels. The `len(labels)` must be greater than 1.
            model (`str`, *optional*):
                The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed
                Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.

        Returns:
            `List[Dict]`: List of classification outputs containing the predicted labels and their confidence.

        Raises:
            [`InferenceTimeoutError`]:
                If the model is unavailable or the request times out.
            `HTTPError`:
                If the request fails with an HTTP error status code other than HTTP 503.

        Example:
        ```py
        >>> from huggingface_hub import InferenceClient
        >>> client = InferenceClient()

        >>> client.zero_shot_image_classification(
        ...     "https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Cute_dog.jpg/320px-Cute_dog.jpg",
        ...     labels=["dog", "cat", "horse"],
        ... )
        [{"label": "dog", "score": 0.956}, ...]
        ```
           zQYou must specify at least 2 classes to compare. Please specify more than 1 class.Zcandidate_labels,)ra   rU   zzero-shot-image-classificationrZ   )r   rh   r<   r   joinr   )r0   ra   r   r)   rE   r1   r1   r2   zero_shot_image_classification  s    &z.InferenceClient.zero_shot_image_classification)r)   r8   r.   c                 C   st   |p| j }|d ur*|ds&|dr*|S |d u rJ|d u rBtdt|}|dv rft d| d| S t d| S )Nzhttp://zhttps://zYou must specify at least a model (repo_id or URL) or a task, either when instantiating `InferenceClient` or when making a request.)r\   r   z
/pipeline//z/models/)r)   
startswithrh   r   r   )r0   r)   r8   r1   r1   r2   r>     s    
zInferenceClient._resolve_url)NNNNN)NN)N)NN))__name__
__module____qualname____doc__r   rj   r	   boolfloatr   r3   r5   r
   r   r   r&   bytesr<   r   r   rR   rT   r   r    rY   r`   rc   r!   rm   intr}   r   r   r   r   r   r   r   r   r   r>   r1   r1   r1   r2   r(   ]   s       S*'  

=((6 T%3+




  Q!0r(   ):loggingrA   r?   Zdataclassesr   typingr   r   r   r   r   r   r	   r
   requestsr   Zrequests.structuresr   Zhuggingface_hub.constantsr   Z!huggingface_hub.inference._commonr   r   r   r   r   r   r   r   r   r   r   r   Z*huggingface_hub.inference._text_generationr   r   r   r   r   Z huggingface_hub.inference._typesr   r    r!   Zhuggingface_hub.utilsr"   r#   r$   r%   Zhuggingface_hub.utils._typingr&   numpyr_   ZPILr'   	getLoggerr   rG   r(   r1   r1   r1   r2   <module>$   s"   (8
