o
    Zi                  &   @   s
  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZ ddlZdd	lmZmZ dd
lmZm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ e"0e1Z2de3dee3 de4e3ee3 f fddZ5dd eD Z6dZ7ee3ej8ddf Z9eee3 ej:ddf Z;eee ej:ddf Z<eee=e3  ej:ddddf Z>eee=e3  ej:dd d!df Z?eee=e3  ej:d"d#d$df Z@eee3 ej:d%d&df ZAeee3 ej:d'df ZBeee3 ej:d(df ZCeeDej:d)d*d+df ZEeee3 ej:d,df ZFeee=e3  ej:d-d.df ZGeee3 ej:d/d0d1df ZHeeeD ej:d2df ZIeeeD ej:d3df ZJee3ej8d4df ZKee3ej8d5df ZLeee=e3  ej8d6df ZMee=e3 ej8d7df ZNee3ej8d8df ZOeee=e3  ej8d9df ZPee3ej8d:df ZQe/d;dZReRjSd<d=d>ig d?d@								A		ddBe9dCeNdDe>dEe?dFe@dGeAdHeBdIe<dJeCdKeEdeFdLe*ddfdMdNZTeRjSdOg dPdQ	A			ddeOdReeDej:dSdTdUdf dVeeeU ej:dWdXdYdf deFdLe*ddfdZd[ZVd\eWe3e3f d]e=e4e3e3e3f  deDfd^d_ZXd`e=e=ee3eUf   dae=e3 dbe=e3 dcee3 ddf
dddeZYdfeUddfdgdhZZde3dieeWe3ef  dje=e3 dee4eDe3e=e=ee3eUf   f  fdkdlZ[eRjSdmdngdQ			ddoePdeFdLe*ddfdpdqZ\eRjSdrdsdtgdQ	A					A	AddueeDej:dvdwdxdf deFdLe*dyeee=e3  ej:dSdzd{df d|eee3 ej:d}df d~e)deeDej:dd>ddf ddfddZ]eRjSddgdQdddZ^eRjSddgdQ		ddoee=e3 ej8ddf deFdLe*ddfddZ_eRjSddgdQ		ddeOdeFdLe*ddfddZ`e/ddZaeRjbeadd eajSd<d=d>ig dd@										A				ddeLdeMdBe;dIe<dDe>dEe?dFe@dGeAdHeBdJeCdKeEdeFdLe*deGdeHddf ddZce/ddZdeRjbeddd edjSd<d=d>idgd@											ddeKdBe9dCeNdeIdeJdDe>dEe?dFe@dGeAdHeBdIe<dJeCdeFdLe*ddfddZeedjSdrdgdQ	A					A	AddueeDej:dvdwddf deFdLe*dyeee=e3  ej:dSdzd{df d|eee3 ej:d}df d~e)deeDej:dd>ddf ddfddZfedjSddgdQ		ddee=e3 ej8ddf deFdLe*ddfddZgedjSddgdQ		ddeQdeFdLe*ddfddZhedjSddgdQ		ddeQdeFdLe*ddfddZiedjSddgdQ		ddeQdeFdLe*ddfddZje/ddZkedjbekdd ekjSd<d=d>iddgd@															ddeKdeLdeMdeIdeJdBe;dIe<dDe>dEe?dFe@dGeAdHeBdJeCdeFdLe*deGdeHddf$ddZldee=e3  deeWe3e3f  fddZmd`e=e=ee3eUf   dae=e3 de3fddZndee3e3f fddĄZoedŃZpdeep dedeep f deWddfdd˄Zqdejrjsdedeep f de=eW deep fddτZtdS )a  Contains commands to interact with jobs on the Hugging Face Hub.

Usage:
    # run a job
    hf jobs run <image> <command>

    # List running or completed jobs
    hf jobs ps [-a] [-f key=value] [--format table|json|TEMPLATE] [-q]

    # Print logs from a job (non-blocking)
    hf jobs logs <job-id>

    # Stream logs from a job (blocking, like `docker logs -f`)
    hf jobs logs -f <job-id>

    # Stream resources usage stats and metrics from a job
    hf jobs stats <job-id>

    # Inspect detailed information about a job
    hf jobs inspect <job-id>

    # Cancel a running job
    hf jobs cancel <job-id>

    # List available hardware options
    hf jobs hardware

    # Run a UV script
    hf jobs uv run <script>

    # Schedule a job
    hf jobs scheduled run <schedule> <image> <command>

    # List scheduled jobs
    hf jobs scheduled ps [-a] [-f key=value] [--format table|json] [-q]

    # Inspect a scheduled job
    hf jobs scheduled inspect <scheduled_job_id>

    # Suspend a scheduled job
    hf jobs scheduled suspend <scheduled_job_id>

    # Resume a scheduled job
    hf jobs scheduled resume <scheduled_job_id>

    # Delete a scheduled job
    hf jobs scheduled delete <scheduled_job_id>

    N)dequeasdict)fnmatch)Path)EmptyQueue)	AnnotatedAnyCallableDictIterableOptionalTypeVarUnion)SpaceHardware	get_token)CLIErrorHfHubHTTPError)logging)_format_size)load_dotenv   )OutputFormatQuietOptTokenOpt_format_cellapi_object_to_dict
get_hf_apiprint_list_outputtyper_factoryjob_id	namespacereturnc                 C   s   | st d| ddkrt d|  dd| vr| |fS | dd\}}|r)|s1t d|  d|durD||krDt d| d| d	||fS )
a&  Extract namespace from job_id if provided in 'namespace/job_id' format.

    Allows users to pass job IDs copied from the Hub UI (e.g. 'username/job_id')
    instead of only bare job IDs. If the namespace is also provided explicitly via
    --namespace and conflicts, a CLIError is raised.
    zJob ID cannot be empty./r   z<Job ID must be in the form 'job_id' or 'namespace/job_id': 'z'.Nz(Conflicting namespace: got --namespace='z ' but job ID implies namespace='')r   countsplit)r!   r"   Zextracted_namespaceZparsed_job_id r(   O/home/app/Keep/.python/lib/python3.10/site-packages/huggingface_hub/cli/jobs.py_parse_namespace_from_job_idd   s   r*   c                 C   s   g | ]
}|j d kr|j qS )z	zero-a10g)value.0itemr(   r(   r)   
<listcomp>   s    r/   g?zThe Docker image to use.)helpz.Use a custom Docker image with `uv` installed.ztFlavor for the hardware, as in HF Spaces. Run 'hf jobs hardware' to list available flavors. Defaults to `cpu-basic`.z-ez--envz/Set environment variables. E.g. --env ENV=valuez-sz	--secretszvSet secret environment variables. E.g. --secrets SECRET=value or `--secrets HF_TOKEN` to pass your Hugging Face token.z-lz--labelz3Set labels. E.g. --label KEY=VALUE or --label LABELz
--env-filez(Read in a file of environment variables.z/Read in a file of secret environment variables.zVMax duration: int/float with s (seconds, default), m (minutes), h (hours) or d (days).z-dz--detachz3Run the Job in the background and print the Job ID.zVThe namespace where the job will be running. Defaults to the current user's namespace.z--withz%Run with the given packages installedz-pz--pythonz5The Python interpreter to use for the run environmentz!Suspend (pause) the scheduled Jobz8Allow multiple instances of this Job to run concurrentlyzWOne of annually, yearly, monthly, weekly, daily, hourly, or a CRON schedule expression.z$UV script to run (local file or URL)zArguments for the scriptzThe command to run.zJob ID (or 'namespace/job_id')zJob IDs (or 'namespace/job_id')z2Scheduled Job ID (or 'namespace/scheduled_job_id')zRun and manage Jobs on the Hub.runZignore_unknown_optionsT)z3hf jobs run python:3.12 python -c 'print("Hello!")'z3hf jobs run -e FOO=foo python:3.12 python script.pyz;hf jobs run --secrets HF_TOKEN python:3.12 python script.py)Zcontext_settingsexamplesFimagecommandenvsecretslabelenv_filesecrets_fileflavortimeoutdetachtokenc              
   C   s  i }|r| tt| tj d |pg D ]}| t|tj d qi }t }|r;| tt| |d |p>g D ]}| t||d q?t|d}|j	| |||t
||||
d}td|j  td|j  |	rsdS |j|j|jjddD ]}t| q~dS )	z
Run a Job.environr=   )r3   r4   r5   r6   labelsr:   r;   r"   Job started with ID: 	View at: NTr!   r"   follow)updater   r   	read_textosr?   copy_get_extended_environr   Zrun_job_parse_labels_mapprintidurlfetch_job_logsownername)r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r"   r=   env_map	env_valuesecrets_mapextended_environsecretapijoblogr(   r(   r)   jobs_run.  s:    

rZ   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>)r2   rE   z-fz--followzqFollow log output (stream until the job completes). Without this flag, only currently available logs are printed.tailz-nz--tailz1Number of lines to show from the end of the logs.c           
   
   C   s   t | |\} }|r|durtdt|d}z|j| ||d}|dur)t||d}|D ]}t| q+W dS  tye } z%|jdurE|jjnd}	|	dkrPtd||	dkrYtd	|td
| |d}~ww )zFetch the logs of a Job.

    By default, prints currently available logs and exits (non-blocking).
    Use --follow/-f to stream logs in real-time until the job completes.
    NzcCannot use --follow and --tail together. Use --follow to stream logs or --tail to show recent logs.r@   rD   )maxlen  'Job not found. Please check the job ID.  <Access denied. You may not have permission to view this job.zFailed to fetch job logs: )	r*   r   r   rO   r   rL   r   responsestatus_code)
r!   rE   r\   r"   r=   rW   r[   rY   estatusr(   r(   r)   	jobs_logsi  s,   



rf   job_propertiesfiltersc                 C   sf   |D ].\}}}|  |}|du r|dkrq dS t| | }|dkr'|r-|dkr0|r0 dS qdS )z5Check if scheduled job matches all specified filters.N!=F=T)getr   lower)rg   rh   keyZop_strpatternr+   matchr(   r(   r)   _matches_filters  s   
rp   rowsheadersaliasesfmtc           
      C   sp   |r.|}| D ]%}|}t |D ]\}}d| d}	|	|v r&||	t|| }qt| qdS tt| |d dS )z,Print output according to the chosen format.z{{.z}})rr   N)	enumeratereplacestrrL   	_tabulate)
rq   rr   rs   rt   templaterowlineifieldplaceholderr(   r(   r)   _print_output  s   
	r   nc                 C   s&   d}d}t | D ]}t||d qd S )Nz[1Az[2K)end)rangerL   )r   ZLINE_UPZ
LINE_CLEARr|   r(   r(   r)   _clear_line  s
   r   metrics_streamtable_headersc                 c   sz   |D ]}| |d  dt |d d dt d|d  |d  d	 dt|d  d
t|d  dt|d  dt|d  dg}|d rt|d tr|gdgt| gt|d d   }t|t|d D ]2\}}|d | }||d  dt d|d  |d  d	 dt|d  d
t|d  dg7 }qjn|dgt|t|  7 }|g}d| |fV  qd| g fV  d S )Ncpu_usage_pct%cpu_millicoresg     @@r   d   Zmemory_used_bytesZmemory_total_bytes   zB / BZrx_bpszbps / Ztx_bpsZbpsZgpus ZutilizationN/AFT)roundr   
isinstancedictlenzipsorted)r!   r   r   Zmetricsrz   rq   Zgpu_idZgpur(   r(   r)   _get_jobs_stats_rows  s.   &r   statszhf jobs stats <job_id>job_idsc              
      s*  | durg }| D ]}t |\}|| q|} t|d du r(  d | du r7dd  jdD } t| dkrCtd dS g d	g d
}ztj	t| }i | D ]}|g}|dd dd D 7 }|g|< qYt

 }fddD }	t|	|d  fdd| D }
t|t|
dD ]6\}}}|r|d n||< t

 }|| tkrtdt|	  fddD }	t|	|d |}qW d   W dS 1 sw   Y  W dS  ty } z&|jdur|jjnd}|dkrtd||dkrtd|td| |d}~ww )z7Fetch the resource usage statistics and metrics of JobsNr@   rQ   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNRUNNINGZUPDATING)re   stagerM   r-   rX   r(   r(   r)   r/     s
    zjobs_stats.<locals>.<listcomp>r"   r   zNo running jobs found)	JOB IDzCPU %zNUM CPUzMEM %z	MEM USAGEzNET I/Oz
GPU UTIL %z	GPU MEM %zGPU MEM USAGE)	rM   r   r   Zmemory_used_bytes_pctZ!memory_used_bytes_and_total_bytesZrx_bps_and_tx_bpsZgpu_utilizationZgpu_memory_used_bytes_pctZ%gpu_memory_used_bytes_and_total_bytesc                 S   s$   g | ]}d |v sd|v rdndqS )r$   USAGEz-- / --z--r(   )r-   headerr(   r(   r)   r/     s   $ r   c                       g | ]} | D ]}|qqS r(   r(   r-   r!   rz   rows_per_job_idr(   r)   r/         c                    s"   g | ]}| j |d dqS )r!   r"   )r!   r   r   )Zfetch_job_metricsr-   r!   )rW   r"   r   r(   r)   r/     s    )kwargs_listr   c                    r   r(   r(   r   r   r(   r)   r/   +  r   r^   r_   r`   ra   zFailed to fetch job stats: )r*   appendr   Zwhoami	list_jobsr   rL   multiprocessingpool
ThreadPooltimer   iflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr   r   rb   rc   r   )r   r"   r=   
parsed_idsr!   headers_aliasesr   rz   Zlast_update_timeZ
total_rowsr   donerq   nowrd   re   r(   )rW   r"   r   r   r)   
jobs_stats  sj   

&


r   Zpsz
hf jobs pszhf jobs ps -aallz-az--allz*Show all Jobs (default shows just running)filterz--filterz>Filter output based on conditions provided (format: key=value)formatzKOutput format: 'table' (default), 'json', or a Go template (e.g. '{{.id}}')quiet	json_flagz--jsonz)Output as JSON (alias for --format json).)Zhiddenr0   c                    s  |rd}t |d}|j|d}g }	g }
|pg D ]}|ds#|dr}|drF|tdd }d|v r=td| d	 q|d
d}}}n*|tdd }d|v r[|dd\}}n|d}}|drnd
}|dd }nd}|
| || f qd|v r|dd\}}|drd
}|dd }nd}|	| || f qtd| d qg }|D ]C}|j	r|j	j
nd}| s|dvrq|jpd}|jpg }|rd|nd}|j|| |d}t||	sqt|jpi |
sq|| q|s(|s|dkr|	rdddd |	D  nd}td|  dS |dkr&td dS g d}g d}dd |D }d tttf d!tt fd"d# |r^|d$vr^t fd%d|D ||| dS |dkrftjntj}t|||d&| d' dS )(z
List Jobs.jsonr@   r   zlabel!=zlabel=Nrj   z6Warning: Ignoring invalid label filter format 'label!=z'. Use label!=key format.ri   *r   !)Warning: Ignoring invalid filter format ''. Use key=value format.r   r   r    )rM   r3   re   r4    matching filters: , c                 S   "   g | ]\}}}| | | qS r(   r(   r-   kovr(   r(   r)   r/        " zjobs_ps.<locals>.<listcomp>r   zNo jobs found[])r   IMAGE/SPACECOMMANDCREATEDZSTATUS)rM   r3   r4   createdre   c                 S      g | ]}t |qS r(   r   r   r(   r(   r)   r/         r.   r#   c              	   S   s   |  di }|  dpg }|rd|nd}t|  ddt|  dp$dt||  dr9| d d d	 d
dndt| ddgS )Nre   r4   r   r   rM   r   docker_imageZ
created_at   Tr   r   )rk   joinrw   r   rv   )r.   re   cmdcommand_strr(   r(   r)   row_fn  s   $zjobs_ps.<locals>.row_fntabler   c                       g | ]} |qS r(   r(   r,   r   r(   r)   r/     r   rM   itemsr   r   Zid_keyrr   r   )r   r   
startswithr   rL   r'   endswithr   rl   re   r   r   r4   r   rM   rp   rA   r   rw   r
   listr   r   r   r   r   )r   r"   r=   r   r   r   r   rW   jobsrh   Zlabels_filtersfZ
label_partZ	label_keyopZlabel_valuerm   r+   filtered_jobsrX   re   image_or_spacer   r   propsfilters_msgrr   rs   r   output_formatr(   r   r)   jobs_ps8  s   








$
 
r   Zhardwarezhf jobs hardwarec            	   
   C   s   t  } |  }g d}g d}g }|D ]H}d}|jr+|jj d|jj d|jj d}|jdur7d|jd	nd}|jdurGd|jd
 dnd}||j|j	pQd|j
|j|||g q|sdtd dS t|||d dS )z(List available hardware options for Jobs)NAMEzPRETTY NAMEZCPUZRAMZACCELERATORzCOST/MINz	COST/HOUR)rQ   Z
prettyNamecpuramacceleratorZcostMinZcostHourr   zx z ()N$z.4f<   z.2fzNo hardware options found)r   Zlist_jobs_hardwarer   ZquantitymodelZvramZunit_cost_usdr   rQ   pretty_namer   r   rL   r   )	rW   Zhardware_listr   r   rq   ZhwZaccelerator_infoZcost_minZ	cost_hourr(   r(   r)   jobs_hardware  s    " &r   inspectzhf jobs inspect <job_id>z*Job IDs to inspect (or 'namespace/job_id')c              
      s   g }| D ]}t |\}|| q|} t|d z fdd| D }ttjdd |D dtd W dS  tyg } z%|jdurG|jj	nd}|dkrRt
d	||d
kr[t
d|t
d| |d}~ww )z0Display detailed information on one or more Jobsr@   c                       g | ]	} j |d qS )r   )Zinspect_jobr   rW   r"   r(   r)   r/     s    z jobs_inspect.<locals>.<listcomp>c                 S   r   r(   r   r   r(   r(   r)   r/     r      indentdefaultNr^   r_   r`   ra   zFailed to inspect job: )r*   r   r   rL   r   dumpsrw   r   rb   rc   r   )r   r"   r=   r   r!   r   rd   re   r(   r   r)   jobs_inspect  s$   
$

r   cancelzhf jobs cancel <job_id>c              
   C   s   t | |\} }t|d}z
|j| |d W dS  tyG } z%|jdur'|jjnd}|dkr2td||dkr;td|td| |d}~ww )	zCancel a Jobr@   r   Nr^   r_   r`   z>Access denied. You may not have permission to cancel this job.zFailed to cancel job: )r*   r   Z
cancel_jobr   rb   rc   r   )r!   r"   r=   rW   rd   re   r(   r(   r)   jobs_cancel  s   


r   zFRun UV scripts (Python with inline dependencies) on HF infrastructure.Zuv)rQ   )zhf jobs uv run my_script.pyz1hf jobs uv run ml_training.py --flavor a10g-smallz+hf jobs uv run --with transformers train.pyscriptscript_argswith_pythonc                 C   s  i }|r| tt| tj d |pg D ]}| t|tj d qi }t }|r;| tt| |d |p>g D ]}| t||d q?t|d}|j	| |pVg |||||t
|||	|d}td|j  td|j  |
rxdS |j|j|jjddD ]}t| qdS )	8Run a UV script (local file or URL) on HF infrastructurer>   r@   )r   r  dependenciesr  r3   r5   r6   rA   r:   r;   r"   rB   rC   NTrD   )rF   r   r   rG   rH   r?   rI   rJ   r   Z
run_uv_jobrK   rL   rM   rN   rO   rP   rQ   )r   r  r3   r:   r5   r6   r7   r8   r9   r;   r<   r"   r=   r  r  rR   rS   rT   rU   rV   rW   rX   rY   r(   r(   r)   jobs_uv_run  s@    

r  z,Create and manage scheduled Jobs on the Hub.Z	scheduledz>hf jobs scheduled run "0 0 * * *" python:3.12 python script.pyschedulesuspendconcurrencyc                 C   s   i }|r| tt| tj d |pg D ]}| t|tj d qi }t }|	r;| tt|	 |d |p>g D ]}| t||d q?t|d}|j	||| ||||t
||
||d}td|j  dS )zSchedule a Job.r>   r@   )r3   r4   r  r  r	  r5   r6   rA   r:   r;   r"   Scheduled Job created with ID: N)rF   r   r   rG   rH   r?   rI   rJ   r   Zcreate_scheduled_jobrK   rL   rM   )r  r3   r4   r  r	  r5   r6   r7   r8   r9   r:   r;   r"   r=   rR   rS   rT   rU   rV   rW   scheduled_jobr(   r(   r)   scheduled_runK  s4    
r  zhf jobs scheduled psz1Show all scheduled Jobs (default hides suspended)c                    s  |rd}t |d}|j|d}g }	|pg D ]3}
d|
v r@|
dd\}}|dr1d}|dd	 }nd}|	| || f qtd
|
 d qg }|D ]6}|jpSd}| sY|rYqM|jj	p^d}|jj
pdg }|rld|nd}|j|t||d}t||	s~qM|| qM|s|s|dkr|	rdddd |	D  nd}td|  dS |dkrtd dS g d}g d}dd |D }dtttf dtt fdd |r|dvrt fdd|D ||| dS |dkrtjntj}t|||d | d! dS )"zList scheduled Jobsr   r@   r   rj   r   r   ri   Nr   r   r   Fr   r   )rM   r3   r  r4   r   r   c                 S   r   r(   r(   r   r(   r(   r)   r/     r   z scheduled_ps.<locals>.<listcomp>r   zNo scheduled jobs foundr   )IDZSCHEDULEr   r   zLAST RUNzNEXT RUNZSUSPEND)rM   r  r3   r4   lastnextr  c                 S   r   r(   r   )r-   Zsjr(   r(   r)   r/     r   r.   r#   c                 S   s   |  di }|  di }| d}| dpg }d}|r-| dr-|d d d dd	}d}| d
r@|d
 d d dd	}|rGd	|nd}t|  ddt|  dpWdt| dp_dt|||t|  ddgS )Njob_specre   last_jobr4   r   atr   r   r   Znext_job_run_atrM   r   r  r   r  F)rk   rv   r   rw   r   )r.   r  re   r  r   Zlast_job_atZnext_runr   r(   r(   r)   r     s&   

zscheduled_ps.<locals>.row_fnr   c                    r   r(   r(   r,   r   r(   r)   r/     r   rM   r   )r   Zlist_scheduled_jobsr'   r   r   rl   rL   r  r  r   r4   r   rM   rw   rp   r   r
   r   r   r   r   r   r   )r   r"   r=   r   r   r   r   rW   scheduled_jobsrh   r   rm   r+   r   r   r  r  r   r   r   r   r   rr   rs   r   r   r(   r   r)   scheduled_ps~  sb   



" 
r  zhf jobs scheduled inspect <id>scheduled_job_idsz>Scheduled Job IDs to inspect (or 'namespace/scheduled_job_id')c                    sj   g }| D ]}t |\}|| q|} t|d  fdd| D }ttjdd |D dtd dS )z:Display detailed information on one or more scheduled Jobsr@   c                    r   )scheduled_job_idr"   )Zinspect_scheduled_job)r-   r  r   r(   r)   r/      s    z%scheduled_inspect.<locals>.<listcomp>c                 S   r   r(   r   )r-   r  r(   r(   r)   r/     r   r   r   N)r*   r   r   rL   r   r   rw   )r  r"   r=   r   r!   r  r(   r   r)   scheduled_inspect  s   
"r  deletezhf jobs scheduled delete <id>r  c                 C   *   t | |\} }t|d}|j| |d dS )zDelete a scheduled Job.r@   r  N)r*   r   Zdelete_scheduled_jobr  r"   r=   rW   r(   r(   r)   scheduled_delete     
r  zhf jobs scheduled suspend <id>c                 C   r  )z Suspend (pause) a scheduled Job.r@   r  N)r*   r   Zsuspend_scheduled_jobr  r(   r(   r)   scheduled_suspend  r  r  resumezhf jobs scheduled resume <id>c                 C   r  )z!Resume (unpause) a scheduled Job.r@   r  N)r*   r   Zresume_scheduled_jobr  r(   r(   r)   scheduled_resume  r  r   z)Schedule UV scripts on HF infrastructure.z.hf jobs scheduled uv run "0 0 * * *" script.pyz<hf jobs scheduled uv run "0 0 * * *" script.py --with pandasc                 C   s   i }|
r| tt|
 tj d |pg D ]}| t|tj d qi }t }|r;| tt| |d |p>g D ]}| t||d q?t|d}|j	||pVg | |||||||t
|	|||d}td|j  dS )r  r>   r@   )r   r  r  r  r	  r  r  r3   r5   r6   rA   r:   r;   r"   r
  N)rF   r   r   rG   rH   r?   rI   rJ   r   Zcreate_scheduled_uv_jobrK   rL   rM   )r  r   r  r  r	  r3   r:   r5   r6   r7   r8   r9   r;   r"   r=   r  r  rR   rS   rT   rU   rV   rW   rX   r(   r(   r)   scheduled_uv_run/  s:    
r!  rA   c                 C   sB   | sdS i }| D ]}d|v r| ddn|df\}}|||< q|S )a  Parse label key-value pairs from CLI arguments.

    Args:
        labels: List of label strings in KEY=VALUE format. If KEY only, then VALUE is set to empty string.

    Returns:
        Dictionary mapping label keys to values, or None if no labels provided.
    Nrj   r   r   )r'   )rA   Z
labels_mapZ	label_varrm   r+   r(   r(   r)   rK   n  s   	 
rK   c           	      C   s&  dd t g | |R  D }tt jt|d }t|t| |krW|t|}||  d  < t|t| |krM|t| t| ||  ||< t|t| |ks$dt| j| }g }|	|j|  |	|jdd |D   | D ]}dd t ||D }|	|j|  qyd
|S )	z
    Inspired by:

    - stackoverflow.com/a/8356620/593036
    - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
    c                 S   s   g | ]}t d d |D qS )c                 s   s    | ]	}t t|V  qd S Nr   rw   )r-   xr(   r(   r)   	<genexpr>  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)r-   colr(   r(   r)   r/     r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r(   )r-   wr(   r(   r)   r/     r   c                 S   s@   g | ]\}}t t||krt|d |d  d nt|qS )N   z...r#  )r-   r$  Z	col_widthr(   r(   r)   r/     s    .
)r   r&  shutilget_terminal_sizecolumnsr   sumindexr   r   r   )	rq   rr   Z
col_widthsterminal_widthZcol_to_minimizeZ
row_formatlinesrz   Zrow_format_argsr(   r(   r)   rx     s$    
rx   c                  C   s$   t j } t  }d ur|| d< | S )NZHF_TOKEN)rH   r?   rI   r   )rU   r=   r(   r(   r)   rJ     s   
rJ   r   queuefunc.kwargsc                 C   s"   |di |D ]}|  | qd S )Nr(   )put)r4  r5  r6  resultr(   r(   r)   _write_generator_to_queue  s   r9  r   r   c                #   s    t   fdd|D }zGz&	 z	jddV  W n ty3   tdd |D r1 r1Y nY nw qW n	 ty?   Y nw W z
dd |D  W d	S  tjyV   Y d	S w z	dd |D  W w  tjyj   Y w w )
a;  
    Takes a function that returns an iterable of items, and run it in parallel using threads to return the flattened iterable of items as they arrive.

    This is inspired by those three `map()` variants, and is the mix of all three:

    * `imap()`: like `map()` but returns an iterable instead of a list of results
    * `imap_unordered()`: like `imap()` but the output is sorted by time of arrival
    * `flatmap()`: like `map()` but given a function which returns a list, `flatmap()` returns the flattened list that is the concatenation of all the output lists
    c                    s   g | ]} t |fqS r(   )Zapply_asyncr9  )r-   r6  r5  r   r4  r(   r)   r/     r   z&iflatmap_unordered.<locals>.<listcomp>T皙?r;   c                 s   s    | ]}|  V  qd S r"  )readyr-   Zasync_resultr(   r(   r)   r%    s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )r;  r<  )rk   r>  r(   r(   r)   r/     s    N)r   rk   r   r   emptyKeyboardInterruptr   TimeoutError)r   r5  r   Zasync_resultsr(   r:  r)   r     s8   r   )
NNNNNNNFNN)FNNN)NNN)FNNNNFF)r#   N)NN)NNNNNNNNNFNNNN)NNNNNNNNNNN)NNNNNNNNNNNNNNN)u__doc__r   r   Zmultiprocessing.poolrH   r-  r   collectionsr   dataclassesr   r   pathlibr   r4  r   r   typingr	   r
   r   r   r   r   r   r   ZtyperZhuggingface_hubr   r   Zhuggingface_hub.errorsr   r   Zhuggingface_hub.utilsr   Z$huggingface_hub.utils._cache_managerr   Zhuggingface_hub.utils._dotenvr   Z
_cli_utilsr   r   r   r   r   r   r   r    Z
get_logger__name__loggerrw   tupler*   ZSUGGESTED_FLAVORSr   ZArgumentZImageArgOptionZImageOptZ	FlavorOptr   ZEnvOptZ
SecretsOptZ	LabelsOptZ
EnvFileOptZSecretsFileOptZ
TimeoutOptboolZ	DetachOptZNamespaceOptZWithOptZ	PythonOptZ
SuspendOptZConcurrencyOptZScheduleArgZ	ScriptArgZScriptArgsArgZ
CommandArgZJobIdArgZ	JobIdsArgZScheduledJobIdArgZjobs_clir4   rZ   intrf   r   rp   r   r   r   r   r   r   r   r   Zuv_appZ	add_typerr  Zscheduled_appr  r  r  r  r  r   Zscheduled_uv_appr!  rK   rx   rJ   r   r9  r   r   r   r(   r(   r(   r)   <module>   s.  2((
&
	
	
		
	


	
2
,1
 
V		


 	
	

6		
.		


o	
	
&7**