o
     
j                  (   @   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mZ ddlm	Z	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 ddlZdd	lmZ dd
lmZ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'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. e/e0Z1de2de2dB de3e2e2dB f fddZ4dd eD Z5dZ6ee2ej7ddf Z8ee2dB ej9ddf Z:eedB ej9ddf Z;ee<e2 dB ej9ddddf Z=ee2dB ej9ddf Z>ee?ej9dd d!df Z@ee2dB ej9d"df ZAee<e2 dB ej9d#d$df ZBee2dB ej9d%d&d'df ZCee?dB ej9d(df ZDee?dB ej9d)df ZEee2ej7d*df ZFee2ej7d+df ZGee<e2 dB ej7d,df ZHee<e2 ej7d-df ZIee2ej7d.df ZJee<e2 dB ej7d/df ZKee2ej7d0df ZLe.d1dZMeMjNd2d3d4ig d5d6									7		dd8e8d9eId:e!d;e%d<e=d=e'd>e d?e$d@e;dAe>dBe@deAdCe&ddfdDdEZOeMjNdFg dGdH	7			ddeJdIee?ej9dJdKdLdf dMeePdB ej9dNdOdPdf deAdCe&ddfdQdRZQdSeRe2e2f dTe<e3e2e2e2f  de?fdUdVZSdWe<e<e2ePB   dXe<e2 dYe<e2 dZe2dB ddf
d[d\ZTd]ePddfd^d_ZUde2d`e
eRe2ef  dae<e2 de
e3e?e2e<e<e2ePB   f  fdbdcZVeMjNdddegdH			ddfeKdeAdCe&ddfdgdhZWeMjNdidjdkgdH	7					7ddlee?ej9dmdndodf deAdCe&dpee<e2 dB ej9dJdqdrdf dsee2dB ej9dtdf due#ddfdvdwZXeMjNdxdygdHddzd{ZYeMjNd|d}gdH		ddfee<e2 ej7d~df deAdCe&ddfddZZeMjNddgdH		ddeJdeAdCe&ddfddZ[e.ddZ\eMj]e\dd e\jNd2d3d4ig dd6											7				ddeGdeHd8e:d@e;d:e!d;e%d<e=d=e'd>e d?e$dAe>dBe@deAdCe&deBdeCddf"ddZ^e.ddZ_eMj]e_dd e_jNd2d3d4idgd6												ddeFd8e8d9eIdeDdeEd:e!d;e%d<e=d=e'd>e d?e$d@e;dAe>deAdCe&ddf ddZ`e_jNdidgdH	7					7ddlee?ej9dmdnddf deAdCe&dpee<e2 dB ej9dJdqdrdf dsee2dB ej9dtdf due#ddfddZae_jNd|dgdH		ddee<e2 ej7ddf deAdCe&ddfddZbe_jNddgdH		ddeLdeAdCe&ddfddZce_jNddgdH		ddeLdeAdCe&ddfddZde_jNddgdH		ddeLdeAdCe&ddfddZee.ddZfe_j]efdd efjNd2d3d4iddgd6																ddeFdeGdeHdeDdeEd8e:d@e;d:e!d;e%d<e=d=e'd>e d?e$dAe>deAdCe&deBdeCddf&ddZgde<e2 dB deRe2e2f dB fddZhdWe<e<e2ePB   dXe<e2 de2fddZiedZjdeej de	de
ej f deRddfddZkdejljmde	de
ej f de<eR de
ej fddZnd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)CallableIterableasdict)fnmatch)EmptyQueue)	AnnotatedAnyTypeVar)SpaceHardware)CLIErrorHfHubHTTPError)logging)_format_size   )
EnvFileOptEnvOptOutputFormatQuietOptSecretsFileOpt
SecretsOptTokenOpt
VolumesOpt_format_cellapi_object_to_dict
get_hf_apiparse_env_mapparse_volumes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+   K/home/kuhnn/.local/lib/python3.10/site-packages/huggingface_hub/cli/jobs.py_parse_namespace_from_job_idi   s   r-   c                 C   s   g | ]
}|j d kr|j qS )z	zero-a10g)value.0itemr+   r+   r,   
<listcomp>   s    r2   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-lz--labelz3Set labels. E.g. --label KEY=VALUE or --label LABELzVMax 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.pyzVhf jobs run -v hf://gpt2:/data -v hf://buckets/org/b:/mnt python:3.12 python script.py)context_settingsexamplesFimagecommandenvsecretslabelvolumeenv_filesecrets_fileflavortimeoutdetachtokenc                 C   s   t ||}t ||}t|d}|j| |||t|t|||	|d	}td|j  td|j  |
r5dS |j|j|j	j
ddD ]}t| q@dS )z
Run a Job.rC   )	r8   r9   r:   r;   labelsvolumesr@   rA   r#   Job started with ID: 	View at: NTr"   r#   follow)r   r   run_job_parse_labels_mapr   printidurlfetch_job_logsownername)r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r#   rC   env_mapsecrets_mapapijoblogr+   r+   r,   jobs_run  s*   



rX   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>)r7   rJ   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   rI   )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   rP   r   rM   r   responsestatus_code)
r"   rJ   rZ   r#   rC   rU   rY   rW   estatusr+   r+   r,   	jobs_logsG  s,   



rd   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)re   rf   keyop_strpatternr.   matchr+   r+   r,   _matches_filters{  s   
ro   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}})rq   N)	enumeratereplacestrrM   	_tabulate)
rp   rq   rr   rs   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)rangerM   )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   metricsry   rp   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   rR   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNRUNNINGUPDATING)rc   stagerN   r0   rV   r+   r+   r,   r2     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)	rN   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+   )r0   headerr+   r+   r,   r2     s   $ r   c                       g | ]} | D ]}|qqS r+   r+   r0   r"   ry   rows_per_job_idr+   r,   r2         c                    s"   g | ]}| j |d dqS )r"   r#   )r"   r   r   )fetch_job_metricsr0   r"   )rU   r#   r   r+   r,   r2     s    )kwargs_listr   c                    r   r+   r+   r   r   r+   r,   r2     r   r\   r]   r^   r_   zFailed to fetch job stats: )r-   appendr   whoami	list_jobsr   rM   multiprocessingpool
ThreadPooltimer~   iflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr   r   r`   ra   r   )r   r#   rC   
parsed_idsr"   headers_aliasesr   ry   last_update_time
total_rowsr   donerp   nowrb   rc   r+   )rU   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}}')quietc                    s  t |d}|j|d}g }g }	|pg D ]}
|
ds|
dry|
drB|
tdd }d|v r9td| d q|d	d
}}}n*|
tdd }d|v rW|dd\}}n|d
}}|drjd	}|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# |rZ|d$vrZt fd%d|D ||| dS |dkrbtjntj}t|||d&| d' dS )(z
List Jobs.rD   r   zlabel!=zlabel=Nrh   z6Warning: Ignoring invalid label filter format 'label!=z'. Use label!=key format.rg   *r   !)Warning: Ignoring invalid filter format ''. Use key=value format.r   r   r    )rN   r8   rc   r9   json matching filters: , c                 S   "   g | ]\}}}| | | qS r+   r+   r0   kovr+   r+   r,   r2   j     " zjobs_ps.<locals>.<listcomp>r   zNo jobs found[])r   IMAGE/SPACECOMMANDCREATEDSTATUS)rN   r8   r9   createdrc   c                 S      g | ]}t |qS r+   r   r   r+   r+   r,   r2   r      r1   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 )Nrc   r9   r   r   rN   r   docker_image
created_at   Tr   r   )ri   joinrv   r   ru   )r1   rc   cmdcommand_strr+   r+   r,   row_fnt  s   $zjobs_ps.<locals>.row_fntabler   c                       g | ]} |qS r+   r+   r/   r   r+   r,   r2     r   rN   itemsr   r   id_keyrq   r   )r   r   
startswithr   rM   r(   endswithr   rj   rc   r   r   r9   r   rN   ro   rE   r   rv   r   listr~   r   r   r   r    )r   r#   rC   r   r   r   rU   jobsrf   labels_filtersf
label_part	label_keyoplabel_valuerk   r.   filtered_jobsrV   rc   image_or_spacer   r   propsfilters_msgrq   rr   r   output_formatr+   r   r,   jobs_ps  s   








$
 
r  hardwarezhf jobs hardwarec            	   
   C   s   t  } |  }g d}g d}g }|D ]D}d}|jr+|jj d|jj d|jj d}|jr5d|jdnd	}|jrCd|jd
 dnd	}||j|j	pMd|j
|j|||g q|s`td dS t|||d dS )z(List available hardware options for Jobs)NAMEzPRETTY NAMECPURAMACCELERATORzCOST/MINz	COST/HOUR)rR   
prettyNamecpuramacceleratorcostMincostHourr   zx z ()$z.4ffree<   z.2fzNo hardware options foundN)r   list_jobs_hardwarer  quantitymodelvramunit_cost_usdr   rR   pretty_namer  r  rM   r~   )	rU   hardware_listr   r   rp   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   rU   r#   r+   r,   r2     s    z jobs_inspect.<locals>.<listcomp>c                 S   r   r+   r   r   r+   r+   r,   r2     r      indentdefaultNr\   r]   r^   r_   zFailed to inspect job: )r-   r   r   rM   r   dumpsrv   r   r`   ra   r   )r   r#   rC   r   r"   r   rb   rc   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 JobrD   r   Nr\   r]   r^   z>Access denied. You may not have permission to cancel this job.zFailed to cancel job: )r-   r   
cancel_jobr   r`   ra   r   )r"   r#   rC   rU   rb   rc   r+   r+   r,   jobs_cancel  s   


r/  zFRun UV scripts (Python with inline dependencies) on HF infrastructure.uv)rR   )zhf jobs uv run my_script.pyz1hf jobs uv run ml_training.py --flavor a10g-smallz+hf jobs uv run --with transformers train.pyzFhf jobs uv run -v hf://gpt2:/data -v hf://buckets/org/b:/mnt script.pyscriptscript_argswith_pythonc                 C   s   t ||}t ||	}t|d}|j| |pg |||||t|t|||
|d}td|j  td|j  |r:dS |j|j|j	j
ddD ]}t| qEdS )8Run a UV script (local file or URL) on HF infrastructurerD   )r1  r2  dependenciesr4  r8   r:   r;   rE   rF   r@   rA   r#   rG   rH   NTrI   )r   r   
run_uv_jobrL   r   rM   rN   rO   rP   rQ   rR   )r1  r2  r8   r@   r:   r;   r<   r=   r>   r?   rA   rB   r#   rC   r3  r4  rS   rT   rU   rV   rW   r+   r+   r,   jobs_uv_run  s0   



r8  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\   t ||	}t ||
}t|d}|j||| ||||t|t||||d}td|j  dS )zSchedule a Job.rD   )r8   r9   r:  r;  r<  r:   r;   rE   rF   r@   rA   r#   Scheduled Job created with ID: N)r   r   create_scheduled_jobrL   r   rM   rN   )r:  r8   r9   r;  r<  r:   r;   r<   r=   r>   r?   r@   rA   r#   rC   rS   rT   rU   scheduled_jobr+   r+   r,   scheduled_run  s$   


r@  zhf jobs scheduled psz1Show all scheduled Jobs (default hides suspended)c                    s  t |d}|j|d}g }|pg D ]3}	d|	v r<|	dd\}
}|
dr-d}|
dd }
nd}||
 || f qtd	|	 d
 qg }|D ]6}|jpOd}| sU|rUqI|jj	pZd}|jj
p`g }|rhd|nd}|j|t||d}t||szqI|| qI|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 JobsrD   r   rh   r   r   rg   Nr   r   r   Fr   r   )rN   r8   r;  r9   r   r   r   c                 S   r   r+   r+   r   r+   r+   r,   r2     r   z scheduled_ps.<locals>.<listcomp>r   zNo scheduled jobs foundr   )IDSCHEDULEr   r   zLAST RUNzNEXT RUNSUSPEND)rN   r:  r8   r9   lastnextr;  c                 S   r   r+   r   )r0   sjr+   r+   r,   r2     r   r1   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_specrc   last_jobr9   r   atr   r   r   next_job_run_atrN   r   r:  r   r;  F)ri   ru   r   rv   r   )r1   rG  rc   rH  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,   r2     r   rN   r   )r   list_scheduled_jobsr(   r   r   rj   rM   r;  rG  r   r9   r   rN   rv   ro   r   r   r   r~   r   r   r   r    )r   r#   rC   r   r   r   rU   scheduled_jobsrf   r   rk   r.   r   r  r?  r;  r  r   r   r  r  rq   rr   r   r  r+   r   r,   scheduled_psG  s^   



" 
rO  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)r0   rR  r&  r+   r,   r2     s    z%scheduled_inspect.<locals>.<listcomp>c                 S   r   r+   r   )r0   r?  r+   r+   r,   r2     r   r'  r(  N)r-   r   r   rM   r   r+  rv   )rP  r#   rC   r   r"   rN  r+   r&  r,   scheduled_inspect  s   
"rT  deletezhf jobs scheduled delete <id>rR  c                 C   *   t | |\} }t|d}|j| |d dS )zDelete a scheduled Job.rD   rQ  N)r-   r   delete_scheduled_jobrR  r#   rC   rU   r+   r+   r,   scheduled_delete     
rY  zhf jobs scheduled suspend <id>c                 C   rV  )z Suspend (pause) a scheduled Job.rD   rQ  N)r-   r   suspend_scheduled_jobrX  r+   r+   r,   scheduled_suspend  rZ  r\  resumezhf jobs scheduled resume <id>c                 C   rV  )z!Resume (unpause) a scheduled Job.rD   rQ  N)r-   r   resume_scheduled_jobrX  r+   r+   r,   scheduled_resume  rZ  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   sf   t ||}t ||}t|d}|j||pg | |||||||t|	t|
|||d}td|j  dS )r5  rD   )r1  r2  r:  r;  r<  r6  r4  r8   r:   r;   rE   rF   r@   rA   r#   r=  N)r   r   create_scheduled_uv_jobrL   r   rM   rN   )r:  r1  r2  r;  r<  r8   r@   r:   r;   r<   r=   r>   r?   rA   r#   rC   r3  r4  rS   rT   rU   rV   r+   r+   r,   scheduled_uv_run  s*   


ra  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.
    Nrh   r   r   )r(   )rE   
labels_map	label_varrk   r.   r+   r+   r,   rL   *  s   	 
rL   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   rv   )r0   xr+   r+   r,   	<genexpr>C  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)r0   colr+   r+   r,   r2   C  r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r+   )r0   wr+   r+   r,   r2   M  r   c                 S   s@   g | ]\}}t t||krt|d |d  d nt|qS )N   z...re  )r0   rf  	col_widthr+   r+   r,   r2   O  s    .
)r   rh  shutilget_terminal_sizecolumnsr   sumindexr   r   r   )	rp   rq   
col_widthsterminal_widthcol_to_minimize
row_formatlinesry   row_format_argsr+   r+   r,   rw   <  s$    
rw   r   queuefunc.kwargsc                 C   s"   |di |D ]}|  | qd S )Nr+   )put)r{  r|  r}  resultr+   r+   r,   _write_generator_to_queueZ  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  )r0   r}  r|  r   r{  r+   r,   r2   o  r   z&iflatmap_unordered.<locals>.<listcomp>T皙?rA   c                 s   s    | ]}|  V  qd S rd  )readyr0   async_resultr+   r+   r,   rg  u  s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )r  r  )ri   r  r+   r+   r,   r2   |  s    N)r	   ri   r   r   emptyKeyboardInterruptr   TimeoutError)r   r|  r   async_resultsr+   r  r,   r   _  s8   r   )NNNNNNNNFNN)FNNN)NNN)FNNNNF)r$   N)NN)NNNNNNNNNNFNNNN)NNNNNNNNNNNN)NNNNNNNNNNNNNNNN)o__doc__r   r   multiprocessing.poolrp  r   collectionsr   collections.abcr   r   dataclassesr   r   r{  r   r	   typingr
   r   r   typerhuggingface_hubr   huggingface_hub.errorsr   r   huggingface_hub.utilsr   $huggingface_hub.utils._cache_managerr   
_cli_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   
get_logger__name__loggerrv   tupler-   SUGGESTED_FLAVORSr   ArgumentImageArgOptionImageOpt	FlavorOptr   	LabelsOpt
TimeoutOptbool	DetachOptNamespaceOptWithOpt	PythonOpt
SuspendOptConcurrencyOptScheduleArg	ScriptArgScriptArgsArg
CommandArgJobIdArg	JobIdsArgScheduledJobIdArgjobs_clir9   rX   intrd   r   ro   r~   r   r   r   r  r"  r,  r/  uv_app	add_typerr8  scheduled_appr@  rO  rT  rY  r\  r_  scheduled_uv_appra  rL   rw   r   r  r   r   r   r+   r+   r+   r,   <module>   s  2D
&
		
	


	
*
,16
V		


z	
	

/		
'		


i	
	
&0&*