
    +j                       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mZ d dlmZ d dlmZmZmZm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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. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 erd dl8m7Z7 d dl9m:Z:  G d d          Z; G d d          Z< G d d          Z= G d d          Z	 d d!dZ>dS )"    )annotationsN)Queue)defaultdict)TYPE_CHECKINGAnyLiteralcast)run_sync)route_utilsroutes)CacheMissError
ProbeCache)PredictBodyInternal)Error)TrackedIterable)	EstimationMessageEventMessage
LogMessageProcessCompletedMessageProcessGeneratingMessageProcessStartsMessageProgressMessageProgressUnitServerMessage)LRUCacheerror_payloadrun_coro_in_backgroundsafe_aclose_iteratorsafe_get_lockset_task_name   )BlockFunction)Blocksc                  B    e Zd ZddZed	             Zed
             ZdS )Eventsession_hash
str | Nonefnr"   requestfastapi.Requestusernamec                `   t          j                    j        | _        |p| j        | _        || _        || _        || _        |j        | _        d | _	        d | _
        d| _        d| _        d| _        d| _        d| _        t!          j                    | _        t'          j                    | _        d S )NFTr   )uuiduuid4hex_idr&   r(   r)   r+   concurrency_iddataprogressprogress_pendingaliveclosedn_callsrun_timetime	monotonicenqueue_timeasyncior%   signal)selfr&   r(   r)   r+   s        S/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/gradio/queueing.py__init__zEvent.__init__7   s     :<<#!-!9  /04	04&+
 #'>#3#3moo    c                "    | j         j        dk    S )Nstream)r(   
connectionr>   s    r?   	streamingzEvent.streamingN   s    w!X--rA   c                    | j         st          d          | j        rdS | j        j        dS | j        | j        j        k    S )Nz6Cannot access if_finished during a non-streaming eventTF)rF   
ValueErrorr6   r(   
time_limitr8   rE   s    r?   is_finishedzEvent.is_finishedR   sM    ~ 	WUVVV; 	47%5} 222rA   N)r&   r'   r(   r"   r)   r*   r+   r'   )__name__
__module____qualname__r@   propertyrF   rJ    rA   r?   r%   r%   6   s^        & & & &. . . X. 3 3 X3 3 3rA   r%   c                      e Zd ZddZdS )
EventQueuer1   strconcurrency_limit
int | Nonec                p    g | _         || _        || _        d| _        t	          t
                    | _        d S Nr   )queuer1   rS   current_concurrencyr   setstart_times_per_fn)r>   r1   rS   s      r?   r@   zEventQueue.__init__^   s>    "$
,!2#$ JUK
 K
rA   N)r1   rR   rS   rT   )rK   rL   rM   r@   rO   rA   r?   rQ   rQ   ]   s(        
 
 
 
 
 
rA   rQ   c                      e Zd Zd ZddZdS )ProcessTimec                0    d| _         d| _        d| _        d S rV   process_timecountavg_timerE   s    r?   r@   zProcessTime.__init__i   s    
rA   r9   floatc                n    | xj         |z  c_         | xj        dz  c_        | j         | j        z  | _        d S )Nr!   r^   )r>   r9   s     r?   addzProcessTime.addn   s:    T!

a

)DJ6rA   N)r9   rb   )rK   rL   rM   r@   rd   rO   rA   r?   r\   r\   h   s7          
7 7 7 7 7 7rA   r\   c                  P   e Zd Z	 dYdZdZed             Zd Zd Zd[dZd Z	d\dZ
d]dZd Zd^d%Zd_d(Zd) Zd`d,Zdad-Zdbd/Zdcd1Zdcd2Zddd5Z	 	 dedfd?Zd@d@dAdgdCZdcdDZ	 dhdidGZdjdIZedkdJ            ZedldL            ZedmdN            ZedndT            ZdodWZd_dXZ d@S )pr   not_setlive_updatesboolconcurrency_countintupdate_intervalsrb   max_sizerT   blocksr#   default_concurrency_limitint | None | Literal['not_set']c                   t          d          | _        i | _        i | _        t	                      | _        i | _        d| _        || _        || _	        g | _
        t	                      | _        d | _        t          t                    | _        || _        t#          j                    dk    rdnd| _        t#          j                    dk    rdnd| _        || _        || _        g | _        |                     |          | _        i | _        di i| _        d	| _        t;          t=          j        d
d                    | _         d S )Ni  FWindowsg?gMbP?g?g{Gz?	functionsr    GRADIO_ANALYTICS_CACHE_FREQUENCY1)!r   pending_messages_per_sessionpending_event_ids_sessionevent_ids_to_eventsr   pending_message_lockevent_queue_per_concurrency_idstoppedmax_thread_countrk   active_jobsdelete_lock
server_appr   r\   process_time_per_fnrg   platformsystemsleep_when_freeprogress_update_sleep_when_freerl   rm   _asyncio_tasks_resolve_concurrency_limitrn   event_analyticscached_event_analytics_summaryevent_count_at_last_cacherj   osgetenvANAYLTICS_CACHE_FREQUENCY)r>   rg   ri   rk   rl   rm   rn   s          r?   r@   zQueue.__init__u   sJ    TNN 	) ?A&57 $1OO!EG+ 1 057(??LWM
 M
  )'/'8'8I'E'Ett5?$$	11CCt 	, !24)-)H)H%*
 *
& JL/:B.?+)*&),I8#>>*
 *
&&&rA   c                Z   dd l }	 |                    dd          5  |                    t          |                                                                         t          j                                      d          	 cd d d            S # 1 swxY w Y   d S # t          $ r}dt          |          v rk|                    t          |                                                                         t          j                                      d          cY d }~S |d }~ww xY w)Nr   zfuture.no_silent_downcastingT)valueF)copyzNo such keys(s))pandasoption_context	DataFramelistvaluesfillnanpnaninfer_objects	ExceptionrR   )r   pdes      r?   _get_dfzQueue._get_df   sp   	""#A4HH  LLo&<&<&>&>!?!?@@V"&V))"]]..                   	 	 	 CFF**LLo&<&<&>&>!?!?@@V"&V))"]]..     
 G	sH   B A'BB BB BB 
D*'A6D%D*#D%%D*c                4   t          |          | j        z
  | j        k    r|                     |          }t          |          | _        |                    d          }di i}|D ]\  }}|d         j        }t          j        |dk              }t          j        |dk              }	||	z   }
|
dk    r||
z  nd }t          j        |d         j        g d          }||d         |d	         |d
         d|j	        d         d|d         |<   || _
        | j
        S )Nfunctionrr   statussuccessfailedr   r_   )2   Z   c   r!      )50th90th99th)success_rateprocess_time_percentilestotal_requests)lenr   r   r   groupbyr   r   sum
percentileshaper   )r>   r   dfgroupedmetricsfn_namefn_dfr   r   failuretotalr   percentiless                r?   compute_analytics_summaryzQueue.compute_analytics_summary   s?     4#AA-. . o..B-0-A-AD*jj,,G"B'G")  x/&9!455&8!344')27!))w mE.,A,H,,,WW$0 +A +A +A1 1
 ',k!n1 1$W-- 3:D/22rA   c                    d g| j         z  | _        t          | j                   t          | j                   | j        st          | j                   d S d S N)r{   r|   r   start_processingstart_progress_updatesrg   notify_clientsrE   s    r?   startzQueue.start   sb     6D$99t4555t:;;;  	8"4#677777	8 	8rA   block_fnr"   c                    |j         }|j        dk    r| j        }n|j        }|| j        vrt	          ||          | j        |<   d S |(| j        |         }|j        ||j        k     r||_        d S d S d S )Ndefault)r1   rS   rn   ry   rQ   )r>   r   r1   rS   existing_event_queues        r?   create_event_queue_for_fnzQueue.create_event_queue_for_fn   s    !0%22 $ > ( :!DDDBL 1C CD/??? )#'#F~#V $6>$';'MMM9J$666 *)
 NMrA   c                    d| _         d S )NT)rz   rE   s    r?   closezQueue.close   s    rA   eventr%   event_messager   c                ~    |j         sd S |j        |_        | j        |j                 }|                    |           d S r   )r5   r0   event_idru   r&   
put_nowait)r>   r   r   messagess       r?   send_messagezQueue.send_message   sG    
 { 	F!&4U5GHM*****rA   returnc                    |dk    r|S t           j                            d          x}r)|                                dk    rdS t	          |          S dS )a  
        Handles the logic of resolving the default_concurrency_limit as this can be specified via a combination
        of the `default_concurrency_limit` parameter of the `Blocks.queue()` or the `GRADIO_DEFAULT_CONCURRENCY_LIMIT`
        environment variable. The parameter in `Blocks.queue()` takes precedence over the environment variable.
        Parameters:
            default_concurrency_limit: The default concurrency limit, as specified by a user in `Blocks.queue()`.
        rf    GRADIO_DEFAULT_CONCURRENCY_LIMITnoneNr!   )r   environgetlowerrj   )r>   rn   default_concurrency_limit_envs      r?   r   z Queue._resolve_concurrency_limit   sj     %	11,,,.JNN.-
 -
 
( 	 -2244>>t89991rA   c                p    d}| j                                         D ]}|t          |j                  z  }|S rV   )ry   r   r   rW   )r>   	total_lenevent_queues      r?   __len__zQueue.__len__  sB    	>EEGG 	0 	0K[.///IIrA   bodyr   r)   r*   r+   r'   etuple[bool, str | list[dict[str, Any]], Literal['success', 'error', 'queue_full', 'validator_error']]c           
       K   |j         dS | j        6t          |           | j        k    rdd| j         dt          |            ddfS |j        r/| j        j        |j                 }|j        j        |j                  }n| j        j        |j                  }t          j	        | j        d |          }| 
                    |           |j        t          j        |||d           }|j        J t          j        |j                  }t          j        |j        || j        j        j        	          }t%          d0i d
|j        dd ddd|j        dd dd d|j        d|j        d|j        ddd|j        dg ddd|j        d|j        dd ddd|j        d|j        d |j        }	t9          |j        |	||          }
	 t          j        | j        j        |||	|!           d {V }|                    d"          }|t?          ||          \  }}|du rd|d#fS nE# t@          $ r8}tC          tE          |                     dtE          |          d$fcY d }~S d }~ww xY wt9          |j        |||          }
||
_#        |j        |
j        |_        | j$        4 d {V  |j        | j%        vrtM                      | j%        |j        <   |j        | j'        vrtQ                      | j'        |j        <   	 d d d           d {V  n# 1 d {V swxY w Y   | j'        |j                 )                    |
j*                   |
| j+        |
j*        <   |j        s|
j*        nd |_,        t[          |j.        d%          r	 t_          j/                    }t          j        |||d           }|j        J t          j        |j                  }t          j        |j        || j        j        j        	          }ta                      5  t          j        | j        j        ||||!           d {V }|r||                    d&d          rf| 1                    |
te          |d'(                     t          j        | j        j        ||||!           d {V }|r|                    d&d          fd d d            n# 1 swxY w Y   t_          j/                    |z
  }|| j3        v r| j3        |         j4        nd }| 1                    |
tk          |d'd)||*                     d'|
j*        d+fS # tl          $ r Y nt@          $ r  w xY w	 | j7        |
j8                 }n"# tr          $ r}ts          d,          |d }~ww xY w|j:        ;                    |
           t_          j/                    d-d |j<        |j        d.| j=        |
j*        <   | >                    |
j8        t          |j:                  d/z
             d'|
j*        d+fS )1N)FzNo function index provided.errorFzQueue is full. Max size is z and size is .
queue_fullr   r(   r+   r)   r)   r)   
route_path	root_pathr(   api_nameapi_visibilityundocumentedbatchr1   rS   inputsoutputs
preprocesspostprocessinputs_as_dicttargetsr0   max_batch_sizetracks_progressjsshow_progresshiddenshow_progress_oncancelscollects_event_dataappr   
gr_requestr(   r   r2   validator_errorr   cacheis_generatingToutputr   fullr   r   
used_cachecache_durationra   r   zEvent not found in queue. If you are deploying this Gradio app with multiple replicas, please enable stickiness to ensure that all requests from the same user are routed to the same instance.queued)r9   r   r_   r   r&   r!   rO   )?fn_indexrl   r   r&   rm   state_holderblocks_configfnsr   get_fnr   	validatorcompile_gr_requestr)   get_api_call_pathget_root_urlr   r   r"   r   r   r   r   r   r   r   r   r   r%   call_process_apir   process_validation_responser   printrR   r2   rx   ru   
AsyncQueuerv   rY   rd   r0   rw   r   hasattrr(   r9   r   r   r   r   ra   r   r   ry   r1   KeyErrorrW   appendr   r   broadcast_estimations)r>   r   r)   r+   session_stater(   r   api_route_pathr   validator_fnr   responsevalidation_responseis_validvalidation_datar   cache_startr  ra   r   s                       r?   pushz
Queue.push  s      = @@=$Tdm)C)CVdmVV#d))VVV   	0 K4T5FGM,0?BB/BT488&&r***<#$7!	  J <+++(:4<PPPN#0)+/3  I
 )   <<  .~ hh	
  $t #'$ yy 		 == "E  "00  B  "00 !# 2 2  4!" 'h#$ "$!4!4%& 

'( %'$:$:)L. !	 E.!,!=)#'" " "       LL(( $ '22M+R3 3/X  5((!+-    . . .c!ffc!ffg-------. 	
 
 
$ % 2D, 	J 	J 	J 	J 	J 	J 	J 	J (IIIGQ||1$2CD (FFFDGEE.t/@AI		J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J
 	&t'89==eiHHH.3 +)+;		t25'"" ;	:"ikk(;% 	  
 |///!,!>t|!T!T!T'4 L-"ko7  	
  \\  %0%A KO!#-"+& & &            H # x||OU'K'K ))!4'/(,     *5)E $!%'1!&/* * * $ $ $ $ $ $ # x||OU'K'K               . "&{!: T555 ,R099 
 !!+' $#)'5!)  	 	 	 UY	11!      	=e>RSKK 	 	 	 R 	 	  '''IKK  -+
 +
UY' 	""5#7[=N9O9ORS9STTTUY	))s   	AH& &
I(0-I#I(#I()AL
LLBT0 B(S 4T0  ST0 SA'T0 0
U<UU 
U=(U88U=r   rR   c                \  K   | j                             |          }|r| j        4 d {V  | j        |j                 }	 |j                            |           | j                             |d            n# t          $ r Y nw xY wd d d           d {V  d S # 1 d {V swxY w Y   d S d S r   )	rw   r   r}   ry   r1   rW   removepoprH   )r>   r   r   qs       r?   remove_from_queuezQueue.remove_from_queue  sZ     (,,X66 	'        78LMGNN5))),004@@@@!   D                             	 	s5   B 5A65B6
B BBB
B#&B#c                P    | j         D ]}|                                 g | _         d S r   )r   cancel)r>   tasks     r?   _cancel_asyncio_taskszQueue._cancel_asyncio_tasks  s1    ' 	 	DKKMMMM rA   r   
routes.Appc                    || _         d S r   )r~   )r>   r   s     r?   set_server_appzQueue.set_server_app  s    rA   c                ,    d}| j         D ]	}||dz  }
|S )Nr   r!   )r|   )r>   r`   workers      r?   get_active_worker_countzQueue.get_active_worker_count  s.    & 	 	F!
rA   $tuple[list[Event], bool, str] | Nonec                   t          | j                                                  }t          j        |           |D ]}| j        |         }t          |j                  r|j        |j        |j        k     rt|j        d         j	        }g}|j
        }|r.|fd|j        dd          D             d |j        dz
           z  }|D ]}|j                            |           |||fc S d S )Nr   c                4    g | ]}|j         j         k    |S rO   )r(   ).0r   first_events     r?   
<listcomp>z$Queue.get_events.<locals>.<listcomp>  s2       ! 8{~55 555rA   r!   )r   ry   keysrandomshuffler   rW   rS   rX   r(   r   r   r  )	r>   concurrency_idsr1   r   r   eventsr   r   r1  s	           @r?   
get_eventszQueue.get_events  s>   tBGGIIJJ'''- 	5 	5N=nMK;$%% 5-52[5RRR)/2&>%  5    %0%6qrr%:   4/!33	5 5F $ 4 4E%,,U3333un4444)	5 	5rA   Nonec                p  K   	 | j         st          |           dk    r t          j        | j                   d {V  ;d | j        vr t          j        | j                   d {V  d| j        4 d {V  |                                 }d d d           d {V  n# 1 d {V swxY w Y   |r$|\  }}}|| j        | j                            d           <   | j	        |         }|xj
        dz  c_
        t          j                    }|j        |d         j                                     |           |D ]}d| j        |j                 d<   t#          | j        |||          }t'          ||d         j        |d         j        j        |d         j        |           | j                            |           | j        r|                     |           nt          j        | j                   d {V  | j         d| _         |                                  d S # d| _         |                                  w xY w)Nr   r!   
processingr   T)rz   r   r<   sleepr   r|   r}   r8  indexry   rX   r9   rZ   r(   rd   r   r0   r   process_eventsr    r&   r   r  rg   r  r&  )	r>   event_batchr7  r   r1   r   
start_timer   process_event_tasks	            r?   r   zQueue.start_processing	  s     )	)l %>t99>>!-(<=========t///!-(<=========  + 4 4 4 4 4 4 4 4"&//"3"3K4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4  >4?1FE>EKD$T%5%;%;D%A%AB"&"En"UK33q833!%J26!9<@DDZPPP!' Q QDP,UY7AA)?+VUJ* *& "*q	.q	(q	   '../ABBB( C22>BBB!-(<=========K l %>N  DL&&(((((  DL&&((((s1   A1H 5B
H 
B&&H )B&*EH H5c                Z  K   | j         sd | j        D             }t          |          dk    r t          j        | j                   d{V  K|D ]2}|j        r)|j        r"d|_        |                     ||j                   3t          j        | j                   d{V  | j         dS dS )ah  
        Because progress updates can be very frequent, we do not necessarily want to send a message per update.
        Rather, we check for progress updates at regular intervals, and send a message if there is a pending update.
        Consecutive progress updates between sends will overwrite each other so only the most recent update will be sent.
        c                     g | ]}||D ]}|S r   rO   r0  jobevts      r?   r2  z0Queue.start_progress_updates.<locals>.<listcomp><  s$    XXXcTWScrA   r   NF)	rz   r|   r   r<   r<  r   r4   r3   r   )r>   r7  r   s      r?   r   zQueue.start_progress_updates5  s       , 	FXXT%5XXXF6{{amD$HIIIIIIIII = =) =en =-2E*%%eU^<<<- DEEEEEEEEE , 	F 	F 	F 	F 	FrA   	iterableslist[TrackedIterable] | Nonec           
        |d S | j         D ]z}||D ]r}|j        |k    reg }|D ]D}t          |j        |j        |j        |j        |j                  }|                    |           Et          |          |_        d|_
        s{d S )N)r=  lengthunitr3   desc)progress_dataT)r|   r0   r   r=  rJ  rK  r3   rL  r  r   r4   )r>   r   rG  rE  rF  rM  iterableprogress_units           r?   set_progresszQueue.set_progressI  s    
 F# 	0 	0C{ 0 07h&&8:M$- < <(4"*.#+?!)%-%6!)) ) ) &,,];;;;#2#O#O#OCL+/C(0	0 	0rA   
   Tlogtitlelevel%Literal['info', 'warning', 'success']durationfloat | Nonevisiblec                    d | j         D             }|D ]7}|j        |k    r*t          |||||          }	|                     ||	           8d S )Nc                     g | ]}||D ]}|S r   rO   rD  s      r?   r2  z%Queue.log_message.<locals>.<listcomp>k  s$    TTT#S_PS__#____rA   )rR  rT  rV  rX  rS  )r|   r0   r   r   )
r>   r   rR  rS  rT  rV  rX  r7  r   log_messages
             r?   r[  zQueue.log_messageb  s}     UT!1TTT 		6 		6EyH$$(%#   !!%555		6 		6rA   N)r&   r   r&   c                 K   | j         D ]&}|r"|D ]}|j        |k    s|j        |k    rd|_         '| j        4 d {V  g }| j                                        D ]7}|j        D ]-}|j        |k    s|j        |k    r|                    |           .8|D ]L}| j        |j	                 j        
                    |           | j                            |j        d            M|rR|| j        v rId |D             }| j        |xx         |z  cc<   | j        |         s| j                            |d            d d d           d {V  d S # 1 d {V swxY w Y   d S )NFc                    h | ]	}|j         
S rO   )r0   )r0  r   s     r?   	<setcomp>z%Queue.clean_events.<locals>.<setcomp>  s    ???qu???rA   )r|   r&   r0   r5   r}   ry   r   rW   r  r1   r  rw   r   rv   )	r>   r&   r   job_setrE  events_to_remover   r   removed_idss	            r?   clean_eventszQueue.clean_eventsw  sx      ' 	* 	*G *" * *C'<7737h;N;N$)	# 	K 	K 	K 	K 	K 	K 	K 	K,.#BIIKK 7 7(. 7 7E)\99UY(=R=R(//6667 * > >3E4HIOVV   (,,UY==== K0N N N??.>???.|<<<K<<<5lC K266|TJJJ#	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	Ks   C7E
EEc                   K   | j         sZt          j        | j                   d{V  t	          |           dk    r| j        D ]}|                     |           | j         XdS dS )zQ
        Notify clients about events statuses in the queue periodically.
        Nr   )rz   r<   r<  rk   r   ry   r  )r>   r1   s     r?   r   zQueue.notify_clients  s       , 	?- 56666666664yy1}}&*&I ? ?N..~>>>>	 , 	? 	? 	? 	? 	?rA   r1   afterc                .   d}| j         |         }d}|j        |j        k    rg }|j                                        D ]J\  }}|| j        vrd } n9|j        dk    r
|j        pdn| j        |         j        |fd|D             z  }K|Ft          |          dk    r3t          |          }	t          |	t          j                    z
  d          }t          |j                  D ]\  }
}|j        | j        v r| j        |j                 j        nd }||
|||z   |z   nd }||
|k    r8|                     |t#          |
|t          |j                                       |j        d}|%|#||j        z  }|j        r|pd|j        z  }||z  }d }d S )Nr   rC   c                    g | ]}|z   S rO   rO   )r0  r@  r_   s     r?   r2  z/Queue.broadcast_estimations.<locals>.<listcomp>  s+     ' ' '2<J-' ' 'rA   )rankrank_eta
queue_size)ry   rX   rS   rZ   itemsr   rD   rI   ra   r   minmaxr9   	enumeraterW   r(   r   r   rF   )r>   r1   rd  wait_so_farr   time_till_available_workerexpected_end_timesr(   start_timestime_of_first_completionrg  r   process_time_for_fnrh  deltar_   s                  @r?   r  zQueue.broadcast_estimations  sQ    9.I12"*k.KKK!##.#A#G#G#I#I 
 
KT55515.E=H,,#%=#5ALL#'#;B#?#HL" ' ' ' '@K' ' ' "" *5#>P:Q:QTU:U:U+./A+B+B(-0,ty{{:A. .* %[%677 &	# &	#KD% 8t777 (2;;   '2+.: $k14NNN   }!!%!H[EVAWAW     ,4(-@-L+k.KK? 627a#56E u$"M&	# &	#rA   r   c                <    t          t          |                     S )N)ri  )r   r   rE   s    r?   
get_statuszQueue.get_status  s"     4yy
 
 
 	
rA   c                H   K   | j                                          d {V  dS )Nr=   )r=   wait)r   s    r?   wait_for_eventzQueue.wait_for_event  s1      l!!!!!!!!!xrA   timeoutc                >   K   t          j        |            d {V  dS )Nrz  )r<   r<  )rz  s    r?   rz  zQueue.timeout  s-      mG$$$$$$$$$yrA   Literal['signal', 'timeout']c                  K   t          j        t                              |                     }t          j        t                              |                    }t          j        ||gt           j                   d {V \  }}d |D             }| j                                         t          t          d         |d                   S )Nreturn_whenc                6    g | ]}|                                 S rO   resultr0  ds     r?   r2  z3Queue.wait_for_event_or_timeout.<locals>.<listcomp>       )))q

)))rA   )r=   rz  r   )r<   create_taskr   ry  rz  rx  FIRST_COMPLETEDr=   clearr	   r   )r   rz  t1t2done_s         r?   wait_for_event_or_timeoutzQueue.wait_for_event_or_timeout  s        !5!5e!<!<== w!7!788H/
 
 
 
 
 
 
 
 
a *)D)))G/0$q':::rA   r7  list[Event]timeoutslist[float]tuple[list[Event], list[Event]]c           	       K   g }t          | |d          D ]E\  }}|                    t          j        t                              ||                               Ft          j        |t          j                   d {V \  }}d |D             }g }g }t          || d          D ]6\  }	}|	dk    r|                    |           !|                    |           7||fS )NF)strictr~  c                6    g | ]}|                                 S rO   r  r  s     r?   r2  z(Queue.wait_for_batch.<locals>.<listcomp>	  r  rA   r=   )zipr  r<   r  r   r  rx  ALL_COMPLETED)
r7  r  tasksr   rz  r  r  awake_eventsclosed_eventsr  s
             r?   wait_for_batchzQueue.wait_for_batch  s0      !&(5AAA 	 	NE7LL#E$C$CE7$S$STT     -
 
 
 
 
 
 
 
 
a *)D))) ve<<< 	, 	,MFE!!##E****$$U++++]**rA   r   
begin_timec                t  K   g }|d         j         }d}	 |D ]]}|j        rT|                     |t          || j        v r| j        |         j        nd                      |                    |           ^|s*	 ddlm}m	}	m
}
 |r! |
            }||	                    |           | j        |d         j                 }|xj        dz  c_        |j        |         }||v r|                    |           	 d | j        | j                            |          <   n# t&          $ r Y nw xY w|D ]u}|                     |j                   d {V  ||v r|rdnd| j        |j                 d<   nd	| j        |j                 d<   t/          | j        | j                   d {V  vd S |}|d         j        }|t'          d
          |d         j        }|s|d         j        nd |_        	 |d         j        |_        n# t&          $ r Y nw xY w|rAt;          t=          d |D             ddi          |_        |d         j        |_        d|_        | j         }|tC          d          tE          j#        |||d           }|j        J tE          j$        |j                  }tE          j%        |j        ||j&                  }d}ddlm}m'}m(} |rr ||d         j        |j)        ptU          |j                   |d         j+                  }tY          j-                    |d         j.        z
  dz  |_/         ||           nd }	 tY          j-                    }tE          j0        |||||           d {V }tY          j-                    }||z
  }d }|D ]'}|xj1        ||z
  z  c_1        |j2        r|j3         |d<   (n# tB          $ r}ti          |tj                    r|j6        rto          j8                     d }|}|D ]}ts          ||:                                j;                  }|                     |ty          ||=                    dd          d                     t/          | j        | j                   d {V  Y d }~nd }~ww xY w|r|=                    dd          r|}|}|r|=                    dd          rstY          j-                    }|}|}|D ]u}|                     |t}          |j2        st~          j@        nt~          jA        ||d u|jB        sd n%|j2        rt          t          |jB                  |z
  nd                      vd |D             }|s*	 ddlm}m	}	m
}
 |r! |
            }||	                    |           | j        |d         j                 }|xj        dz  c_        |j        |         }||v r|                    |           	 d | j        | j                            |          <   n# t&          $ r Y nw xY w|D ]u}|                     |j                   d {V  ||v r|rdnd| j        |j                 d<   nd	| j        |j                 d<   t/          | j        | j                   d {V  vd S 	 tY          j-                    }|d         j2        r|t          F                    |t          t          |jB        pd          |z
  gt          |          z             d {V \  }}|D ]'} |                     | ty          |d                     (|sn
t          t          |d         j                  }|r(t;          t=          d |D             ddi          |_        tE          j0        |||||           d {V }tY          j-                    }|D ]'}|xj1        ||z
  z  c_1        |j2        r|j3         |d<   (nJ# tB          $ r=}ti          |tj                    r|j6        rto          j8                     d }|}Y d }~nd }~ww xY w|r|=                    dd          s|rd}|}!n-d}|p|}"ts          |"|:                                j;                  }!|!}#|r.|!=                    d          s|r|=                    d          r|}#|r|#=                    d          nd }$|$d v rt          t          d          |$          nd }$|D ]P}|                     |ty          |!||$|#=                    d!          |#=                    d"          #                     Qn|rt          jL        |          }!t          |          D ]\  }}|r9d$|!v r5t;          t=          |=                    d$          ddi          |         |!d$<   |d u}|r|!=                    d          nd }$|$d v rt          t          d          |$          nd }$|                     |ty          |!||$|!=                    d!          |!=                    d"          #                     tY          j,                    }%|n|d         j2        s|%|z
  n|}&|=                    d          s+| j        |d         j                                      |&           |D ]}|&| j        |j                 d%<   nF# tB          $ r9}ti          |tj                    r|j6        rto          j8                     Y d }~nd }~ww xY wddlm}m	}	m
}
 |r! |
            }||	                    |           | j        |d         j                 }|xj        dz  c_        |j        |         }||v r|                    |           	 d | j        | j                            |          <   n# t&          $ r Y nw xY w|D ]u}|                     |j                   d {V  ||v r|rdnd| j        |j                 d<   nd	| j        |j                 d<   t/          | j        | j                   d {V  vd S # ddlm}m	}	m
}
 |r! |
            }||	                    |           | j        |d         j                 }|xj        dz  c_        |j        |         }||v r|                    |           	 d | j        | j                            |          <   n# t&          $ r Y nw xY w|D ]u}|                     |j                   d {V  ||v r|rdnd| j        |j                 d<   nd	| j        |j                 d<   t/          | j        | j                   d {V  vw xY w)&Nr   F)eta)PROFILING_ENABLED	collectorget_current_tracer!   r   r   r   	cancelledzNo event datac                4    g | ]}|j         	|j         j         S rO   r2   r0  r   s     r?   r2  z(Queue.process_events.<locals>.<listcomp>6  s#    LLLeL%*/LLLrA   r  TServer app has not been set.r   r   r   )r  RequestTraceset_current_trace)r   r   r&   i  r   r   rS  r   )r   rS  r   )msgr   r   rI   c                     g | ]}|j         	|S rO   )r5   r  s     r?   r2  z(Queue.process_events.<locals>.<listcomp>  s    #S#S#Seu{#SE#S#S#SrA      r   c                4    g | ]}|j         	|j         j         S rO   r  r  s     r?   r2  z(Queue.process_events.<locals>.<listcomp>  s4     && && &&,1+0:&&(-
&& && &&rA   r  )r   partialrV  average_durationr   r2   r_   )Nr(   r5   r   r   r   ra   r  gradio.profilingr  r  r  rd   ry   r1   rX   rZ   r  r|   r=  rH   reset_iteratorsr0   r   r
   r   r2   r+   r   r)   r   r  batchedr~   r   r   r
  r  r  r   r  r  r   rR   r&   r9   r:   r;   queue_wait_msr  r8   rF   rJ   
isinstancer   print_exception	traceback	print_excr   
get_blocks
show_errorr   r   r   r   process_generatingprocess_streamingrI   r	   rj   r   r  rb   r   r   r   r   deepcopyrm  )'r>   r7  r   r  r  r(   r   r   r  r  r  tracer   rq  r   r+   r   r   r  r   first_iterationr  r  r   r  enderrr   contentold_responseold_errr  closed_eventr   r   cache_sourcer  end_timerV  s'                                          r?   r>  zQueue.process_events  s      %'AY\_	U 
/ 
/; 	/%%,!T%=== !% 8 < E E!%     !''... b YXXXXXXXXX  )))++$MM%(((=fQi>VWK++q0++%8<K[(("":...CG !1!7!7!?!?@@   
    U U
 **59555555555L((%,:		( (3H== ALD(3H=t=t?STTTTTTTTTTU UG "F!9>D| 111ay)H16@F1IMMDDM%ay0     $ LLvLLL$  	  &ay0#/C{ >???$7!	  J <+++(:4<PPPN#0)-  I
  O          ! 	$#AY]K53ru::!'!7  
 (,~'7'7&):P'PTX&X#!!%(((( Y((!,!=)'" " "       n&&"%+) J JENNcEk1NN J8=8I4I1J
  Y Y Y!!U++ *q/@ *')))) 
Y 
YE+C1A1A1LMMG%%/#*")++gw"?"?$)     #4#A4CWXXXXXXXXXX
Y 
Y 
Y 
Y 
YY   EHLL%@@ E' H 8<<#G#G H  N,,E#+L!G!-  ))!4',%EM$D$D%2%D'3(4D(@')},*44 $)?&*T#r}%=%=%O%O%)      $T#S|#S#S#SL' x YXXXXXXXXX  )))++$MM%(((=fQi>VWK++q0++%8<K[(("":...CG !1!7!7!?!?@@   
    U U
 **59555555555L((%,:		( (3H== ALD(3H=t=t?STTTTTTTTTTU U_1  $ 0 0'?4 "@E@T@T ,
 "&eR]-@b!A!AO!S T"%l"3"3!4A A ; ; ; ; ; ;7L- 1> " " $ 1 1$0$;/7%& %& %&!" !" !" !"  , "!#$7a9MNN  
(, #&& &&5;&& && &&!" ,1!" !"	) 	)DI *5)E #!%'1!&/* * * $ $ $ $ $ $ #n..%1 R RE!NNcEk9NN$ R@E@Q<Q 9R %      )!U33 2q7H 2%/111#'	 I  H 8<<#G#G H T  O"G%FF#GN7E*5#..2B2B2MNNF%0"JJ|440 %0 %((66	0 $0L?FP\--l;;;D
 "%888 !23Z@@@ 
 * 
 
E%%/#)$+'1+7+;+;J+G+G%1%5%56H%I%I  	 	 	 	
  x00 ), 7 7  HAu 6!1!1)-c8<<3G3G.VPU.V.V)W)W*v 'd2G=D!NL!9!9!9$J &)<<< W%67DDD! 
 %%/#)$+'1+1::j+A+A%+ZZ0B%C%C  	 	 	 	 y{{H# "!9.)Hz))( 
  ||L11 I,VAY\:>>xHHH# O OEFND(3NCC 	& 	& 	&a'' &1+< &#%%%	& YXXXXXXXXX  )))++$MM%(((=fQi>VWK++q0++%8<K[(("":...CG !1!7!7!?!?@@   
    U U
 **59555555555L((%,:		( (3H== ALD(3H=t=t?STTTTTTTTTTU U) YXXXXXXXXX  )))++$MM%(((=fQi>VWK++q0++%8<K[(("":...CG !1!7!7!?!?@@   
    U U
 **59555555555L((%,:		( (3H== ALD(3H=t=t?STTTTTTTTTTUs  A#j 5"D 
D%$D%"Aj &G9 8j 9
Hj HD<j A6N: 9j :
RB9R=j RCj "X   
XXB_ )j +B&_ j 
`3`j `Jj p 
k")/kp k""p !"n 
nnA<t7"r.-t7.
r;8t7:r;;A<t7c                Z  K   | j         }|t          d          ||j        vrd S |j        4 d {V  	 t	          |j        |                    d {V  n# t          $ r Y nw xY w|j        |= |j                            |           d d d           d {V  n# 1 d {V swxY w Y   d S )Nr  )r~   r   	iteratorslockr   iterators_to_resetrd   )r>   r   r   s      r?   r  zQueue.reset_iterators:  sa     o;:;;;3=((F8 	1 	1 	1 	1 	1 	1 	1 	1*3=+BCCCCCCCCCC   h'"&&x000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	s4   B AB
A# B"A##%B
B$'B$)rf   )rg   rh   ri   rj   rk   rb   rl   rT   rm   r#   rn   ro   )r   r"   )r   r%   r   r   )rn   ro   r   rT   )r   r   r)   r*   r+   r'   r   r   )r   rR   )r   r'  )r   rj   )r   r-  )r   r9  )r   rR   rG  rH  )rQ  T)r   rR   rR  rR   rS  rR   rT  rU  rV  rW  rX  rh   )r&   r'   r   r'   r   r9  r   )r1   rR   rd  rT   r   r9  )r   r   )r   r%   r   rR   )rz  rb   r   rR   )r   r%   rz  rb   r   r|  )r7  r  r  r  r   r  )r7  r  r   rh   r  rb   r   r9  )!rK   rL   rM   r@   staticmethodr   r   r   r   r   r   r   r   r  r"  r&  r)  r,  r8  r   r   rP  r[  rb  r   r  rv  ry  rz  r  r  r>  r  rO   rA   r?   r   r   t   s        FO)
 )
 )
 )
 )
V   \&3 3 388 8 8K K K K*  	+ 	+ 	+ 	+   ,  }* }* }* }*~	 	 	 	! ! !
      5 5 5 52*) *) *) *)XF F F F(0 0 0 0> "$6 6 6 6 6, -1K K K K K K8? ? ? ? 8<@# @# @# @# @#D
 
 
 

    \    \ ; ; ; \; + + + \+,eU eU eU eUN	     rA   r   r  %list[dict[str, Any]] | dict[str, Any]r(   BlockFunction | Noner   !tuple[bool, list[dict[str, Any]]]c                   g }g }|rF|j         r?t          j        |j                   }t          |j                                                  }t          | t                    rt          |           D ]\  }}t          |t                    rX|	                    dd           dk    r>|t          |          k     r||         nd| }i |d|i}|                    |           r|                    ddd           n[t          | t                    r.| 	                    dd           d	u r|                    |            n|                    ddd           t          d
 |D                       |fS )N__type__validate
parameter_parameter_nameT )r  messager  Fc              3  F   K   | ]}|                     d d          du V  dS )r  NT)r   )r0  xs     r?   	<genexpr>z.process_validation_response.<locals>.<genexpr>l  sF        ,-j$4'     rA   )r(   inspect	signaturer   
parametersr3  r  rm  dictr   r   r  all)	r  r(   r  param_namessigir2   
param_namedata_with_names	            r?   r  r  L  s    -/OK	 2be 2&&3>..0011%t,, B !455 	J 	JGAt$%% J$((:t*D*D
*R*R&'#k*:*:&:&:KNN@PQ@P@P  "HD!G*:J!G!G&&~6666&&DR'H'HIIII	J 	&--B##J55>>	
 	
 	
 	
 	DR@@AAA  1@     rA   r   )r  r  r(   r  r   r  )?
__future__r   r<   r   r  r   r   r4  r9   r  r-   r   r  collectionsr   typingr   r   r   r	   fastapinumpyr   anyio.to_threadr
   gradior   r   gradio.cachingr   r   gradio.data_classesr   gradio.exceptionsr   gradio.helpersr   gradio.server_messagesr   r   r   r   r   r   r   r   r   gradio.utilsr   r   r   r   r   r    block_functionr"   gradio.block_functiongradio.blocksr#   r%   rQ   r\   r  rO   rA   r?   <module>r     s   " " " " " "    				         ' ' ' ' ' ' # # # # # # 4 4 4 4 4 4 4 4 4 4 4 4      $ $ $ $ $ $ & & & & & & & & 5 5 5 5 5 5 5 5      $ # # # # # * * * * * *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                * ) ) ) ) ) %333333$$$$$$$3 $3 $3 $3 $3 $3 $3 $3N
 
 
 
 
 
 
 
	7 	7 	7 	7 	7 	7 	7 	7U U U U U U U Ut  $" " " " " " "rA   