
    5pjG                     R   S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	r	SSK
JrJrJrJrJrJrJrJrJrJrJrJr  SSKJr  SSKJrJr  SS	KJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&  SS
K'J(r(J)r)  SSK*J+r+  SSK,J-r-J.r.  \&" SS9r/\&" SS9r0\/Rc                  \0SS9  \&" SS9r2\/Rc                  \2SS9  \\3\(" SS94   r4\S\3S\3S-  S\3S-  S\\   4S j5       r5\/Rm                  S/ SQS9            SrS\\3S-  \(" SS94   S\\3S-  \)" SS S94   S!\-S"\\3S-  \)" S#S94   S$\S%\ S&\S'\S(\"S\.S)\\7\)" S*S+S94   S\!SS4S, jj5       r8\/Rm                  S-S.S/0S0S1/S29      SsS\4S3\\9\3   \(" S4S94   S5\\3S-  \)" S6S7S8S94   S$\S&\S9\\:S-  \)" S:S;S94   S\.S\!SS4S< jj5       r;\/Rm                  S=S.S/0S>S?/S29     StS\4S3\\9\3   \(" S@S94   S5\\3S-  \)" S6S7S8S94   S$\S&\S\.S\!SS4SA jj5       r<\/Rm                  SBSCSD/S9  SuSE\\3\(" SFS94   SG\\3\(" SHS94   S\.S\!SS4
SI jj5       r=\/Rm                  SJ/ SKQS9     SvS\\3S-  \(" SLS94   SM\\7\)" SNSOS94   SP\\7\)" SQSRSSS94   S\.S\!SS4ST jj5       r>\0Rm                  S/ SUQS9SS\SSSS4S\\3S-  \(" SVS94   S!\-SW\\?\)" SXSSY\ SZ3S[94   S\\\?S-  \)" S]SS^S[94   S"\\3S-  \)" S_S94   S\.S\!SS4S` jj5       r@\0Rm                  SaSb/S9  SuS\.S\!SS4Sc jj5       rA\0Rm                  SdSe/S9   SwSf\\3\(" SgS94   SP\\7\)" SQSRSSS94   S\.S\!SS4
Sh jj5       rBSi\3\9\3   -  S\34Sj jrCS\S\34Sk jrD\2Rm                  SaSl/S9  SuS\4S\.S\!SS4Sm jj5       rE\2Rm                  SJSn/S9  SuS\4So\\?\(" SpS94   S\.S\!SS4
Sq jj5       rFg)xzCContains commands to run and manage sandboxes on Hugging Face Jobs.    N)contextmanager)	AnnotatedAnyIterator)DEFAULT_IDLE_TIMEOUTDEFAULT_IMAGEDEFAULT_SANDBOXES_PER_HOST
MODE_LABEL	MODE_POOL
POOL_LABELSANDBOX_LABELSHARED_ID_SEPSandboxSandboxPoolSandboxProcess_split_sandbox_id)delete_pool_cache)CLIErrorSandboxError   )

EnvFileOptEnvOptSecretsFileOpt
SecretsOptTokenOpt
VolumesOpt
get_hf_apiparse_env_mapparse_volumestyper_factory)ArgumentOption)out)	FlavorOptNamespaceOptz.Run and manage sandboxes on Hugging Face Jobs.)helpzBWarm pools of host VMs and spawn cheap shared sandboxes from them.pool)namez8List and stop background processes running in a sandbox.processz1The sandbox id as printed by `hf sandbox create`.
sandbox_id	namespacetokenreturnc             #      #    [         R                  " XUS9n Uv   UR                  5         g! UR                  5         f = f7f)zIReattach to a sandbox and close the HTTP client when the command is done.r+   r,   N)r   connectclose)r*   r+   r,   sandboxs       W/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/huggingface_hub/cli/sandbox.py_connectr4   B   s3      oojUKGs   A. AA  Acreate)zhf sandbox createzhf sandbox create ubuntu:24.04z%hf sandbox create --flavor a10g-smallz@hf sandbox create --pool pool-ab12cd34ef56 --env LOG_LEVEL=debug)examplesimagezDocker image (needs /bin/sh).z--poolzJSpawn a cheap shared sandbox in this pool (from `hf sandbox pool create`).flavoridle_timeoutzTAuto-terminate the sandbox after this much inactivity (e.g. '10m'). Defaults to 10m.envsecretsenv_filesecrets_filevolumeforward_hf_tokenz--forward-hf-tokenz0Inject your HF token as HF_TOKEN in the sandbox.c                    [         R                   " 5       nUb  UO[        nUb  U c
  Uc  U(       a  [        S5      eU(       d  U(       a  [        S5      e[        R                  " XUS9R                  [        XF5      UU
S9n[        R                  " SUR                  UR                  U[         R                   " 5       U-
  S S3S	9  [        R                  " S
UR                   S35        [        R                  " SUR                   S35        g[        R
                  " U =(       d    [        U=(       d    SU[        XF5      [        XW5      [        U5      U	U
US9	nUR                  5         [        R                  " SUR                  UR                   [         R                   " 5       U-
  S S3S9  [        R                  " S
UR                   S35        [        R                  " SUR                   S35        g)a~  Create a sandbox: a dedicated VM by default, or a cheap shared one with `--pool`.

Env and idle-timeout apply to the sandbox in both modes. With `--pool`, the image and
flavor come from the pool, so passing them here is an error; `--secrets` is also
rejected since pooled sandboxes have no encrypted-secrets channel (use `--env`). Define
a pool first with `hf sandbox pool create`.
NzQ--pool fixes the image/flavor (and volumes aren't supported); drop those options.zF--pool can't encrypt secrets; pass them with --env/--env-file instead.r/   )r:   r9   r?   zSandbox ready.1fs)idhostr'   elapsedz$Run a command with `hf sandbox exec z -- echo hello`.z#Terminate it with `hf sandbox kill `.	cpu-basic)	r7   r8   r9   r:   r;   volumesr+   r?   r,   )rC   r7   rE   )timer   r   r   r0   r5   r   r#   resultrC   host_idhintr   r   r   r1   r7   )r7   r'   r8   r9   r:   r;   r<   r=   r>   r+   r?   r,   startidlesbxr2   s                   r3   sandbox_createrP   L   s   L IIKE'3<9MD 2fnoolcdd!!$5IPPc,- Q 

 	

?svvCKKdW[W`W`WbejWjknVoopTqr7x?OPQ6svvhbABnn$}$#(g4f%)
G MMOJJ7::W]]tyy{]bObcfNgghLijHH3GJJ<?OPQHH27::,bAB    execignore_unknown_optionsTz5hf sandbox exec <sandbox_id> -- python -c "print(42)"z1hf sandbox exec -w /app <sandbox_id> -- pytest -x)context_settingsr6   commandzThe command to run.workdirz-wz	--workdirzWorking directory.exec_timeoutz	--timeoutz)Kill the command after this many seconds.c                    S[         SS4S jnS[         SS4S jn	[        XUS9 n
U
R                  [        U5      [	        X45      UUUU	SS9nSSS5        WR
                  (       aJ  [        R                  " S	U S
35        [        R                  R                  UR                  =(       d    SS9eUR                  S:w  a8  [        R                  R                  UR                  b  UR                  S9eSS9eg! , (       d  f       N= f)zRun a command in a sandbox, streaming output. Exits with the command's exit code.

To start a long-running command in the background instead of waiting for it, use
`hf sandbox spawn`.
datar-   Nc                 ~    [         R                  R                  U 5        [         R                  R                  5         g N)sysstdoutwriteflushrY   s    r3   write_stdout"sandbox_exec.<locals>.write_stdout   $    



rQ   c                 ~    [         R                  R                  U 5        [         R                  R                  5         g r[   )r\   stderrr^   r_   r`   s    r3   write_stderr"sandbox_exec.<locals>.write_stderr   rc   rQ   r/   F)r:   cwdtimeout	on_stdout	on_stderrcheckzCommand timed out after zs.|   )coder   r   )strr4   runlistr   	timed_outr#   errorclick
exceptionsExit	exit_code)r*   rU   rV   r:   r<   rW   r+   r,   ra   rf   r2   rJ   s               r3   sandbox_execrx      s   43 4 3 4  
*	?7Mc, ""  
 
@ 		,\N"=>##)9)9)@S#AA1##V=M=M=Y)9)9#aa_`#aa  
@	?s   (C99
Dspawnz;hf sandbox spawn <sandbox_id> -- python -m http.server 8000z8hf sandbox spawn -w /app <sandbox_id> -- uvicorn app:appz%The command to run in the background.c                 V   [        XUS9 nUR                  [        U5      [        X45      USS9nSSS5        [        R
                  " SU WR                  S9  [        R                  " SU  S35        [        R                  " S	U  S
UR                   S35        g! , (       d  f       Np= f)zStart a long-running command in the background and return its pid (don't wait).

List a sandbox's processes with `hf sandbox process ls` and stop one with
`hf sandbox process kill`.
r/   T)r:   rh   
backgroundNzProcess startedr2   pidz+List processes with `hf sandbox process ls rF   z&Stop it with `hf sandbox process kill  )r4   rp   rq   r   r#   rJ   r}   rL   )	r*   rU   rV   r:   r<   r+   r,   r2   r)   s	            r3   sandbox_spawnr      s    , 
*	?7++d7ms1MSZgk+l 
@JJ *'++FHH::,bIJHH5j\7;;-rRS	 
@	?s   %B
B(cpz2hf sandbox cp data.csv <sandbox_id>:/data/data.csvz7hf sandbox cp <sandbox_id>:/app/result.json result.jsonsrcz,Source: a local path or <sandbox_id>:<path>.dstz1Destination: a local path or <sandbox_id>:<path>.c                    S[         S[        [         S-  [         4   4S jnU" U 5      u  pVU" U5      u  pxUSL USL :X  a  [        S5      eUb/  [        XRUS9 n	U	R                  R                  Xh5        SSS5        O3Uc   e[        XrUS9 n	U	R                  R                  Xh5        SSS5        [        R                  " SXS9  g! , (       d  f       N$= f! , (       d  f       N5= f)	zCCopy a file between the local machine and a sandbox (docker-style).refr-   Nc                     SU ;   a<  U R                  S5      (       d&  U R                  SS5      u  p[        U5      S:  a  X4$ S U 4$ )N:)./~r   )
startswithsplitlen)r   r*   paths      r3   parsesandbox_cp.<locals>.parse   sK     #:cnn_=="yya0J:"!''SyrQ   zHExactly one of SRC and DST must be a sandbox path (<sandbox_id>:<path>).r/   Copied)r   r   )	ro   tupler   r4   filesdownloaduploadr#   rJ   )
r   r   r+   r,   r   src_sandboxsrc_pathdst_sandboxdst_pathr2   s
             r3   
sandbox_cpr      s    3 5tS1  "#JK!#JKt!45abbkeDMM""86 ED &&&keDMM  4 EJJxS* ED EDs   CC
C
C%kill)zhf sandbox kill <sandbox_id>zKhf sandbox kill <host_id>   # kills a whole shared host (all its sandboxes)zhf sandbox kill --allz$The sandbox or host id to terminate.all_z--allz2Terminate every sandbox and host in the namespace.yesz-yz--yesz$Answer Yes to prompts automatically.c                    [        US9nU(       a  [        UR                  SS/[        S0US95      nU(       d  [        R
                  " S5        g[        R                  " S[        U5       S	3US
9  U H1  nUR                  UR                  UR                  R                  S9  M3     [        R                  " S[        U5      S9  gU c  [        S5      e[        X5      u  p[        U;   a:   [!        XUS9 n
U
R#                  5         SSS5        [        R                  " SU S9  gUR)                  XS9nUR*                  =(       d    0 R-                  [.        5      [0        :X  a]  [        R                  " SU S3US
9  UR                  UR                  UR                  R                  S9  [        R                  " SUS9  g [!        XUS9 n
U
R#                  5         SSS5        [        R                  " SU S9  g! , (       d  f       N= f! [$         a  n[        ['        U5      5      UeSnAff = f! , (       d  f       N\= f! [$         a  n[        ['        U5      5      UeSnAff = f)z@Terminate a sandbox, a whole shared host, or everything (--all).r,   RUNNING
SCHEDULING1statuslabelsr+   zNo running sandboxes.N
Terminate zA sandbox job(s) (including shared hosts and all their sandboxes)?r   job_idr+   
Terminated)jobsz*Provide a sandbox id, a host id, or --all.r/   zSandbox terminated)rC   zTerminate shared host z and all of its sandboxes?zHost terminated)r   rq   	list_jobsr   r#   textconfirmr   
cancel_jobrC   ownerr(   rJ   r   r   r   r4   r   r   ro   inspect_jobr   getr
   r   )r*   r   r   r+   r,   apir   jobsidnsr2   es               r3   sandbox_killr     s     5
!CCMM)\)BM[^K_ktMuvHH,-jT+lmsvwCNN#&&CIINNNC 

<c$i0CDD
6GC	**G7 H 	

'J7 ///
3C

bj)Y6,SE1KLRUVcff		?

$-&jUCwLLN D JJ#
3' HG 	*3q6")	* DC &s1vA%&sl   /
H 9G>
H 
I H6 I >
HH H 
H3H..H36
I I I 
I+I&&I+)zhf sandbox pool createz5hf sandbox pool create python:3.12 --flavor cpu-basicz7hf sandbox pool create --per-host 50 --idle-timeout 30mz+Docker image for the hosts (needs /bin/sh).per_hostz
--per-hostz&Sandboxes packed per host VM (default z).)minr&   	max_hostsz--max-hostsz'Optional cap on the number of host VMs.zZShut a host down once it has had no sandboxes for this long (e.g. '10m'). Defaults to 10m.c           
         [         R                   " 5       nU =(       d    [        n [        U U=(       d    SUUUb  UO[        UUS9nUR                  n	UR
                  n
[        R                  " SU	U U=(       d    SU
S   [         R                   " 5       U-
  S S3S9  [        R                  " S	U	 S
35        [        R                  " SU	 S
35        g)zRWarm a pool: boot one host VM now, tagged so it can be found later by its pool id.rG   N)r7   r8   sandboxes_per_hostr   r9   r+   r,   zPool createdr   rA   rB   )rC   r7   r8   rD   rE   z/Spawn a sandbox with `hf sandbox create --pool rF   z=Delete the pool (and its hosts) with `hf sandbox pool delete )	rI   r   r   r   r(   host_idsr#   rJ   rL   )r7   r8   r   r   r9   r+   r,   rM   r'   pool_idr   s              r3   pool_creater   G  s    4 IIKE"]E$#%1%=\CWD iiG}}HJJ$a[99;&s+1- HH>wirJKHHLWIUWXYrQ   z	ls | listzhf sandbox pool lsc                    [        US9n0 nUR                  S[        [        0U S9 H  nUR                  =(       d    0 R                  [        5      nU(       d  M4  [        UR                  [        5      (       a  UR                  O0 nUR                  UUUR                  =(       d    UR                  UR                  UR                  SS5      SS.5      nUS==   S	-  ss'   M     [        UR                  5       5      n[         R"                  " US
S9  U(       d  [         R$                  " S5        g[         R$                  " S5        g)z9List running sandbox pools (grouped from their host VMs).r   r   r   SBX_CAPACITY r   )rC   r7   r8   r   hostsr   r   rC   id_keyz)Create one with `hf sandbox pool create`.z5Spawn a sandbox with `hf sandbox create --pool <id>`.N)r   r   r
   r   r   r   r   
isinstanceenvironmentdict
setdefaultdocker_imagespace_idr8   rq   valuesr#   tablerL   )	r+   r,   r   poolsr   r}   r:   inforowss	            r3   pool_lsr   z  s     5
!C')E}}Iz96MYb}czzR$$Z0!+COOT!B!Bcoo))9S\\**GGNB7	
 	W d  DIId4 <=HIrQ   zdelete | rmz hf sandbox pool delete <pool_id>r   zPool id to delete.c           	         [        US9n[        UR                  SS/[        [        [
        U 0US95      nU(       d&  [        U 5        [        R                  " SU  S35        g[        R                  " S[        U5       S	U  S
3US9  U H1  nUR                  UR                  UR                  R                  S9  M3     [        U 5        [        R                  " SU [        U5      S9  g)zDTerminate every host VM of a pool (and therefore all its sandboxes).r   r   r   r   zNo running hosts for pool 'z'.Nr   z host(s) of pool 'z' (and all their sandboxes)?r   r   zPool deleted)rC   hosts_terminated)r   rq   r   r
   r   r   r   r#   r   r   r   r   rC   r   r(   rJ   )r   r   r+   r,   r   r   r   s          r3   pool_deleter     s     5
!C|,	:w? 	 	
E '".wir:;KK*SZL(:7)C_`fijcff		? gJJ~'CJGrQ   cmdc                 R    [        U [        5      (       a  U $ SR                  U 5      $ )Nr~   )r   ro   join)r   s    r3   _fmt_cmdr     s!    S#&&39CHHSM9rQ   c                 d    U R                   (       a  gU R                  c  S$ SU R                   S3$ )Nrunningexitedzexited ())r   rw   )r)   s    r3   _fmt_statusr     s3    ((08UARAR@SST6UUrQ   z"hf sandbox process ls <sandbox_id>c                    [        XUS9 nUR                  5       nSSS5        W Vs/ s H.  oUR                  [        U5      [	        UR
                  5      S.PM0     nn[        R                  " USS9  U(       d  [        R                  " SU  S35        g[        R                  " SU  S	35        g! , (       d  f       N= fs  snf )
zUList the background processes running in a sandbox (started with `hf sandbox spawn`).r/   N)r}   r   r   r}   r   z!Start one with `hf sandbox spawn z -- <cmd>`.z'Stop one with `hf sandbox process kill z <pid>`.)	r4   	processesr}   r   r   r   r#   r   rL   )r*   r+   r,   r2   r   pr   s          r3   
process_lsr     s     
*	?7%%'	 
@V_`V_QREE[^HQUUOLV_D`IId5!4ZLLM::,hOP 
@	?`s   B15C1
B?z*hf sandbox process kill <sandbox_id> <pid>r}   z.The pid as printed by `hf sandbox process ls`.c                   ^ [        XUS9 n[        U4S jUR                  5        5       S5      nUc  [        ST SU  S35      eUR	                  5         SSS5        [
        R                  " SU TS9  g! , (       d  f       N%= f)	z/Stop a background process running in a sandbox.r/   c              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr[   )r}   ).0r   r}   s     r3   	<genexpr>process_kill.<locals>.<genexpr>  s     G#6a%%3,#6s   #	#NzNo process with pid z in sandbox r   zProcess stoppedr|   )r4   nextr   r   r   r#   rJ   )r*   r}   r+   r,   r2   r)   s    `    r3   process_killr     sx     
*	?7G7#4#4#6GN?1#l:,aPQQ	 
@
 JJ *#> 
@	?s   A
A55
B)NNNNNNNNNNFN)NNNNNN)NNNNN)NN)NFFNN)FNN)G__doc__r\   rI   
contextlibr   typingr   r   r   rt   huggingface_hub._sandboxr   r   r	   r
   r   r   r   r   r   r   r   r   huggingface_hub._sandbox_cacher   huggingface_hub.errorsr   r   
_cli_utilsr   r   r   r   r   r   r   r   r   r    
_frameworkr!   r"   _outputr#   r   r$   r%   sandbox_clipool_cli	add_groupprocess_cliro   SandboxIdArgr4   rU   boolrP   rq   floatrx   r   r   r   intr   r   r   r   r   r   r    rQ   r3   <module>r      s+   J 
  % + +     = 9   )  ) !QRbc   hV  ,![\   k	  2h,_``a  C$J sTz hW^N_      TX 	 	#'" 	)>CS4Z/N!OOP>C
d
xjk	m>C >C d
jk	m>C 
>C >C >C !>C >C  !>C"  f)0bcc#>C( )>C* 
+>C>CB 
.5?;   \` 	"(b(btCy(0E"FFG(b sTz6$BV#WWX(b 
	(b
 (b f[/Z[[(b (b (b 
(b(bV .5EB   \`"TTtCy(0W"XXYT sTz6$BV#WWXT 
	T
 T T T 
TT* <A   #	+	3&TUU	V+	3&YZZ	[+ + 	+
 
++@ 
   `dhm_d"/4#*h4Z&[[\/4
D&/cdd
e/4 
4g4Z[[	\/4 	/4
 /4 
/4/4d 
   bf 	# 	 	"(ZS4Z/\!]]^(Z(Z |+QRlQmmo)pq	s(Z d
F=a6_``(Z d
pq	s(Z (Z (Z  
!(Z(ZV 
+)=(>?"JJJ 
J @J> 
01   `e"	HsH*>??@H	4g4Z[[	\H H 	H
 
H	H4:#S	/ :c :V VC V [,P+QR #QQQ Q 
	Q SQ  V'S&TU #	??	3&VWW	X? ? 	?
 
? V?rQ   