o
    2jН                  *   @   s
  d Z ddlZddlZddlZddlZddlmZ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 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' ddl(m)Z)m*Z* e+e,Z-de.de.dB de/e.e.dB f fddZ0dZ1ee.ej2ddf Z3ee.dB ej4ddf Z5ee.dB ej4de!edf Z6ee7e. dB ej4ddddf Z8ee.dB ej4ddf Z9ee:ej4ddd df Z;ee.dB ej4d!df Z<ee7e= dB ej4d"d#df Z>ee7e. dB ej4d$d%df Z?ee.dB ej4d&d'd(df Z@ee:dB ej4d)df ZAee:dB ej4d*df ZBee.ej2d+df ZCee.ej2d,df ZDee7e. dB ej2d-df ZEee7e. ej2d.df ZFee.ej2d/df ZGee7e. dB ej2d0df ZHee.ej2d1df ZIe'd2dZJeJjKd3d4d5ig d6d7									8			dd9e3d:eFd;ed<e d=e8d>e#d?ed@edAe6dBe9dCe;dDe>de<dEe"ddfdFdGZLeJjKdHg dIdJ	8			ddeGdKee:ej4dLdMdNdf dOee=dB ej4dPdQdRdf de<dEe"ddfdSdTZMdUeNe.e.f dVe7e/e.e.e.f  de:fdWdXZOdYe=ddfdZd[ZPde.d\eeNe.ef  d]e7e. dee/e:e.e7e7e.e=B   f  fd^d_ZQeJjKd`dagdJ			ddbeHde<dEe"ddfdcddZReJjKdedfdggdJ	8			ddhee:ej4didjdkdf de<dEe"dlee7e. dB ej4dLdmdndf ddf
dodpZSeJjKdqdrgdJddsdtZTeJjKdudvgdJ		ddbee7e. ej2dwdf de<dEe"ddfdxdyZUeJjKdzd{gdJ		ddeGde<dEe"ddfd|d}ZVeJjKd~ddgdJ		8		ddeGd=e8dee:ej4dddf de<dEe"ddfddZWe'ddZXeJjYeXdd eXjKd3d4d5ig dd7											8					ddeDdeEd9e5dAe6d;ed<e d=e8d>e#d?ed@edBe9dCe;dDe>de<dEe"de?de@ddf$ddZZe'ddZ[eJjYe[dd e[jKd3d4d5idgd7													ddeCd9e3d:eFdeAdeBd;ed<e d=e8d>e#d?ed@edAe6dBe9dDe>de<dEe"ddf"ddZ\e[jKdedgdJ	8			ddhee:ej4didjddf de<dEe"dlee7e. dB ej4dLdmdndf ddf
ddZ]e[jKdudgdJ		ddee7e. ej2ddf de<dEe"ddfddZ^e[jKddgdJ		ddeIde<dEe"ddfddZ_e[jKddgdJ		ddeIde<dEe"ddfddZ`e[jKddgdJ		ddeIde<dEe"ddfddZae[jKd~ddgdJ		8		ddeId=e8dee:ej4dddf de<dEe"ddfddZbe'ddZce[jYecdd ecjKd3d4d5iddgd7																	ddeCdeDdeEdeAdeBd9e5dAe6d;ed<e d=e8d>e#d?ed@edBe9dDe>de<dEe"de?de@ddf(ddZdd~e7e. dB deNe.e.f dB fddZede7e7e.e=B   de7e. de.fddZfedZgdeeg dedeeg f deNddfddÄZhdejijjdedeeg f de7eN deeg fddǄZkd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]

    # 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]

    # 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)CallableIterable)fnmatch)EmptyQueue)	AnnotatedAnyTypeVar)JobHardware)CLIErrorHfHubHTTPError)logging)_format_size)format_duration   )
EnvFileOptEnvOptSecretsFileOpt
SecretsOpt
SoftChoiceTokenOpt
VolumesOpt
get_hf_apiparse_env_mapparse_volumestyper_factory)_dataclass_to_dictout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'   S/var/www/html/whisper/venv/lib/python3.10/site-packages/huggingface_hub/cli/jobs.py_parse_namespace_from_job_idd   s   r)   g?zThe Docker image to use.)helpz.Use a custom Docker image with `uv` installed.zcFlavor for the hardware. Run 'hf jobs hardware' to list available flavors. Defaults to `cpu-basic`.)r*   
click_typez-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--exposezExpose a container port through the jobs proxy. Repeat the flag for multiple ports (e.g. `--expose 8000 --expose 8001`). Each exposed port is reachable on the public jobs domain; access requires an HF token with read access to the job'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!")'z1hf jobs run --detach python:3.12 python script.pyz3hf jobs run -e FOO=foo python:3.12 python script.pyz;hf jobs run --secrets HF_TOKEN python:3.12 python script.pyz^hf jobs run -v hf://org/my-model:/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exposetokenc                 C   s   t ||}t ||}t|d}|j| |||t|t|||	||d
}tjd|j|jd t	|j
jtrGddd |j
jD }td|  |
ra|jj d	|j }td
| d| d dS |j|j|jjddD ]}t| qldS )z
Run a Job.r<   )
r0   r1   r2   r3   labelsvolumesr8   r9   r;   r   Job startedidurl
c                 s       | ]}d | V  qdS z  Nr'   .0rC   r'   r'   r(   	<genexpr>D      zjobs_run.<locals>.<genexpr>SExposed ports are reachable at (requires an HF token with read access to the job):
r!   Use `hf jobs logs -f &` to stream logs, or `hf jobs inspect ` to check status.NTr   r   follow)r   r   run_job_parse_labels_mapr   r   resultrB   rC   
isinstancestatusexpose_urlslistjoinhintownernamefetch_job_logstext)r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r   r<   env_mapsecrets_mapapijoburlsjob_reflogr'   r'   r(   jobs_run  s4   


re   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>z#hf jobs logs -f --tail 100 <job_id>)r/   rP   z-fz--followzqFollow log output (stream until the job completes). Without this flag, only currently available logs are printed.tailz-nz--tailzvNumber of lines to show from the end of the logs. When combined with --follow, starts streaming from the last N lines.c              
   C   s   t | |\} }t|d}z/|j| |||d}|D ]}t| q|r9|r+| d|  n| }td| d W dS W dS  tyl }	 z%|	jdurL|	jjnd}
|
dkrWt	d|	|
d	kr`t	d
|	t	d|	 |	d}	~	ww )a  Fetch 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.
    Use --tail/-n to limit the number of lines returned (server-side when supported).

    Note: following exits when the log stream ends, regardless of whether the Job
    succeeded or failed. Run `hf jobs inspect <job_id>` to check the final status.
    r=   )r   r   rP   rg   r!   z#Stream ended. Run `hf jobs inspect z6` to check the final status (e.g. COMPLETED or ERROR).N  '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\   r   r]   rY   r   responsestatus_coder   )r   rP   rg   r   r<   r`   rf   rd   rc   erU   r'   r'   r(   	jobs_logsN  s*   '




ro   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)rp   rq   keyop_strpatternvaluematchr'   r'   r(   _matches_filters  s   
r{   nc                 C   s&   d}d}t | D ]}t||d qd S )Nz[1Az[2K)end)rangeprint)r|   LINE_UP
LINE_CLEARir'   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   rT   dictlenzipsorted)r   r   r   metricsrowrows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rDtd dS g d	ztj	
t| }i | D ]}|g}|d
d dd D 7 }|g|< qVt }fddD }tt|d  fdd| D }	t|t|	dD ]7\}
}}|
r|d n||< t }|| tkrtdt|  fddD }t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=   r[   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNRUNNINGUPDATING)rU   stagerB   rH   ra   r'   r'   r(   
<listcomp>  s
    zjobs_stats.<locals>.<listcomp>r   r   zNo running jobs found)	zJOB IDzCPU %zNUM CPUzMEM %z	MEM USAGEzNET I/Oz
GPU UTIL %z	GPU MEM %zGPU MEM USAGEc                 S   s$   g | ]}d |v sd|v rdndqS )r!   USAGEz-- / --z--r'   )rH   headerr'   r'   r(   r     s   $ r   c                       g | ]} | D ]}|qqS r'   r'   rH   r   r   rows_per_job_idr'   r(   r         )headersc                    s"   g | ]}| j |d dqS )r   r   )r   r   r   )fetch_job_metricsrH   r   )r`   r   r   r'   r(   r     s    )kwargs_listr   c                    r   r'   r'   r   r   r'   r(   r     r   rh   ri   rj   rk   zFailed to fetch job stats: )r)   appendr   whoami	list_jobsr   r   r]   multiprocessingpool
ThreadPooltimer   	_tabulateiflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr   r   rl   rm   r   )r   r   r<   
parsed_idsr   r   r   last_update_time
total_rowsr   doner   nowrn   rU   r'   )r`   r   r   r   r(   
jobs_stats  sh   


&


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)c                 C   s.  t |d}|j|d}g }g }|pg D ]}|ds|drz|drC|tdd }	d|	v r:td|	 d q|	d	d
}
}}n*|tdd }	d|	v rX|	dd\}
}n|	d
}
}|
drkd	}|
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|| qg }|D ]f}t|}|dpi }|dpg }|dd|d< |dpd|d< |r)d|nd|d< |dr?|d dd ddnd|d< |d pJi d!d|d < t|d"|d#< || qtj|g d$dd% |s|rd&d'd( |D }td)|  dS | s|std* dS dS dS dS )+z
List Jobs.r=   r   zlabel!=zlabel=Nrs   z-Ignoring invalid label filter format 'label!=z'. Use label!=key format.rr   *r   ! Ignoring invalid filter format ''. Use key=value format.r   r   r    )rB   r0   rU   r1   	durationsr1   rB   r   r   docker_imageimage/space
created_at   TcreatedrU   r   running_secsruntime)r   r   r1   r   rU   r   r   id_key, c                 s   &    | ]\}}}| | | V  qd S Nr'   rH   kovr'   r'   r(   rI   k     $ zjobs_ps.<locals>.<genexpr>zNo jobs matched filters: zHNo running jobs. Use `-a`/`--all` to include finished (and failed) jobs.)r   r   
startswithr   r   warningr$   endswithr   ru   rU   r   r   r1   rX   rB   r{   r>   r   rt   replacer   tabler]   rY   )r   r   r<   r   r`   jobsrq   labels_filtersf
label_part	label_keyoplabel_valuerv   ry   filtered_jobsra   rU   image_or_spacecmdcommand_strpropsitemsitemr   filters_msgr'   r'   r(   jobs_ps
  s   







,r   hardwarezhf jobs hardwarec                  C   s   t  } |  }g }|D ]E}d}|jr#|jj d|jj d|jj d}|jr-d|jdnd}|jr;d|jd d	nd}||j|j	|j
|j|j|||d
 qt| td dS )z(List available hardware options for Jobsr   zx z ()$z.4ffree<   z.2f)r[   zpretty namecpuramstorageacceleratorzcost/minz	cost/hourzLUse `hf jobs run --flavor <name> ...` to request a specific hardware flavor.N)r   list_jobs_hardwarer   quantitymodelvramunit_cost_usdr   r[   pretty_namer   r   ephemeral_storager   r   rY   )r`   hardware_listr   hwaccelerator_infocost_min	cost_hourr'   r'   r(   jobs_hardwareq  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 }W n1 tyW } z%|jdur7|jjnd}|dkrBtd||dkrKtd|td	| |d}~ww td
d |D  dS )z0Display detailed information on one or more Jobsr=   c                       g | ]	} j |d qS )r   )inspect_jobr   r`   r   r'   r(   r     s    z jobs_inspect.<locals>.<listcomp>Nrh   ri   rj   rk   zFailed to inspect job: c                 S      g | ]}t |qS r'   r   r   r'   r'   r(   r         )	r)   r   r   r   rl   rm   r   r   r   )r   r   r<   r   r   r   rn   rU   r'   r  r(   jobs_inspect  s$   


r  cancelzhf jobs cancel <job_id>c              
   C   s   t | |\} }t|d}z	|j| |d W n1 tyF } z%|jdur&|jjnd}|dkr1td||dkr:td|td| |d}~ww tjd	| d
 dS )zCancel a Jobr=   r   Nrh   ri   rj   z>Access denied. You may not have permission to cancel this job.zFailed to cancel job: zJob cancelledrB   )	r)   r   
cancel_jobr   rl   rm   r   r   rS   )r   r   r<   r`   rn   rU   r'   r'   r(   jobs_cancel  s   


r  r>   z8hf jobs labels <job_id> --label env=prod --label team=mlzhf jobs labels <job_id> --clearclearz--clearzRemove all labels from the job.c                 C   h   |s|st d|r|rt dt| |\} }t|pi }t|d}|j| ||d}tjd|jd dS )z5Update labels on a Job. Replaces all existing labels.OPlease set at least one label with --label. To remove all labels, pass --clear.bCannot set labels and clear them at the same time. Please use either --label or --clear, not both.r=   )r   r>   r   Labels updatedr  N)r   r)   rR   r   update_job_labelsr   rS   rB   )r   r4   r  r   r<   r>   r`   ra   r'   r'   r(   jobs_labels  s   
r   zFRun UV scripts (Python with inline dependencies) on HF infrastructure.uv)r[   )zhf jobs uv run my_script.pyz$hf jobs uv run --detach my_script.pyz1hf jobs uv run ml_training.py --flavor a10g-smallz+hf jobs uv run --with transformers train.pyzNhf jobs uv run -v hf://org/my-model:/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jd|j|jd t	|j
jtrLddd |j
jD }td|  |rf|jj d	|j }td
| d| d dS |j|j|jjddD ]}t| qqdS )8Run a UV script (local file or URL) on HF infrastructurer=   )r"  r#  dependenciesr%  r0   r2   r3   r>   r?   r8   r9   r;   r   r@   rA   rD   c                 s   rE   rF   r'   rG   r'   r'   r(   rI     rJ   zjobs_uv_run.<locals>.<genexpr>rK   r!   rL   rM   rN   NTrO   )r   r   
run_uv_jobrR   r   r   rS   rB   rC   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   )r"  r#  r0   r8   r2   r3   r4   r5   r6   r7   r9   r:   r;   r   r<   r$  r%  r^   r_   r`   ra   rb   rc   rd   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   sr   t ||	}t ||
}t|d}|j||| ||||t|t|||||d}tjd|jd td|j d dS )zSchedule a Job.r=   )r0   r1   r+  r,  r-  r2   r3   r>   r?   r8   r9   r;   r   Scheduled Job createdr  Use `hf jobs scheduled inspect ` to view its details.N)	r   r   create_scheduled_jobrR   r   r   rS   rB   rY   )r+  r0   r1   r,  r-  r2   r3   r4   r5   r6   r7   r8   r9   r;   r   r<   r^   r_   r`   scheduled_jobr'   r'   r(   scheduled_run$  s(   


r3  zhf jobs scheduled psz1Show all scheduled Jobs (default hides suspended)c                 C   s8  t |d}|j|d}g }|pg D ]4}d|v r<|dd\}}	|dr-d}
|dd }nd}
|| |
|	 f qtd	| d
 qg }|D ]6}|jpPd}| sV|rVqJ|j	j
p[d}|j	jpag }|rid|nd}|j|t||d}t||s{qJ|| qJg }|D ]n}t|}|dpi }|dpi }|d}|dpg }|dpd|d< |rd|nd|d< |r|dr|d dd ddnd|d< |dr|d dd ddnd|d< |dpd|d< || qtj|g ddd |s|rdd d! |D }td"|  dS dS dS )#zList scheduled Jobsr=   r   rs   r   r   rr   Nr   r   r   Fr   r   )rB   r0   r,  r1   job_specrU   last_jobr1   r   r   atr   r   last_runnext_job_run_atnext_runr,  )rB   r+  r   r1   r7  r9  r,  rB   r   r   c                 s   r   r   r'   r   r'   r'   r(   rI     r   zscheduled_ps.<locals>.<genexpr>z#No scheduled jobs matched filters: )r   list_scheduled_jobsr$   r   r   ru   r   r   r,  r4  r   r1   rX   rB   strr{   r   rt   r   r   r]   )r   r   r<   r   r`   scheduled_jobsrq   r   rv   ry   r   r   r2  r,  r   r   r   r   r   sjr   r4  status_dictr5  r   r'   r'   r(   scheduled_psS  s^   




.$r?  zhf jobs scheduled inspect <id>scheduled_job_idsz>Scheduled Job IDs to inspect (or 'namespace/scheduled_job_id')c                    s`   g }| D ]}t |\}|| q|} t|d  fdd| D }tdd |D  dS )z:Display detailed information on one or more scheduled Jobsr=   c                    r  )scheduled_job_idr   )inspect_scheduled_job)rH   rB  r  r'   r(   r     s    z%scheduled_inspect.<locals>.<listcomp>c                 S   r  r'   r  )rH   r2  r'   r'   r(   r     r  N)r)   r   r   r   r   )r@  r   r<   r   r   r<  r'   r  r(   scheduled_inspect  s   
rD  deletezhf jobs scheduled delete <id>rB  c                 C   8   t | |\} }t|d}|j| |d tjd| d dS )zDelete a scheduled Job.r=   rA  zScheduled Job deletedr  N)r)   r   delete_scheduled_jobr   rS   rB  r   r<   r`   r'   r'   r(   scheduled_delete     
rI  zhf jobs scheduled suspend <id>c                 C   sJ   t | |\} }t|d}|j| |d tjd| d td|  d dS )z Suspend (pause) a scheduled Job.r=   rA  zScheduled Job suspendedr  zUse `hf jobs scheduled resume z` to resume it.N)r)   r   suspend_scheduled_jobr   rS   rY   rH  r'   r'   r(   scheduled_suspend  s
   
rL  resumezhf jobs scheduled resume <id>c                 C   rF  )z!Resume (unpause) a scheduled Job.r=   rA  zScheduled Job resumedr  N)r)   r   resume_scheduled_jobr   rS   rH  r'   r'   r(   scheduled_resume  rJ  rO  z>hf jobs scheduled labels <id> --label env=prod --label team=mlz%hf jobs scheduled labels <id> --clearz)Remove all labels from the scheduled job.c                 C   r  )z?Update labels on a scheduled Job. Replaces all existing labels.r  r  r=   )rB  r>   r   r  r  N)r   r)   rR   r   update_scheduled_job_labelsr   rS   rB   )rB  r4   r  r   r<   r>   r`   r2  r'   r'   r(   scheduled_labels  s   
rQ  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   sH  t ||}t ||}t|d}|jdi d|d|p_g d| d|d|d|d|d	|d
|d|dt|	dt|
d|d|d|d|}tjd|jd td|j d dS d| d|d|d|d|d	|d
|d|dt|	dt|
d|d|d|d|}tjd|jd td|j d dS )r&  r=   r"  r#  r+  r,  r-  r'  r%  r0   r2   r3   r>   r?   r8   r9   r;   r   r.  r  r/  r0  Nr'   )	r   r   create_scheduled_uv_jobrR   r   r   rS   rB   rY   )r+  r"  r#  r,  r-  r0   r8   r2   r3   r4   r5   r6   r7   r9   r;   r   r<   r$  r%  r^   r_   r`   ra   r'   r'   r(   scheduled_uv_run  s   



	
	
rS  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.
    Nrs   r   r   )r$   )r>   
labels_map	label_varrv   ry   r'   r'   r(   rR   @  s   	 
rR   r   r   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 r   r   r;  )rH   xr'   r'   r(   rI   Y  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)rH   colr'   r'   r(   r   Y  r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r'   )rH   wr'   r'   r(   r   c  r  c                 S   s@   g | ]\}}t t||krt|d |d  d nt|qS )N   z...rV  )rH   rW  	col_widthr'   r'   r(   r   e  s    .rD   )r   rX  shutilget_terminal_sizecolumnsr   sumindexformatr   rX   )	r   r   
col_widthsterminal_widthcol_to_minimize
row_formatlinesr   row_format_argsr'   r'   r(   r   R  s$    
r   r   queuefunc.kwargsc                 C   s"   |di |D ]}|  | qd S )Nr'   )put)rk  rl  rm  rS   r'   r'   r(   _write_generator_to_queuep  s   ro  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_asyncro  )rH   rm  rl  r   rk  r'   r(   r     r   z&iflatmap_unordered.<locals>.<listcomp>T皙?r9   c                 s   s    | ]}|  V  qd S r   )readyrH   async_resultr'   r'   r(   rI     s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )rr  rs  )rt   ru  r'   r'   r(   r     s    N)r   rt   r   r   emptyKeyboardInterruptr   TimeoutError)r   rl  r   async_resultsr'   rq  r(   r   u  s8   r   )NNNNNNNNFNNN)FNNN)NNN)r    N)NN)NFNN)NNNNNNNNNNFNNNNN)NNNNNNNNNNNNN)NNNNNNNNNNNNNNNNN)l__doc__r   multiprocessing.poolr_  r   collections.abcr   r   r   rk  r   r   typingr   r   r	   typerhuggingface_hubr
   huggingface_hub.errorsr   r   huggingface_hub.utilsr   $huggingface_hub.utils._cache_managerr   huggingface_hub.utils._parsingr   
_cli_utilsr   r   r   r   r   r   r   r   r   r   r   _outputr   r   
get_logger__name__loggerr;  tupler)   r   ArgumentImageArgOptionImageOpt	FlavorOptrW   	LabelsOpt
TimeoutOptbool	DetachOptNamespaceOptint	ExposeOptWithOpt	PythonOpt
SuspendOptConcurrencyOptScheduleArg	ScriptArgScriptArgsArg
CommandArgJobIdArg	JobIdsArgScheduledJobIdArgjobs_clir1   re   ro   r   r{   r   r   r   r   r  r  r  r   uv_app	add_typerr)  scheduled_appr3  r?  rD  rI  rL  rO  rQ  scheduled_uv_apprS  rR   r   r   ro  r   r   r   r'   r'   r'   r(   <module>   s.  24
&
		

	


	
-
,4
M		

f		
	

3		
*		

N		
	
&3&*