a
    d!                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddl	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 ddlmZmZmZ ddlZddlZddlZdd	lmZmZmZ dd
lm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 e*d ej23dpde4ee	5 d Z6e)dddddG dd dZ7G dd dZ8e)dddG dd  d eZ9dS )!z,The main Client class for the Python client.    )annotationsN)Future)datetime)Path)Lock)AnyCallableLiteral)CommitOperationAddSpaceHardware
SpaceStage)RepositoryNotFoundErrorbuild_hf_headerssend_telemetry)version)serializingutils)documentset_documentation_group)Serializable)Communicator	JobStatusStatusStatusUpdatez	py-clientZGRADIO_TEMP_DIRgradiopredictsubmitview_api	duplicatedeploy_discordc                   @  s:  e Zd ZdZdddedfddddd	dd
ddZedDdddddddddd	ddZdd ZdddddddddZ	ddddddddddd Z
dEd!dd"d#d$d%d&Zd'd(d)d*Zd+d,dd-d.d/Zd0d1 Zd2d3 Zd'd(d4d5Zddddd6d7Zd8d9 Zdd(d:d;Zd<d(d=d>ZdFdd@ddddAdBdCZdS )GClienta  
    The main Client class for the Python client. This class is used to connect to a remote Gradio app and call its API endpoints.

    Example:
        from gradio_client import Client

        client = Client("abidlabs/whisper-large-v2")  # connecting to a Hugging Face Space
        client.predict("test.mp4", api_name="/predict")
        >> What a nice recording! # returns the result of the remote API call

        client = Client("https://bec81a83-5b5c-471e.gradio.live")  # connecting to a temporary Gradio share URL
        job = client.submit("hello", api_name="/predict")  # runs the prediction in a background thread
        job.result()
        >> 49 # returns the result of the remote API call (blocking call)
    N(   Tstr
str | Noneintboolzstr | Path | None)srchf_tokenmax_workers	serialize
output_dirverbosec           	        s  | _ | _| _t|dtjd _d _| _|	dsD|	dr\|
drR|n|d }n( |}|du r~td| d| _| _  }|tjkrĈ j rtd	   tjkrtd
 q|tjv rtd| d j rtd j d tj jtj _tj jdddtj _tj jtj _tj jtj _  !  _"t#t$%  _& fddt' j"d D  _(t)j*j+|d _,t-j. j/d0  dS )a-  
        Parameters:
            src: Either the name of the Hugging Face Space to load, (e.g. "abidlabs/whisper-large-v2") or the full URL (including "http" or "https") of the hosted Gradio app to load (e.g. "http://mydomain.com/app" or "https://bec81a83-5b5c-471e.gradio.live/").
            hf_token: The Hugging Face token to use to access private Spaces. Automatically fetched if you are logged in via the Hugging Face Hub CLI. Obtain from: https://huggingface.co/settings/token
            max_workers: The maximum number of thread workers that can be used to make requests to the remote Gradio app simultaneously.
            serialize: Whether the client should serialize the inputs and deserialize the outputs of the remote API. If set to False, the client will pass the inputs and outputs as-is, without serializing/deserializing them. E.g. you if you set this to False, you'd submit an image in base64 format instead of a filepath, and you'd get back an image in base64 format from the remote API instead of a filepath.
            output_dir: The directory to save files that are downloaded from the remote API. If None, reads from the GRADIO_TEMP_DIR environment variable. Defaults to a temporary directory on your machine.
            verbose: Whether the client should print statements to the console.
        gradio_client)tokenlibrary_namelibrary_versionNzhttp://zhttps:///Could not find Space: z7. If it is a private Space, please provide an hf_token.z'Space is still building. Please wait...   z+The current space is in the invalid state: z'. Please contact the owner to fix this.zLoaded as API: u    ✔httpws   c                   s   g | ]\}}t  ||qS  )Endpoint).0fn_index
dependencyselfr6   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio_client/client.py
<listcomp>   s   z#Client.__init__.<locals>.<listcomp>dependencies)r(   )target)1r+   r'   r)   r   r   __version__headersspace_idr*   
startswithendswith_space_name_to_src
ValueErrorr&   _get_space_stater   ZBUILDINGprinttimesleepZINVALID_RUNTIMEurllibparseurljoinZAPI_URLapi_urlreplaceZWS_URLws_urlZ
UPLOAD_URL
upload_urlZ	RESET_URL	reset_url_get_configconfigr"   uuiduuid4session_hash	enumerate	endpoints
concurrentZfuturesZThreadPoolExecutorexecutor	threadingThread_telemetry_threadstart)	r<   r&   r'   r(   r)   r*   r+   Z_srcstater6   r;   r=   __init__D   sZ    







zClient.__init__   z}Literal['cpu-basic', 'cpu-upgrade', 't4-small', 't4-medium', 'a10g-small', 'a10g-large', 'a100-large'] | SpaceHardware | Nonezdict[str, str] | None)	from_idto_idr'   privatehardwaresecretssleep_timeoutr(   r+   c
              
   C  s  zt j||d}
W n6 tyH } ztd| d|W Y d}~n
d}~0 0 |rtd|v rd|dd }t j||d}nt j|dd |d}z@t j||d |	rtdtj	| d |durt
d	 W n tyT   |	rtd
| d t j|||d|d |dur4| D ]\}}t j||||d q|	rPtdtj	|  Y n0 t j||d}|jprt jj}|p~|
j}||krt || td| d |t jjkrtj|||d d |	rtd | ||||	d}|S )a.
  
        Duplicates a Hugging Face Space under your account and returns a Client object
        for the new Space. No duplication is created if the Space already exists in your
        account (to override this, provide a new name for the new Space using `to_id`).
        To use this method, you must provide an `hf_token` or be logged in via the Hugging
        Face Hub CLI.

        The new Space will be private by default and use the same hardware as the original
        Space. This can be changed by using the `private` and `hardware` parameters. For
        hardware upgrades (beyond the basic CPU tier), you may be required to provide
        billing information on Hugging Face: https://huggingface.co/settings/billing

        Parameters:
            from_id: The name of the Hugging Face Space to duplicate in the format "{username}/{space_id}", e.g. "gradio/whisper".
            to_id: The name of the new Hugging Face Space to create, e.g. "abidlabs/whisper-duplicate". If not provided, the new Space will be named "{your_HF_username}/{space_id}".
            hf_token: The Hugging Face token to use to access private Spaces. Automatically fetched if you are logged in via the Hugging Face Hub CLI. Obtain from: https://huggingface.co/settings/token
            private: Whether the new Space should be private (True) or public (False). Defaults to True.
            hardware: The hardware tier to use for the new Space. Defaults to the same hardware tier as the original Space. Options include "cpu-basic", "cpu-upgrade", "t4-small", "t4-medium", "a10g-small", "a10g-large", "a100-large", subject to availability.
            secrets: A dictionary of (secret key, secret value) to pass to the new Space. Defaults to None. Secrets are only used when the Space is duplicated for the first time, and are not updated if the duplicated Space already exists.
            sleep_timeout: The number of minutes after which the duplicate Space will be puased if no requests are made to it (to minimize billing charges). Defaults to 5 minutes.
            max_workers: The maximum number of thread workers that can be used to make requests to the remote Gradio app simultaneously.
            verbose: Whether the client should print statements to the console.
        Example:
            import os
            from gradio_client import Client
            HF_TOKEN = os.environ.get("HF_TOKEN")
            client = Client.duplicate("abidlabs/whisper", hf_token=HF_TOKEN)
            client.predict("audio_sample.wav")
            >> "This is a test of the whisper speech recognition model."
        r-   r1   z9. If it is a private Space, please provide an `hf_token`.Nr0   r5   zUsing your existing Space: u    🤗zSecrets are only used when the Space is duplicated for the first time, and are not updated if the duplicated Space already exists.zCreating a duplicate of u    for your own use... 🤗T)rd   re   r-   exist_okrf   zCreated new Space: z1-------
NOTE: this Space uses upgraded hardware: zG... see billing info at https://huggingface.co/settings/billing
-------<   )r'   Ztimeout_in_seconds )r'   r(   r+   )huggingface_hubget_space_runtimer   rG   splitget_full_repo_namerI   r   Z	SPACE_URLformatwarningswarnZduplicate_spaceitemsadd_space_secretrg   r   Z	CPU_BASICZrequest_space_hardwareZset_space_timeout)clsrd   re   r'   rf   rg   rh   ri   r(   r+   Zoriginal_infoZrnferC   keyvalueZcurrent_infoZcurrent_hardwareclientr6   r6   r=   r      sz    5





zClient.duplicatec                 C  s"   | j s
d S tj| j | jd}|jS Nrj   )rC   rn   ro   r'   Zstage)r<   infor6   r6   r=   rH     s    zClient._get_space_stateapi_namer9   z
int | Noner   )r~   r9   returnc                G  s6   |  ||}| j| jr td| j|||d S )a  
        Calls the Gradio API and returns the result (this is a blocking call).

        Parameters:
            args: The arguments to pass to the remote API. The order of the arguments must match the order of the inputs in the Gradio app.
            api_name: The name of the API endpoint to call starting with a leading slash, e.g. "/predict". Does not need to be provided if the Gradio app has only one named API endpoint.
            fn_index: As an alternative to api_name, this parameter takes the index of the API endpoint to call, e.g. 0. Both api_name and fn_index can be provided, but if they conflict, api_name will take precedence.
        Returns:
            The result of the API call. Will be a Tuple if the API has multiple outputs.
        Example:
            from gradio_client import Client
            client = Client(src="gradio/calculator")
            client.predict(5, "add", 4, api_name="/predict")
            >> 9.0
        zOCannot call predict on this function as it may run forever. Use submit instead.r}   )_infer_fn_indexrZ   is_continuousrG   r   result)r<   r~   r9   argsinferred_fn_indexr6   r6   r=   r   	  s    zClient.predict)r~   r9   result_callbacksz Callable | list[Callable] | NoneJob)r~   r9   r   r   c                G  s   |  ||}d}| j| jr8tt t | j| j| j}| j| |}| j	j
|g|R  }t||| j| jd}	|rt|tr|g}dddd}
|D ]}|	|
| q|	S )a  
        Creates and returns a Job object which calls the Gradio API in a background thread. The job can be used to retrieve the status and result of the remote API call.

        Parameters:
            args: The arguments to pass to the remote API. The order of the arguments must match the order of the inputs in the Gradio app.
            api_name: The name of the API endpoint to call starting with a leading slash, e.g. "/predict". Does not need to be provided if the Gradio app has only one named API endpoint.
            fn_index: As an alternative to api_name, this parameter takes the index of the API endpoint to call, e.g. 0. Both api_name and fn_index can be provided, but if they conflict, api_name will take precedence.
            result_callbacks: A callback function, or list of callback functions, to be called when the result is ready. If a list of functions is provided, they will be called in order. The return values from the remote API are provided as separate parameters into the callback. If None, no callback will be called.
        Returns:
            A Job object that can be used to retrieve the status and result of the remote API call.
        Example:
            from gradio_client import Client
            client = Client(src="gradio/calculator")
            job = client.submit(5, "add", 4, api_name="/predict")
            job.status()
            >> <Status.STARTING: 'STARTING'>
            job.result()  # blocking call
            >> 9.0
        N)communicatorr+   rC   r   r   c                   s    fdd}|S )Nc                   s,   t |  tr |    n |   d S N)
isinstancer   tuple)futurecallbackr6   r=   fnU  s    z,Client.submit.<locals>.create_fn.<locals>.fnr6   )r   r   r6   r   r=   	create_fnT  s    z Client.submit.<locals>.create_fn)r   rZ   use_wsr   r   r   process_predictionsrS   make_end_to_end_fnr\   r   r   r+   rC   r   r   Zadd_done_callback)r<   r~   r9   r   r   r   helperZend_to_end_fnr   jobr   r   r6   r6   r=   r   %  s*    

	zClient.submitzbool | NonezLiteral['dict', 'str'] | Nonezdict | str | None)all_endpoints
print_inforeturn_formatr   c                 C  s  | j rtj| jtj}ntj| jtj}t| j	
ddtdkr|tj
|| jd}|jrj| }qtd| j nDtjtjt| j	| j dd}|jr| d }ntd| j t|d	 }t|d
 }	|dkr|du rd}d}
|
d| d7 }
|d	  D ]\}}|
| ||7 }
q|rl|
d|	 d7 }
|d
  D ]\}}|
| t||7 }
qJn|	dkr|
d|	 d7 }
|rt|
 |dkr|
S |dkr|S dS )au  
        Prints the usage info for the API. If the Gradio app has multiple API endpoints, the usage info for each endpoint will be printed separately. If return_format="dict" the info is returned in dictionary format, as shown in the example below.

        Parameters:
            all_endpoints: If True, prints information for both named and unnamed endpoints in the Gradio app. If False, will only print info about named endpoints. If None (default), will print info about named endpoints, unless there aren't any -- in which it will print info about unnamed endpoints.
            print_info: If True, prints the usage info to the console. If False, does not print the usage info.
            return_format: If None, nothing is returned. If "str", returns the same string that would be printed to the console. If "dict", returns the usage info as a dictionary that can be programmatically parsed, and *all endpoints are returned in the dictionary* regardless of the value of `all_endpoints`. The format of the dictionary is in the docstring of this method.
        Example:
            from gradio_client import Client
            client = Client(src="gradio/calculator")
            client.view_api(return_format="dict")
            >> {
                'named_endpoints': {
                    '/predict': {
                        'parameters': [
                            {
                                'label': 'num1',
                                'type_python': 'int | float',
                                'type_description': 'numeric value',
                                'component': 'Number',
                                'example_input': '5'
                            },
                            {
                                'label': 'operation',
                                'type_python': 'str',
                                'type_description': 'string value',
                                'component': 'Radio',
                                'example_input': 'add'
                            },
                            {
                                'label': 'num2',
                                'type_python': 'int | float',
                                'type_description': 'numeric value',
                                'component': 'Number',
                                'example_input': '5'
                            },
                        ],
                        'returns': [
                            {
                                'label': 'output',
                                'type_python': 'int | float',
                                'type_description': 'numeric value',
                                'component': 'Number',
                            },
                        ]
                    },
                    '/flag': {
                        'parameters': [
                            ...
                            ],
                        'returns': [
                            ...
                            ]
                        }
                    }
                'unnamed_endpoints': {
                    2: {
                        'parameters': [
                            ...
                            ],
                        'returns': [
                            ...
                            ]
                        }
                    }
                }
            }

        r   2.0z3.36.1rB   zCould not fetch api info for )rU   r)   )jsonapiZnamed_endpointsZunnamed_endpointsr   NTz8Client.predict() Usage Info
---------------------------
zNamed API endpoints: 
z
Unnamed API endpoints: z3, to view, run Client.view_api(all_endpoints=True)
r"   dict)r)   rL   rM   rN   r&   r   ZAPI_INFO_URLZRAW_API_INFO_URLr   rU   getVersionrequestsrB   okr   rG   postZSPACE_FETCHER_URLdumpslenru   _render_endpoints_infor$   rI   )r<   r   r   r   Zapi_info_urlrr|   fetchZnum_named_endpointsZnum_unnamed_endpoints
human_infor~   Zendpoint_infor9   r6   r6   r=   r   b  sF    K



zClient.view_apiNoner   c                 C  s   t t | _d S r   )r"   rV   rW   rX   r;   r6   r6   r=   reset_session  s    zClient.reset_sessionz	str | intzdict[str, list[dict[str, Any]]])name_or_indexendpoints_infor   c              
   C  s  dd |d D }dd |D }d |}|r6|d }dd |d D }dd |D }d |}t|d	krxd
| d}t|trd| d}nt|trd| }ntdd| | d| d}|d7 }|d rH|d D ]f}	|	d drd|	d d  dnd}
|	d d }|d|	d  dt|	d  d| |
 d7 }qn|d7 }|d 7 }|d r|d D ]h}	|	d drd|	d d  dnd}
|	d d }|d|	d  dt|	d  d| |
 d7 }qjn|d7 }|S )!Nc                 S  s   g | ]}|d  qS labelr6   r8   pr6   r6   r=   r>         z1Client._render_endpoints_info.<locals>.<listcomp>
parametersc                 S  s   g | ]}t |qS r6   r   sanitize_parameter_namesr   r6   r6   r=   r>     r   z, c                 S  s   g | ]}|d  qS r   r6   r   r6   r6   r=   r>     r   returnsc                 S  s   g | ]}t |qS r6   r   )r8   r   r6   r6   r=   r>     r   r5   ()z
api_name=""z	fn_index=z)name_or_index must be a string or integerz
 - predict(z) -> r   z    Parameters:
Zpython_typedescriptionz (rm   typez     - [	componentz] r   z: z 
z     - None
z    Returns:
)	joinr   r   r"   r$   rG   r   r   r   )r<   r   r   Zparameter_namesZrendered_parametersZreturn_valuesZrendered_return_valuesZfinal_paramr   r|   desctype_r6   r6   r=   r     sJ    




2
4zClient._render_endpoints_infoc                 C  s   | j dddS NFr"   )r   r   r   r;   r6   r6   r=   __repr__  s    zClient.__repr__c                 C  s   | j dddS r   r   r;   r6   r6   r=   __str__  s    zClient.__str__c                 C  s8   d| j i}ztddtj|d W n ty2   Y n0 d S )Nr&   zpy_client/initiatedr,   )Ztopicr.   r/   
user_agent)r&   r   r   rA   	Exceptionr<   datar6   r6   r=   r_     s    
zClient._telemetry_threadc           	      C  s   d }|d ur|t | jd D ]8\}}|d}|d u s|du r>qd| |kr|} qqd| d}|dsr|d7 }t|n>|d ur|}n0dd	 | jD }t|d
kr|d j}ntd|S )Nr?   r~   Fr0   z(Cannot find a function with `api_name`: .z% Did you mean to use a leading slash?c                 S  s    g | ]}|j r|jd ur|qS r   )is_validr~   )r8   er6   r6   r=   r>   <  s   z*Client._infer_fn_index.<locals>.<listcomp>r5   r   zYThis Gradio app might have multiple endpoints. Please specify an `api_name` or `fn_index`)rY   rU   r   rD   rG   rZ   r   r9   )	r<   r~   r9   r   idZconfig_api_nameerror_messageZvalid_endpointsr6   r6   r=   r   *  s0    


zClient._infer_fn_indexc                 C  s   t | dr| jjdd d S )Nr\   T)wait)hasattrr\   shutdownr;   r6   r6   r=   __del__G  s    
zClient.__del__c                 C  s   t j|| jdjS r{   )rn   
space_infor'   host)r<   spacer6   r6   r=   rF   K  s    zClient._space_name_to_srcr   c              
   C  s   t jtj| jtj| jd}|j	r,|
 S t j| j| jd}td|j}zt
|d}W n6 ty } ztd| j |W Y d }~n
d }~0 0 d|v rtd|S d S )Nr   z+window.gradio_config = (.*?);[\s]*</script>r5   z"Could not get Gradio config from: Zallow_flaggingzcGradio 2.x is not supported by this client. Please upgrade your Gradio app to Gradio 3.x or higher.)r   r   rL   rM   rN   r&   r   Z
CONFIG_URLrB   r   r   researchtextloadsgroupAttributeErrorrG   )r<   r   r   rU   Zaer6   r6   r=   rT   N  s(    
zClient._get_configFz"list[str | tuple[str, str]] | None)discord_bot_token	api_namesre   r'   rf   c              	     s  | j d dkr sdg t to.dd  D } du s<|sJtd  t dks^J d	t D ]\}}t|trf||f |< qft fd
d| jD dstd d d  d| j	p| j
 fddjD }	fddjD }
|	dgks|
dgkrtd|	 d|
 d}| j	rTt| j	j}|rT|sTJ d| j	 d|rd|v rr|dd }tj||d}n>| j	r| j	dd  d}ndtd }tj||d}t }zt| d}W n tjjy   d}Y n0 tj|dd|d|d |r*tj|dd d!gid" ttttjd# d$ }| }W d   n1 sd0    Y  |d%| j
}|d& d d }|d' d d }tjd(dd)\}tjd(dd),}| | | d*!d+g W d   n1 s0    Y  W d   n1 s0    Y  t"d,|j#d-t"d.|j#d-g}|j$|d/d||d0 |rhtj%|d1||d |rtj%|d2||d d3| }t&d4|  |S )5a  
        Deploy the upstream app as a discord bot. Currently only supports gr.ChatInterface.
        Parameters:
            discord_bot_token: This is the "password" needed to be able to launch the bot. Users can get a token by creating a bot app on the discord website. If run the method without specifying a token, the space will explain how to get one. See here: https://huggingface.co/spaces/freddyaboulton/test-discord-bot-v1.
            api_names: The api_names of the app to turn into bot commands. This parameter currently has no effect as ChatInterface only has one api_name ('/chat').
            to_id: The name of the space hosting the discord bot. If None, the name will be gradio-discord-bot-{random-substring}
            hf_token: HF api token with write priviledges in order to upload the files to HF space. Can be ommitted if logged in via the HuggingFace CLI, unless the upstream space is private. Obtain from: https://huggingface.co/settings/token
            private: Whether the space hosting the discord bot is private. The visibility of the discord bot itself is set via the discord website. See https://huggingface.co/spaces/freddyaboulton/test-discord-bot-v1
        modeZchat_interface)chatr   c                 s  s>   | ]6}t |tp4t |to4t |d  to4t |d tV  qdS )r   r5   N)r   r"   r   )r8   nr6   r6   r=   	<genexpr>y  s   
z(Client.deploy_discord.<locals>.<genexpr>NzPEach entry in api_names must be either a string or a tuple of strings. Received r5   z7Currently only one api_name can be deployed to discord.c                 3  s*   | ]"}|j d  d d  kr|V  qdS )r0   r   N)r~   )r8   ep)r   r6   r=   r     r   z	api_name r   z not present in c                   s   g | ]} t jvr|qS r6   r   SKIP_COMPONENTSr8   inpr   r6   r=   r>     s   z)Client.deploy_discord.<locals>.<listcomp>c                   s   g | ]} t jvr|qS r6   r   r   r   r6   r=   r>     s   Ztextboxz[Currently only api_names with a single textbox as input and output are supported. Received z and FzSince zo is private, you must explicitly pass in hf_token so that it can be added as a secret in the discord bot space.r0   rj   z-gradio-discord-botzgradio-discord-bot-   Tr   r   )	repo_typeZ	space_sdkr-   rk   rf   tagszgradio-discord-bot)repo_idr   metadataZ	templateszdiscord_chat.pyz<<app-src>>z<<api-name>>z<<command-name>>w)r   deleter   zdiscord.py==2.3.1zapp.py)Zpath_in_repoZpath_or_fileobjzrequirements.txtzDeploy Discord Bot)r   Zcommit_messager   
operationsr-   ZDISCORD_TOKENZHF_TOKENzhttps://huggingface.co/spaces/zSee your discord bot here! )'rU   r   listrG   r   rY   r"   nextrZ   rC   r&   input_component_typesrn   r   rf   rp   rq   rh   Z	token_hexZHfApir   r   Zcreate_repoZmetadata_updateopenr   __file__parentreadrP   tempfileNamedTemporaryFilewriter   r
   nameZcreate_commitrv   rI   )r<   r   r   re   r'   rf   Z
valid_listr   r   inputsoutputs
is_privaterC   r   Zfirst_uploadfZappZapp_filerequirementsr   urlr6   )r   r   r=   r   d  s    






(
R
zClient.deploy_discord)NNTNNrc   r!   T)NTN)NNNNF)__name__
__module____qualname____doc__DEFAULT_TEMP_DIRrb   classmethodr   rH   r   r   r   r   r   r   r   r_   r   r   rF   rT   r   r6   r6   r6   r=   r    2   sX   J        "t	?   }3     r    c                   @  s   e Zd ZdZddddddZdd	 Zd
d Zd8ddddZd9ddddZddddZ	dddddZ
dddddd Zd!dd"d#Zd!dd$d%Zddd&d'Zd!dd(d)Zd!dd*d+Zd,d- Zd.dd/d0Zdd1d2d3d4Zd5dd6d7ZdS ):r7   zIHelper class for storing all the information about a single API endpoint.r    r$   r   )rz   r9   r:   c                 C  s   || _ || _|| _|d}|d u s,|du r0d nd| | _| | j| _g | _g | _|j	
dsj|j	d n|j	| _|di dd| _z*|  \| _| _| jd o| jdu| _W n ty   d| _Y n0 d S )Nr~   Fr0   typesZ
continuousZ
backend_fn)rz   r9   r:   r   r~   _use_websocketr   r   output_component_typesr&   rE   root_urlr   _setup_serializersserializersdeserializersr   AssertionError)r<   rz   r9   r:   r~   r6   r6   r=   rb     s     
zEndpoint.__init__c                 C  s   d| j j d| j d| j S )NzEndpoint src: z, api_name: z, fn_index: )rz   r&   r~   r9   r;   r6   r6   r=   r   
  s    zEndpoint.__repr__c                 C  s   |   S r   )r   r;   r6   r6   r=   r     s    zEndpoint.__str__NCommunicator | None)r   c                   s      fdd}|S )Nc                    s   j st j|  } jjr*j|  }  |  }j| }r|j& jj	s^jj	
| W d    n1 sr0    Y  |S r   )r   r   ZInvalidAPIEndpointErrorinsert_staterz   r)   r   lockr   r   append)r   predictions_predictr   r<   r6   r=   _inner  s    


,z+Endpoint.make_end_to_end_fn.<locals>._inner)make_predict)r<   r   r  r6   r  r=   r     s    
zEndpoint.make_end_to_end_fnc                   s   dd fdd}|S )Nr   r   c               
     s6  t | jjjd} t jjjd}jr\tj| | }d|v rt	|d n*t
jjjjj| d}t |jd}z|d }W n ty, } zjjotjjj }d|v rd|d v r|rtdjj d	d nd|v rt	|d d td
| |W Y d }~n
d }~0 0 t|S )N)r   r9   rX   )r9   rX   error)rB   r   zutf-8r   Z429zToo many requests to the API, please try again later. To avoid being rate-limited, please duplicate the Space using Client.duplicate(z&) and pass in your Hugging Face token.z:Could not find 'data' key in response. Response received: )r   r   r9   rz   rX   r   r   Zsynchronize_async_ws_fnrG   r   r   rO   rB   r   contentdecodeKeyErrorrC   rn   r   rf   ZTooManyRequestsErrorr   )r   	hash_datar   responseoutputZkeZis_public_spacer   r<   r6   r=   r  &  sR    
z'Endpoint.make_predict.<locals>._predictr6   )r<   r   r  r6   r  r=   r  %  s    ,zEndpoint.make_predictr   r   c                 G  s*   |   | }t| jd dkr&|d S |S )z`Needed for gradio.load(), which has a slightly different signature for serializing/deserializingr   r5   r   )r  r   r:   r<   r   r   r6   r6   r=   _predict_resolveT  s    zEndpoint._predict_resolvezlist[str | list[str]]zClist[str | list[str]] | list[dict[str, Any] | list[dict[str, Any]]])
file_pathsr   c              	     s   |sg S g }g t |D ]J\ }t|ts0|g}|D ],}|dt|jt|dff   q4qtj| j	j
| j	j|d}|jdkr|}ng }| }t |D ]z\ }t|tr fddt |D }dd t||D }	n0t fdd	t |D }
d
|
t|jd d}	||	 q|S )Nfilesrb)rB   r#     c                   s    g | ]\}}|  kr|qS r6   r6   r8   ixor   indicesr6   r=   r>   v  r   z$Endpoint._upload.<locals>.<listcomp>c                 S  s$   g | ]\}}d |t |jddqS )TNis_filer   Z	orig_namer   )r   r   )r8   r   r(  r6   r6   r=   r>   w  s   c                 3  s"   | ]\}}|  kr|V  qd S r   r6   r&  r)  r6   r=   r     r   z#Endpoint._upload.<locals>.<genexpr>Tr+  )rY   r   r   r  r   r   r   r   r   rz   rR   rB   status_coder   zipr   )r<   r"  r#  fsr   r   Zuploadedr   r  resr(  r6   r)  r=   _upload[  s>    



zEndpoint._uploadz	list[Any]r   )r#  r   r   c                 C  s8   d}t | jD ]$\}}|dv r|| ||< |d7 }qdS )zAHelper function to modify the input data with the uploaded files.r   fileZuploadbuttonr5   N)rY   r   )r<   r#  r   Zfile_counterr   tr6   r6   r=   _add_uploaded_files_to_data  s
    z$Endpoint._add_uploaded_files_to_datar   c                 G  s:   t |}t| jD ]\}}|tjkr||d  qt|S r   )r   rY   r   r   ZSTATE_COMPONENTinsertr   )r<   r   r   Zinput_component_typer6   r6   r=   r    s
    
zEndpoint.insert_statec                 G  s   dd t || jD }t|S )Nc                 S  s   g | ]\}}|t jvr|qS r6   r   )r8   r   octr6   r6   r=   r>     s   
z6Endpoint.remove_skipped_components.<locals>.<listcomp>)r.  r  r   r   r6   r6   r=   remove_skipped_components  s    
z"Endpoint.remove_skipped_componentsc                 G  s(   t dd | jD dkr |d S |S d S )Nc                 S  s   g | ]}|t jvr|qS r6   r   )r8   r7  r6   r6   r=   r>     s   
z4Endpoint.reduce_singleton_output.<locals>.<listcomp>r5   r   )r   r  r   r6   r6   r=   reduce_singleton_output  s    
z Endpoint.reduce_singleton_outputc                 G  s   t |t | jks0J dt | j dt | dd t|| jD }| |}t|}| || tdd t| j|D }|S )N	Expected z arguments, got c                 S  s   g | ]\}}|d v r|qS )r2  r6   )r8   r   r4  r6   r6   r=   r>     s   z&Endpoint.serialize.<locals>.<listcomp>c                 S  s   g | ]\}}| |qS r6   )r)   r8   sr   r6   r6   r=   r>     r   )r   r  r.  r   r1  r   r5  r   )r<   r   r#  Zuploaded_filesr(  r6   r6   r=   r)     s    

zEndpoint.serializec                   sR   t |t  jks0J dt  j dt | t fddt j|D }|S )Nr:  z outputs, got c                   s,   g | ]$\}}|j | jj jj jd qS ))save_dirr'   r	  )deserializerz   r*   r'   r	  r;  r;   r6   r=   r>     s   z(Endpoint.deserialize.<locals>.<listcomp>)r   r  r   r.  r   r6   r;   r=   r>    s    

zEndpoint.deserializec                 G  s*   | j jr| j| }| j| }| j| }|S r   )rz   r)   r>  r8  r9  )r<   r  r6   r6   r=   r     s
    


zEndpoint.process_predictionsz-tuple[list[Serializable], list[Serializable]]c                 C  s  | j d }g }|D ]}| jjd D ]}|d |kr"|d }| j| |dr~|d }|tjv srJ d| dtj| }n$|tjv sJ d| dtj| }||  q"q| j d	 }g }	|D ]}| jjd D ]}|d |kr|d }| j	| |dr4|d }|tjv s(J d| dtj| }
n:|t
jv rHtj}
n&|tjv sdJ d| dtj| }
|	|
  qq||	fS )
Nr   
componentsidr   
serializerzUnknown serializer: z4, you may need to update your gradio_client version.zUnknown component: r   )r:   rz   rU   r   r  r   r   ZSERIALIZER_MAPPINGZCOMPONENT_MAPPINGr  r   r   ZSimpleSerializable)r<   r   r  r   r   Zcomponent_nameZserializer_namerA  r   r  Zdeserializerr6   r6   r=   r
    sV    








zEndpoint._setup_serializersr%   )r:   r   c                 C  sL   | j jdd}t| j jddtdk}|dddu}|oJ|oJ|S )NZenable_queueFr   r   z3.2queue)rz   rU   r   r   rM   r   )r<   r:   Zqueue_enabledZqueue_uses_websocketZdependency_uses_queuer6   r6   r=   r    s    zEndpoint._use_websocketr   c              	     sh   t j| jjd| jjdd4 I d H ,}t||||I d H W  d   I d H  S 1 I d H sZ0    Y  d S )N
   i   @)Zopen_timeoutextra_headersmax_size)
websocketsconnectrz   rQ   rB   r   Zget_pred_from_ws)r<   r   r  r   Z	websocketr6   r6   r=   r    s    zEndpoint._ws_fn)N)N)r   r  r  r  rb   r   r   r   r  r!  r1  r5  r  r8  r9  r)   r>  r   r
  r  r  r6   r6   r6   r=   r7     s$   /0.r7   r   r   statusc                      s   e Zd ZdZd dddddd	d
Zd dddZddddZd!ddd fddZddddZddddZ	dd Z
ddddZ  ZS )"r   a  
    A Job is a wrapper over the Future class that represents a prediction call that has been
    submitted by the Gradio client. This class is not meant to be instantiated directly, but rather
    is created by the Client.submit() method.

    A Job object includes methods to get the status of the prediction call, as well to get the outputs of
    the prediction call. Job objects are also iterable, and can be used in a loop to get the outputs
    of prediction calls as they become available for generator endpoints.
    NTr   r  r%   r#   )r   r   r+   rC   c                 C  s"   || _ || _d| _|| _|| _dS )a  
        Parameters:
            future: The future object that represents the prediction call, created by the Client.submit() method
            communicator: The communicator object that is used to communicate between the client and the background thread running the job
            verbose: Whether to print any status-related messages to the console
            space_id: The space ID corresponding to the Client object that created this Job object
        r   N)r   r   _counterr+   rC   )r<   r   r   r+   rC   r6   r6   r=   rb   )  s
    zJob.__init__r   c                 C  s   | S r   r6   r;   r6   r6   r=   __iter__=  s    zJob.__iter__ztuple | Anyc                 C  s   | j st | j j( | j jjjtjkr.t W d    n1 sB0    Y  | j jp t| j jj	| j
d kr| j jj	| j
 }|  j
d7  _
|W  d    S | j jjjtjkrt W d    qL1 s0    Y  qLd S )Nr5   )r   StopIterationr  r   latest_statuscoder   FINISHEDr   r   rI  )r<   r(  r6   r6   r=   __next__@  s    
$
zJob.__next__zfloat | Noner   )timeoutr   c                   s   t  j|dS )a.  
        Return the result of the call that the future represents. Raises CancelledError: If the future was cancelled, TimeoutError: If the future didn't finish executing before the given timeout, and Exception: If the call raised then that exception will be raised.

        Parameters:
            timeout: The number of seconds to wait for the result if the future isn't done. If None, then there is no limit on the wait time.
        Returns:
            The result of the call that the future represents. For generator functions, it will return the final iteration.
        Example:
            from gradio_client import Client
            calculator = Client(src="gradio/calculator")
            job = calculator.submit("foo", "add", 4, fn_index=0)
            job.result(timeout=5)
            >> 9
        )rP  )superr   )r<   rP  	__class__r6   r=   r   Q  s    z
Job.resultzlist[tuple | Any]c                 C  s@   | j s
g S | j j | j jjW  d   S 1 s20    Y  dS )a  
        Returns a list containing the latest outputs from the Job.

        If the endpoint has multiple output components, the list will contain
        a tuple of results. Otherwise, it will contain the results without storing them
        in tuples.

        For endpoints that are queued, this list will contain the final job output even
        if that endpoint does not use a generator function.

        Example:
            from gradio_client import Client
            client = Client(src="gradio/count_generator")
            job = client.submit(3, api_name="/count")
            while not job.done():
                time.sleep(0.1)
            job.outputs()
            >> ['0', '1', '2']
        N)r   r  r   r   r;   r6   r6   r=   r   b  s    
zJob.outputsr   c              	   C  s>  t  }d}| jrB| jj | jj}W d   n1 s80    Y  |r^ttjddd|dddS |  r| j	j
sttjddd|dddS ttjddd|dddS n| jsttjddd|dddS | jjb | jjjj}| jr| jr|r|dkrtdt| d| j d	 | jjjW  d   S 1 s00    Y  dS )
a<  
        Returns the latest status update from the Job in the form of a StatusUpdate
        object, which contains the following fields: code, rank, queue_size, success, time, eta, and progress_data.

        progress_data is a list of updates emitted by the gr.Progress() tracker of the event handler. Each element
        of the list has the following fields: index, length, unit, progress, desc. If the event handler does not have
        a gr.Progress() tracker, the progress_data field will be None.

        Example:
            from gradio_client import Client
            client = Client(src="gradio/calculator")
            job = client.submit(5, "add", 4, api_name="/predict")
            job.status()
            >> <Status.STARTING: 'STARTING'>
            job.status().eta
            >> 43.241  # seconds
        FNr   )rM  ZrankZ
queue_sizesuccessrJ   etaZprogress_dataT   zIDue to heavy traffic on this app, the prediction will take approximately zn seconds.For faster predictions without waiting in queue, you may duplicate the space using: Client.duplicate(r   )r   nowr   r  should_cancelr   r   Z	CANCELLEDdoner   
_exceptionrN  
PROCESSINGr   rL  rU  r+   rC   rI   r$   )r<   rJ   Z	cancelledrU  r6   r6   r=   rH  |  sl    
&	



 z
Job.statusc                 C  s   t | j|S )z,Forwards any properties to the Future class.)getattrr   )r<   r   r6   r6   r=   __getattr__  s    zJob.__getattr__c                 C  sB   | j r8| j j d| j _W d   dS 1 s.0    Y  | j S )a  Cancels the job as best as possible.

        If the app you are connecting to has the gradio queue enabled, the job
        will be cancelled locally as soon as possible. For apps that do not use the
        queue, the job cannot be cancelled if it's been sent to the local executor
        (for the time being).

        Note: In general, this DOES not stop the process from running in the upstream server
        except for the following situations:

        1. If the job is queued upstream, it will be removed from the queue and the server will not run the job
        2. If the job has iterative outputs, the job will finish as soon as the current iteration finishes running
        3. If the job has not been picked up by the queue yet, the queue will not pick up the job
        TN)r   r  rX  r   cancelr;   r6   r6   r=   r^    s
    
 z
Job.cancel)NTN)N)r   r  r  r  rb   rJ  rO  r   r   rH  r]  r^  __classcell__r6   r6   rR  r=   r     s      Kr   ):r  
__future__r   concurrent.futuresr[   r   osr   rh   r   r]   rJ   urllib.parserL   rV   rs   r   r   pathlibr   r   typingr   r   r	   rn   r   rF  r
   r   r   Zhuggingface_hub.utilsr   r   r   	packagingr   r,   r   r   Zgradio_client.documentationr   r   Zgradio_client.serializingr   Zgradio_client.utilsr   r   r   r   environr   r"   
gettempdirr  r    r7   r   r6   r6   r6   r=   <module>   sV        D  -
