
    Pi                       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 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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 erd dl6m5Z5 d dl7m8Z8  G d d          Z9 G d d          Z: G d d          Z; G d d          Z	 dd dZ<dS )!    )annotationsN)defaultdict)Queue)TYPE_CHECKINGAnyLiteralcast)run_sync)route_utilsroutes)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                0   t          j                    j        | _        |p| j        | _        || _        || _        || _        |j        | _        d | _	        d | _
        d| _        d| _        d| _        d| _        d| _        t!          j                    | _        d S )NFTr   )uuiduuid4hex_idr$   r&   r'   r)   concurrency_iddataprogressprogress_pendingaliveclosedn_callsrun_timeasyncior#   signal)selfr$   r&   r'   r)   s        c/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/queueing.py__init__zEvent.__init__6   s     :<<#!-!9  /04	04&+
 moo    c                "    | j         j        dk    S )Nstream)r&   
connectionr9   s    r:   	streamingzEvent.streamingL   s    w!X--r<   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)rA   
ValueErrorr4   r&   
time_limitr6   r@   s    r:   is_finishedzEvent.is_finishedP   sM    ~ 	WUVVV; 	47%5} 222r<   N)r$   r%   r&   r    r'   r(   r)   r%   )__name__
__module____qualname__r;   propertyrA   rE    r<   r:   r#   r#   5   s^        & & & &, . . X. 3 3 X3 3 3r<   r#   c                      e Zd ZddZdS )
EventQueuer/   strconcurrency_limit
int | Nonec                p    g | _         || _        || _        d| _        t	          t
                    | _        d S Nr   )queuer/   rN   current_concurrencyr   setstart_times_per_fn)r9   r/   rN   s      r:   r;   zEventQueue.__init__\   s>    "$
,!2#$ JUK
 K
r<   N)r/   rM   rN   rO   )rF   rG   rH   r;   rJ   r<   r:   rL   rL   [   s(        
 
 
 
 
 
r<   rL   c                      e Zd Zd ZddZdS )ProcessTimec                0    d| _         d| _        d| _        d S rQ   process_timecountavg_timer@   s    r:   r;   zProcessTime.__init__g   s    
r<   timefloatc                n    | xj         |z  c_         | xj        dz  c_        | j         | j        z  | _        d S )Nr   rY   )r9   r]   s     r:   addzProcessTime.addl   s:    T!

a

)DJ6r<   N)r]   r^   )rF   rG   rH   r;   r`   rJ   r<   r:   rW   rW   f   s7          
7 7 7 7 7 7r<   rW   c                  H   e Zd Z	 dXdYdZed             Zd Zd ZdZdZd Z	d[dZ
d\dZd Zd]d%Zd& Zd^d)Zd_d*Zd`d,Zdad.Zdad/Zdbd4Z	 	 dcddd>Zd?d?d@dedBZdadCZ	 dfdgdFZdhdHZedidI            ZedjdK            ZedkdM            ZedldS            ZdmdVZdndWZd?S )or   not_setlive_updatesboolconcurrency_countintupdate_intervalsr^   max_sizerO   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_countrg   active_jobsdelete_lock
server_appr   rW   process_time_per_fnrc   platformsystemsleep_when_freeprogress_update_sleep_when_freerh   ri   _asyncio_tasks_resolve_concurrency_limitrj   event_analyticscached_event_analytics_summaryevent_count_at_last_cacherf   osgetenvANAYLTICS_CACHE_FREQUENCY)r9   rc   re   rg   rh   ri   rj   s          r:   r;   zQueue.__init__s   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#>>*
 *
&&&r<   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	ExceptionrM   )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functionrn   statussuccessfailedr   rZ   )2   Z   c   r      )50th90th99th)success_rateprocess_time_percentilestotal_requests)lenr   r   r   groupbyr   r   sum
percentileshaper   )r9   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2r<   c                    d g| j         z  | _        t          | j                   t          | j                   | j        st          | j                   d S d S N)rw   rx   r   start_processingstart_progress_updatesrc   notify_clientsr@   s    r:   startzQueue.start   sb     6D$99t4555t:;;;  	8"4#677777	8 	8r<   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)r/   rN   rj   ru   rL   )r9   r   r/   rN   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Mr<   c                    d| _         d S )NT)rv   r@   s    r:   closezQueue.close   s    r<   eventr#   event_messager   c                ~    |j         sd S |j        |_        | j        |j                 }|                    |           d S r   )r3   r.   event_idrq   r$   
put_nowait)r9   r   r   messagess       r:   send_messagezQueue.send_message   sG    
 { 	F!&4U5GHM*****r<   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.queu()`.
        rb    GRADIO_DEFAULT_CONCURRENCY_LIMITnoneNr   )r   environgetlowerrf   )r9   rj   default_concurrency_limit_envs      r:   r   z Queue._resolve_concurrency_limit   sj     %	11,,,.JNN.-
 -
 
( 	 -2244>>t89991r<   c                p    d}| j                                         D ]}|t          |j                  z  }|S rQ   )ru   r   r   rR   )r9   	total_lenevent_queues      r:   __len__zQueue.__len__  sB    	>EEGG 	0 	0K[.///IIr<   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%          d+i 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,        |
j-                 }n"# t\          $ r}t]          d%          |d }~ww xY w|j/        0                    |
           tc          j1                    d&d |j2        |j        d'| j3        |
j*        <   | 4                    |
j-        t          |j/                  d(z
             d)|
j*        d*fS ),N)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batchr/   rN   inputsoutputs
preprocesspostprocessinputs_as_dicttargetsr.   max_batch_sizetracks_progressjsshow_progresshiddenshow_progress_oncancelscollects_event_dataappr   
gr_requestr&   r   r0   validator_errorr   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)r]   r   rZ   r   r$   r   Tr   rJ   )5fn_indexrh   r   r$   ri   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   printrM   r0   rt   rq   ThreadQueuerr   rT   r`   r.   rs   ru   r/   KeyErrorrR   appendr]   r   r   broadcast_estimations)r9   r   r'   r)   session_stater&   r   api_route_pathr   validator_fnr   responsevalidation_responseis_validvalidation_datar   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IGR}}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 +	=e>RSKK 	 	 	 R 	 	  '''IKK  -+
 +
UY' 	""5#7[=N9O9ORS9STTTUY	))sJ   	AH& &
I(0-I#I(#I()AL
LLM- -
N7NNc                P    | j         D ]}|                                 g | _         d S r   )r   cancel)r9   tasks     r:   _cancel_asyncio_taskszQueue._cancel_asyncio_tasks  s1    ' 	 	DKKMMMM r<   r   
routes.Appc                    || _         d S r   )rz   )r9   r   s     r:   set_server_appzQueue.set_server_app  s    r<   c                ,    d}| j         D ]	}||dz  }
|S )Nr   r   )rx   )r9   r[   workers      r:   get_active_worker_countzQueue.get_active_worker_count  s.    & 	 	F!
r<   $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 rJ   )r&   ).0r   first_events     r:   
<listcomp>z$Queue.get_events.<locals>.<listcomp>  s2       ! 8{~55 555r<   r   )r   ru   keysrandomshuffler   rR   rN   rS   r&   r   r   remove)	r9   concurrency_idsr/   r   r   eventsr   r   r  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 	5r<   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)rv   r   r7   sleepr~   rx   ry   r&  indexru   rS   r]   rU   r&   r`   r   r.   r   process_eventsr   r$   r   r  rc   r  r  )	r9   event_batchr%  r   r/   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   rJ   r  jobevts      r:   r  z0Queue.start_progress_updates.<locals>.<listcomp>  s$    XXXcTWScr<   r   NF)	rv   rx   r   r7   r*  r   r2   r1   r   )r9   r%  r   s      r:   r   zQueue.start_progress_updates  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r<   r   rM   	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unitr1   desc)progress_dataT)rx   r.   r   r+  r8  r9  r1   r:  r  r   r2   )r9   r   r5  r3  r4  r;  iterableprogress_units           r:   set_progresszQueue.set_progress  s    
 F# 	0 	0C{ 0 07h&&8:M$- < <(4"*.#+?!)%-%6!)) ) ) &,,];;;;#2#O#O#OCL+/C(0	0 	0r<   
   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   rJ   r2  s      r:   r  z%Queue.log_message.<locals>.<listcomp>  s$    TTT#S_PS__#____r<   )r@  rB  rD  rF  rA  )rx   r.   r   r   )
r9   r   r@  rA  rB  rD  rF  r%  r   log_messages
             r:   rI  zQueue.log_message  s}     UT!1TTT 		6 		6EyH$$(%#   !!%555		6 		6r<   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 ],}| j        |j	                 j        
                    |           -	 d d d           d {V  d S # 1 d {V swxY w Y   d S )NF)rx   r$   r.   r3   ry   ru   r   rR   r  r/   r#  )r9   r$   r   job_setr3  events_to_remover   r   s           r:   clean_eventszQueue.clean_events+  s      ' 	* 	*G *" * *C'<7737h;N;N$)	# 
	 
	 
	 
	 
	 
	 
	 
	,.#BIIKK 7 7(. 7 7E)\99UY(=R=R(//6667 *  3E4HIOVV   
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	s   BC
C"C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   )rv   r7   r*  rg   r   ru   r  )r9   r/   s     r:   r   zQueue.notify_clients@  s       , 	?- 56666666664yy1}}&*&I ? ?N..~>>>>	 , 	? 	? 	? 	? 	?r<   r/   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   r>   c                    g | ]}|z   S rJ   rJ   )r  r.  rZ   s     r:   r  z/Queue.broadcast_estimations.<locals>.<listcomp>[  s+     ' ' '2<J-' ' 'r<   )rankrank_eta
queue_size)ru   rS   rN   rU   itemsr{   r?   rD   r\   r   minmaxr]   	enumeraterR   r&   r   r   rA   )r9   r/   rO  wait_so_farr   time_till_available_workerexpected_end_timesr&   start_timestime_of_first_completionrR  r   process_time_for_fnrS  deltarZ   s                  @r:   r  zQueue.broadcast_estimationsJ  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&	# &	#r<   r   c                <    t          t          |                     S )N)rT  )r   r   r@   s    r:   
get_statuszQueue.get_status  s"     4yy
 
 
 	
r<   c                H   K   | j                                          d {V  dS )Nr8   )r8   wait)r   s    r:   wait_for_eventzQueue.wait_for_event  s1      l!!!!!!!!!xr<   timeoutc                >   K   t          j        |            d {V  dS )Nre  )r7   r*  )re  s    r:   re  zQueue.timeout  s-      mG$$$$$$$$$yr<   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 rJ   resultr  ds     r:   r  z3Queue.wait_for_event_or_timeout.<locals>.<listcomp>       )))q

)))r<   )r8   re  r   )r7   create_taskr   rd  re  rc  FIRST_COMPLETEDr8   clearr	   r   )r   re  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':::r<   r%  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)strictri  c                6    g | ]}|                                 S rJ   rl  rn  s     r:   r  z(Queue.wait_for_batch.<locals>.<listcomp>  rp  r<   r8   )zipr  r7   rq  r   rx  rc  ALL_COMPLETED)
r%  rz  tasksr   re  rv  rw  awake_eventsclosed_eventsrm  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++++]**r<   r   
begin_timec                  K   g }|d         j         }d}	 |D ]]}|j        rT|                     |t          || j        v r| j        |         j        nd                      |                    |           ^|s	 | 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|rAt1          t3          d
 |D             ddi          |
_        |d         j        |
_        d|
_        | j        }|t9          d          t;          j        |
||d           }|
j        J t;          j        |
j                  }t;          j         |
j        ||j!                  }d}	 tE          j#                    }t;          j$        ||
|||           d {V }tE          j#                    }||z
  }d }|D ]'}|xj%        ||z
  z  c_%        |j&        r|j'         |d<   (n# t8          $ r}tQ          |tR                    r|j*        rtW          j,                     d }|}|D ]}t[          ||.                                j/                  }|                     |ta          ||1                    dd          d                     t%          | j        | j                   d {V  Y d }~nd }~ww xY w|r|1                    dd          r|}|}|r\|1                    dd          rEtE          j#                    }|}|}|D ]u}|                     |te          |j&        stf          j4        ntf          j5        ||d u|j6        sd n%|j&        rto          tp          |j6                  |z
  nd                      vd |D             }|s	 | 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 	 tE          j#                    }|d         j&        r|tr          :                    |to          tv          |j6        pd          |z
  gty          |          z             d {V \  }}|D ]'}|                     |ta          |d                     (|sn
to          tz          |d         j                  }
|r(t1          t3          d |D             ddi          |
_        t;          j$        ||
|||           d {V }tE          j#                    }|D ]'}|xj%        ||z
  z  c_%        |j&        r|j'         |d<   (nJ# t8          $ r=}tQ          |tR                    r|j*        rtW          j,                     d }|}Y d }~nd }~ww xY w|r|1                    dd          E|rd}|}n-d}|p|}t[          ||.                                j/                  }|D ]'}|                     |ta          ||                     (n|rt}          j?        |          }t          |          D ]i\  }}|r9d|v r5t1          t3          |1                    d          ddi          |         |d<   |d u}|                     |ta          ||                     jtE          j"                    }|Y|d         j&        s||z
  n|}| j        |d         j                  A                    |           |D ]}|| j        |j                 d<   nF# t8          $ r9}tQ          |tR                    r|j*        rtW          j,                     Y d }~nd }~ww xY w| 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 # | 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)etar   r   r   r   	cancelledzNo event datac                4    g | ]}|j         	|j         j         S rJ   r0   r  r   s     r:   r  z(Queue.process_events.<locals>.<listcomp>  s#    LLLeL%*/LLLr<   r~  TServer app has not been set.r   r   r   r   is_generatingrA  r   )outputrA  r   )msgr  r   rD   c                     g | ]}|j         	|S rJ   )r3   r  s     r:   r  z(Queue.process_events.<locals>.<listcomp>4  s    #S#S#Seu{#SE#S#S#Sr<      )r  r   c                4    g | ]}|j         	|j         j         S rJ   r  r  s     r:   r  z(Queue.process_events.<locals>.<listcomp>P  s4     && && &&,1+0:&&(-
&& && &&r<   r0   rZ   )Br&   r3   r   r   r{   r\   r  ru   r/   rS   rU   r#  rx   r+  rC   reset_iteratorsr.   r   r
   r   r0   r)   r   r'   r   r  batchedrz   r   r   r   r   r   r   r]   	monotonicr  r6   rA   rE   
isinstancer   print_exception	traceback	print_excr   
get_blocks
show_errorr   r   r   r   process_generatingprocess_streamingrD   r	   rf   r   r  r^   r   r   r   deepcopyrX  r`   )r9   r%  r   r  r  r&   r   r   r   r\  r   r)   r   r   r	  r   first_iterationr   r  enderrr   contentold_responseold_errr  closed_eventr  r   end_timerD  s                                  r:   r,  zQueue.process_events  s      %'AY\g	U 
/ 
/; 	/%%,!T%=== !% 8 < E E!%     !''... @ =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W "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 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   gHLL%@@ g' 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' z =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S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)  E%%6fgVVV   
  x00 ), 7 7  HAu 6!1!1)-c8<<3G3G.VPU.V.V)W)W*v 'd2G%%/#)$+      y{{H# "!9.)Hz))( 
 (6::8DDD# O OEFND(3NCC 	& 	& 	&a'' &1+< &#%%%	& =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 =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"b6 "C* *
C76C74Ab6 8G 
b6 
Gb6 GB<b6 A6L b6 
OB9Ob6 OCb6 "T$ $
T10T1/B[6 b6 B&[6 5b6 6
\= 3\83b6 8\==E8b6 5g8 6
c9 /c4/g8 4c99g8 "e. .
e;:e;8Ak4"i+*k4+
i85k47i88A<k4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  )rz   r   	iteratorslockr   iterators_to_resetr`   )r9   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$)rb   )rc   rd   re   rf   rg   r^   rh   rO   ri   r!   rj   rk   )r   r    )r   r#   r   r   )rj   rk   r   rO   )r   r   r'   r(   r)   r%   r   r   )r   r  )r   rf   )r   r  )r   r'  )r   rM   r5  r6  )r?  T)r   rM   r@  rM   rA  rM   rB  rC  rD  rE  rF  rd   )r$   r%   r   r%   r   r'  r   )r/   rM   rO  rO   r   r'  )r   r   )r   r#   r   rM   )re  r^   r   rM   )r   r#   re  r^   r   rg  )r%  ry  rz  r{  r   r|  )r%  ry  r   rd   r  r^   r   r'  )r   rM   ) rF   rG   rH   r;   staticmethodr   r   r   r   r   r   r   r   r  r  r  r  r&  r   r   r>  rI  rM  r   r  ra  rd  re  rx  r  r,  r  rJ   r<   r:   r   r   r   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     *? ? ? ? 8<@# @# @# @# @#D
 
 
 

    \    \ ; ; ; \; + + + \+,mU mU mU mU^     r<   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   )r  xs     r:   	<genexpr>z.process_validation_response.<locals>.<genexpr>  sF        ,-j$4'     r<   )r&   inspect	signaturer   
parametersr   r  rX  dictr   r   r  all)	r  r&   r  param_namessigir0   
param_namedata_with_names	            r:   r  r    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 	?D))B
D11U::	
 	
 	
 	
 	DR@@AAA  1@     r<   r   )r  r  r&   r  r   r  )=
__future__r   r7   r   r  r   r|   r!  r]   r  r+   collectionsr   rR   r   r  typingr   r   r   r	   fastapinumpyr   anyio.to_threadr
   gradior   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#   rL   rW   r  rJ   r<   r:   <module>r     s   " " " " " "    				         # # # # # # & & & & & & 4 4 4 4 4 4 4 4 4 4 4 4      $ $ $ $ $ $ & & & & & & & &      $ # # # # # * * * * * *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                * ) ) ) ) ) %333333$$$$$$#3 #3 #3 #3 #3 #3 #3 #3L
 
 
 
 
 
 
 
	7 	7 	7 	7 	7 	7 	7 	7L L L L L L L Lb  $" " " " " " "r<   