o
    ei                  &   @   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"   extracted_namespaceparsed_job_id r*   V/home/ubuntu/transcripts/venv/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    r1   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.run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)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rA   )r7   r8   r9   r:   labelsr>   r?   r"   Job started with ID: 	View at: NTr!   r"   follow)updater   r   	read_textosrC   copy_get_extended_environr   run_job_parse_labels_mapprintidurlfetch_job_logsownername)r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r"   rA   env_map	env_valuesecrets_mapextended_environsecretapijoblogr*   r*   r+   jobs_run.  s:    

r_   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>)r6   rI   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.rD   rH   )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   rT   r   rQ   r   responsestatus_code)
r!   rI   ra   r"   rA   r\   r`   r^   estatusr*   r*   r+   	jobs_logsi  s,   



rk   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)rl   rm   keyop_strpatternr-   matchr*   r*   r+   _matches_filters  s   
rv   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}})rx   N)	enumeratereplacestrrQ   	_tabulate)
rw   rx   ry   rz   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)rangerQ   )r   LINE_UP
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   memory_used_bytesmemory_total_bytes   zB / Brx_bpszbps / tx_bpsbpsgpus utilizationN/AFT)roundr   
isinstancedictlenzipsorted)r!   r   r   metricsr   rw   gpu_id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 JobsNrD   rV   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNRUNNINGUPDATING)rj   stagerR   r/   r]   r*   r*   r+   r1     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)	rR   r   r   memory_used_bytes_pct!memory_used_bytes_and_total_bytesrx_bps_and_tx_bpsgpu_utilizationgpu_memory_used_bytes_pct%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+   r1     s   $ r   c                       g | ]} | D ]}|qqS r*   r*   r/   r!   r   rows_per_job_idr*   r+   r1         c                    s"   g | ]}| j |d dqS )r!   r"   )r!   r   r   )fetch_job_metricsr/   r!   )r\   r"   r   r*   r+   r1     s    )kwargs_listr   c                    r   r*   r*   r   r   r*   r+   r1   +  r   rc   rd   re   rf   zFailed to fetch job stats: )r,   appendr   whoami	list_jobsr   rQ   multiprocessingpool
ThreadPooltimer   iflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr   r   rg   rh   r   )r   r"   rA   
parsed_idsr!   headers_aliasesr   r   last_update_time
total_rowsr   donerw   nowri   rj   r*   )r\   r"   r   r   r+   
jobs_stats  sj   

&


r   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).)hiddenr2   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.jsonrD   r   zlabel!=zlabel=Nro   z6Warning: Ignoring invalid label filter format 'label!=z'. Use label!=key format.rn   *r   !)Warning: Ignoring invalid filter format ''. Use key=value format.r   r   r    )rR   r7   rj   r8    matching filters: , c                 S   "   g | ]\}}}| | | qS r*   r*   r/   kovr*   r*   r+   r1        " zjobs_ps.<locals>.<listcomp>r   zNo jobs found[])r   IMAGE/SPACECOMMANDCREATEDSTATUS)rR   r7   r8   createdrj   c                 S      g | ]}t |qS r*   r   r   r*   r*   r+   r1         r0   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 )Nrj   r8   r   r   rR   r   docker_image
created_at   Tr   r   )rp   joinr}   r   r|   )r0   rj   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+   r1     r   rR   itemsr   r   id_keyrx   r   )r   r   
startswithr   rQ   r'   endswithr   rq   rj   r   r   r8   r   rR   rv   rE   r   r}   r
   listr   r   r   r   r   )r   r"   rA   r   r   r   r   r\   jobsrm   labels_filtersf
label_part	label_keyoplabel_valuerr   r-   filtered_jobsr]   rj   image_or_spacer   r   propsfilters_msgrx   ry   r   output_formatr*   r   r+   jobs_ps8  s   








$
 
r  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 NAMECPURAMACCELERATORzCOST/MINz	COST/HOUR)rV   
prettyNamecpuramacceleratorcostMincostHourr   zx z ()N$z.4f<   z.2fzNo hardware options found)r   list_jobs_hardwarer  quantitymodelvramunit_cost_usdr   rV   pretty_namer  r  rQ   r   )	r\   hardware_listr   r   rw   hwaccelerator_infocost_min	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 JobsrD   c                       g | ]	} j |d qS )r   )inspect_jobr   r\   r"   r*   r+   r1     s    z jobs_inspect.<locals>.<listcomp>c                 S   r   r*   r   r   r*   r*   r+   r1     r      indentdefaultNrc   rd   re   rf   zFailed to inspect job: )r,   r   r   rQ   r   dumpsr}   r   rg   rh   r   )r   r"   rA   r   r!   r  ri   rj   r*   r.  r+   jobs_inspect  s$   
$

r4  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 JobrD   r   Nrc   rd   re   z>Access denied. You may not have permission to cancel this job.zFailed to cancel job: )r,   r   
cancel_jobr   rg   rh   r   )r!   r"   rA   r\   ri   rj   r*   r*   r+   jobs_cancel  s   


r7  zFRun UV scripts (Python with inline dependencies) on HF infrastructure.uv)rV   )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 infrastructurerB   rD   )r9  r:  dependenciesr<  r7   r9   r:   rE   r>   r?   r"   rF   rG   NTrH   )rJ   r   r   rK   rL   rC   rM   rN   r   
run_uv_jobrP   rQ   rR   rS   rT   rU   rV   )r9  r:  r7   r>   r9   r:   r;   r<   r=   r?   r@   r"   rA   r;  r<  rW   rX   rY   rZ   r[   r\   r]   r^   r*   r*   r+   jobs_uv_run  s@    

r@  z,Create and manage scheduled Jobs on the Hub.	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.rB   rD   )r7   r8   rB  rC  rD  r9   r:   rE   r>   r?   r"   Scheduled Job created with ID: N)rJ   r   r   rK   rL   rC   rM   rN   r   create_scheduled_jobrP   rQ   rR   )rB  r7   r8   rC  rD  r9   r:   r;   r<   r=   r>   r?   r"   rA   rW   rX   rY   rZ   r[   r\   scheduled_jobr*   r*   r+   scheduled_runK  s4    
rH  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   rD   r   ro   r   r   rn   Nr   r   r   Fr   r   )rR   r7   rC  r8   r   r   c                 S   r   r*   r*   r   r*   r*   r+   r1     r   z scheduled_ps.<locals>.<listcomp>r   zNo scheduled jobs foundr   )IDSCHEDULEr   r   zLAST RUNzNEXT RUNSUSPEND)rR   rB  r7   r8   lastnextrC  c                 S   r   r*   r   )r/   sjr*   r*   r+   r1     r   r0   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_specrj   last_jobr8   r   atr   r   r   next_job_run_atrR   r   rB  r   rC  F)rp   r|   r   r}   r   )r0   rO  rj   rP  r   last_job_atnext_runr   r*   r*   r+   r     s&   

zscheduled_ps.<locals>.row_fnr   c                    r   r*   r*   r.   r   r*   r+   r1     r   rR   r   )r   list_scheduled_jobsr'   r  r   rq   rQ   rC  rO  r   r8   r   rR   r}   rv   r   r
   r  r   r   r   r   r   )r   r"   rA   r   r   r   r   r\   scheduled_jobsrm   r  rr   r-   r	  r  rG  rC  r  r   r   r  r  rx   ry   r   r  r*   r   r+   scheduled_ps~  sb   



" 
rW  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 JobsrD   c                    r,  )scheduled_job_idr"   )inspect_scheduled_job)r/   rZ  r.  r*   r+   r1      s    z%scheduled_inspect.<locals>.<listcomp>c                 S   r   r*   r   )r/   rG  r*   r*   r+   r1     r   r/  r0  N)r,   r   r   rQ   r   r3  r}   )rX  r"   rA   r   r!   rV  r*   r.  r+   scheduled_inspect  s   
"r\  deletezhf jobs scheduled delete <id>rZ  c                 C   *   t | |\} }t|d}|j| |d dS )zDelete a scheduled Job.rD   rY  N)r,   r   delete_scheduled_jobrZ  r"   rA   r\   r*   r*   r+   scheduled_delete     
ra  zhf jobs scheduled suspend <id>c                 C   r^  )z Suspend (pause) a scheduled Job.rD   rY  N)r,   r   suspend_scheduled_jobr`  r*   r*   r+   scheduled_suspend  rb  rd  resumezhf jobs scheduled resume <id>c                 C   r^  )z!Resume (unpause) a scheduled Job.rD   rY  N)r,   r   resume_scheduled_jobr`  r*   r*   r+   scheduled_resume  rb  rg  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=  rB   rD   )r9  r:  rB  rC  rD  r>  r<  r7   r9   r:   rE   r>   r?   r"   rE  N)rJ   r   r   rK   rL   rC   rM   rN   r   create_scheduled_uv_jobrP   rQ   rR   )rB  r9  r:  rC  rD  r7   r>   r9   r:   r;   r<   r=   r?   r"   rA   r;  r<  rW   rX   rY   rZ   r[   r\   r]   r*   r*   r+   scheduled_uv_run/  s:    
ri  rE   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.
    Nro   r   r   )r'   )rE   
labels_map	label_varrr   r-   r*   r*   r+   rP   n  s   	 
rP   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   r}   )r/   xr*   r*   r+   	<genexpr>  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)r/   colr*   r*   r+   r1     r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r*   )r/   wr*   r*   r+   r1     r   c                 S   s@   g | ]\}}t t||krt|d |d  d nt|qS )N   z...rm  )r/   rn  	col_widthr*   r*   r+   r1     s    .
)r   rp  shutilget_terminal_sizecolumnsr   sumindexr   r   r   )	rw   rx   
col_widthsterminal_widthcol_to_minimize
row_formatlinesr   row_format_argsr*   r*   r+   r~     s$    
r~   c                  C   s$   t j } t  }d ur|| d< | S )NHF_TOKEN)rL   rC   rM   r   )rZ   rA   r*   r*   r+   rN     s   
rN   r   queuefunc.kwargsc                 C   s"   |di |D ]}|  | qd S )Nr*   )put)r  r  r  resultr*   r*   r+   _write_generator_to_queue  s   r  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*   )apply_asyncr  )r/   r  r  r   r  r*   r+   r1     r   z&iflatmap_unordered.<locals>.<listcomp>T皙?r?   c                 s   s    | ]}|  V  qd S rl  )readyr/   async_resultr*   r*   r+   ro    s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )r  r  )rp   r  r*   r*   r+   r1     s    N)r   rp   r   r   emptyKeyboardInterruptr   TimeoutError)r   r  r   async_resultsr*   r  r+   r     s8   r   )
NNNNNNNFNN)FNNN)NNN)FNNNNFF)r#   N)NN)NNNNNNNNNFNNNN)NNNNNNNNNNN)NNNNNNNNNNNNNNN)u__doc__r   r   multiprocessing.poolrL   rx  r   collectionsr   dataclassesr   r   pathlibr   r  r   r   typingr	   r
   r   r   r   r   r   r   typerhuggingface_hubr   r   huggingface_hub.errorsr   r   huggingface_hub.utilsr   $huggingface_hub.utils._cache_managerr   huggingface_hub.utils._dotenvr   
_cli_utilsr   r   r   r   r   r   r   r    
get_logger__name__loggerr}   tupler,   SUGGESTED_FLAVORSr   ArgumentImageArgOptionImageOpt	FlavorOptr  EnvOpt
SecretsOpt	LabelsOpt
EnvFileOptSecretsFileOpt
TimeoutOptbool	DetachOptNamespaceOptWithOpt	PythonOpt
SuspendOptConcurrencyOptScheduleArg	ScriptArgScriptArgsArg
CommandArgJobIdArg	JobIdsArgScheduledJobIdArgjobs_clir8   r_   intrk   r   rv   r   r   r   r   r  r*  r4  r7  uv_app	add_typerr@  scheduled_apprH  rW  r\  ra  rd  rg  scheduled_uv_appri  rP   r~   rN   r   r  r   r   r   r*   r*   r*   r+   <module>   s.  2((
&
	
	
		
	


	
2
,1
 
V		


 	
	

6		
.		


o	
	
&7**