o
    0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#   Zextracted_namespaceZparsed_job_id r)   Y/var/www/html/Deteccion_Ine/venv/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    r0   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.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.pyzVhf jobs run -v hf://gpt2:/data -v hf://buckets/org/b:/mnt python:3.12 python script.py)Z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.r?   )	r4   r5   r6   r7   labelsvolumesr<   r=   r#   Job started with ID: 	View at: NTr"   r#   follow)r   r   Zrun_job_parse_labels_mapr   printidurlfetch_job_logsownername)r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r#   r?   env_mapsecrets_mapapijoblogr)   r)   r*   jobs_run  s*   



rS   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>)r3   rF   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@   rE   )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   rK   r   rH   r   responsestatus_code)
r"   rF   rU   r#   r?   rP   rT   rR   estatusr)   r)   r*   	jobs_logsG  s,   



r_   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)r`   ra   keyop_strpatternr,   matchr)   r)   r*   _matches_filters{  s   
rj   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}})rl   N)	enumeratereplacestrrH   	_tabulate)
rk   rl   rm   rn   templaterowlineifieldplaceholderr)   r)   r*   _print_output  s   
	ry   nc                 C   s&   d}d}t | D ]}t||d qd S )Nz[1Az[2K)end)rangerH   )rz   ZLINE_UPZ
LINE_CLEARrv   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metricsrt   rk   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@   rM   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNZRUNNINGZUPDATING)r^   stagerI   r.   rQ   r)   r)   r*   r0     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)	rI   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*   r0     s   $ r   c                       g | ]} | D ]}|qqS r)   r)   r.   r"   rt   rows_per_job_idr)   r*   r0         c                    s"   g | ]}| j |d dqS )r"   r#   )r"   r~   r   )Zfetch_job_metricsr.   r"   )rP   r#   r   r)   r*   r0     s    )kwargs_listr   c                    r   r)   r)   r   r   r)   r*   r0     r   rW   rX   rY   rZ   zFailed to fetch job stats: )r+   appendr   Zwhoami	list_jobsr   rH   multiprocessingpool
ThreadPooltimery   iflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr}   r   r[   r\   r   )r   r#   r?   
parsed_idsr"   headers_aliasesr   rt   Zlast_update_timeZ
total_rowsr   donerk   nowr]   r^   r)   )rP   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}}')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.r@   r   zlabel!=zlabel=Nrc   z6Warning: Ignoring invalid label filter format 'label!=z'. Use label!=key format.rb   *r   !)Warning: Ignoring invalid filter format ''. Use key=value format.r   r   r    )rI   r4   r^   r5   json matching filters: , c                 S   "   g | ]\}}}| | | qS r)   r)   r.   kovr)   r)   r*   r0   j     " zjobs_ps.<locals>.<listcomp>r   zNo jobs found[])r   IMAGE/SPACECOMMANDCREATEDZSTATUS)rI   r4   r5   createdr^   c                 S      g | ]}t |qS r)   r   r   r)   r)   r*   r0   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 )Nr^   r5   r   r   rI   r   docker_imageZ
created_at   Tr   r   )rd   joinrq   r   rp   )r/   r^   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*   r0     r   rI   itemsr   r   Zid_keyrl   r   )r   r   
startswithr   rH   r(   endswithr   re   r^   r   r   r5   r   rI   rj   rA   r   rq   r   listry   r   r   r   r    )r   r#   r?   r   r   r   rP   jobsra   Zlabels_filtersfZ
label_partZ	label_keyopZlabel_valuerf   r,   filtered_jobsrQ   r^   image_or_spacer   r   propsfilters_msgrl   rm   r   output_formatr)   r   r*   jobs_ps  s   








$
 
r   Z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 NAMEZCPUZRAMZACCELERATORzCOST/MINz	COST/HOUR)rM   Z
prettyNamecpuramacceleratorZcostMinZcostHourr   zx z ()$z.4ffree<   z.2fzNo hardware options foundN)r   Zlist_jobs_hardwarer   ZquantitymodelZvramZunit_cost_usdr   rM   pretty_namer   r   rH   ry   )	rP   Zhardware_listr   r   rk   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   rP   r#   r)   r*   r0     s    z jobs_inspect.<locals>.<listcomp>c                 S   r   r)   r   r   r)   r)   r*   r0     r      indentdefaultNrW   rX   rY   rZ   zFailed to inspect job: )r+   r   r   rH   r   dumpsrq   r   r[   r\   r   )r   r#   r?   r   r"   r   r]   r^   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   NrW   rX   rY   z>Access denied. You may not have permission to cancel this job.zFailed to cancel job: )r+   r   Z
cancel_jobr   r[   r\   r   )r"   r#   r?   rP   r]   r^   r)   r)   r*   jobs_cancel  s   


r   zFRun UV scripts (Python with inline dependencies) on HF infrastructure.Zuv)rM   )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 infrastructurer@   )r   r   dependenciesr   r4   r6   r7   rA   rB   r<   r=   r#   rC   rD   NTrE   )r   r   Z
run_uv_jobrG   r   rH   rI   rJ   rK   rL   rM   )r   r   r4   r<   r6   r7   r8   r9   r:   r;   r=   r>   r#   r?   r   r   rN   rO   rP   rQ   rR   r)   r)   r*   jobs_uv_run  s0   



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\   t ||	}t ||
}t|d}|j||| ||||t|t||||d}td|j  dS )zSchedule a Job.r@   )r4   r5   r   r  r  r6   r7   rA   rB   r<   r=   r#   Scheduled Job created with ID: N)r   r   Zcreate_scheduled_jobrG   r   rH   rI   )r   r4   r5   r  r  r6   r7   r8   r9   r:   r;   r<   r=   r#   r?   rN   rO   rP   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 Jobsr@   r   rc   r   r   rb   Nr   r   r   Fr   r   )rI   r4   r  r5   r   r   r   c                 S   r   r)   r)   r   r)   r)   r*   r0     r   z scheduled_ps.<locals>.<listcomp>r   zNo scheduled jobs foundr   )IDZSCHEDULEr   r   zLAST RUNzNEXT RUNZSUSPEND)rI   r   r4   r5   lastnextr  c                 S   r   r)   r   )r.   Zsjr)   r)   r*   r0     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_specr^   last_jobr5   r   atr   r   r   Znext_job_run_atrI   r   r   r   r  F)rd   rp   r   rq   r   )r/   r	  r^   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*   r0     r   rI   r   )r   Zlist_scheduled_jobsr(   r   r   re   rH   r  r	  r   r5   r   rI   rq   rj   r   r   r   ry   r   r   r   r    )r   r#   r?   r   r   r   rP   scheduled_jobsra   r   rf   r,   r   r   r  r  r   r   r   r   r   rl   rm   r   r   r)   r   r*   scheduled_psG  s^   



" 
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*   r0     s    z%scheduled_inspect.<locals>.<listcomp>c                 S   r   r)   r   )r.   r  r)   r)   r*   r0     r   r   r   N)r+   r   r   rH   r   r   rq   )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?   rP   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   sf   t ||}t ||}t|d}|j||pg | |||||||t|	t|
|||d}td|j  dS )r   r@   )r   r   r   r  r  r   r   r4   r6   r7   rA   rB   r<   r=   r#   r  N)r   r   Zcreate_scheduled_uv_jobrG   r   rH   rI   )r   r   r   r  r  r4   r<   r6   r7   r8   r9   r:   r;   r=   r#   r?   r   r   rN   rO   rP   rQ   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.
    Nrc   r   r   )r(   )rA   Z
labels_mapZ	label_varrf   r,   r)   r)   r*   rG   *  s   	 
rG   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   rq   )r.   xr)   r)   r*   	<genexpr>C  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)r.   colr)   r)   r*   r0   C  r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r)   )r.   wr)   r)   r*   r0   M  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*   r0   O  s    .
)r   r  shutilget_terminal_sizecolumnsr   sumindexr   r   r   )	rk   rl   Z
col_widthsterminal_widthZcol_to_minimizeZ
row_formatlinesrt   Zrow_format_argsr)   r)   r*   rr   <  s$    
rr   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   r2  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_asyncr2  )r.   r/  r.  r   r-  r)   r*   r0   o  r   z&iflatmap_unordered.<locals>.<listcomp>T皙?r=   c                 s   s    | ]}|  V  qd S r  )readyr.   Zasync_resultr)   r)   r*   r  u  s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )r4  r5  )rd   r7  r)   r)   r*   r0   |  s    N)r	   rd   r   r   emptyKeyboardInterruptr   TimeoutError)r   r.  r   Zasync_resultsr)   r3  r*   r   _  s8   r   )NNNNNNNNFNN)FNNN)NNN)FNNNNF)r$   N)NN)NNNNNNNNNNFNNNN)NNNNNNNNNNNN)NNNNNNNNNNNNNNNN)o__doc__r   r   Zmultiprocessing.poolr&  r   collectionsr   collections.abcr   r   dataclassesr   r   r-  r   r	   typingr
   r   r   ZtyperZhuggingface_hubr   Zhuggingface_hub.errorsr   r   Zhuggingface_hub.utilsr   Z$huggingface_hub.utils._cache_managerr   Z
_cli_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   Z
get_logger__name__loggerrq   tupler+   ZSUGGESTED_FLAVORSr   ZArgumentZImageArgOptionZImageOptZ	FlavorOptr   Z	LabelsOptZ
TimeoutOptboolZ	DetachOptZNamespaceOptZWithOptZ	PythonOptZ
SuspendOptZConcurrencyOptZScheduleArgZ	ScriptArgZScriptArgsArgZ
CommandArgZJobIdArgZ	JobIdsArgZScheduledJobIdArgZjobs_clir5   rS   intr_   r   rj   ry   r}   r   r   r   r   r   r   Zuv_appZ	add_typerr   Zscheduled_appr  r  r  r  r  r  Zscheduled_uv_appr  rG   rr   r   r2  r   r   r   r)   r)   r)   r*   <module>   s  2D
&
		
	


	
*
,16
V		


z	
	

/		
'		


i	
	
&0&*