o
    e&'                     @  sx  d Z 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m	Z	 ddl
mZ ddlZddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ erXddlmZ eeddZeeddZeddZdZ e!eddZ"e"redd#dng Z$eddZ%eddZ&G dd dej'Z'd>d!d"Z(d?d&d'Z)						d@dAd2d3Z*dBd8d9Z+dCd<d=Z,dS )Dz^
Defines helper methods useful for setting up ports, launching servers, and
creating tunnels.
    )annotationsN)partial)TYPE_CHECKING)Config)ServerFailedToStartError)App)Tunnel)SourceFileReloaderwatchfn)BlocksZGRADIO_SERVER_PORTZ7860ZGRADIO_NUM_PORTS100ZGRADIO_SERVER_NAMEz	127.0.0.1z(https://api.gradio.app/v2/tunnel-requestGRADIO_WATCH_DIRSF ,GRADIO_WATCH_FILEappGRADIO_WATCH_DEMO_NAMEdemoc                      s:   e Zd Z	dd fdd	Zd
d Zdd Zdd Z  ZS )ServerNconfigr   reloaderSourceFileReloader | NonereturnNonec                   s@   |j | _t | || _| jrt | _tt	| j| _
d S d S N)r   Zrunning_appsuper__init__r   	threadingEventeventr   r
   watch)selfr   r   	__class__ AD:\Projects\ConvertPro\env\Lib\site-packages\gradio/networking.pyr   +   s   
zServer.__init__c                 C  s   d S r   r$   r!   r$   r$   r%   install_signal_handlers5   s   zServer.install_signal_handlersc                 C  s|   t j| jdd| _| jrt j| jdd| _| j  | j  t }| j	s<t
d t | dkr7td| j	r&d S d S )NT)targetdaemongMbP?   z@Server failed to start. Please check that the port is available.)r   Threadrunthreadr   r    watch_threadstarttimestartedsleepr   )r!   r/   r$   r$   r%   run_in_thread8   s   


zServer.run_in_threadc                 C  s.   d| _ | jr| j  | j  | j  d S )NT)Zshould_exitr   stopr.   joinr-   r&   r$   r$   r%   closeF   s
   

zServer.closer   )r   r   r   r   r   r   )__name__
__module____qualname__r   r'   r3   r6   __classcell__r$   r$   r"   r%   r   *   s    
r   initialintfinalr   c              	   C  sv   t | |D ](}zt }|tjtjd |t|f |  |W   S  ty-   Y qw td|  d|d  d)a-  
    Gets the first open port in a specified range of port numbers
    Parameters:
    initial: the initial value in the range of port numbers
    final: final (exclusive) value in the range of port numbers, should be greater than `initial`
    Returns:
    port: the first open port in the range
       zAll ports from z to z! are in use. Please close a port.)	rangesocket
setsockopt
SOL_SOCKETSO_REUSEADDRbindLOCALHOST_NAMEr6   OSError)r;   r=   portsr$   r$   r%   get_first_available_portN   s   	
rI   r   blocksr   c                 C  sX   |j }|d urt|sdd |D | _ n|| _ nd | _ || _t | _|j| _i | _| S )Nc                 S  s   i | ]	}|d  |d qS )r   r>   r$   ).0accountr$   r$   r%   
<dictcomp>i   s    z!configure_app.<locals>.<dictcomp>)authcallablerJ   osgetcwdcwdZfavicon_pathtokens)r   rJ   rN   r$   r$   r%   configure_appe   s   
rT   server_name
str | Noneserver_port
int | Nonessl_keyfilessl_certfilessl_keyfile_password
app_kwargsdict | None!tuple[str, int, str, App, Server]c                 C  s  |dur|du rt d|pt}|dkrdn|}|dr)|dr)|dd }n|}tj| |d	}	|dur9|gntttt }
|
D ]V}zIt		 }|
t	jt	jd |t|f |  tj|	||d
|||dd}d}trt }||	_t|	tttt |d}t||d}|  W  n ttfy   Y qBw tdt|
 dt|
 d|durd| d| d}n	d| d| d}||||	|fS )ar  Launches a local server running the provided Interface
    Parameters:
        blocks: The Blocks object to run on the server
        server_name: to make app accessible on local network, set this to "0.0.0.0". Can be set by environment variable GRADIO_SERVER_NAME.
        server_port: will start gradio app on this port (if available). Can be set by environment variable GRADIO_SERVER_PORT.
        auth: If provided, username and password (or list of username-password tuples) required to access the Blocks. Can also provide function that takes username and password and returns True if valid login.
        ssl_keyfile: If a path to a file is provided, will use this as the private key file to create a local server running on https.
        ssl_certfile: If a path to a file is provided, will use this as the signed certificate for https. Needs to be provided if ssl_keyfile is provided.
        ssl_keyfile_password: If a password is provided, will use this with the ssl certificate for https.
        app_kwargs: Additional keyword arguments to pass to the gradio.routes.App constructor.

    Returns:
        port: the port number the server is running on
        path_to_local_server: the complete address that the local server can be accessed at
        app: the FastAPI app object
        server: the server object that is a subclass of uvicorn.Server (used to close the server)
    Nz9ssl_certfile must be provided if ssl_keyfile is provided.z0.0.0.0	localhost[]r>   )r\   warningi   @)r   rG   host	log_levelrY   rZ   r[   Zws_max_size)r   Z
watch_dirsZ
watch_fileZ	demo_nameZ
stop_eventchange_event)r   r   z!Cannot find empty port in range: -z. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.zhttps://:/zhttp://)
ValueErrorrE   
startswithendswithr   Z
create_appr?   INITIAL_PORT_VALUETRY_NUM_PORTSr@   rA   rB   rC   rD   r6   uvicornr   r   r   r   rf   r	   r   r   r   r3   rF   r   minmax)rJ   rU   rW   rY   rZ   r[   r\   Zurl_host_namerd   r   Zserver_portsrG   rH   r   r   rf   serverZpath_to_local_serverr$   r$   r%   start_serveru   sj   
rs   
local_hoststr
local_portshare_tokenc           
   
   C  s   t t}|r@|jdkr@z | d }|d t|d }}t||| ||}| }|W S  ty? }	 zt	t
|	|	d }	~	ww t	d)N   r   rd   rG   z0Could not get share link from Gradio API Server.)requestsgetGRADIO_API_SERVERstatus_codejsonr<   r   Zstart_tunnel	ExceptionRuntimeErrorru   )
rt   rv   rw   responsepayloadZremote_hostZremote_portZtunneladdresser$   r$   r%   setup_tunnel   s   

r   urlboolc              	   C  s   z9t dD ]1}t  td tj| ddd}W d    n1 s#w   Y  |jdv r1 W dS td qW dS  t	tj
j	fyG   Y dS w )	Nr*   ignore   F)timeoutverify)rx   i  i.  Tg      ?)r?   warningscatch_warningsfilterwarningsry   headr|   r0   r2   ConnectionError
exceptions)r   _rr$   r$   r%   url_ok   s   


	r   )r;   r<   r=   r<   r   r<   )r   r   rJ   r   r   r   )NNNNNN)rJ   r   rU   rV   rW   rX   rY   rV   rZ   rV   r[   rV   r\   r]   r   r^   )rt   ru   rv   r<   rw   ru   r   ru   )r   ru   r   r   )-__doc__
__future__r   rP   r@   r   r0   r   	functoolsr   typingr   ry   ro   Zuvicorn.configr   Zgradio.exceptionsr   Zgradio.routesr   Zgradio.tunnelingr   Zgradio.utilsr	   r
   Zgradio.blocksr   r<   getenvrm   rn   rE   r{   r   Zshould_watchsplitr   r   r   r   rI   rT   rs   r   r   r$   r$   r$   r%   <module>   sN    
$

f