
    wi                    B   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mZ d dlmZ d dlmZ d dlmZmZ e
r	  e ej        d	d
                    Z e ej        dd                    Z ej        dd          Z e ej        dd                    Zer$ ej        dd                              d          ng Z ej        dd          Z ej        dd          Z ej        dd          Z  G d dej!                  Z!	 	 	 	 	 d$d%d#Z"dS )&    )annotationsN)partial)TYPE_CHECKING)Config)ServerFailedToStartError)App)SourceFileReloaderwatchfnGRADIO_SERVER_PORT7860GRADIO_NUM_PORTS100GRADIO_SERVER_NAMEz	127.0.0.1GRADIO_WATCH_DIRS ,GRADIO_WATCH_MODULE_NAMEappGRADIO_WATCH_DEMO_NAMEdemoGRADIO_WATCH_DEMO_PATHc                  6     e Zd Z	 dd fdZd	 Zd
 Zd Z xZS )ServerNconfigr   reloaderSourceFileReloader | NonereturnNonec                    |j         | _        t                                          |           || _        | j        r9t          j                    | _        t          t          | j                  | _
        d S d S N)r   running_appsuper__init__r   	threadingEventeventr   r
   watch)selfr   r   	__class__s      b/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/gradio/http_server.pyr#   zServer.__init__%   sh     ":    = 	9"**DJ $-88DJJJ	9 	9    c                    d S r     r(   s    r*   install_signal_handlerszServer.install_signal_handlers/   s    r+   c                   t          j        | j        d          | _        | j        r9t          j        | j        d          | _        | j                                         | j                                         t          j                    }| j	        sFt          j
        d           t          j                    |z
  dk    rt          d          | j	        Dd 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(   r8   s     r*   run_in_threadzServer.run_in_thread2   s    &dhtDDD= 	& ) 0
4 P P PD##%%%	, 	Jty{{U"Q&&.V   , 	 	 	 	 	r+   c                    d| _         | j        r2| j                                         | j                                         | j                            d           d S )NTr3   )timeout)should_exitr   stopr7   joinr6   r.   s    r*   closezServer.close@   s\    = 	%M   ""$$$#####r+   r    )r   r   r   r   r   r   )__name__
__module____qualname__r#   r/   r<   rB   __classcell__)r)   s   @r*   r   r   $   sr        DH9 9 9 9 9 9 9    $ $ $ $ $ $ $r+   r   r   server_name
str | Noneserver_port
int | Nonessl_keyfilessl_certfilessl_keyfile_passwordr   tuple[str, int, str, Server]c                   ||t          d          |pt          }|dk    rdn|}|                    d          r |                    d          r|dd         }n|}||gn!t	          t
          t
          t          z             }|D ]2}		 t          j                    }
|
                    t          j	        t          j
        d           |
                    t          |	f           |
                                 t          j        | |	|d	|||
          }d}t          r_t!          | t          t"          t$          t'          j                    t*          t,          j        d         t1          j        dd                    }t5          ||          }|                                  nH# t8          t:          f$ r Y 0w xY wt9          dt=          |           dt?          |           d          |
d| d|	 d}n	d| d|	 d}||	||fS )a(  Launches a local server running the provided Interface
    Parameters:
        app: the FastAPI app object to run
        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.

    Returns:
        server_name: the name of the server (default is "localhost")
        port: the port number the server is running on
        path_to_local_server: the complete address that the local server can be accessed at
        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[]   warning)r   porthost	log_levelrK   rL   rM   __main__GRADIO_WATCH_ENCODINGzutf-8)r   
watch_dirswatch_module_name	demo_name
stop_event	demo_filewatch_moduleencoding)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://) 
ValueErrorLOCALHOST_NAME
startswithendswithrangeINITIAL_PORT_VALUETRY_NUM_PORTSsocket
setsockopt
SOL_SOCKETSO_REUSEADDRbindrB   uvicornr   r   r	   r   r   r$   r%   r   sysmodulesosgetenvr   r<   OSErrorr   minmax)r   rG   rI   rK   rL   rM   url_host_namerW   server_portsrV   sr   r   serverpath_to_local_servers                  r*   start_serverr~   H   sz   0 <#7TUUU/K#.)#;#;KKM
 c"" {';';C'@'@ 1R4  " 
%'9M'IJJ   +
 +
&	 ALL*F,?CCC FFND)***GGIII ^#')%9  F H  
-0&>4(004!$Z!8Y'>HH	 	 	 6H===F  """E12 	 	 	D	  gL0A0A  g  gCDUDU  g  g  g
 
 	
 A-AA$AAA@@@@@@2F::s   DF##F87F8)NNNNN)r   r   rG   rH   rI   rJ   rK   rH   rL   rH   rM   rH   r   rN   )#
__future__r   rt   rl   rr   r$   r9   	functoolsr   typingr   rq   uvicorn.configr   gradio.exceptionsr   gradio.routesr   gradio.utilsr	   r
   intru   rj   rk   rf   boolshould_watchsplitr   r   r   r   r   r~   r-   r+   r*   <module>r      s   " " " " " " 				  



                         ! ! ! ! ! ! 6 6 6 6 6 6       4 4 4 4 4 4 4 4 	 S#7@@AA IBI0%8899/==tIBI1266775AIIBI!2&&,,S111r  %29%?GG "#;VDD "#;R@@ !$ !$ !$ !$ !$W^ !$ !$ !$L #""#'+^; ^; ^; ^; ^; ^; ^;r+   