
    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mZ d dl	m
Z
 d dlmZ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mZmZmZmZ er ed
e          Z e ej        dd                    Z e ej        dd                    Z  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)Callable)partial)TYPE_CHECKINGTypeVar)Config)ServerFailedToStartError)App)ServerReloaderSourceFileReloaderSpacesReloader	get_spacewatchfnwatchfn_spaces_ServerReloaderT)boundGRADIO_SERVER_PORT7860GRADIO_NUM_PORTS100GRADIO_SERVER_NAMEz	127.0.0.1GRADIO_HOT_RELOADfalseGRADIO_WATCH_DIRS ,GRADIO_WATCH_MODULE_NAMEappGRADIO_WATCH_DEMO_NAMEGRADIO_WATCH_DEMO_PATHc                  8     e Zd Zdefd fd
Zd Zd Zd Z xZS )ServerNconfigr   reloader_ServerReloaderT | Noner   "Callable[[_ServerReloaderT], None]returnNonec                    |j         | _        t                                          |           || _        | j        r4t          j                    | _        t          || j                  | _	        d S d S N)
r   running_appsuper__init__r$   	threadingEventeventr   watch)selfr#   r$   r   	__class__s       f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/http_server.pyr-   zServer.__init__/   sh     ":    = 	9"**DJ $-88DJJJ	9 	9    c                    d S r*    r2   s    r4   install_signal_handlerszServer.install_signal_handlers<   s    r5   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$   r1   watch_threadstarttimestartedsleepr	   )r2   rB   s     r4   run_in_threadzServer.run_in_thread?   s    &dhtDDD= 	& ) 0
4 P P PD##%%%	, 	Jty{{U"Q&&.V   , 	 	 	 	 	r5   c                    d| _         | j        r2| j                                         | j                                         | j                            d           d S )NTr=   )timeout)should_exitr$   stoprA   joinr@   r8   s    r4   closezServer.closeM   s\    = 	%M   ""$$$#####r5   )r#   r   r$   r%   r   r&   r'   r(   )	__name__
__module____qualname__r   r-   r9   rF   rL   __classcell__)r3   s   @r4   r"   r"   .   sw         -16=	9 9 9 9 9 9 9    $ $ $ $ $ $ $r5   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 ]}		 t          j                    }
|
                    t          j	        t          j
        d           |
                    t          |	f           |
                                 t          j        | |	|d	|||
          }t                      Wt!          | t"          t$          t'          j                    t*          j        d                   }t/          ||t0                    }nt"          rqt3          | t"          t4          t$          t'          j                    t6          t*          j        d         t9          j        dd                    }t/          ||          }nt/          |          }|                                  nH# t>          t@          f$ r Y w xY wt?          dtC          |           dtE          |           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_levelrU   rV   rW   __main__)r   
watch_dirs	demo_name
stop_eventwatch_module)r#   r$   r   GRADIO_WATCH_ENCODINGzutf-8)r   rd   watch_module_namere   rf   	demo_filerg   encoding)r#   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bindrL   uvicornr   r   r   r   r   r.   r/   sysmodulesr"   r   r   r   r    osgetenvrF   OSErrorr	   minmax)r   rQ   rS   rU   rV   rW   url_host_namera   server_portsr`   sr#   r$   serverpath_to_local_servers                  r4   start_serverr   U   s   0 <#7TUUU/K#.)#;#;KKM
 c"" {';';C'@'@ 1R4  " 
%'9M'IJJ   7
 7
2	 ALL*F,?CCC FFND)***GGIII ^#')%9  F {{&)04(00!$Z!8    !Hn   # /-0&>4(004!$Z!8Y'>HH	 	 	  vAAAv...  """E12 	 	 	D	  gL0A0A  g  gCDUDU  g  g  g
 
 	
 A-AA$AAA@@@@@@2F::s   F
HH,+H,)NNNNN)r   r
   rQ   rR   rS   rT   rU   rR   rV   rR   rW   rR   r'   rX   )-
__future__r   r~   rv   r|   r.   rC   collections.abcr   	functoolsr   typingr   r   r{   uvicorn.configr   gradio.exceptionsr	   gradio.routesr
   gradio.utilsr   r   r   r   r   r   r   intr   rt   ru   rp   lowerr   boolshould_watchsplitr   r   r   r    r"   r   r7   r5   r4   <module>r      s   " " " " " " 				  



      $ $ $ $ $ $       ) ) ) ) ) ) ) )  ! ! ! ! ! ! 6 6 6 6 6 6                       Iw1HHH S#7@@AA IBI0%8899/==BI17;;AACC tIBI1266775AIIBI!2&&,,S111r  %29%?GG "#;R@@ "#;R@@ $$ $$ $$ $$ $$W^ $$ $$ $$R #""#'+j; j; j; j; j; j; j;r5   