
    Fgt                   @   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJrJr  SSKJr  SSKJr  SSKJrJrJrJrJ r J!r!  SSK"r"SSK#r#SSK$r$SSK%r%SSK"J&r&J'r'J(r(J)r)J*r*J+r+  SS	K,J-r-J.r.J/r/J0r0J1r1J2r2  SS
K3J4r4  SSK5J6r6  SSK7J8r8J9r9  SSK:J;r<  SSK=J>r>  SSK?J@r@  SSKAJBrB  SSKCJDrD  SSKEJFrF  SSKGJHrI  SSKJJKrK  SSKLrLSSKLJMrMJNrNJ;r;JOrO  SSKPJQrQ  SSKRJSrSJTrTJUrUJVrVJWrWJXrXJYrYJZrZJ[r[J\r\  SSK]J^r^J_r_  SSK`Jara  SSKbJcrc  SSKdJereJfrfJgrgJhrhJiriJjrjJkrkJlrlJmrmJnrnJoroJprp  SSKqJrrrJsrsJtrtJuruJvrvJwrwJxrx  SSKyJzrz  SSK{J|r|J}r}J~r~Jr  \(       a  SSKJr  \GR                  " 5         \!" \W\GR                  GR	                  S 5      GR                  S!5      GR                  5       5      r\!" \W\GR                  GR	                  S 5      GR                  S"5      GR                  5       5      r\!" \W\GR                  GR	                  S 5      GR                  S#5      GR                  5       5      r\~" 5       r1 S$kr " S% S&\/5      rS' r\6" \S(9r\\GR                  GR                   S)'   \#GR"                  " \#GR$                  " S*S+S,9\#GR&                  " S-5      S.9r\g" 5       r " S/ S0\)5      rS7S1 jrS8S2 jr\>" 5           S9SSSSSSSS3SSSSS4.                                       S:S5 jjj5       r/ S6Qrg);zImplements a FastAPI server to run the gradio interface. Note that some types in this
module use the Optional/Union notation so that they work correctly with pydantic.    )annotationsN)AsyncIteratorCallable)Path)Empty)TYPE_CHECKINGAnyLiteralOptionalUnioncast)	APIRouterBackgroundTasksDependsFastAPIHTTPExceptionstatus)FileResponseHTMLResponseJSONResponsePlainTextResponseResponseStreamingResponse)OAuth2PasswordRequestForm)Jinja2Templates)	WebSocketWebSocketDisconnect)utils)document)ServerMessage)TemplateNotFound)parse_options_header)BackgroundTask)
UploadFile)RedirectResponse)ranged_responseroute_utilsr   
wasm_utils)Context)

CancelBodyComponentServerBlobBodyComponentServerJSONBodyDataWithFilesDeveloperPathPredictBodyPredictBodyInternal	ResetBodySimplePredictBodyUserProvidedPath)ErrorInvalidPathError)start_node_server)attach_oauth)
API_PREFIXCustomCORSMiddlewareFileUploadProgress FileUploadProgressNotQueuedError!FileUploadProgressNotTrackedErrorGradioMultiPartParserGradioUploadFileMultiPartExceptionRequestcompare_passwords_securelycreate_lifespan_handlermove_uploaded_files_to_cache)CloseStreamMessageEstimationMessageEventMessageHeartbeatMessageProcessCompletedMessageProcessGeneratingMessageUnexpectedErrorMessage)StateHolder)cancel_tasksget_node_pathget_package_versionget_upload_folder)Blockgradio	templatesztemplates/frontend/staticztemplates/frontend/assets>   	audio/ogg	audio/wav	image/gif	image/png	video/mp4	video/ogg
audio/mpeg
image/jpeg
image/webp
text/plain
video/webmapplication/jsonc                  J    \ rS rSrSr\SS j5       rSS jr\S	S j5       rSr	g)
ORJSONResponse   r^   c                t    [         R                  " U [         R                  [         R                  -  [        S9$ )N)optiondefault)orjsondumpsOPT_SERIALIZE_NUMPYOPT_PASSTHROUGH_DATETIMEstrcontents    ^/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/gradio/routes.py_renderORJSONResponse._render   s-    ||--0O0OO
 	
    c                ,    [         R                  U5      $ N)r`   rm   )selfrk   s     rl   renderORJSONResponse.render   s    %%g..ro   c                J    [         R                  U 5      R                  S5      $ )Nutf-8)r`   rm   decoderj   s    rl   _render_strORJSONResponse._render_str   s    %%g.55g>>ro    N)rk   r	   returnbytes)rk   r	   r{   ri   )
__name__
__module____qualname____firstlineno__
media_typestaticmethodrm   rs   rx   __static_attributes__rz   ro   rl   r`   r`      s2    #J
 
/ ? ?ro   r`   c                    [         R                  " [        R                  U 5      R	                  SS5      R	                  SS5      R	                  SS5      R	                  SS5      5      $ )	N<z\u003c>z\u003e&z\u0026'z\u0027)
markupsafeMarkupr`   rx   replace)values    rl   toorjsonr      sS    ""5)	i	 	i	 	i	 	i	  ro   )	directoryr   d      )max_connectionsmax_keepalive_connectionsg      $@)limitstimeoutc                     ^  \ rS rSrSrSr S SU 4S jjjr\R                  " 5       r	\
  S             SS jj5       rSS jrSS jrS rS	 r\
    S           SS
 jj5       rSrU =r$ )App   z
FastAPI App Wrapper
Nc                  > 0 U l         S U l        [        R                  " S5      U l        SU l        S U l        [        5       U l        0 U l	        [        5       U l        [        R                  " 5       U l        [        R                  " 5       U l        [        R                  " S5      U l        [        R                  " S5      U l        SU l        [)        5       U l        SU l        S U l        S U l        / U l        Xl        S U l        S U l        UR;                  SS 5        UR;                  SS 5        0 U l        [>        TU ]  " S0 UD6  g )N   F    r   docs_url	redoc_urlrz   )!tokensauthsecretstoken_urlsafeanalytics_keymonitoring_enabledblocksrK   state_holder	iteratorssetiterators_to_resetr   safe_get_locklocksafe_get_stop_event
stop_event	cookie_idqueue_tokenstartup_events_triggeredrO   uploaded_file_dirchange_countchange_typereload_error_message_asyncio_tasksauth_dependencyapi_infoall_app_info
setdefaultcustom_component_hashessuper__init__)rr   r   kwargs	__class__s      rl   r   App.__init__   s   
 	$2226"',0'M35,/E'')	335 ..r2"004(-%!2!4!">B04!24.  	*d++t,79$"6"ro   c                
  #    U R                   R                  nU(       a  UR                  US5      nU R                   R                  (       a  USU R                   R                   3-  nU SU SU U 3nU SU 3nU(       a  USU 3-  nU(       a  X-  n[	        U R
                  5      n	XS'   [        U5      U	S'   [        R                  " S5      (       a  SU	S	'   [        R                  R                  U R                  [        R                  " U5      U	S
9n
[        R                  R                  U
SS9I S h  vN n[!        UR#                  5       UR$                  UR
                  ['        UR(                  5      S9$  NE7f)N ?z://:zx-gradio-serverzx-gradio-portGRADIO_LOCAL_DEV_MODE1zx-gradio-local-dev-modeheadersTstreamstatus_coder   
background)urlpathr   querydictr   ri   osgetenvr   clientbuild_requestmethodhttpxURLsendr   	aiter_rawr   r#   aclose)requestserver_name	node_portpython_portschememounted_path	full_pathr   
server_urlr   new_requestnode_responses               rl   proxy_to_nodeApp.proxy_to_node   sd     KK$$	!)),;I;;1W[[../00IK=)YK@xs;-0
Ak]++J&Jw'%/!"#&{#3 99,--14G-.jj..NNEIIcNG / 
 "jjook$oGG ##%%11!))%m&:&:;	
 	
 Hs   D9F;F<AFc                n   UR                   nUb6  [        U5      (       d  U Vs0 s H  o3S   US   _M     snU l         OX l         OS U l         Xl        [        R                  " 5       U l        UR                  U l        0 U l        UR                  U l        U R                  R                  U5        g s  snf )Nr      )r   callabler   r   getcwdcwdfavicon_pathr   	root_pathr   
set_blocks)rr   r   r   accounts       rl   configure_appApp.configure_app  s    {{D>>CGH4QZ34H	 	DI99;"//))$$V, Is   B2c                J    U R                   c  [        S5      eU R                   $ )Nz+No Blocks has been configured for this app.)r   
ValueError)rr   s    rl   
get_blocksApp.get_blocks/  s"    ;;JKK{{ro   c                  ^ [         R                  " U5      mU R                  (       d   e[        U4S jU R                  R                   5       5      nU(       d  [        S5      eTR                  R                  S5      n0 n[        R                  b  U(       a  S[        R                   3US'   [        R                  STUS9nU$ )Nc              3  ~   >#    U  H2  nTR                   [        R                  " U5      R                   :H  v   M4     g 7frq   )hostr   r   ).0rootr   s     rl   	<genexpr>*App.build_proxy_request.<locals>.<genexpr>9  s,      
9OCHH		$,,,9Os   :=zThis URL cannot be proxied.z	.hf.spacezBearer AuthorizationGETr   )r   r   r   any
proxy_urlsPermissionErrorr   endswithr)   hf_tokenr   r   )rr   url_pathis_safe_url	is_hf_urlr   rp_reqr   s         @rl   build_proxy_requestApp.build_proxy_request4  s    ii!{{{  
9=9O9O
 
 !"?@@HH%%k2	'I)01A1A0B'CGO$%%eS'%Bro   c                X    U R                    H  nUR                  5         M     / U l         g rq   )r   cancel)rr   tasks     rl   _cancel_asyncio_tasksApp._cancel_asyncio_tasksE  s#    ''DKKM ( ro   c           	     0  ^ ^,^-^.^/^0^1^2^3^4 U=(       d    0 nUR                  S[        5        T R                  =(       d    Sn[        UR	                  SS 5      /UQ76 US'   [        SwSU0UDSS0D6m-[        [        S9nT-R                  T 5        [        R                  (       d  T-R                  [        US9  U(       a  T-R                  S	5        SxU 4S
 jj5       nUR	                  S5      UR	                  S5      SyU-4S jj5       5       m/UR	                  S5      UR	                  S5      [        T/5      4SzU-4S jjj5       5       nUR	                  S5      UR	                  S5      S{U-4S jj5       5       n	UR	                  S5      UR	                  S5      S{U-4S jj5       5       n
UR	                  S[        U5      /S9  SxU-4S jj5       nT-R                  S5      T-R                  S5      [        5       4S|U-4S jjj5       5       nT-R                   b'  T-R                   R"                  (       a  [%        T-5        O(T-R	                  S5      [        T/5      4SzU-4S jjj5       nT-R	                  S5      S}S  j5       nU-U/U14S! jnT R&                   H  nUS"   nUS#:w  d  M  U" U5        M     T-R)                  S$[*        S%9T-R	                  S$[*        S%9[        T/5      S#4     S~U,U-4S& jjj5       5       m1UR	                  S'[        U5      /S9UR	                  S([        U5      /S9SxU-4S) jj5       5       m,T-R	                  S*[        U5      /S9T-R	                  S+[        U5      /S9SxU-U/4S, jj5       5       nT-R	                  S-5      S}S. j5       m4UR	                  S/5                SU-4S0 jj5       nT-R	                  S15      S}S2 j5       nT-R	                  S35      U-U44S4 j5       nUR)                  S5[        U5      /S9UR	                  S5[        U5      /S9SU-4S6 jj5       5       nUR)                  S7[        U5      /S9UR	                  S7[        U5      /S9SU-4S8 jj5       5       m.UR                  S95      SU-4S: jj5       nUR-                  S95      SU-4S; jj5       nUR                  S<5      SU-4S= jj5       nUR	                  S>5      SU-4S? jj5       nUR	                  S@5                SU-4SA jj5       nUR	                  SB5      SU-4SC jj5       nUR	                  SD[        U5      /S9SU.4SE jj5       nUR                  SF5      UR                  SG5      SSH j5       5       nUR	                  SI5      [        T/5      4       SU-4SJ jjj5       nUR                  SK[        U5      /S9UR                  SL[        U5      /S9UR                  SM[        U5      /S9UR                  SN[        U5      /S9[        T/5      4       SU-4SO jjj5       5       5       5       nUR                  SP[        U5      /S9UR                  SQ[        U5      /S9[        T/5      4       SU-U34SR jjj5       5       nUR                  SS[        U5      /S9[        T/5      4     SU34ST jjj5       n      SU-4SU jjm3UR                  SV5      SU-4SW jj5       nUR	                  SX[        U5      /S9    SU24SY jj5       nUR	                  SZ[        U5      /S9    SU24S[ jj5       n       SU-4S\ jjm2    SS] jm0UR                  S^[        U5      /S9UR                  S_[        U5      /S9  SxU-U04S` jj5       5       n!UR	                  Sa[        U5      /[.        Sb9U-4Sc j5       n"UR	                  Sd5      SSe j5       n#UR                  Sf[        U5      /S9 S     SU-U 4Sg jjj5       n$UR	                  Sh5      U-4Si j5       n%UR	                  Sj[0        S%9T-R	                  Sj[0        S%9U-4Sk j5       5       n&T-R	                  Sl[0        S%9U-4Sm j5       n'T-R	                  Sn5      T-R	                  So5      SSU-4Sp jjj5       5       n(T-R	                  Sq5      U-U 4Sr j5       n)UR	                  Ss[        U5      /S9SxU-U 4St jj5       n*UR	                  Su5      SU-U 4Sv jj5       n+T-R3                  U5        T-$ )Ndefault_response_class)NNlifespanr   debugT)prefix)strict_corshttpc           	     0  >^#    TR                   nUb9  U R                  R                  R                  TR                   =(       d    SS5      OU R                  R                  m[	        TSS 5      b  TR
                  b  [        U4S j[         5       5      (       d  [        R                  cF  U R                  R                  =(       d     [        [        R                  " SS5      5      [        l	         [        R                  U TR                  =(       d    STR
                  [        R                  U R                  R                   U=(       d    S5      I S h  vN $ U" U 5      I S h  vN nU$  N! ["         a  n[%        U5         S nAN.S nAff = f N*7f)Nr   node_processc              3  L   >#    U  H  nTR                  S U 35      v   M     g7f)/N)
startswith)r   r   r   s     rl   r   IApp.create_app.<locals>.conditional_routing_middleware.<locals>.<genexpr>p  s#     Vos!C5	 : :os   !$GRADIO_SERVER_PORT78600.0.0.0)custom_mount_pathr   r   r   getattrr   r   INTERNAL_ROUTESr   app_portportintr   r   r   node_server_namer   	Exceptionprint)r   	call_nextr  eresponser   r   s        @rl   conditional_routing_middleware6App.create_app.<locals>.conditional_routing_middlewareb  sC     %+$<$<! )4 KK$$,,V-E-E-KRP ))  FND9E((4VoVVV||+'.{{'7'7 (3II&:FC<
!%(%6%6#"33@y",,LL#KK..-3&    "+7!33  % !a!3sO   C/F4A%E2 E0E2 F)F*F0E2 2
F<FFFFz/userz/user/c                $  > TR                   b  TR                  U 5      $ U R                  R                  STR                   35      =(       d(    U R                  R                  STR                   35      nTR                  R                  U5      $ )Naccess-token-access-token-unsecure-)r   cookiesgetr   r   r   tokenapps     rl   get_current_user(App.create_app.<locals>.get_current_user  s     "".**733OO''/ O$$'=cmm_%MN  ::>>%((ro   z/login_checkz/login_check/c                p   > TR                   c  TR                  b  U b  g [        [        R                  SS9e)NNot authenticatedr   detail)r   r   r   r   HTTP_401_UNAUTHORIZED)userr2  s    rl   login_check#App.create_app.<locals>.login_check  s;      S%8%8%@TEU"88AT ro   z/tokenz/token/c                   > U R                   R                  STR                   35      nUTR                  R                  U5      S.$ )Nr,  )r1  r:  )r.  r/  r   r   r0  s     rl   	get_token!App.create_app.<locals>.get_token  s<     OO''-(GHE"CJJNN5,ABBro   z/app_idz/app_id/c                <   > STR                  5       R                  0$ )Napp_id)r   rA  )r   r2  s    rl   rA  App.create_app.<locals>.app_id  s     cnn.5566ro   z/dev/reload)dependenciesc                <   >#    SU4S jjn[        U" U 5      SS9$ 7f)Nc                 >#    SnSn[         R                  " 5       nTR                  n U R                  5       I S h  vN (       a  g TR                  U:w  aU  TR                  nTR                  S:X  a!  [
        R                  " TR                   5      OSnSTR                   SU S37v   [        R                  " U5      I S h  vN   [         R                  " 5       U-
  U:  a  S7v   [         R                   " 5       nM   N N>7f)	N   皙?errorz{}event: 
data: 

zevent: heartbeat
data: {}

)
timeperf_counterr   is_disconnectedr   jsonrf   r   asynciosleep)r   heartbeat_rate
check_ratelast_heartbeatcurrent_countmsgr2  s         rl   reload_checker>App.create_app.<locals>.notify_changes.<locals>.reload_checker  s     !#!
!%!2!2!4 # 0 0$44666''=8(+(8(8  #'9 !JJ#*B*B)CE!% 
 #*#//):(3%tNN!--
333((*^;nLBB)-! 6 4s"   :DDBDD=DDtext/event-streamr   r   fastapi.Requestr   )r   rW  r2  s     rl   notify_changes&App.create_app.<locals>.notify_changes  s%     50 %w'. s   z/loginz/login/c                  > U R                   R                  5       U R                  p!TR                  c  [	        S[
        R                  S9$ [        TR                  5      (       d.  UTR                  ;   a  [        UTR                  U   5      (       d:  [        TR                  5      (       a  TR                  R                  X5      (       ap  [        R                  " S5      nUTR                  U'   [        SS0S9nUR                  STR                   3USSSS	9  UR                  S
TR                   3USS9  U$ [!        SSS9e)Nr  r   r   r   successTrj   r,  none)keyr   httponlysamesitesecurer-  )rd  r   re    zIncorrect credentials.r7  )usernamestrippasswordr   r%   r   HTTP_302_FOUNDr   rA   __call__r   r   r   r   
set_cookier   r   )	form_datari  rk  r1  r(  r2  s        rl   loginApp.create_app.<locals>.login  s*    "+!3!3!9!9!;Y=O=Ohxx'CV=R=RSSSXX&&(.x(9KLL388$$):):8)N)N--b1$,

5!'D0AB##'7!# $  ##0@! $ 
  #<TUUro   z/logoutc                D  > [        S[        R                  S9nUR                  STR                   3SS9  UR                  STR                   3SS9  [        TR                  R                  5       5       H%  nTR                  U   U :X  d  M  TR                  U	 M'     U$ )Nr  ra  r,  )rd  r   r-  )r%   r   rl  delete_cookier   listr   keys)r:  r(  r1  r2  s      rl   logoutApp.create_app.<locals>.logout  s    +AVAVW&&]3==/+JQT&U&&0@s '  "#**//"34Ezz%(D0JJu- 5  ro   z/svelte/{path:path}c           	         [        [        5      S-  n[        [        [	        [        U5      5      [        U 5      5      5      $ )Nsvelte)r   BUILD_PATH_LIBr   routes_safe_joinr.   ri   r3   )r   svelte_paths     rl   _App.create_app.<locals>._  s;    ~.9K !#k"235Ed5K ro   c                   >^  TR                  ST  3[        S9TR                  ST  S3[        S9[        T5      4   SUU 4S jjj5       5       ng )Nr  response_classc                   > T" XT5      $ rq   rz   )r   r:  mainpages     rl   
page_route7App.create_app.<locals>.attach_page.<locals>.page_route  s     G400ro   )r   r\  r:  ri   )r/  r   r   )r  r  r2  r3  r  s   ` rl   attach_page#App.create_app.<locals>.attach_page  se    WWqZW=WWqa[W> $$451(11 1 ? >1ro   r   r   r  r  c           
       > [         R                  " SS5        TR                  5       n[        R                  " U SU 3TR
                  S9nTR                  c  TR                  b  Ub  [        R                  " UR                  5      n[        R                  " XT5      nXS'   US    Vs/ s H  nUS   US   U   S   ;   d  M  UPM     snUS'   UR                  S	/ 5       Vs/ s H  nUS   US   U   S	   ;   d  M  UPM     snUS	'   US   U   S
   US
'   X%S'   ONTR                  (       a  [        [        R                  SS9eSUR                   UR"                  USS
0 00S// / SS.	n UR$                  (       a  SOSnT" U 5      n	[&        R)                  U UUU	S.S9n
U
$ s  snf s  snf ! [*         a.  nUR$                  (       a  [-        S5      Ue[-        S5      UeS nAff = f)Nzapplication/javascriptz.jsr  r   
route_pathr   ri  
componentsidr  rC  layoutcurrent_pager6  r7  Tr   )	auth_requiredauth_messagespace_idr   r  pagesr  rC  r  zfrontend/share.htmlzfrontend/index.html)configgradio_api_info)r   namecontextzqDid you install Gradio from source files? Share mode only works when Gradio is installed through the pip package.znDid you install Gradio from source files? You need to build the frontend by running /scripts/build_frontend.sh)	mimetypesadd_typer   r'   get_root_urlr   r   r   r   safe_deepcopyr  update_root_in_configr/  r   r   r9  r  r  sharerR   TemplateResponser!   r   )r   r:  r  r   r   r  	component
dependencytemplater  resperrr   r2  s               rl   r  App.create_app.<locals>.main  s>    7?^^%F++tf:--D
  S%8%8%@TEU,,V]];$::6H%)z" &,L%9(%9	 &.*>|*LL %9(|$ '-jj&D*&D
!$'6&>$+?+OO &D*~&
 $*&>$#7#Ax )-~&$$# & < <EX 
 &*$*$7$7 & (B0 T"$$&$&
-3\\)?T  #+7"3 11#!"(+: 2  U(
*L $ 
<<$R 
 %M 
s0   *F#F#$F(>F(,6F- -
G%7)G  G%z/info/z/infoc                  > U R                   R                  SS5      nU(       a?  TR                  (       d"  TR                  5       R	                  SS9Tl        TR                  $ TR
                  (       dl  [        R                  " TR                  5       R	                  5       5      n[        [        [        [        4   U5      n[        R                  " U5      nUTl        TR
                  $ )Nall_endpointsFT)r  )query_paramsr/  r   r   get_api_infor   r   r  r   r   ri   r	   r'   'update_example_values_to_use_public_url)r   r  r   r2  s      rl   r    App.create_app.<locals>.api_info^  s     $0044_eLM'''*~~'7'D'DSW'D'XC$'''<< ..s~~/?/L/L/NOS#X9&NNxX'<<ro   z/config//configc                   > [         R                  " TR                  5       R                  5      n[        R
                  " U STR                  S9n[        R                  " X5      nT" U 5      US'   [        US9$ )Nr  r  ri  rj   )	r   r  r   r  r'   r  r   r  r`   )r   r  r   r2  r3  s      rl   
get_config"App.create_app.<locals>.get_configm  si     (()9)@)@AF++ID !66vDF!1'!:F:!&11ro   z/static/{path:path}c                J    [        [        [        U 5      5      n[        U5      $ rq   )r{  STATIC_PATH_LIBr3   r   )r   static_files     rl   static_resource'App.create_app.<locals>.static_resourcex  s    *?<LT<RSK,,ro   z7/custom_component/{id}/{environment}/{type}/{file_name}c           	       >^  US;  a
  [        SSS9e[        R                  " 5       n[        U 4S jU 5       S 5      nUc
  [        SSS9eUR                  n[
        R                  U   R                  nUc
  [        SSS9e [        R                  " UR                  [        U SU 35      5      n	[        [        [        [        U5      R                   5      5      [        U	5      5      n
T  SU SU 3nUTR"                  ;  aU  [$        R&                  " [        U
5      R)                  S	S
9R+                  5       5      R-                  5       TR"                  U'   TR"                  R/                  U5      nSS0nU(       a  XS'   U(       a)  UR0                  R/                  S5      U:X  a
  [3        SUS9$ [5        XS9$ ! [         a    [        SSS9S ef = f)N)r   server  zEnvironment not supported.r7  c              3  R   >#    U  H  oR                  5       T:X  d  M  Uv   M     g 7frq   )get_component_class_id)r   itemr  s     rl   r   @App.create_app.<locals>.custom_component_path.<locals>.<genexpr>  s"     T*$0K0K0MQS0S*s   '	'zComponent not found.r  -rv   )encodingzCache-Controlzmax-age=0, must-revalidateETagzif-none-matchi0  )r   r   r   )r   r   get_all_componentsnextr~   sysmodules__file__	safe_joinTEMPLATE_DIRr3   r5   r{  r.   ri   r   parentr   hashlibsha256	read_textencode	hexdigestr/  r   r   r   )r  environmenttype	file_namereqr  locationmodule_namemodule_pathrequested_pathr   rd  versionr   r2  s   `             rl   custom_component_path-App.create_app.<locals>.custom_component_path}  s    "66# #,H  113JT*TH #<RSS"--K++k2;;K"#<RSS!&))$vQyk%:;" $c${"3":":;< 0D D$q,C#5553:>>J(('(:AAC4)+ ++C0 1155c:G&(DEG")3;;???;wF(S'JJ66; $ # #,Bs   /F= =Gz/assets/{path:path}c                J    [        [        [        U 5      5      n[        U5      $ rq   )r{  rz  r3   r   )r   
build_files     rl   build_resource&App.create_app.<locals>.build_resource  s    ).:J4:PQJ
++ro   z/favicon.icoc                    >#    TR                  5       n U R                  c  T" S5      $ [        U R                  5      $ 7f)Nzimg/logo.svg)r   r   r   )r   r2  r  s    rl   faviconApp.create_app.<locals>.favicon  s:     ^^%F""*&~66#F$7$788s   ;>z/proxy={url_path:path}c                  >#     TR                  U 5      n[        R                  USS9I S h  vN n[        R                  " U 5      u  pEU[        ;  a:  UR                  R                  SS05        UR                  R                  SS05        [        UR                  5       UR                  UR                  [        UR                  5      S	9$ ! [         a  n[        S[        U5      S9UeS nAff = f N7f)
Nrh  r7  Tr   Content-Disposition
attachmentContent-Typeapplication/octet-streamr   )r  r   r   ri   r   r   r  
guess_typeXSS_SAFE_MIMETYPESr   updater   r   r   r#   r   )r  r  r  rp_resp	mime_typer}  r2  s         rl   reverse_proxy%App.create_app.<locals>.reverse_proxy  s     O00: #KKtK<<G$//9LI 22&&(=|'LM&&8R'ST$!!##//)'..9	  # O#CHE3NO<s2   C7C C7C5B!C7
C2C--C22C7z/file={path_or_url:path}c                  >#    TR                  5       n[        R                  " U 5      (       a  [        U [        R
                  S9$ [        R                  " U 5      (       a  [        SSU  S35      e[        R                  " U 5      nUR                  5       (       d  UR                  5       (       d  [        SSU  S35      eSSKJn  [        R                  " UUR                   UR"                  UR$                  -   TR&                  [        R(                  " 5       /S9u  pVU(       d  [        SSU  S35      e[*        R,                  " U5      u  pxU[.        ;   d  US:X  a  U=(       d    S	n	S
n
OS	n	Sn
UR0                  R3                  SS5      R5                  5       nUR7                  S5      (       a  SU;   a  USS  nUR9                  S5      u  pUR;                  5       (       a  UR;                  5       (       at  [=        U5      n[=        U5      n[?        UR0                  5      nXS'   XS'   [@        RB                  " U[@        RD                  " X5      U[F        RH                  " U5      S9nU$ [K        USS0U
U	URL                  S9$ 7f)Nra    zFile not allowed: .r   )_StaticFiles)blocked_pathsallowed_pathscreated_pathsallowedr  inliner  Ranger   zbytes=r     r  r  )stat_resultzAccept-Rangesr|   )r   content_disposition_typer   filename)'r   client_utilsis_http_url_liker%   r   rl  r'   starts_with_protocolr   r   abspathis_direxistsgradio.data_classesr  is_allowed_filer  r  	all_pathsr   get_cache_folderr  r  r  r   r/  rj  r  split	isnumericr"  r   r&   RangedFileResponse	OpenRanger   statr   r  )path_or_urlr   r   abs_pathr  r  reasonr  r}  r   r  	range_valstartendr   r(  r2  s                   rl   fileApp.create_app.<locals>.file  sA     ^^%F,,[99'#1F1F  //<<#C+=k]!)LMM}}[1H  (9(9#C+=k]!)LMM8#33$22$22\5K5KK"44e6L6L6NO	OG #C+=k]!)LMM$//9LI..&I2E&D*D
+3(7
+7(++GR8>>@I##H--#2B%abM	&__S1
??$$JEc(C"7??3G5M12.8N+.AA '11%=$&GGH$5	 H $O('2)A%! s   JJz/stream/{event_id}c                   >#    TR                  5       R                  R                  U    n[        S0 UR	                  5       DSU0D6nXl        UR                  R                  5         SS0$ 7fNr   rV  rb  rz   )r   _queueevent_ids_to_eventsr0   
model_dumpdatasignalr   )event_idbodyr   eventr2  s       rl   r}  r~    s\     NN$++??IE&L):LGLDJLL9%%s   A)A,c                  >#    U R                  5       I S h  vN     U R                  5       I S h  vN n[        S0 UD6nTR                  5       R                  R
                  U   n[        S0 UR                  5       DSS 0D6nXTl        UR                  R                  5         U R                  SS05      I S h  vN   M   N N N
! [         a     g f = f7fr  )acceptreceive_jsonr/   r   r  r  r0   r  r  r  r   	send_jsonr   )	websocketr  r  r  r  body_internalr2  s         rl   websocket_endpoint*App.create_app.<locals>.websocket_endpoint!  s     ""$$$!*!7!7!99D&..DNN,33GGQE$7 %//+%59%M "/JLL$$&#--ui.@AAA  % : B& sU   CCCC CB	C ;C<C CC C 
CCCCz/stream/{event_id}/closec                   >#    TR                  5       R                  R                  U    n[        R                  Ul        UR                  R                  5         SS0$ 7f)NrV  rb  )r   r  r  mathinfrun_timer  r   )r  r  r2  s     rl   r}  r~  2  sI     NN$++??IE!XXENLL9%%s   AAz9/stream/{session_hash}/{run}/{component_id}/playlist.m3u8c                d  >#    TR                  5       R                  U    R                  U0 5      R                  US 5      nU(       d	  [        SS9$ SUR                   S3nUR
                   H  nUSUS   S S3-  nXES	    US
    S3-  nM!     UR                  (       a  US-  n[        USS9$ 7f)Nr  r   z9#EXTM3U
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-TARGETDURATION:z*
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
z#EXTINF:durationz.3fz,
r  	extension
z#EXT-X-ENDLIST
zapplication/vnd.apple.mpegurlrk   r   )r   pending_streamsr/  r   max_durationsegmentsended)session_hashruncomponent_idr   playlistsegmentr2  s         rl   r}  r~  9  s        /S"\4(	  C00TU[UhUhTi  jW  XH!??hwz':3&?sCCt}ogk.B-C2FF + ||.. -L s   B-B0z>/stream/{session_hash}/{run}/{component_id}/{segment_id}.{ext}c                d  >^#    US;  a
  [        SSS9$ TR                  5       R                  U    R                  U0 5      R                  US 5      nU(       d
  [        SSS9$ [	        U4S jUR
                   5       S 5      nUc
  [        SSS9$ US	:X  a  [        US
   SS9$ [        US
   SS9$ 7f)N)aactsrh  zUnsupported file extension)r   rk   r  zStream not foundc              3  <   >#    U  H  oS    T:X  d  M  Uv   M     g7f)r  Nrz   )r   s
segment_ids     rl   r   ,App.create_app.<locals>._.<locals>.<genexpr>b  s     P!D'Z:OAAs   	zSegment not foundr4  r  z	audio/aacr)  z
video/MP2T)r   r   r*  r/  r  r,  )r.  r/  r0  r8  extr   r2  r2  s      `   rl   r}  r~  R  s      -'C9UVV  /S"\4(	  C9KLLPvPRVWGC9LMMe|KPPLQQs   B,B0z9/stream/{session_hash}/{run}/{component_id}/playlist-filec                  >#    TR                  5       R                  U    R                  U0 5      R                  US 5      nU(       d	  [        SS9$ UR                  (       do  UR
                   Vs/ s H  oDS   PM	     nnTR                  5       R                  U5      R                  USUR                  S9I S h  vN nUR                  Ul        [        UR                  5      $ s  snf  N/7f)Nr  r%  r  T)	only_filedesired_output_format)r   r*  r/  r   combined_filer,  get_componentcombine_streamr=  r   r   )r.  r/  r0  r   r7  stream_datar>  r2  s          rl   r}  r~  l  s        /S"\4(	  C00''28//B/Qy/B..*"]<0#^#"&.4.J.J $   (5'9'9$ 4 455 Cs   A-C-0C&>=C-;C+<0C-z/file/{path:path}c                0   >#    T" X5      I S h  vN $  N7frq   rz   )r   r   r  s     rl   file_deprecated'App.create_app.<locals>.file_deprecated  s     d,,,,s   z/reset/z/resetc                   #    SS0$ 7f)Nrb  Trz   )r  s    rl   reset_iterator&App.create_app.<locals>.reset_iterator  s      t$$s   z/heartbeat/{session_hash}c                   >^ ^^^^^^ [         R                  " SS5      (       a  SOSmU4S jmU4S jmUUUU UUU4S jn[        U" 5       SS	9$ )
zClients make a persistent connection to this endpoint to keep the session alive.
When the client disconnects, the session state is deleted.
GRADIO_IS_E2E_TESTNg      ?rF  c                 N   >#    [         R                  " T 5      I S h  vN   g N7f)Nwait)rP  rQ  )rR  s   rl   rK  /App.create_app.<locals>.heartbeat.<locals>.wait  s      mmN333 4s   %#%c                 V   >#    T R                   R                  5       I S h  vN   g N7f)Nstop)r   rK  r2  s   rl   stop_stream6App.create_app.<locals>.heartbeat.<locals>.stop_stream  s$     nn))+++ ,s   )')c                  >#      S7v   [         R                  " T" 5       5      n [         R                  " T" 5       5      n[         R                  " X/[         R                  S9I S h  vN u  p#U Vs/ s H  oDR	                  5       PM     nnSU;   a  [         R
                  " 5       e M   NCs  snf ! [         R
                   Ga    [        TTTS9n[        R                  " T[         ST 3TR                  S9n[        T/ TS9nTR                  5       R                  R                  5        VV	s/ s H*  u  p[        S	 U	R                    5       5      (       d  M(  UPM,     Os  sn	nf n
nn	U
 H@  nTR#                  [        R$                  TUUTR                  5       R                  U   US
9  MB     TTR&                  R(                  ;   a  STR&                  R(                  T   l        TR                  5       R,                  R.                  R1                  T/ 5       HY  nTR                  5       R,                  R2                  U   n[4        R6                  Ul        UR:                  R=                  5         M[      g f = f7f)NTzdata: ALIVE

)return_whenrN  r.  z
/hearbeat/r  )r.  r  r   c              3  :   #    U  H  oS    S:X  d  M  Uv   M     g7f)r   unloadNrz   )r   ts     rl   r   FApp.create_app.<locals>.heartbeat.<locals>.iterator.<locals>.<genexpr>  s     "NkqTX=M11ks   	r2  r  
gr_requestfnr   )rP  create_taskrK  FIRST_COMPLETEDresultCancelledErrorr@   r'   r  r8   r   r0   r   fnsitemsr   targetsadd_taskcall_process_apir   session_data	is_closedr  pending_event_ids_sessionr/  r  r!  r"  r#  r  r   )	wait_taskstop_stream_taskdoner}  dr  r   r  idepunload_fn_indicesfn_indexr  r  r2  background_tasksr   r.  rP  ri  rK  s                 rl   iterator3App.create_app.<locals>.heartbeat.<locals>.iterator  sB    4// %,$7$7$?	+2+>+>{}+M((/&9(/(?(?) # 599Dq
D9!T>")"8"8":: * #  : #11 '%gxlS$/$<$<$+*4Z~'N&)mm%	
  3)5B 
 +...*:*>*>*D*D*F-*F""Nckk"NN *F-) -
 ):H -55 + < <$'%)+.#&>>#3#7#7#A*3 6  ): (3+;+;+H+HHTXC,,99,GQ !^^-44NNRR(" % %(NN$4$;$;$O$O (%E .2XXEN!LL,,. O'sa   JA"B3 (B,)
B3 3B.B3 )J,B3 .B3 3A?I?2'E$E$#DI?<J>I??JrY  rZ  )r   r   r   )	r.  r   rp  ri  rq  rR  rP  rK  r2  s	   ```` @@@rl   	heartbeat!App.create_app.<locals>.heartbeat  sE     &(YY/CT%J%JTPRN6 6p %XZ<OPPro   z/run/{api_name}z/run/{api_name}/z/api/{api_name}z/api/{api_name}/c                  >#    [        S0 UR                  5       DSU0D6n[        R                  " T
R	                  5       XS9nT
R	                  5       R
                  (       d)  UR                  (       a  [        S[        R                  S9e[        R                  " UUUUS9n[        R                  " U[         SU  3T
R                  S9n [        R                  " T
UUUUS9I S h  vN nU$  N! [         a~  n[         R"                  " UT
R	                  5       R$                  5      n	['        U[(        5      (       a  UR*                  (       a  [,        R.                  " 5         [1        U	S	S
9s S nA$ S nAff = f7f)Nr   r   api_namer  zcThis API endpoint does not accept direct HTTP POST requests. Please join the queue to use this API.)r8  r   )r[  ri  r   z/api/r  rY  i  )rk   r   rz   )r0   r  r'   get_fnr   api_openqueuer   r   HTTP_404_NOT_FOUNDcompile_gr_requestr  r8   r   rd  BaseExceptionr   error_payload
show_error
isinstancer4   print_exception	traceback	print_excr   )rw  r  r   ri  r[  rZ  r   outputrH  rk   r2  s             rl   predictApp.create_app.<locals>.predict  sR     'L):LGLD##~~'(B >>#,,# A & 9 9  %77!	J $00(\xj9--I
*;;)'   M ! --eS^^5E5P5PQ!%//53H3H'')## # 	sI   C	E<C1 (C/)C1 -E</C1 1
E9;A3E4.E9/E<4E99E<z/call/{api_name}z/call/{api_name}/c                   >#    [        S0 UR                  5       DSS0D6n[        R                  " TR	                  5       XS9nUR
                  Ul        T" XBU5      I S h  vN $  N7f)Nsimple_formatTrv  rz   )r/   r  r'   rx  r   _idro  )rw  r  r   ri  	full_bodyr[  r2  queue_join_helpers         rl   simple_predict_post+App.create_app.<locals>.simple_predict_post  s^      $Ldoo&7LtLI##~~'(B "$I*9xHHHHs   AA(!A&"A(z/queue/joinc                |   >#    U R                   c  [        [        R                  SS9eT" XU5      I S h  vN $  N7f)NzSession hash not found.r7  )r.  r   r   HTTP_400_BAD_REQUEST)r  r   ri  r  s      rl   
queue_join"App.create_app.<locals>.queue_join  sB        (# & ; ;4  +4(CCCCs   2<:<c                  >#    TR                  5       nUR                  R                  c  UR                  R                  T5        UR                  R                  (       a  [        [        R                  SS9e[        S0 U R                  5       DSU0D6n UR                  R                  XUS9I S h  vN u  pEU(       d/  SU;   a  [        R                  O[        R                  n[        XeS9eSU0$  N@7f)NzQueue is stopped.r7  r   )r  r   ri  zQueue is full.r  rz   )r   r  
server_appset_server_appstoppedr   r   HTTP_503_SERVICE_UNAVAILABLEr0   r  pushr  )r  r   ri  r   rb  r  r   r2  s          rl   r  )App.create_app.<locals>.queue_join_helper,  s     
 ^^%F}}''/,,S1}}$$# & C C.  'L):LGLD&,mm&8&8X '9 ' !G  (83 7744 
 $MM))!s   B0C63C44AC6z/cancelc                >  >#    [        U R                   SU R                   315      I S h  vN   TR                  5       nU R                  UR                  R
                  ;   nU R                  UR                  R                  R                  U R                  0 5      ;   nU(       aN  U(       aG  [        0 SU R                  S9nUR                  R
                  U R                     R                  U5        U R                  TR                  ;   ae  TR                   IS h  vN   TR                  U R                  	 TR                  R                  U R                  5        S S S 5      IS h  vN   SS0$ SS0$  GNM N[ N! , IS h  vN  (       d  f       SS0$ = f7f)Nr}  T)r  rb  r  rb  )rL   r.  ro  r   r  pending_messages_per_sessionr  rg  r/  rH   
put_nowaitr   r   r   add)r  r   session_openevent_runningmessager2  s        rl   cancel_event$App.create_app.<locals>.cancel_eventH  sM    4#4#4"5Qt}}o FGHHH^^%F !!V]]%O%OO  ==::>>t?P?PRTUV  1tdmm ::%%*W%}}-888dmm4**..t}}= $8 t$$It$$+ I$ $888 t$$sX   )FE9C3F E<!F$=F !F,E>-F<F>F FF	F
Fz/call/{api_name}/{event_id}c                <   >#    SS jnT" XU5      I S h  vN $  N7f)Nc                   U R                  5       n[        U [        5      (       a*  U R                  (       a  SOSnUS   R	                  S5      nO~[        U [
        5      (       a*  U R                  (       a  SOSnUS   R	                  S5      nO?[        U [        5      (       a  SnS nO%[        U [        5      (       a  SnU R                  nOg SU S[        R                  " U5       S	3$ )
NcompleterH  r  r  
generatingrs  rI  rJ  rK  )r  r  rH   rb  r/  rI   rG   rJ   r  rO  rf   )r  rV  r  r  s       rl   process_msg?App.create_app.<locals>.simple_predict_get.<locals>.process_msgf  s    ((*g'>??*1//JwEx=,,V4D)ABB,3OOLEx=,,V4D)9::'ED)?@@#E"??D x

40@/AFFro   )r  rF   r{   
str | Nonerz   )r   r  r  queue_data_helpers      rl   simple_predict_get*App.create_app.<locals>.simple_predict_geta  s!     
G$ +7kJJJJ   z/queue/datac                <   >#    SS jnT" XU5      I S h  vN $  N7f)Nc                v    S[         R                  " U R                  5       [        S9R	                  S5       S3$ )Ndata: )rd   rv   rK  )re   rf   r  ri   rw   r  s    rl   r  7App.create_app.<locals>.queue_data.<locals>.process_msg  s3    W-?-?-A3 O V VW^ _``deero   )r  rF   r{   ri   rz   )r   r.  r  r  s      rl   
queue_data"App.create_app.<locals>.queue_dataz  s!     
f +7+NNNNr  c                f   >^^^#    TR                  5       mSUUU4S jjn[        U" U 5      SS9$ 7f)Nc                 >#     [         R                  " 5       n U R                  5       I S h  vN (       a"  TR                  R	                  T
S9I S h  vN   g T
TR                  R
                  ;  a  [        [        R                  SS9eSnSnS n TR                  R
                  T
   nUR                  5       nTR                  R                  (       a
  [        SSS9nU(       a  T	" U5      nUb  U7v   [!        U["        5      (       a  UR$                  (       a  TR                  R&                  T
   R)                  UR$                  5        UR*                  [,        R.                  :X  dD  UR*                  [,        R0                  :X  aA  [3        TR                  R&                  T
   5      S	:X  a  [5        5       nT	" U5      nUb  U7v   g GM   GN GN! [         a^    [        R                  " U5      I S h  vN    [         R                  " 5       U-
  U:  a  [        5       n[         R                  " 5       n GNf = f! [6         a  n[        [9        U5      S
9nT	" U5      n[!        U[        R:                  5      (       a9  TR                  R
                  T
	 TR                  R	                  T
S9I S h  vN    Ub  U7v   UeS nAff = f7f)NrT  zSession not found.r7  rF  rG  zServer stopped unexpectedly.F)r  rb  r   r  )rL  rM  rN  r  clean_eventsr  r   r   r{  
get_nowait
EmptyQueuerP  rQ  rG   r  rJ   r  rH   r  rg  removerV  r    server_stoppedprocess_completedlenrD   r}  ri   r_  )r   rT  rR  rS  r  messagesr(  r'  r   r  r.  s           rl   
sse_stream=App.create_app.<locals>.queue_data_helper.<locals>.sse_stream  s    L%)%6%6%8N!(!8!8!:::"(--"<"<,"<"WWW" )#)==#M#MN #0,2,E,E';# 
 *,%)
"&E'-}}'Q'Q ,(H '/&9&9&;G "==00&<(F(-'G #'27';H'3&. *74K L L$+$4$4 & G G$0!""(&)9)9":#*;;-2N2N#N$+KK=3R3R$R(+,2MM,S,S0<-.)*
 ,-)- /A.BG/:7/CH'/';.6$* :W(  * E")--
";;;#002^CnT*:*< 261B1B1DET % 
4 #AG  +73H!!W%;%;<<"MMFF|T$mm88l8SSS+&G
s   K)I G%I GI K8I )G <DI KI I #H>9G<:A H>:I =H>>I 
KA,K7J:8KKKrY  rZ  r[  )r   r   )r   r.  r  r  r   r2  s    `` @rl   r  )App.create_app.<locals>.queue_data_helper  s;     
 ^^%FM M^ %7#. s   +1c                L  #    U R                   R                  S5      n[        U[        5      (       Gas  UR	                  S5      (       Ga\  / n0 nU R                  5        IS h  vN nUR                  5        H  u  pV[        U[        5      (       a  [        U5      S:  a  [        US   [        5      (       al  [        U5       H[  u  px[        U[        5      (       a8  UR                  n	UR                  5       I S h  vN n
UR                  X45        MR  XU SU 3'   M]     M  [        U[        5      (       a8  UR                  n	UR                  5       I S h  vN n
UR                  X45        M  XcU'   GM     S S S 5      IS h  vN   [        [        X2S9US   US   US	   S
9$  U R!                  5       I S h  vN n[#        US   US   US   US	   S
9$  GNu N N NX! , IS h  vN  (       d  f       Nm= f N?! [$         a    ['        [(        R*                  SS9S ef = f7f)Nr  zmultipart/form-datar   r   r  )r  filesr0  r.  fn_name)r  r0  r.  r  r  zInvalid JSON body.r7  )r   r/  r  ri   r  formra  rt  r  StarletteUploadFile	enumerater  readappendr+   r-   rO  r,   r$  r   r   r  )r   content_typer  r  r  rd  r   rl  vr  contentss              rl   get_item_or_file(App.create_app.<locals>.get_item_or_file  s     #??..~>L,,,1H1H%2 2 "<<>>T&*jjl
&ud33 #E
Q *585H I I(1%(8#-a1D#E#E/0zzH56VVX~H$)LL(1E$F9:C5!$6 )9 (/BCC',~~H-2ZZ\'9H!LL()=>(-I% '3 *>* /&D>!%n!5!%n!5 O	  !(/D2!&\%).%9%).%9 $Y	 = * 0> (:! *>>>: 0 !  '$*$?$?3    s   A!H$#G$H$'BG!=G
>AG!G!G!;H$G H$(G= ;G;<G= H$G!G!H$!G8'G*(G84H$;G= =$H!!H$z/component_serverz/component_server/c                  >#    T" U 5      I S h  vN nTR                   UR                     nUR                  nX2;   a  X#   nOTR                  5       R                  U   n[        XAR                  S 5      nUb  [        USS5      (       d  [        [        R                  SS9e[        R                  " U5      (       a  U" UR                  5      I S h  vN $ U" UR                  5      $  N N7f)N_is_server_fnFzFunction not found.r7  )r   r.  r0  r   r   r  r  r   r   r{  inspectiscoroutinefunctionr  )r   r  stater0  blockr[  r2  r  s         rl   component_server(App.create_app.<locals>.component_server  s      *'22D$$T%6%67E,,L$+(//=d3Bz_e!D!D# & 9 90  **2..		]**$))}$# 3 +s"   C.C*CC.C,C.,C.z/queue/status)rC  response_modelc                 \   >#    T R                  5       R                  R                  5       $ 7frq   )r   r  
get_statusrO  s   rl   get_queue_status(App.create_app.<locals>.get_queue_status2  s$      >>#**5577s   ),z/upload_progressc                4   ^  SU 4S jjn[        U" U5      SS9$ )Nc                 >#    [         R                  " 5       nSn U R                  5       I S h  vN (       a  [        R	                  T5        g U(       a  [        R	                  T5        g SnSn [        R                  T5      (       a  SS0nSnO/[        R                  T5      nSUR                  UR                  S.nS	[        R                  " U5       S
37v   M   N! [         a     g [         at    [        R                  " U5      I S h  vN    [         R                  " 5       U-
  U:  a6  SS0nS	[        R                  " U5       S
37v   [         R                  " 5       n Nf = f7f)NFTrF  rG  rV  rj  r  )rV  	orig_name
chunk_sizer  rK  rs  )rL  rM  rN  file_upload_statusesstop_trackingis_donepopr  r  rO  rf   r<   r;   rP  rQ  )r   rT  r  rR  rS  r  r  	upload_ids          rl   r  ?App.create_app.<locals>.get_upload_progress.<locals>.sse_stream<  sF    !%!2!2!4$44666,::9E,::9E%'N!%JA/77	BB',foG&*G%9%=%=i%HF'/-3__.4.?.?'G
 !'tzz'':&;4@@+ 6* = ; A%mmJ777,,.?.P',k&:G$*4::g+>*?t"DD-1->->-@NAsN   ,E.C A E.1A-C" E."
E+,E.."E+DAE+(E.*E++E.rY  rZ  r[  r]  )r  r   r  s   `  rl   get_upload_progress+App.create_app.<locals>.get_upload_progress:  s$     AD %7#. ro   z/uploadc           
     h  >#    U R                   R                  S5      n[        U=(       d    S5      u  pEUS:w  a
  [        SSS9e U(       a  [        R                  U5        TR                  5       R                  nUb  UO[        R                  n[        U R                   U R                  5       SSUU(       a  UOS U(       a  [        OS S9nUR                  5       I S h  vN n/ n/ n/ nUR!                  S5       GHi  n[#        U[$        5      (       d  ['        S5      eUR(                  (       a6  [+        UR(                  5      R,                  n[.        R0                  " U5      nOS[2        R4                  " S5       3n[+        TR6                  5      UR8                  R;                  5       -  nUR=                  SSS9   [>        R@                  " [C        [E        U5      5      [G        U5      5      nURJ                  RM                  5          [N        RP                  " URJ                  R,                  U5        URU                  U5        TRV                  RY                  U5        GMl     U(       a  UR[                  [\        X5        U$  GN! [         a2  n	S	U	R                  ;   a  S
OSn
[        U	R                  U
S9s S n	A	$ S n	A	ff = f! [H         a  n[        SSU 3S9UeS nAff = f! [R         a9    URU                  URJ                  R,                  5        URU                  U5         Nf = f7f)Nr  r   s   multipart/form-datarh  zInvalid content type.r7  i  )	max_files
max_fieldsmax_file_sizer  upload_progresszmaximum allowed sizei  r%  r  z+File is not an instance of GradioUploadFiletmp   T)exist_okparentszInvalid file name: )/r   r/  r"   r   r  trackr   r  r!  r"  r=   r   parser?   r  r   getlistr  r>   	TypeErrorr  r   r  r  !strip_invalid_filename_charactersr   	token_hexr   shar  mkdirr   r  r.   ri   r3   r5   r  closer   renameOSErrorr  upload_file_setr  rc  rC   )r   bg_tasksr  content_type_headerr  r}  r  multipart_parserr  exccodeoutput_filesfiles_to_copy	locations	temp_filer  r  r   destr  r2  r   s                       rl   upload_file#App.create_app.<locals>.upload_filec  s     #*//"5"5n"E23F3L"MOL55#<STTH(..y9 # 0 > >1>1JPTPXPX#8OONN$"#"/+4i$<E$84$  .3355
 LM#%I!\\'2	!)-=>>#$QRR%% $Y%7%7 8 = =I'II)TD !2!21!5 67D !6!67)--:Q:Q:SS	t< ??%c)n57G7MD $$&+IIinn1148 ##D)&&**40= 3> !!0-  Y 6% H4Cs($GGH, ( '$'2EdV0L  +!(()<)<=$$T*+s   AL2BJ !J
"J &CL22K4L2+K,:AL2
J 
K	'K>K	?L2K		L2
K)K$$K))L2,A L/,L2.L//L2z/startup-eventsc                    >#    T R                   (       dL  T R                  5       R                  5         T R                  5       R                  5       I S h  vN   ST l         gg N7f)NTF)r   r   run_startup_eventsrun_extra_startup_eventsrO  s   rl   startup_events&App.create_app.<locals>.startup_events  sO     // 335nn&??AAA/3, Bs   AA$A"A$z
/theme.cssc                 H   > [        T R                  5       R                  SS9$ )Nztext/cssrZ  )r   r   	theme_cssrO  s   rl   r  !App.create_app.<locals>.theme_css  s     %S^^%5%?%?JWWro   z/robots.txtc                 F   > T R                  5       R                  (       a  gg)NzUser-agent: *
Disallow: /zUser-agent: *
Disallow: )r   r  rO  s   rl   
robots_txt"App.create_app.<locals>.robots_txt  s    ~~%%32ro   z	/pwa_iconz/pwa_icon/{size}c                  >#    TR                  5       nUR                  nUc	  [        SS9eU c  [        U5      $ SS KnUR
                  R                  U5      nUR                  X 45      n[        R                  " 5       nUR                  USS9  UR                  S5        [        [        R                  " UR                  5       5      SS9$ 7f)Nr  r%  r   PNG)formatrV   rZ  )r   r   r   r   	PIL.ImageImageopenresizeioBytesIOsaveseekr   r  )sizer   r   PILimgimg_byte_arrayr2  s         rl   pwa_icon App.create_app.<locals>.pwa_icon  s      ^^%F!..L##44|#L11))...C**d\*CZZ\NHH^EH2"$

>..01k s   C
Cz/manifest.jsonc                 x  > TR                   (       d	  [        SS9eTR                  n U c	  SSSSS./nOXU R                  S5      (       a  TR	                  S5      SSSS./nO*TR	                  SS	S
9SSSS.TR	                  SSS
9SSSS./n[        TR                  5       R                  =(       d    SUSSS.SS9$ )Nr  r%  zstatic/img/logo_nosize.svgr   zimage/svg+xml)srcsizesr  purposez.svgr"     )r  192x192rV   i   512x512Gradioz./
standalone)r  icons	start_urldisplayzapplication/manifest+jsonr)  )pwar   r   r   url_path_forr`   r   title)r   r-  r2  r   s     rl   manifest_json%App.create_app.<locals>.manifest_json  s    ::#44!..L#  <!& /#(	 &&v..  #//
;!& /#(	  #//
/E!* +#(	  #//
/E!* +#(	 "  NN,22>h"!%+ 7	 	ro   /monitoringc                   >#    TR                   (       d
  [        SSS9e[        R                  " U [         S3TR
                  S9nU STR                   3n[        SU S35        [        S	5      $ 7f)
Nr  Monitoring is not enabled.r7  r5  r  /monitoring/z* Monitoring URL: z *zSee console for monitoring URL.)	enable_monitoringr   r'   r  r8   r   r   r%  r   )r   root_urlmonitoring_urlr2  r   s      rl   analytics_login'App.create_app.<locals>.analytics_login  s     ++# #,H  #//(\5--H
 !)zc6G6G5HIN&~&6b9: ABBs   A.A1z/monitoring/{key}c                  >#    TR                   (       d
  [        SSS9e[        U TR                  5      (       a  STR                   S3nTR                  (       d_  SSKJn  SSKJn  [        TX1S	9  UR                  R                  5         TR                  5       R                  R                  nXBS
'   STl        [        U[        R                  S9$ [        SSS9e7f)Nr  r7  r7  r8  z
/dashboardr   )r  )demo)r   r  Tra  zInvalid key.)r9  r   rA   r   r   gradio.monitoring_dashboardr  r?  mount_gradio_appr  r
  r   event_analyticsr%   r   rl  )rd  analytics_urlr  	dashboard	analyticsr2  r   s        rl   analytics_dashboard+App.create_app.<locals>.analytics_dashboard  s     ++# #,H  *#s/@/@AA".s/@/@.A L--@M$S)H$$**, # 0 7 7 G GI#,L-1C*'%63H3H  $NKKs   CCrz   r[  )r   r\  r{   Optional[str])r:  ri   )r   r\  r{   r   )ro  r   )r   ri   )r   r\  r:  ri   r  ri   )
r  ri   r  zLiteral['client', 'server']r  ri   r  ri   r  r\  )r  ri   )r  ri   r   r\  )r  ri   r  r/   r   r\  )r  r   r  ri   )r  ri   )r.  ri   r/  r"  r0  r"  )
r.  ri   r/  r"  r0  r"  r8  ri   r:  ri   )r   ri   r   r\  )r  r1   )r.  ri   r   r\  rp  r   ri  ri   )rw  ri   r  r/   r   r\  ri  ri   )rw  ri   r  r2   r   r\  ri  ri   )r  r/   r   r\  ri  ri   )r  r*   )r   r\  r  ri   )r   r\  r.  ri   )r   r\  r.  ri   r  z$Callable[[EventMessage], str | None])r   r\  r{   z7Union[ComponentServerJSONBody, ComponentServerBlobBody])r  ri   r   r\  rq   )r   r\  r  r   r  rH  )r  
int | None)rd  ri   )r   r`   delete_cacherB   r/  r   r   r8   r   r(   IS_WASMadd_middlewarer9   
middlewarer   postr   expects_oauthr7   r  headr   r  rE   r   include_router)5r   
app_kwargsr   r  ssr_moderJ  routerr)  r;  r>  rA  r^  rp  rv  r}  r  pagesetr  r  r  r  r  r  r  rC  rF  rs  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r3  r<  rF  r   r2  r  r3  r  r  r  r  r  s5   `                                           @@@@@@@@@rl   
create_appApp.create_appJ  s#     %2
6G**:l!8NN:t,"
/;"

: L/LZLtL*-&!!!3M^^F#  (   $  D 
G		H		) 
 
	) 
N	#	O	$$+,<$= 	 	 
% 
$	 
H		I		C 
 
	C 
I		J		7 
  
	7 
M1E0F	G	$	 
H	@ 
(		)	9@ 	V 	V 
 
	VF ::!cjj&>&> WWY#*+;#< 
  
   
   
&	'	 
(		1 ||G1:DrzD! $
 
#l	3	\	2   01E	$E	E	 E	 E	 
3 
4E	N 
HGK,@+A	B	G7;+?*@	A	  
B 
C	  
7;+?*@	A	'+*>)?	@	2 
A 
B	2 
&	'	- 
(	- 
M	N;	7;	74;	7 ;	7 	;	7
 !;	7 
O;	7z 
&	'	, 
(	, 
	 	9 
!	9 
-W[=Q<R	S	,GK<P;Q	R	 
S 
T	$ 
/w{?S>T	U	.gk>R=S	T9	 
U 
V9	v 
)	*	& 
+	& 
		.	/	 
0	  
/	0	& 
1	& 
O	P	 
Q	0 
T	U	R	R$'	R7:	RHK	RRU	R 
V	R2 
O	P	6 
Q	62 
'w{7K6L	M	- 
N	- 
Y		X		% 
 
 	% 
/	0
 $$45	K	QK	Q$K	Q .K	Q 	K	Q 
1K	Q\ 
&gk6J5K	L	'w{7K6L	M	&gk6J5K	L	'w{7K6L	M
 $$45	+	+	+	 %+	 	+	 
N 
M 
N 
M+	Z 
'w{7K6L	M	(8L7M	N
 $$45		I	I#	I %	I 		I 	I 
O 
N	I 
]'+2F1G	H $$45
	D
	D$
	D 
	D 
I
	D	*	*$	* 	*8 
Y		% 
 	%0 
1AU@V	W	K$	K	K 
X	K0 
M1E0F	G	O$	O	O 
H	OY	$Y	Y	 >Y	v3	 $3	 D3	 j 
!+./ 
 

 
 !+./ 
 

	%$	%	

	

	%, 
!+./, 
 


	8


	8 
&	'&	 
(&	P 
Ygk.B-C	D (,E	 $E	 %E	  %E	  E	  
EE	 N 
%	&	 
'	 
L1B	C	.?	@	X 
A 
D	X 
/@	A	3 
B	3 
		#	$	 	 
% 
	, 
!	"0	 
#0	d 
M1E0F	G	C 
H	C 
'	(	L 
)	L, 	6"
ro   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rq   )r   .Callable[[fastapi.Request], str | None] | None)r  r   )r   r\  r   ri   r   r"  r   r"  r   ri   r   ri   r{   r   )r   gradio.Blocksr{   None)r{   rY  )NNTF)r   rY  rR  dict[str, Any] | Noner   rX  r  boolrS  r\  r{   r   )r}   r~   r   r   __doc__r   r   r   AsyncClientr   r   r   r   r   r  r  rV  r   __classcell__)r   s   @rl   r   r      s    H KO #G #  #J  F '
 '
'
 '
 	'

 '
 '
 
'
 '
R-"
"!
  -1JN hh)h Hh 	h
 h 
h hro   r   c                   US:X  a  [         R                  " S5      e[        R                  " U5      (       a  [         R                  " S5      e [	        [
        R                  " X5      5      nUR                  5       (       a  [         R                  " S5      eUR                  5       (       d  [         R                  " S5      e[        U5      $ ! [         a  n[         R                  " S5      UeSnAff = f)zSafely join the user path to the directory while performing some additional http-related checks,
e.g. ensuring that the full path exists on the local file system and is not a directory
r   rh  r  Nr  )fastapir   r'   r  r   r   r  r5   r  r  ri   )r   r   fullpathr'  s       rl   r{  r{  ;  s     rz##C((''--##C((0	89 ##C((??##C((x=  0##C(a/0s   C 
C5C00C5c                Z   / n/ nU  H  n[         R                  " U5      =(       d    SnUR                  S5      nU H@  nSU;   d  M  UR                  UR                  S5      S   R                  S5      S   5        MB     UR                  US   R                  S5      S   5        M     X4$ )	Nr   r(  zvalue (r   )r   r   )r  getdocr  r  )cls_setdocsettypesclsdoc	doc_lineslines          rl   	get_typesrn  N  s    FEnnS!'RIIdO	DD TZZ	215;;C@CD  	il((-b12  =ro   T)r   r  r   r   r  r  r   r  r  rS  r#  r   c                 ^^ Ub  US:w  a  [         R                  " S5        SUl        Ub  XQl        [        R
                  " U5      Ul        UR                  5       Ul        UR                  5         X!l
        XAl        X1l        Ub  U	b  [        S5      eU(       aI  [        U5      (       d9  [        US   [         5      (       d!  [        US   ["        5      (       d	  U/Ul        OXql        Xl        Xl        U=(       d    / Ul        U=(       d    / Ul        Xl        [        UR*                  ["        5      (       d  [        S5      e[        UR,                  ["        5      (       d  [        S5      eU
b  Xl        [2        R4                  (       a  SO,Ub  UO'[6        R8                  " SS	5      R;                  5       S
:H  Ul        [6        R>                  RA                  S[2        R4                  (       a  SO	[C        5       5      Ul"        UUl#        UUl$        UR<                  (       a<  [K        URF                  URH                  URD                  S9u  Ul#        Ul&        Ul$        [N        RQ                  UUU	UR<                  S9mU RR                  RT                  m[V        RX                  SUU4S jj5       nUU RR                  l*        U R[                  UT5        U $ )a  Mount a gradio.Blocks to an existing FastAPI application.

Parameters:
    app: The parent FastAPI application.
    blocks: The blocks object we want to mount to the parent app.
    path: The path at which the gradio application will be mounted, e.g. "/gradio".
    server_name: The server name on which the Gradio app will be run.
    server_port: The port on which the Gradio app will be run.
    app_kwargs: Additional keyword arguments to pass to the underlying FastAPI app as a dictionary of parameter keys and argument values. For example, `{"docs_url": "/docs"}`
    auth: If provided, username and password (or list of username-password tuples) required to access the gradio app. Can also provide function that takes username and password and returns True if valid login.
    auth_message: If provided, HTML message provided on login page for this gradio app.
    auth_dependency: A function that takes a FastAPI request and returns a string user ID or None. If the function returns None for a specific request, that user is not authorized to access the gradio app (they will see a 401 Unauthorized response). To be used with external authentication systems like OAuth. Cannot be used with `auth`.
    root_path: The subpath corresponding to the public deployment of this FastAPI application. For example, if the application is served at "https://example.com/myapp", the `root_path` should be set to "/myapp". A full URL beginning with http:// or https:// can be provided, which will be used in its entirety. Normally, this does not need to provided (even if you are using a custom `path`). However, if you are serving the FastAPI app behind a proxy, the proxy may not provide the full path to the Gradio app in the request headers. In which case, you can provide the root path here.
    allowed_paths: List of complete filepaths or parent directories that this gradio app is allowed to serve. Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app.
    blocked_paths: List of complete filepaths or parent directories that this gradio app is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default.
    favicon_path: If a path to a file (.png, .gif, or .ico) is provided, it will be used as the favicon for this gradio app's page.
    show_error: If True, any errors in the gradio app will be displayed in an alert modal and printed in the browser console log. Otherwise, errors will only be visible in the terminal session running the Gradio app.
    max_file_size: The maximum file size in bytes that can be uploaded. Can be a string of the form "<value><unit>", where value is any positive integer and unit is one of "b", "kb", "mb", "gb", "tb". If None, no limit is set.
    show_api: If False, hides the "Use via API" button on the Gradio interface.
    ssr_mode: If True, the Gradio app will be rendered using server-side rendering mode, which is typically more performant and provides better SEO, but this requires Node 20+ to be installed on the system. If False, the app will be rendered using client-side rendering mode. If None, will use GRADIO_SSR_MODE environment variable or default to False.
    node_server_name: The name of the Node server to use for SSR. If None, will use GRADIO_NODE_SERVER_NAME environment variable or search for a node binary in the system.
    node_port: The port on which the Node server should run. If None, will use GRADIO_NODE_SERVER_PORT environment variable or find a free port.
Example:
    from fastapi import FastAPI
    import gradio as gr
    app = FastAPI()
    @app.get("/")
    def read_main():
        return {"message": "This is your main app"}
    io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
    app = gr.mount_gradio_app(app, io, path="/gradio")
    # Then run `uvicorn run:app` from the terminal and navigate to http://localhost:8000/gradio.
r  zThe 'favicon_path' parameter is set but will be ignored because 'path' is not '/'. Please add the favicon directly to your FastAPI app.Fz^You cannot provide both `auth` and `auth_dependency` in mount_gradio_app(). Please choose one.r   z.`allowed_paths` must be a list of directories.z.`blocked_paths` must be a list of directories.GRADIO_SSR_MODEFalsetrueGRADIO_NODE_PATHr   )r   server_port	node_path)rR  r   rS  c                 >#    T" U 5       IS h  vN   TR                   R                  T5       IS h  vN   TR                  5       R                  5         TR                  5       R	                  5       I S h  vN   S 7v   S S S 5      IS h  vN   S S S 5      IS h  vN   g  N Np N. N! , IS h  vN  (       d  f       N0= f N'! , IS h  vN  (       d  f       g = f7frq   )rT  lifespan_contextr   r	  r
  )r2  
gradio_appold_lifespans    rl   new_lifespan&mount_gradio_app.<locals>.new_lifespan  s     
 
 "((99*EE%%'::< ++-FFHHH FE
 
 
 FH FEEE
 
 
 
s   C!B#C! CB%CA B+8B'9	B+CB)CC!CC!%C'B+)C+C	1B42C	>CC!CCCC!)r2  r   ).warningswarndev_modeshow_apir   _parse_file_sizer  get_config_filer  validate_queue_settingsr  rt  r   r   r   r  tuplert  r   r  r   r  r  r  r   r(   rK  r   r   lowerrS  environr/  rM   ru  r#  r   r6   r  r   rV  rT  rw  
contextlibasynccontextmanagermount)r2  r   r   r   rt  r  rR  r   r  r   r   r  r  r   r  r  rS  r#  r   rz  rx  ry  s                       @@rl   rA  rA  [  sq   p DCKC	

 FO" 11-@F**,FM
""$#$$O7l
 	
 	47E**47D))f&&(.BF(.BF"f**D11IJJf**D11IJJ$  	 # ,g6<<>&H O zz~~*"4"4B-/F /F F"33",, ** 	G!4f6F '	   J ::..L## $ #/CJJIIdJJro   )z	theme.cssz
robots.txtr"  zmanifest.jsonrp  rv  ry  r  staticassetszfavicon.ico
gradio_api)r   r.   r   r3   r{   ri   )rg  z
list[type])r  i  NN)(r2  fastapi.FastAPIr   rY  r   ri   r   ri   rt  r"  r  bool | NonerR  r[  r   z9Callable | tuple[str, str] | list[tuple[str, str]] | Noner  r  r   rX  r   r  r  list[str] | Noner  r  r   r  r  r\  r  zstr | int | NonerS  r  r#  r  r   rI  r{   r  )r]  
__future__r   rP  r  r  importlib.resources	importlibr  r  rO  r!  r  r   r   r  rL  r  r|  collections.abcr   r   pathlibr   rz  r   r  typingr   r	   r
   r   r   r   ra  r   r   re   r   r   r   r   r   r   fastapi.responsesr   r   r   r   r   r   fastapi.securityr   fastapi.templatingr   fastapi.websocketsr   r   gradio_clientr   r  gradio_client.documentationr   gradio_client.utilsr    jinja2.exceptionsr!   python_multipart.multipartr"   starlette.backgroundr#   starlette.datastructuresr$   r  starlette.responsesr%   rQ   r&   r'   r(   gradio.contextr)   r  r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   gradio.exceptionsr4   r5   gradio.node_serverr6   gradio.oauthr7   gradio.route_utilsr8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   gradio.server_messagesrD   rE   rF   rG   rH   rI   rJ   gradio.state_holderrK   gradio.utilsrL   rM   rN   rO   gradio.blocksrP   init	resourcesr  joinpathas_posixSTATIC_TEMPLATE_LIBr  rz  VERSIONr  r`   r   rR   envfiltersr^  LimitsTimeoutr   r  r   r{  rn  rA  r  rz   ro   rl   <module>r     ss  U #      	    	  
    3  %        7 . = / 0 - . ; / F 0  B B "   6 &      ,  # 
 h'00=FFH  h'X)*XZ	 h'X)*XZ	 
  ?\ ?& &9:	$,	  j !			<<"$ MM$
 *+ m' mj+&
 

 ! (,Q GK#FJ &*&*#&* #' )Q	QQ Q 	Q
 Q Q &Q DQ Q DQ Q $Q $Q Q  !Q" $#Q$ %Q& !'Q( )Q* +Q Qhro   