o
    An0jt6                     @  s   d Z ddlmZ ddl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 ddlmZmZ ddlmZ dd	lmZ erJdd
lmZ G dd dZdS )zThis module contains the EndpointV3Compatibility class, which is used to connect to Gradio apps running 3.x.x versions of Gradio.    )annotationsN)Path)TYPE_CHECKINGAnyLiteral)version)serializingutils)SerializationSetupError)Communicator)Clientc                   @  s   e Zd ZdZd<dd	Zd
d Zdd Zd=d>ddZd=d>ddZd=d>ddZ	d?ddZ
d@ddZdAd$d%ZdBd'd(ZdBd)d*Zd?d+d,ZdBd-d.ZdBd/d0Zd1d2 ZdCd4d5ZdDd7d8ZdEd:d;ZdS )FEndpointV3CompatibilityzGEndpoint class for connecting to v3 endpoints. Backwards compatibility.clientr   fn_indexint
dependencydictc                 G  s   || _ || _|| _|d}t|trd| n|| _| | j| _| jr&dnd| _	g | _
g | _|jds:|jd n|j| _z|  \| _| _| jd oP| jdu| _W n ty_   d| _Y nw |d| _d| _d S )Napi_name/wshttp
backend_fnFT)r   r   r   get
isinstancestrr   _use_websocketuse_wsprotocolinput_component_typesoutput_component_typessrcendswithroot_url_setup_serializersserializersdeserializersis_validr
   r   Zshow_api)selfr   r   r   _argsr    r)   b/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/gradio_client/compatibility.py__init__   s&   


z EndpointV3Compatibility.__init__c                 C  s   d| j j d| j d| j S )NzEndpoint src: z, api_name: z, fn_index: )r   r    r   r   r'   r)   r)   r*   __repr__2   s   z EndpointV3Compatibility.__repr__c                 C  s   |   S N)r-   r,   r)   r)   r*   __str__5   s   zEndpointV3Compatibility.__str__NhelperCommunicator | Nonec                   s      fdd}|S )Nc                    s   j st j|  } j|  }  |  }j| }rCj jjs3jj	| W d    |S W d    |S 1 s>w   Y  |S r.   )
r&   r	   ZInvalidAPIEndpointErrorinsert_state	serializeprocess_predictionslockZjoboutputsappend)datapredictions_predictr0   r'   r)   r*   _inner;   s"   




z:EndpointV3Compatibility.make_end_to_end_fn.<locals>._inner)make_predict)r'   r0   r<   r)   r:   r*   make_end_to_end_fn8   s   
z*EndpointV3Compatibility.make_end_to_end_fnc                 C  s   d S r.   r)   )r'   r0   r)   r)   r*   make_cancelL   s   z#EndpointV3Compatibility.make_cancelc                   s   d fdd}|S )Nreturntuplec               
     s4  t | jjjd} t jjjd}jr.tj| | }d|v r-t	|d nt
jjjfjj| jjdjj}t |jd}z	|d }W t|S  ty } z8jjoitjjj }d|v rd|d v r|rtdjj d	d d|v rt	|d d td
| |d }~ww )N)r8   r   session_hash)r   rB   error)headersjsonverifyzutf-8r8   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: )rE   dumpsr   r   rB   r   r	   Zsynchronize_async_ws_fn
ValueErrorhttpxpostZapi_urlrD   
ssl_verifyhttpx_kwargsloadscontentdecodeKeyErrorZspace_idhuggingface_hubZ
space_infoprivateZTooManyRequestsErrorrA   )r8   	hash_dataresultresponseoutputZkeZis_public_spacer0   r'   r)   r*   r;   P   sd   
z6EndpointV3Compatibility.make_predict.<locals>._predictr@   rA   r)   )r'   r0   r;   r)   rX   r*   r=   O   s   /z$EndpointV3Compatibility.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/deserializingr6      r   )r=   lenr   r'   r8   r6   r)   r)   r*   _predict_resolve   s   z(EndpointV3Compatibility._predict_resolve
file_pathslist[str | list[str]]Clist[str | list[str]] | list[dict[str, Any] | list[dict[str, Any]]]c              	     s8  |sg S g }g t |D ]%\ }t|ts|g}|D ]}|dt|jt|dff   qqtj| j	j
f| j	j|| j	jd| j	j}|jdkrP|}|S g }| }t |D ]?\ }t|tr| fddt |D }dd t||dd	D }	nt fd
dt |D }
d|
t|jd d}	||	 qZ|S )Nfilesrb)rD   ra   rF      c                   s    g | ]\}}|  kr|qS r)   r)   .0Zixoiindicesr)   r*   
<listcomp>   s     z3EndpointV3Compatibility._upload.<locals>.<listcomp>c                 S  s$   g | ]\}}d |t |jddqS )TNis_filenameZ	orig_namer8   )r   rm   )re   frf   r)   r)   r*   rj      s    Fstrictc                 3  s$    | ]\}}|  kr|V  qd S r.   r)   rd   rg   r)   r*   	<genexpr>   s   " z2EndpointV3Compatibility._upload.<locals>.<genexpr>Trk   )	enumerater   listr7   r   rm   openrJ   rK   r   Z
upload_urlrD   rL   rM   status_coderE   zipnext)r'   r^   ra   fsrn   rZuploadedrU   rW   resrf   r)   rg   r*   _upload   sN   



zEndpointV3Compatibility._uploadra   r8   	list[Any]Nonec                 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uploadbuttonrZ   N)rr   r   )r'   ra   r8   Zfile_counterrh   tr)   r)   r*   _add_uploaded_files_to_data   s   z3EndpointV3Compatibility._add_uploaded_files_to_datarA   c                 G  s:   t |}t| jD ]\}}|tjkr||d  q	t|S r.   )rs   rr   r   r	   ZSTATE_COMPONENTinsertrA   )r'   r8   rh   Zinput_component_typer)   r)   r*   r2      s   
z$EndpointV3Compatibility.insert_statec                 G  s"   dd t || jddD }t|S )Nc                 S  s   g | ]\}}|t jvr|qS r)   r	   SKIP_COMPONENTS)re   doctr)   r)   r*   rj      s
    
zEEndpointV3Compatibility.remove_skipped_components.<locals>.<listcomp>Fro   )rv   r   rA   r'   r8   r)   r)   r*   remove_skipped_components   s   z1EndpointV3Compatibility.remove_skipped_componentsc                 G  s$   t dd | jD dkr|d S |S )Nc                 S  s   g | ]	}|t jvr|qS r)   r   )re   r   r)   r)   r*   rj      s
    
zCEndpointV3Compatibility.reduce_singleton_output.<locals>.<listcomp>rZ   r   )r[   r   r   r)   r)   r*   reduce_singleton_output   s   	z/EndpointV3Compatibility.reduce_singleton_outputc                 G  s   t |t | jkrtdt | j dt | dd t|| jddD }| |}t|}| || tdd t| j|ddD }|S )N	Expected z arguments, got c                 S  s   g | ]
\}}|d v r|qS )r~   r)   )re   rn   r   r)   r)   r*   rj      s
    z5EndpointV3Compatibility.serialize.<locals>.<listcomp>Fro   c                 S  s   g | ]	\}}| |qS r)   )r3   re   sr   r)   r)   r*   rj      s    )	r[   r$   rI   rv   r   r{   rs   r   rA   )r'   r8   ra   Zuploaded_filesrf   r)   r)   r*   r3      s   
z!EndpointV3Compatibility.serializec                   sV   t |t  jkrtdt  j dt | t fddt j|ddD }|S )Nr   z outputs, got c                   s,   g | ]\}}|j | jj jj jd qS ))save_dirhf_tokenr"   )deserializer   
output_dirr   r"   r   r,   r)   r*   rj      s    z7EndpointV3Compatibility.deserialize.<locals>.<listcomp>Fro   )r[   r%   rI   rA   rv   r\   r)   r,   r*   r      s   
z#EndpointV3Compatibility.deserializec                 G  s*   | j jr	| j| }| j| }| j| }|S r.   )r   Zdownload_filesr   r   r   )r'   r9   r)   r)   r*   r4     s
   


z+EndpointV3Compatibility.process_predictionsEtuple[list[serializing.Serializable], list[serializing.Serializable]]c                 C  s  | j d }g }|D ]P}| jjd D ]G}|d |krX|d }| j| |dr?|d }|tjvr9td| dtj| }n|tj	v rJtj	| }ntd| d||  qq	| j d	 }g }	|D ]Y}| jjd D ]P}|d |kr|d }| j
| |dr|d }|tjvrtd| dtj| }
n|tjv rtj}
n|tj	v rtj	| }
ntd| d|	|
  qkqc||	fS )
Ninputs
componentsidtype
serializerzUnknown serializer: z4, you may need to update your gradio_client version.zUnknown component: r6   )r   r   configr   r7   r   r   ZSERIALIZER_MAPPINGr
   ZCOMPONENT_MAPPINGr   r	   r   ZSimpleSerializable)r'   r   r$   rh   	componentZcomponent_nameZserializer_namer   r6   r%   Zdeserializerr)   r)   r*   r#     s^   












z*EndpointV3Compatibility._setup_serializersboolc                 C  sL   | j jdd}t| j jddtdk}|dddu}|o%|o%|S )NZenable_queueFr   z2.0z3.2queue)r   r   r   r   parseVersion)r'   r   Zqueue_enabledZqueue_uses_websocketZdependency_uses_queuer)   r)   r*   r   F  s   z&EndpointV3Compatibility._use_websocketr   c              	     sj   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 s.w   Y  d S )N
   i   @)Zopen_timeoutextra_headersmax_size)
websocketsconnectr   Zws_urlrD   r	   Zget_pred_from_ws)r'   r8   rT   r0   Z	websocketr)   r)   r*   rH   N  s   0zEndpointV3Compatibility._ws_fn)r   r   r   r   r   r   r.   )r0   r1   )r@   r   )r^   r_   r@   r`   )ra   r`   r8   r|   r@   r}   rY   )r@   r   )r   r   r@   r   )r0   r   )__name__
__module____qualname____doc__r+   r-   r/   r>   r?   r=   r]   r{   r   r2   r   r   r3   r   r4   r#   r   rH   r)   r)   r)   r*   r      s(    

2

4






4r   )r   
__future__r   rE   pathlibr   typingr   r   r   rJ   rR   r   	packagingr   Zgradio_clientr   r	   Zgradio_client.exceptionsr
   Zgradio_client.utilsr   r   r   r)   r)   r)   r*   <module>   s    