o
    e                     @  s   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
mZmZ d dlmZ d dlmZ d dlmZ d d	lmZ erFd d
lmZ ed G dd dZe G dd dZG dd deZd)ddZd*d!d"Zd+d#d$Zd,d'd(ZdS )-    )annotationsN)deepcopy)TYPE_CHECKINGOptionalUnion)documentset_documentation_group)utils)PredictBody)Error)	EventData)AppZroutesc                   @  sf   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
dd ZdddZdddZdS )Objz
    Using a class to convert dictionaries into objects. Used by the `Request` class.
    Credit: https://www.geeksforgeeks.org/convert-nested-python-dictionary-to-object/
    c                 C  sD   | j | | D ]\}}t|ttfrt|}t| || q
d S N)__dict__updateitems
isinstancedictlistr   setattr)selfZdict_keyvalue r   BD:\Projects\ConvertPro\env\Lib\site-packages\gradio/route_utils.py__init__   s   zObj.__init__c                 C  s
   | j | S r   r   )r   itemr   r   r   __getitem__"      
zObj.__getitem__c                 C  s   || j |< d S r   r   r   r   r   r   r   r   __setitem__%   s   zObj.__setitem__c                 c  s>    | j  D ]\}}t|tr|t|fV  q||fV  qd S r   )r   r   r   r   r   )r   r   r   r   r   r   __iter__(   s   
zObj.__iter__returnboolc                 C  s:   || j v rdS | j  D ]}t|tr||v r dS qdS )NTF)r   valuesr   r   r!   r   r   r   __contains__/   s   
zObj.__contains__c                 C  
   | j  S r   )r   keysr   r   r   r   r)   7   r    zObj.keysc                 C  r(   r   )r   r&   r*   r   r   r   r&   :   r    z
Obj.valuesc                 C  r(   r   )r   r   r*   r   r   r   r   =   r    z	Obj.itemsstrc                 C  
   t | jS r   r+   r   r*   r   r   r   __str__@   r    zObj.__str__c                 C  r,   r   r-   r*   r   r   r   __repr__C   r    zObj.__repr__N)r$   r%   )r$   r+   )__name__
__module____qualname____doc__r   r   r"   r#   r'   r)   r&   r   r.   r/   r   r   r   r   r      s    

r   c                   @  s0   e Zd ZdZ		ddddZd	d
 Zdd ZdS )Requesta  
    A Gradio request object that can be used to access the request headers, cookies,
    query parameters and other information about the request from within the prediction
    function. The class is a thin wrapper around the fastapi.Request class. Attributes
    of this class include: `headers`, `client`, `query_params`, and `path_params`. If
    auth is enabled, the `username` attribute can be used to get the logged in user.
    Example:
        import gradio as gr
        def echo(name, request: gr.Request):
            print("Request headers dictionary:", request.headers)
            print("IP address:", request.client.host)
            return name
        io = gr.Interface(echo, "textbox", "textbox").launch()
    Nrequestfastapi.Request | Noneusername
str | Nonec                 K  s   || _ || _|| _dS )z
        Can be instantiated with either a fastapi.Request or by manually passing in
        attributes (needed for websocket-based queueing).
        Parameters:
            request: A fastapi.Request
        N)r5   r7   kwargs)r   r5   r7   r9   r   r   r   r   X   s   
zRequest.__init__c                 C  s"   t |trtjt|tdS |S )N)object_hook)r   r   jsonloadsdumpsr   )r   dr   r   r   dict_to_objh   s   
zRequest.dict_to_objc              
   C  s\   | j r| t| j |S z| j| }W n ty( } z	td| d|d }~ww | |S )Nz#'Request' object has no attribute '')r5   r?   getattrr9   KeyErrorAttributeError)r   nameobjZker   r   r   __getattr__n   s   

zRequest.__getattr__)NN)r5   r6   r7   r8   )r0   r1   r2   r3   r   r?   rF   r   r   r   r   r4   G   s    r4   c                   @  s   e Zd ZdS )FnIndexInferErrorN)r0   r1   r2   r   r   r   r   rG   {   s    rG   appr   api_namer+   bodyr
   r$   intc                 C  sL   |j d u r#t|  jD ]\}}|d |kr|  S qtd| d|j S )NrI   z&Could not infer fn_index for api_name .)fn_index	enumerate
get_blocksdependenciesrG   )rH   rI   rJ   ifnr   r   r   infer_fn_index   s   
rS   fn_index_inferredr7   Optional[str]r5   Optional[fastapi.Request]c                   s   |   j| d r|jg|_|jr4|jr  fdd|jD }|S t|jts(J tdd i|j}|S |d u r<t	dt |d}|S )NZcancelsc                   s   g | ]}t dd  i|qS )r7   r   )r4   ).0reqr7   r   r   
<listcomp>   s    z&compile_gr_request.<locals>.<listcomp>r7   z0request must be provided if body.request is None)r7   r5   r   )
rO   rP   session_hashdatar5   batchedr   r   r4   
ValueError)rH   rJ   rT   r7   r5   
gr_requestr   rY   r   compile_gr_request   s   	
	r`   c                 C  s   |j }t|dd }|d urD|| jvr!dd |  j D | j|< | j| }|| j| v r;i }| j| | ||fS | j| }||fS i }i }||fS )Nr[   c                 S  s.   i | ]\}}t |d dr|tt |ddqS )ZstatefulFr   N)rA   r   )rW   Z_idblockr   r   r   
<dictcomp>   s    
z)restore_session_state.<locals>.<dictcomp>)	rM   rA   Zstate_holderrO   blocksr   Ziterators_to_resetremove	iterators)rH   rJ   rM   r[   session_statere   r   r   r   restore_session_state   s"   


rg   r_   Union[Request, list[Request]]c                   s  t | |d\}}|  j| }t|d r|d d nd }t|r)|  j|nd |j}t|dd }	|j	}
t|dd }|j
}|j oG|d }|rM|g}zBt  |  j|||||||	|dI d H }W d    n1 sow   Y  |dd }t|dr|| j|j |
< t|tr|W n- ty   ||
d }|d urt|}|  j| |i }| D ]}|d  q w |r|d	 d |d	< |S )
N)rH   rJ   targetsr   event_idr[   batch)rM   inputsr5   statere   r[   rj   
event_dataiteratorr\   )rg   rO   rP   lenr   rc   getrn   rA   rM   r\   r]   r	   ZMatplotlibBackendManangerZprocess_apipophasattrre   r[   r   r   BaseExceptionidpending_streamsr&   append)rH   rJ   r_   rT   rf   re   
dependencytargetrn   rj   rM   r[   rl   Zbatch_in_single_outoutputro   Zrun_idrv   streamr   r   r   call_process_api   s^   


r|   )rH   r   rI   r+   rJ   r
   r$   rK   )
rH   r   rJ   r
   rT   rK   r7   rU   r5   rV   )rH   r   rJ   r
   )rH   r   rJ   r
   r_   rh   )
__future__r   r;   copyr   typingr   r   r   ZfastapiZgradio_client.documentationr   r   Zgradior	   Zgradio.data_classesr
   Zgradio.exceptionsr   Zgradio.helpersr   Zgradio.routesr   r   r4   	ExceptionrG   rS   r`   rg   r|   r   r   r   r   <module>   s*    23


