wL idZddlZddlZddlZddlmZmZddlmZddl m Z ddl m Z m Z mZmZddlZddlmZmZmZddlmZdd lmZd d lmZej6eZeDcgc]}|j<d k7s|j< c}ZGd deZ GddeZ!GddeZ"de e ee#e$fde e#de#fdZ%GddeZ&GddeZ'GddeZ(GddeZ)de e#e#ffdZ*Gd d!eZ+Gd"d#eZ,Gd$d%eZ-Gd&d'eZ.Gd(d)eZ/Gd*d+eZ0Gd,d-eZ1Gd.d/eZ2ycc}w)0aContains commands to interact with jobs on the Hugging Face Hub. Usage: # run a job hf jobs run # List running or completed jobs hf jobs ps [-a] [-f key=value] [--format TEMPLATE] # Stream logs from a job hf jobs logs # Inspect detailed information about a job hf jobs inspect # Cancel a running job hf jobs cancel N) Namespace_SubParsersAction)asdict)Path)DictListOptionalUnion)HfApi SpaceHardware get_token)logging) load_dotenv)BaseHuggingfaceCLICommandz zero-a10gc"eZdZedefdZy) JobsCommandsparserc|jddjd}jfdtj |t j |t j |tj |tj |tj |tj |y)NjobszRun and manage Jobs on the Hub.helpz$huggingface.co jobs related commandsc$jSN print_help)args jobs_parsers ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/cli/jobs.pyz2JobsCommands.register_subcommand..>s;3I3I3Kfunc) add_parseradd_subparsers set_defaultsInspectCommandregister_subcommand LogsCommand PsCommand RunCommand CancelCommand UvCommandScheduledJobsCommands)rjobs_subparsersrs @rr(z JobsCommands.register_subcommand8s''5V'W %44:`4a   &K L **?;''8%%o6&&7))/:%%o611/Br!N__name__ __module__ __qualname__ staticmethodrr(r!rrr7sC$5CCr!rc>eZdZededdfdZdeddfdZddZy) r+rreturnNcz|jdd}|jdtd|jddd d |jd d d d |jdtd|jdtd|jdtddjtd|jdtd|jdddd |jdtd|jdtd |jd!d"d#$|j t %y)&Nrunz Run a JobrimageThe Docker image to use.typer-e--envappend/Set environment variables. E.g. --env ENV=valueactionr-s --secretsvSet secret environment variables. E.g. --secrets SECRET=value or `--secrets HF_TOKEN` to pass your Hugging Face token. --env-file(Read in a file of environment variables.--secrets-file/Read in a file of secret environment variables.--flavorTFlavor for the hardware, as in HF Spaces. Defaults to `cpu-basic`. Possible values: , . --timeoutVMax duration: int/float with s (seconds, default), m (minutes), h (hours) or d (days).-d--detach store_truez3Run the Job in the background and print the Job ID. --namespaceVThe namespace where the Job will be created. Defaults to the current user's namespace.--tokenIA User Access Token generated from https://huggingface.co/settings/tokenscommand...The command to run.nargsrr")r$ add_argumentstrjoinSUGGESTED_FLAVORSr&r+r run_parsers rr(zRunCommand.register_subcommandKs&&u;&? c8RSghEvw  K    3=gh 0sArs ghlhqhqsDiEhFFGH   i    F   i   \   =RSZ0r!rc|j|_|j|_i|_|jrc|jj t t |jjtjj|jxsgD]D}|jj t |tjjFi|_ t}|jrG|jj t t |jj||jxsgD](}|jj t ||*|j|_|j|_|j |_|j"|_|j$|_yNenviron)r:rXenvenv_fileupdaterr read_textosrfcopysecrets_get_extended_environ secrets_fileflavortimeoutdetach namespacetokenselfr env_valueextended_environsecrets r__init__zRunCommand.__init__xsL** "&,, -/ == HHOOKT]](;(E(E(GQSQ[Q[Q`Q`Qbc dR OI HHOOK 2::??;LM N O13 02    LL   D1B1B,C,M,M,OYi j kll(b OF LL   FeZdZededdfdZdeddfdZddZy) r)rr7Nc|jdd}|jdtd|jdtd|jd td |jt y) NlogszFetch the logs of a JobrrJob IDr<rTQThe namespace where the job is running. Defaults to the current user's namespace.rVrWr")r$r]r^r&r)ras rr(zLogsCommand.register_subcommandsy&&v4M&N sB d   C&q   [1r!rcj|j|_|j|_|j|_yrrrsrtrvrs rrzzLogsCommand.__init__s#;; (,$(JJ r!ct|j}|j|j|jD] }t |yNr|rrs)r rtrrrsr)rvrrs rr9zLogsCommand.runs>$**%%%T[[DNN%S C #J r!rrr5r!rr)r)s; 2$5 2$ 2 2/Y/4/ r!r)rowsheadersr7c tg||Dcgc]}td|D}}ttjjt |dz}t |t |z|kDr|jt|}||xxdzcc<t |t |z|kr#|t |z t |z ||z||<t |t |z|kDrdt |zj|}g}|j|j||j|j|Dcgc]}d|z c}|D]r} t| |D cgc];\} } t t| | kDrt| d| dz dzn t| =} } } |j|j| td j|Scc}wcc}wcc} } w) z Inspired by: - stackoverflow.com/a/8356620/593036 - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data c3DK|]}tt|ywr)lenr^).0xs r z_tabulate..s/ac#a&k/s  z{{:{}}} -NrY ) zipmaxrkget_terminal_sizecolumnsrsumindexformatr@r^r_) rrcol col_widthsterminal_widthcol_to_minimize row_formatlineswrowr col_widthrow_format_argss r _tabulaters 9$**3z?;?#)# w<#j/ )^ ;*83z?*JSQX\*Y\fgv\w*wJ ' g,Z (> 9 4*s7|+33Z@J E LL"""G,- LL"""j$AS1W$ABC:!$C 4 9033q6{Y/FCF?Y] #e +CPQF R    &Z&&89 : 99U #P%B sG* G/ 6AG4c^eZdZededdfdZdeddfdZd dZde e e fde fd Z d Z y) r*rr7Nc<|jdd}|jdddd|jd td |jd td |jdddgd|jdtd |jty)Npsz List Jobsr-a--allrSz*Show all Jobs (default shows just running)rBrTUThe namespace from where it lists the jobs. Defaults to the current user's namespace.r<rVrW-f--filterr@>Filter output based on conditions provided (format: key=value)rCdefaultr--format%Format output using a custom templater")r$r]r^r&r*ras rr(zPsCommand.register_subcommands&&t+&>   =   h   \    Q    8  Y/r!rcH|j|_|j|_|j|_|j|_i|_|j D]H}d|vr3|j dd\}}||j|j<:td|dJyN=rz)Warning: Ignoring invalid filter format 'z'. Use key=value format. allrsrtrfiltersfiltersplitlowerrrvrfkeyvalues rrzzPsCommand.__init__(,$(JJ %)[[ ')  _AaxWWS!_ U,1 SYY[)A!D\]^  _r!c L t|j}|j|j}gd}g}|D]}|jr|jj nd}|j s|dvr8|j}|jxsd}|jxsg} | rdj| nd} |jr|jjdnd} |||j| d } |j| s|j||| | |g|sad } |j rDd d j|j j#Dcgc] \}}|d |c}}} t%d| y|j'||ycc}}w#t(j*$r}t%d|Yd}~yd}~wt,t.t0f$r}t%d|Yd}~yd}~wt2$r.}t%dt5|j6d|Yd}~yd}~wwxYw)z Fetch and display job information for the current user. Uses Docker-style filtering with -f/--filter flag and key=value pairs. r|rs)zJOB ID IMAGE/SPACECOMMANDCREATEDSTATUSUNKNOWN)RUNNINGUPDATINGN/A %Y-%m-%d %H:%M:%S)rr:statusrX matching filters: rMrz No jobs foundNzError fetching jobs data: zError processing jobs data: Unexpected error - : )r rt list_jobsrsrstagerr docker_imagerXr_ created_atstrftimer_matches_filtersr@ritemsr _print_outputrequestsRequestExceptionKeyError ValueError TypeError Exceptionr=r1)rvrr table_headersrrrrimage_or_spacerX command_strrjob_properties filters_msgkves rr9z PsCommand.run s A Adjj)C==4>>=:DVMD" W-0ZZ))YxxF2I$I"%!1!1!:U+++3:chhw/ NQ^^S^^445HIaf !+$lln* ",,^< V^[*fUVE" WJ <<$7 Z^ZfZfZlZlZnBoRVRSUVaS!:Bo8p7q"rK k]34   t] 3 Cp(( 4 .qc2 3 3*i0 6 04 5 5 A 'Q(8(8'9A3? @ @ AsHEFF 'FFFH#,F??H#G)) H#5$HH#rcP|jjD]\}}||vryd|vsd|vrQ|jddjdd}tjd|d||tj rcy|j ||j vsyy) z+Check if job matches all specified filters.F*?.*rN^$Trrreplaceresearch IGNORECASErrvrrpattern regex_patterns rrzPsCommand._matches_filtersS LL..0 LC.(g~ 'T : B B3 L yy1]O1!5~c7JBMMZ s(;(A(A(CC r!c |jr_|j}|D]M}|}tgdD].\}}d|d}||vs|j|t||}0t |Oyt t ||y),Print output according to the chosen format.)rr:rXcreatedr{{.}}rNr enumeraterr^rr rvrrtemplaterlineifield placeholders rrzPsCommand._print_outputfs ;;{{H  )*Y ZFHAu$)%"5K"d*#||KSVEFd   # r!rr1r2r3r4rr(rrzr9rr^boolrrr5r!rr*r*sb 0$5 0$ 0 0D _Y _4 _FAPtCH~$&r!r*c>eZdZededdfdZdeddfdZddZy) r'rr7Nc|jdd}|jdtd|jdtd|jd d d |jt y)Ninspectz0Display detailed information on one or more JobsrrTrr<rVrWjob_idsrYzThe jobs to inspectr[r")r$r]r^r&r'ras rr(z"InspectCommand.register_subcommand}sz&&y7i&j  d   C&q    =RS^4r!rcj|j|_|j|_|j|_yr)rsrtrrs rrzzInspectCommand.__init__s#(,$(JJ "&,, r!c $t|j}|jDcgc]}|j||j!}}t t j|Dcgc] }t|c}dtycc}wcc}w)Nr|rindentr) r rtr inspect_jobrsrjsondumpsrr^)rvrrrrs rr9zInspectCommand.runsg$**%W[WcWcdVvHdd djj6#&+6q#NOe6 $B#B rrr5r!rr'r'|s< 5$5 5$ 5 5/Y/4/ Pr!r'c>eZdZededdfdZdeddfdZddZy) r,rr7Nc|jdd}|jdtd|jdtd|jd td |jt y) Ncancelz Cancel a Jobrrrr<rTrrVrWr")r$r]r^r&r,ras rr(z!CancelCommand.register_subcommandsx&&xn&E sB d   C&q   ]3r!rcj|j|_|j|_|j|_yrrrs rrzzCancelCommand.__init__s#;; $(JJ r!c~t|j}|j|j|jyr)r rt cancel_jobrrsrvrs rr9zCancelCommand.runs($**% dkkT^^Dr!rrr5r!rr,r,s< 4$5 4$ 4 4/Y/4/ Er!r,c8eZdZdZedZdeddfdZddZy) r-z.Run UV scripts on Hugging Face infrastructure.cr|jdd}|jddd}|jdd }|jd d |jd d dg|jdtd|jdd|jdtddj t d|jdddd|jdddd |jd!td"|jd#td$|jd%td&|jd'd(d)d*|jd+td,|jd-td.|jd/dd0d12|jd3d4td5|j t6y7)8Register UV run subcommand.uvzERun UV scripts (Python with inline dependencies) on HF infrastructurer uv_command UV commandsTdestrrequiredr98Run a UV script (local file or URL) on HF infrastructurescript$UV script to run (local file or URL) script_argsrYArguments for the scriptr\rr--image.Use a custom Docker image with `uv` installed.r<--repoCRepository name for the script (creates ephemeral if not specified)rKrLrMrNr>r?r@Environment variablesrBrDrErFrGrHrIrJrO Max duration (e.g., 30s, 5m, 1h)rQrRrSRun in backgroundrTrUrVHF token--with%Run with the given packages installedwith_rCrr2-p--python5The Python interpreter to use for the run environmentr"N)r$r%r]r^r_r`r&r-r uv_parser subparsersrbs rr(zUvCommand.register_subcommands%% X& --.3s=V=a=a=cr!r") r$r%r&ScheduledRunCommandr(ScheduledPsCommandScheduledInspectCommandScheduledDeleteCommandScheduledSuspendCommandScheduledResumeCommandScheduledUvCommand)rscheduled_jobs_parserr^s @rr(z)ScheduledJobsCommands.register_subcommand+s & 1 1+Dr 1 s$9$H$HA%I% ! **0c*d //0IJ../HI334MN223LM334MN223LM../HIr!Nr0r5r!rr.r.*sJ$5JJr!r.c>eZdZededdfdZdeddfdZddZy) r_rr7Nc|jdd}|jdtd|jdtd|jd d d d |jd d dd |jdddd|jdddd|jdtd|jdtd|jdtddjtd|jdtd |jd!td"|jd#td$|jd%d&d'(|j t )y)*Nr9zSchedule a JobrscheduleWOne of annually, yearly, monthly, weekly, daily, hourly, or a CRON schedule expression.r<r:r; --suspendrS!Suspend (pause) the scheduled JobrCrr --concurrency8Allow multiple instances of this Job to run concurrentlyr>r?r@rArBrDrErFrGrHrIrJrKrLrMrNrOrPrTz`The namespace where the scheduled Job will be created. Defaults to the current user's namespace.rVrWrXrYrZr[r")r$r]r^r_r`r&r_ras rr(z'ScheduledRunCommand.register_subcommand@s&&u3C&D  j  c8RS 4   K  ghEvw  K    3=gh 0sArs ghlhqhqsDiEhFFGH   i   s   \   =RS%89r!rc|j|_|j|_|j|_|j|_|j|_i|_|j rc|j jtt|j jtjj|j xsgD]D}|j jt|tjjFi|_t}|j rG|jjtt|j j||jxsgD](}|jjt||*|j"|_|j$|_|j&|_|j(|_yrd)rir:rXsuspend concurrencyrgrhrirrrjrkrfrlrmrnrorprqrsrtrus rrzzScheduledRunCommand.__init__xse!]] ** "&,, '+|| +/+;+;-/ == HHOOKT]](;(E(E(GQSQ[Q[Q`Q`Qbc dR OI HHOOK 2::??;LM N O13 02    LL   D1B1B,C,M,M,OYi j kll(b OF LL   F}|j}|j s|r|j }|j}|jjxsd} |jjxsg} | rdj| nd} |jjr/|jjjjdnd} |jj r%|jj jdnd} || t#|| d}|j%|s'|j'||| | | | |gA|sad}|j(rDd d j|j(j+Dcgc] \}}|d |c}}}t-d |y |j/||y cc}}w#t0j2$r}t-d|Yd }~y d }~wt4t6t8f$r}t-d|Yd }~y d }~wt:$r.}t-dt=|j>d|Yd }~y d }~wwxYw)z Fetch and display scheduked job information for the current user. Uses Docker-style filtering with -f/--filter flag and key=value pairs. r|r)IDSCHEDULErrzLAST RUNzNEXT RUNSUSPENDrrr)rr:rqrXrrrMrzNo scheduled jobs foundNz$Error fetching scheduled jobs data: z&Error processing scheduled jobs data: rr) r rtlist_scheduled_jobsrsrqrrrijob_specrrXr_rlast_jobatrnext_job_run_atr^rr@rrrrrrrrrrr=r1)rvrscheduled_jobsrrrvrqscheduled_job_idrirrXr last_job_atrrrrrrs rr9zScheduledPsCommand.runs _ Adjj)C!44t~~4NNMD!/8  '//xxG$1#3#3 )11"/!7!7!D!D!M(0088>B3:chhw/ %++44"((1144==>QR%++;;"((88AABUV ++"7|* ",,^< ( &##' ]8 v <<$7 Z^ZfZfZlZlZnBoRVRSUVaS!:Bo8p7q"rK/ }=>   t] 3 Cp(( > 8< = =*i0 @ :1#> ? ? A 'Q(8(8'9A3? @ @ AsHF5G:7G4 G:!G:4G::J H  J7I  J$I??JrcP|jjD]\}}||vryd|vsd|vrQ|jddjdd}tjd|d||tj rcy|j ||j vsyy) z5Check if scheduled job matches all specified filters.FrrrrNrrTrrs rrz#ScheduledPsCommand._matches_filters9rr!c |jr_|j}|D]M}|}tgdD].\}}d|d}||vs|j|t||}0t |Oyt t ||y)r)rrir:rXrrrqr r r Nr rs rrz ScheduledPsCommand._print_outputLs ;;{{H  )g!FHAu%*%"5K"d*#||KSVE F d   # r!rrr5r!rr`r`sb 9$5 9$ 9 9D _Y _4 _dALtCH~$&r!r`c>eZdZededdfdZdeddfdZddZy) rarr7Nc|jdd}|jdtd|jdtd|jd d d |jt y)Nrz:Display detailed information on one or more scheduled JobsrrTSThe namespace where the scheduled job is. Defaults to the current user's namespace.r<rVrWscheduled_job_idsrYzThe scheduled jobs to inspectr[r")r$r]r^r&raras rr(z+ScheduledInspectCommand.register_subcommandes|&&y7s&t  f   C&q    35Gfg%<=r!rcj|j|_|j|_|j|_yr)rsrtrrs rrzz ScheduledInspectCommand.__init__ss&(,$(JJ ,0,B,Br!c $t|j}|jDcgc]}|j||j!}}t t j|Dcgc] }t|c}dtycc}wcc}w)Nr|rrsrr) r rtrinspect_scheduled_jobrsrr!r"rr^)rvrrrrvs rr9zScheduledInspectCommand.runxs$**%%)$:$:    % %7GSWSaSa % b   djj^TM&/T]^hklm  Ur#rrr5r!rrarads? >$5 >$ > >CYC4C nr!rac>eZdZededdfdZdeddfdZddZy) rbrr7Nc|jdd}|jdtd|jdtd|jd td |jt y) NdeletezDelete a scheduled JobrrScheduled Job IDr<rTrrVrWr")r$r]r^r&rbras rr(z*ScheduledDeleteCommand.register_subcommands|&&x6N&O  2CUV f   C&q   %;eZdZededdfdZdeddfdZddZy) rcrr7Nc|jdd}|jdtd|jdtd|jd td |jt y) NrqzSuspend (pause) a scheduled Jobrrrr<rTrrVrWr")r$r]r^r&rcras rr(z+ScheduledSuspendCommand.register_subcommands|&&y7X&Y  2CUV f   C&q   %<=r!rcj|j|_|j|_|j|_yrrrs rrzz ScheduledSuspendCommand.__init__rr!c~t|j}|j|j|jyr)r rtsuspend_scheduled_jobrrsr*s rr9zScheduledSuspendCommand.runs0$**% !!43H3HTXTbTb!cr!rrr5r!rrcrcs< >$5 >$ > >/Y/4/ dr!rcc>eZdZededdfdZdeddfdZddZy) rdrr7Nc|jdd}|jdtd|jdtd|jd td |jt y) Nresumez Resume (unpause) a scheduled Jobrrrr<rTrrVrWr")r$r]r^r&rdras rr(z*ScheduledResumeCommand.register_subcommands|&&x6X&Y  2CUV f   C&q   %;y)?r-r.zJSchedule UV scripts (Python with inline dependencies) on HF infrastructurerr/r0Tr1r9r4rirjr<r5r6r7rYr8r9rkrSrlNrmrnror:r;r<r=rKrLrMrNr>r?r@r>rBrDrErFrGrHrIrJrOr?rQrRr@rTrUrVrArBrCrDrErFrGrHr")r$r%r]r^r_r`r&rerIs rr(z&ScheduledUvCommand.register_subcommandsz%% ]& --rs& 1..;;)5'   H %,9WDTZZ;=VTZZWC,C&V*Vr+4DeCHo./$s)6a)aHP.P4E-E2q)qhtCH~J5J*^D3^DBA2AHn7n:c6c2d7d2c6c2@:2@:qXs %E:E