
    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	m
Z
 d dlmZ d dlmZ d dlmZ er	  e ej        dd                    Z e ej        d	d
                    Z ej        d ej        dd                    Z	 	 	 d+d,dZ ej        d          duZ ee          j                            ddd          Zd-dZd.d#Zd/d0d'Zd1d*ZdS )2    )annotationsN)TimeoutError)closing)Path)TYPE_CHECKINGGRADIO_SERVER_PORT7860GRADIO_NODE_NUM_PORTS100GRADIO_NODE_SERVER_NAMEGRADIO_SERVER_NAMEz	127.0.0.1server_name
str | Noneserver_port
int | None	node_pathreturn=tuple[str | None, subprocess.Popen[bytes] | None, int | None]c                8   | pt           } |                     d          r |                     d          r| dd         }n| }||gn't          t          dz   t          dz   t
          z             }t          |pt          j        d          ||          \  }}| ||fS )a  Launches a local server running the provided Interface
    Parameters:
        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.
        node_path: the path to the node executable. Can be set by environment variable GRADIO_NODE_PATH.
        ssr_mode: If False, will not start the node server and will serve the SPA from the Python server

    Returns:
        server_name: the name of the server (default is "localhost")
        node_process: the node process that is running the SSR app
        node_port: the port the node server is running on
    []   NGRADIO_NODE_PATH)r   r   server_ports)	LOCALHOST_NAME
startswithendswithrangeINITIAL_PORT_VALUETRY_NUM_PORTSstart_node_processosgetenv)r   r   r   hostr   node_process	node_ports          f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/node_server.pystart_node_serverr)      s    $ /K
 c"" {';';C'@'@ 1R4  " 
%)+=+AM+QRR  1<ry);<<!  L) i//    GRADIO_LOCAL_DEV_MODE	templatesnodebuildstrr   list[int] | range1tuple[subprocess.Popen[bytes] | None, int | None]c                  	 t           rdS | sdS d 	|D ]}	 t          j                    }|                    t          j        t          j        d           |                    ||f           |                                 t          j        }t          |          |d<   ||d<   t           rd|d<   t          t          t                    j                            dd	                    }t          j        d
k    rd|z   }t!          j        | d|t$          gt           j        |          	t)          ||d          }|rJt+          j        t*          j        	fd           t+          j        t*          j        	fd           	|fc S 	                                 	                    d           d 	# t4          $ r Y t6          $ r;}t9          j        d| d           	r	                                 d 	Y d }~d }~ww xY wt=          d|d          d|d          d           t=          d           t=          d           dS )N)Ni&  )NNr   PORTHOST1r+   r,   zregister.mjswin32zfile://z--import)stdoutenv   timeoutc                "    t                    S Nhandle_sigterm___r&   s     r(   <lambda>z$start_node_process.<locals>.<lambda>y   s    .2N2N r*   c                "    t                    S r=   r>   r@   s     r(   rC   z$start_node_process.<locals>.<lambda>{   s    >,;W;W r*      z-Unexpected error while starting Node server: z. Trying next port...z2Cannot start Node server on any port in the range r   -r   .zwPlease install Node 20 or higher and set the environment variable GRADIO_NODE_PATH to the path of your Node executable.zWYou can explicitly specify a port by setting the environment variable GRADIO_NODE_PORT.)r+   socket
setsockopt
SOL_SOCKETSO_REUSEADDRbindcloser#   environr/   r   __file__parentjoinpathsysplatform
subprocessPopenSSR_APP_PATHDEVNULLverify_server_startupsignalSIGTERMSIGINT	terminatewaitOSError	Exceptionwarningswarnprint)
r   r   r   portsr8   register_file
is_workinger&   s
            @r(   r"   r"   J   s   
  z zL 8 87	 ALL*F,?CCC FFK&'''GGIII *Cd))CK%CK$ 3/2+,X%..{NKK M |w&& )M 9%+J|D!)  L /{D!LLLJ $N$N$N$N$N   fm-W-W-W-WXXX#T)))) &&(((!!!!,,,# 	 	 	H 	 	 	MXXXX    $&&(((#HHHH	 
b\!_bb|\^O_bbb   
 	B   
a   :s$   E,F4,F44
H	H
0H  Hr%   rc   intboolc                    	 t          t          j        | |fd                    5  	 ddd           dS # 1 swxY w Y   dS # t          t          f$ r Y dS t
          $ r Y dS w xY w)z+Attempts a single connection to the server.r   r:   NTF)r   rH   create_connectionr   ConnectionRefusedErrorr_   )r%   rc   s     r(   attempt_connectionrm      s    V-tTlAFFFGG 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	01   uu   uus2   %A 6A :A :A A$	A$#A$      @r;   floatc                R   t          j                     }t          j                     |z
  |k     ry	 t          j        | |fd          5  	 ddd           dS # 1 swxY w Y   n+# t          t          f$ r t          j        d           Y nw xY wt          j                     |z
  |k     ydS )z@Verifies if a server is up and running by attempting to connect.r   r:   NTg?F)timerH   rk   r   r^   sleep)r%   rc   r;   
start_times       r(   rX   rX      s    J
)++

"W
,
,	)4,BBB                  g& 	 	 	JsOOOOO		 )++

"W
,
, 5s5   A" A	A" AA" AA" "%B
	B
r&   subprocess.Popen[bytes] | Nonec                    | Mt          d           |                                  |                                  t          j        d           d S d S )Nz
Stopping Node.js server...r   )rb   r\   r]   rR   exit)r&   s    r(   r?   r?      sT    ,---   	  r*   )NNN)r   r   r   r   r   r   r   r   )r   r   r   r/   r   r0   r   r1   )r%   r/   rc   rh   r   ri   )rn   )r%   r/   rc   rh   r;   ro   r   ri   )r&   rt   ) 
__future__r   r#   rY   rH   rT   rR   rq   r`   concurrent.futuresr   
contextlibr   pathlibr   typingr   rh   r$   r    r!   r   r)   r+   rO   rP   rQ   rV   r"   rm   rX   r?    r*   r(   <module>r}      s   " " " " " " 				       



   + + + + + +                         	 S#7@@AA IBI5u==>>yry)={KK  #" (0 (0 (0 (0 (0V "	"9::$F tH~~$--k67KKQ Q Q Qh   	 	 	 	 	     r*   