
    ;3ht6                        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rdd
lmZ  G d d      Zy)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                      e Zd ZdZddZd Zd ZdddZdddZdddZ	dd	Z
	 	 	 	 dd
Z	 	 	 	 	 	 ddZddZddZddZddZddZd Z	 	 ddZddZddZy)EndpointV3CompatibilityzGEndpoint class for connecting to v3 endpoints. Backwards compatibility.c                n   || _         || _        || _        |j                  d      }t	        |t
              rd|z   n|| _        | j                  | j                        | _        | j                  rdnd| _	        g | _
        g | _        |j                  j                  d      s|j                  dz   n|j                  | _        	 | j                         \  | _        | _        | j                  d   xr | j                  du| _        |j                  d      | _        d| _        y # t&        $ r
 d| _        Y 0w xY w)Napi_name/wshttp
backend_fnFT)clientfn_index
dependency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   show_api)selfr   r   r   _argsr   s         [/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio_client/compatibility.py__init__z EndpointV3Compatibility.__init__   s   $ $>>*-(37C(NX 	 ))$//: $%'"&(#06

0C0CC0H

S(fjj	" 483J3J3L0Dd0 OOL9XdmmSX>XDM %..6 ' 	"!DM	"s   AD! !D43D4c                h    d| j                   j                   d| j                   d| j                   S )NzEndpoint src: z, api_name: z, fn_index: )r   r!   r   r   r)   s    r+   __repr__z EndpointV3Compatibility.__repr__2   s0    0T]]O<X\XeXeWfgg    c                "    | j                         S N)r/   r.   s    r+   __str__zEndpointV3Compatibility.__str__5   s    }}r0   Nc                <      j                         fd}|S )Nc                 n   j                   st        j                          j                  |  }  j                  |  }  |  } j
                  | }rRj                  5  j                  j                  s%j                  j                  j                  |       d d d        |S |S # 1 sw Y   |S xY wr2   )
r'   r
   InvalidAPIEndpointErrorinsert_state	serializeprocess_predictionslockjoboutputsappend)datapredictions_predicthelperr)   s     r+   _innerz:EndpointV3Compatibility.make_end_to_end_fn.<locals>._inner;   s    ==3355$4$$d+D!4>>4(D"D/K2$22K@K [[ ?!::--

**11+>? ;? s   "<B**B4)make_predict)r)   rA   rB   r@   s   `` @r+   make_end_to_end_fnz*EndpointV3Compatibility.make_end_to_end_fn8   s    $$V,	 r0   c                     y r2    )r)   rA   s     r+   make_cancelz#EndpointV3Compatibility.make_cancelL   s    r0   c                     d fd}|S )Nc                 $   t        j                  | j                  j                  j                  d      } t        j                  j                  j                  j                  d      }j
                  r4t        j                  j                  | |      }d|v rt        |d         t        j                  j                  j                  fj                  j                  | j                  j                  dj                  j                  }t        j                   |j"                  j%                  d            }	 |d   }t3        |      S # t&        $ r}j                  j(                  xr4 t+        j,                  j                  j(                        j.                   }d|v r7d|d   v r0|r.t        j0                  dj                  j(                   d	      d d|v rt        |d         d t'        d
|       |d }~ww xY w)N)r>   r   session_hash)r   rJ   error)headersjsonverifyzutf-8r>   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: )rM   dumpsr   r   rJ   r   r
   synchronize_async_ws_fn
ValueErrorhttpxpostapi_urlrL   
ssl_verifyhttpx_kwargsloadscontentdecodeKeyErrorspace_idhuggingface_hub
space_infoprivateTooManyRequestsErrortuple)	r>   	hash_dataresultresponseoutputkeis_public_spacerA   r)   s	          r+   r@   z6EndpointV3Compatibility.make_predict.<locals>._predictP   s   ::  $$(KK$<$<D 

 $$(KK$<$<I {{00dIvVf$$VG_55 ::KK'' KK//;;11	
 kk.. H$4$4$;$;G$DE" = !  KK(( U+66t{{7K7KLTTT   f$&/)Ao44MMQ[[MaMaLb c?@  	 
 &$VG_54?PQWPXYs   E 	H!B)H

Hreturnrb   rF   )r)   rA   r@   s   `` r+   rC   z$EndpointV3Compatibility.make_predictO   s    -	!^ r0   c                l     | j                         | }t        | j                  d         dk(  r|d   S |S )z`Needed for gradio.load(), which has a slightly different signature for serializing/deserializingr<      r   )rC   lenr   )r)   r>   r<   s      r+   _predict_resolvez(EndpointV3Compatibility._predict_resolve   s<    %$##%t,ty)*a/1:r0   c           
        |sg S g }g t        |      D ]b  \  }t        |t              s|g}|D ]E  }|j                  dt	        |      j
                  t        |d      ff       j                         G d t        j                  | j                  j                  f| j                  j                  || j                  j                  d| j                  j                  }|j                  dk7  r|}|S g }|j                         }t        |      D ]  \  }t        |t              r_t        |      D 	cg c]  \  }}	|   k(  s|	 }
}}	t!        ||
d      D 	cg c]  \  }}	d|	t	        |      j
                  d d! }}}	n8t#        fd	t        |      D              }	d|	t	        |      j
                  d d}|j                  |        |S c c}	}w c c}	}w )
Nfilesrb)rL   rp   rN      FstrictT)is_filename	orig_namer>   c              3  :   K   | ]  \  }}|   k(  s|  y wr2   rF   ).0ixoiindicess      r+   	<genexpr>z2EndpointV3Compatibility._upload.<locals>.<genexpr>   s!     R52qPQAQQRs   )	enumerater   listr=   r   rv   openrT   rU   r   
upload_urlrL   rW   rX   status_coderM   zipnext)r)   
file_pathsrp   fsfruploadedrd   rz   r{   rf   resr|   r}   s               @@r+   _uploadzEndpointV3Compatibility._upload   s    I
 z* 	"EArb$'T "gQd1dm'DEFq!"	" JJKK""
KK'';;))	

 kk&&
 ==C!H2 / HVVXF":. %2b$'-6v->SEB'"+QRBRaSFS %(F5$A !Aq (,$%)-a$(	C  RIf,=RRA#' !%)"X]] $	C $)%* ' Ts   9G
G"$Gc                f    d}t        | j                        D ]  \  }}|dv s||   ||<   |dz  } y)zAHelper function to modify the input data with the uploaded files.r   fileuploadbuttonrl   N)r   r   )r)   rp   r>   file_counterr|   ts         r+   _add_uploaded_files_to_dataz3EndpointV3Compatibility._add_uploaded_files_to_data   sH     d889 	"DAq,,-Q!	"r0   c                    t        |      }t        | j                        D ]+  \  }}|t        j                  k(  s|j                  |d        - t        |      S r2   )r   r   r   r
   STATE_COMPONENTinsertrb   )r)   r>   r|   input_component_types       r+   r7   z$EndpointV3Compatibility.insert_state   sS    Dz'01K1K'L 	%#A##u'<'<<At$	% T{r0   c                    t        || j                  d      D cg c]  \  }}|t        j                  vr| }}}t	        |      S c c}}w )NFrs   )r   r    r
   SKIP_COMPONENTSrb   )r)   r>   docts       r+   remove_skipped_componentsz1EndpointV3Compatibility.remove_skipped_components   sS     dD$?$?N
3%/// 
 

 T{
s   Ac                    t        | j                  D cg c]  }|t        j                  vr| c}      dk(  r|d   S |S c c}w )Nrl   r   )rm   r    r
   r   )r)   r>   r   s      r+   reduce_singleton_outputz/EndpointV3Compatibility.reduce_singleton_output   sU      $::%"7"77   7NKs   A c           	        t        |      t        | j                        k7  r-t        dt        | j                         dt        |             t        || j                  d      D cg c]  \  }}|dv r| }}}| j                  |      }t        |      }| j                  ||       t        t        | j                  |d      D cg c]  \  }}|j                  |       c}}      }|S c c}}w c c}}w )N	Expected z arguments, got Frs   r   )
rm   r%   rS   r   r   r   r   r   rb   r8   )	r)   r>   r   r   rp   uploaded_filessr   r{   s	            r+   r8   z!EndpointV3Compatibility.serialize   s    t9D,,--C 0 0122B3t9+N  D$"<"<UK
1,, 
 

 e,Dz((>(+D,<,<d5(QR1Q[[^R
 
 Ss   *C0C6
c                   t        |      t        | j                        k7  r-t        dt        | j                         dt        |             t        t	        | j                  |d      D cg c]L  \  }}|j                  || j                  j                  | j                  j                  | j                        N c}}      }|S c c}}w )Nr   z outputs, got Frs   )save_dirhf_tokenr#   )
rm   r&   rS   rb   r   deserializer   
output_dirr   r#   )r)   r>   r   r   r<   s        r+   r   z#EndpointV3Compatibility.deserialize   s    t9D..//C 2 234N3t9+N     2 2DG Aq ![[33![[11!]]	  

 s   /AC

c                    | j                   j                  r | j                  | } | j                  | } | j                  | }|S r2   )r   download_filesr   r   r   )r)   r?   s     r+   r9   z+EndpointV3Compatibility.process_predictions  sK    ;;%%*$**K8K4d44kB2d22K@r0   c                (   | j                   d   }g }|D ]  }| j                  j                  d   D ]  }|d   |k(  s|d   }| j                  j	                  |       |j                  d      r:|d   }|t        j                  vrt        d| d      t        j                  |   }n5|t        j                  v rt        j                  |   }nt        d| d      |j	                   |                | j                   d	   }g }	|D ]  }| j                  j                  d   D ]  }|d   |k(  s|d   }| j                  j	                  |       |j                  d      r:|d   }|t        j                  vrt        d| d      t        j                  |   }
nX|t        j                  v rt        j                  }
n5|t        j                  v rt        j                  |   }
nt        d| d      |	j	                   |
                ||	fS )
Ninputs
componentsidtype
serializerzUnknown serializer: z4, you may need to update your gradio_client version.zUnknown component: r<   )r   r   configr   r=   r   r	   SERIALIZER_MAPPINGr   COMPONENT_MAPPINGr    r
   r   SimpleSerializable)r)   r   r%   r|   	componentcomponent_nameserializer_namer   r<   r&   deserializers              r+   r$   z*EndpointV3Compatibility._setup_serializers  s8    * 	5A![[//= 5	T?a'%.v%6N..55nE }}\2*3L*A*+2P2PP"9"66GG{ |#  &1%C%CO%T
';+H+HH%0%B%B>%R
51.1AAuv   &&z|4#5	5( //), 	9A![[//= 9	T?a'%.v%6N//66~F }}\2*3L*A*+2P2PP"9"66GG{ |#  (3'E'Eo'V'5+@+@@'2'E'E';+H+HH'2'D'D^'T51.1AAuv  "((8'9	9, M))r0   c                (   | j                   j                  j                  dd      }t        j                  | j                   j                  j                  dd            t        j
                  d      k\  }|j                  dd      du}|xr |xr |S )Nenable_queueFr   z2.0z3.2queue)r   r   r   r   parseVersion)r)   r   queue_enabledqueue_uses_websocketdependency_uses_queues        r+   r   z&EndpointV3Compatibility._use_websocketF  s    **..~uE&}}KK""9e4 
__U# $ !+w >e KO!5O:OOr0   c                6  K   t        j                  | j                  j                  d| j                  j                  d      4 d {   }t        j                  ||||       d {   cd d d       d {    S 7 57 7 	# 1 d {  7  sw Y   y xY ww)N
   i   @)open_timeoutextra_headersmax_size)
websocketsconnectr   ws_urlrL   r
   get_pred_from_ws)r)   r>   rc   rA   	websockets        r+   rR   zEndpointV3Compatibility._ws_fnN  s     %%KK++--'	
 	T 	T
 //	4FSS	T 	T 	T T	T 	T 	T 	TsZ   ABA>	BB(B )B,B8B9B BBB
BBB)r   r   r   intr   dictr2   )rA   zCommunicator | None)rj   r   )r   zlist[str | list[str]]rj   Clist[str | list[str]] | list[dict[str, Any] | list[dict[str, Any]]])rp   r   r>   z	list[Any]rj   Noneri   )rj   zEtuple[list[serializing.Serializable], list[serializing.Serializable]])r   r   rj   bool)rA   r   )__name__
__module____qualname____doc__r,   r/   r3   rD   rG   rC   rn   r   r   r7   r   r   r8   r   r9   r$   r   rR   rF   r0   r+   r   r      s    Q.h(0d2/2	L2h
"R
" 
" 
	
"&$2*	N2*hPTr0   r   )r   
__future__r   rM   pathlibr   typingr   r   r   rT   r^   r   	packagingr   gradio_clientr	   r
   gradio_client.exceptionsr   gradio_client.utilsr   r   r   rF   r0   r+   <module>r      sG    H "   . .     , < $}T }Tr0   