o
    0iG                  (   @   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	 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 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  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/m0Z0 e 1e2Z3de4dee4 de5e4ee4 f fddZ6dd eD Z7dZ8ee4ej9ddf Z:eee4 ej;ddf Z<eee ej;ddf Z=eee>e4  ej;ddddf Z?eee4 ej;ddf Z@eeAej;dd d!df ZBeee4 ej;d"df ZCeee>e4  ej;d#d$df ZDeee4 ej;d%d&d'df ZEeeeA ej;d(df ZFeeeA ej;d)df ZGee4ej9d*df ZHee4ej9d+df ZIeee>e4  ej9d,df ZJeee>e4  ej;d-d.d/df ZKee>e4 ej9d0df ZLee4ej9d1df ZMeee>e4  ej9d2df ZNee4ej9d3df ZOe0d4dZPePjQd5d6d7ig d8d9									:		dd;e:d<eLd=e%d>e)d?e?d@eKdAe$dBe(dCe=dDe@dEeBdeCdFe*ddfdGdHZRePjQdIg dJdK	:			ddeMdLeeAej;dMdNdOdf dPeeeS ej;dQdRdSdf deCdFe*ddfdTdUZTdVeUe4e4f dWe>e5e4e4e4f  deAfdXdYZVdZe>e>ee4eSf   d[e>e4 d\e>e4 d]ee4 ddf
d^d_ZWd`eSddfdadbZXde4dceeUe4ef  dde>e4 dee5eAe4e>e>ee4eSf   f  fdedfZYePjQdgdhgdK			ddieNdeCdFe*ddfdjdkZZePjQdldmdngdK	:					:ddoeeAej;dpdqdrdf deCdFe*dseee>e4  ej;dMdtdudf dveee4 ej;dwdf dxe'ddfdydzZ[ePjQd{d|gdKdd}d~Z\ePjQddgdK		ddiee>e4 ej9ddf deCdFe*ddfddZ]ePjQddgdK		ddeMdeCdFe*ddfddZ^e0ddZ_ePj`e_dd e_jQd5d6d7ig dd9											:				ddeIdeJd;e<dCe=d=e%d>e)d?e?d@eKdAe$dBe(dDe@dEeBdeCdFe*deDdeEddf"ddZae0ddZbePj`ebdd ebjQd5d6d7idgd9												ddeHd;e:d<eLdeFdeGd=e%d>e)d?e?d@eKdAe$dBe(dCe=dDe@deCdFe*ddf ddZcebjQdldgdK	:					:ddoeeAej;dpdqddf deCdFe*dseee>e4  ej;dMdtdudf dveee4 ej;dwdf dxe'ddfddZdebjQddgdK		ddee>e4 ej9ddf deCdFe*ddfddZeebjQddgdK		ddeOdeCdFe*ddfddZfebjQddgdK		ddeOdeCdFe*ddfddZgebjQddgdK		ddeOdeCdFe*ddfddZhe0ddZiebj`eidd eijQd5d6d7iddgd9																ddeHdeIdeJdeFdeGd;e<dCe=d=e%d>e)d?e?d@eKdAe$dBe(dDe@deCdFe*deDdeEddf&ddZjdee>e4  dee>e  fddZkdee>e4  deeUe4e4f  fddZldZe>e>ee4eSf   d[e>e4 de4fddZmedZndeen dedeen f deUddfddZodejpjqdedeen f de>eU deen fddƄZrd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)EmptyQueue)	AnnotatedAnyCallableIterableOptionalTypeVarUnion)SpaceHardware	constants)Volume)CLIErrorHfHubHTTPError)logging)_format_size   )
EnvFileOptEnvOptOutputFormatQuietOptSecretsFileOpt
SecretsOptTokenOpt_format_cellapi_object_to_dict
get_hf_apiparse_env_map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+   R/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/cli/jobs.py_parse_namespace_from_job_idg   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-vz--volumeaI  Mount a volume. Format: hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. TYPE is one of: models, datasets, spaces, buckets. TYPE defaults to models if omitted. models, datasets and spaces are always mounted read-only. buckets are read+write by default.E.g. -v hf://gpt2:/data or -v hf://datasets/org/ds:/data or -v hf://buckets/org/b:/mnt:roz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.rA   )	r6   r7   r8   r9   labelsvolumesr>   r?   r%   Job started with ID: 	View at: NTr$   r%   follow)r!   r    Zrun_job_parse_labels_map_parse_volumesprintidurlfetch_job_logsownername)r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r%   rA   env_mapsecrets_mapapijoblogr+   r+   r,   jobs_run  s*   



rV   logs)zhf jobs logs <job_id>zhf jobs logs -f <job_id>zhf jobs logs --tail 20 <job_id>)r5   rH   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.rB   rG   )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    rN   r   rK   r   responsestatus_code)
r$   rH   rX   r%   rA   rS   rW   rU   estatusr+   r+   r,   	jobs_logsQ  s,   



rb   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)rc   rd   keyop_strpatternr.   matchr+   r+   r,   _matches_filters  s   
rm   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}})ro   N)	enumeratereplacestrrK   	_tabulate)
rn   ro   rp   rq   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)rangerK   )r}   ZLINE_UPZ
LINE_CLEARry   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metricsrw   rn   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 JobsNrB   rP   c                 S   s(   g | ]}|j r|j jnd dv r|jqS )UNKNOWNZRUNNINGZUPDATING)ra   stagerL   r0   rT   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)	rL   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+   )r0   headerr+   r+   r,   r2     s   $ r   c                       g | ]} | D ]}|qqS r+   r+   r0   r$   rw   rows_per_job_idr+   r,   r2          c                    s"   g | ]}| j |d dqS )r$   r%   )r$   r   r   )Zfetch_job_metricsr0   r$   )rS   r%   r   r+   r,   r2     s    )kwargs_listr   c                    r   r+   r+   r   r   r+   r,   r2     r   rZ   r[   r\   r]   zFailed to fetch job stats: )r-   appendr    Zwhoami	list_jobsr   rK   multiprocessingpool
ThreadPooltimer|   iflatmap_unorderedr   popSTATS_UPDATE_MIN_INTERVALr   r   r^   r_   r   )r   r%   rA   
parsed_idsr$   headers_aliasesr   rw   Zlast_update_timeZ
total_rowsr   donern   nowr`   ra   r+   )rS   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.rB   r   zlabel!=zlabel=Nrf   z6Warning: Ignoring invalid label filter format 'label!=z'. Use label!=key format.re   *r   !)Warning: Ignoring invalid filter format ''. Use key=value format.r   r   r    )rL   r6   ra   r7   json matching filters: , c                 S   "   g | ]\}}}| | | qS r+   r+   r0   kovr+   r+   r,   r2   v     " zjobs_ps.<locals>.<listcomp>r   zNo jobs found[])r   IMAGE/SPACECOMMANDCREATEDZSTATUS)rL   r6   r7   createdra   c                 S      g | ]}t |qS r+   r   r   r+   r+   r,   r2   ~      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 )Nra   r7   r   r   rL   r   docker_imageZ
created_at   Tr   r   )rg   joinrt   r   rs   )r1   ra   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,   r2     r   rL   itemsr   r   Zid_keyro   r   )r    r   
startswithr   rK   r*   endswithr   rh   ra   r   r   r7   r   rL   rm   rC   r   rt   r	   listr|   r   r   r   r"   )r   r%   rA   r   r   r   rS   jobsrd   Zlabels_filtersfZ
label_partZ	label_keyopZlabel_valueri   r.   filtered_jobsrT   ra   image_or_spacer   r   propsfilters_msgro   rp   r   output_formatr+   r   r,   jobs_ps   s   








$
 
r   Zhardwarezhf jobs hardwarec            	   
   C   s   t  } |  }g d}g d}g }|D ]H}d}|jr+|jj d|jj d|jj d}|jdur7d|jd	nd}|jdurGd|jd
 dnd}||j|j	pQd|j
|j|||g q|sdtd dS t|||d dS )z(List available hardware options for Jobs)NAMEzPRETTY NAMEZCPUZRAMZACCELERATORzCOST/MINz	COST/HOUR)rP   Z
prettyNamecpuramacceleratorZcostMinZcostHourr   zx z ()N$z.4f<   z.2fzNo hardware options found)r    Zlist_jobs_hardwarer   ZquantitymodelZvramZunit_cost_usdr   rP   pretty_namer   r   rK   r|   )	rS   Zhardware_listr   r   rn   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 JobsrB   c                       g | ]	} j |d qS )r   )Zinspect_jobr   rS   r%   r+   r,   r2     s    z jobs_inspect.<locals>.<listcomp>c                 S   r   r+   r   r   r+   r+   r,   r2     r      indentdefaultNrZ   r[   r\   r]   zFailed to inspect job: )r-   r   r    rK   r   dumpsrt   r   r^   r_   r   )r   r%   rA   r   r$   r   r`   ra   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 JobrB   r   NrZ   r[   r\   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%   rA   rS   r`   ra   r+   r+   r,   jobs_cancel  s   


r   zFRun UV scripts (Python with inline dependencies) on HF infrastructure.Zuv)rP   )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 infrastructurerB   )r   r   dependenciesr   r6   r8   r9   rC   rD   r>   r?   r%   rE   rF   NTrG   )r!   r    Z
run_uv_jobrI   rJ   rK   rL   rM   rN   rO   rP   )r   r   r6   r>   r8   r9   r:   r;   r<   r=   r?   r@   r%   rA   r   r   rQ   rR   rS   rT   rU   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.rB   )r6   r7   r  r  r  r8   r9   rC   rD   r>   r?   r%   Scheduled Job created with ID: N)r!   r    Zcreate_scheduled_jobrI   rJ   rK   rL   )r  r6   r7   r  r  r8   r9   r:   r;   r<   r=   r>   r?   r%   rA   rQ   rR   rS   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 JobsrB   r   rf   r   r   re   Nr   r   r   Fr   r   )rL   r6   r  r7   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   )IDZSCHEDULEr   r   zLAST RUNzNEXT RUNZSUSPEND)rL   r  r6   r7   lastnextr  c                 S   r   r+   r   )r0   Z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_specra   last_jobr7   r   atr   r   r   Znext_job_run_atrL   r   r  r   r  F)rg   rs   r   rt   r   )r1   r  ra   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,   r2     r   rL   r   )r    Zlist_scheduled_jobsr*   r   r   rh   rK   r  r  r   r7   r   rL   rt   rm   r   r	   r   r|   r   r   r   r"   )r   r%   rA   r   r   r   rS   scheduled_jobsrd   r   ri   r.   r   r   r  r  r   r   r   r   r   ro   rp   r   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                    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 JobsrB   c                    r   )scheduled_job_idr%   )Zinspect_scheduled_job)r0   r  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    rK   r   r   rt   )r  r%   rA   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.rB   r  N)r-   r    Zdelete_scheduled_jobr  r%   rA   rS   r+   r+   r,   scheduled_delete     
r  zhf jobs scheduled suspend <id>c                 C   r  )z Suspend (pause) a scheduled Job.rB   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.rB   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   rB   )r   r   r  r  r  r   r   r6   r8   r9   rC   rD   r>   r?   r%   r  N)r!   r    Zcreate_scheduled_uv_jobrI   rJ   rK   rL   )r  r   r   r  r  r6   r>   r8   r9   r:   r;   r<   r=   r?   r%   rA   r   r   rQ   rR   rS   rT   r+   r+   r,   scheduled_uv_run  s*   


r  rD   c              
   C   s  | sdS d}t jt jt jdd}g }| D ]}|}d}|dr(d}|dd }n|dr5d	}|dd }||sBtd
| d|t|d }|d}|dkr[td
| d|d| }||d d }	|d}
|
dkrzt j}|}d}n;|d|
 }||v r|| }||
d d }nt j}|}|	dd}t|dkr|d d |d  }|d }n|}d}|
t|||	||d q|S )a  Parse volume specs from CLI arguments.

    Format: hf://[TYPE/]SOURCE[/PATH]:/MOUNT_PATH[:ro|:rw]
    Where TYPE is one of: models, datasets, spaces, buckets (defaults to models if omitted).
    SOURCE is the repo/bucket identifier (e.g. 'username/my-model').
    PATH is an optional subfolder inside the repo/bucket.
    MOUNT_PATH starts with '/'.
    Optional ':ro' or ':rw' suffix for read-only or read-write.

    Examples:
        hf://gpt2:/data                          (model, implicit type)
        hf://my-org/my-model:/data                (model, implicit type)
        hf://models/my-org/my-model:/data         (model, explicit type)
        hf://datasets/my-org/my-dataset:/data:ro
        hf://buckets/my-org/my-bucket:/mnt
        hf://spaces/my-org/my-space:/app
        hf://datasets/org/ds/train:/data          (with path inside repo)
        hf://buckets/org/b/sub/dir:/mnt           (with path inside bucket)
    Nzhf://Zbucket)modelsZdatasetsspacesZbucketsz:roTz:rwFzInvalid volume format: 'ze'. Source must start with 'hf://'. Expected hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. E.g. hf://gpt2:/dataz:/r   zE'. Expected hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. E.g. hf://gpt2:/datar   r'   r      r   )typesource
mount_path	read_onlypath)r   ZREPO_TYPE_MODELZREPO_TYPE_DATASETZREPO_TYPE_SPACEr   r   r   r   findr*   r   r   )rD   Z	HF_PREFIXZHF_TYPES_MAPPINGresultZraw_specspecr$  Zcolon_slash_idxZsource_partr#  Z	slash_idxZvol_type_strr"  r%  Zfirst_segment	remainingpartsr+   r+   r,   rJ   6  sr   







	rJ   rC   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.
    Nrf   r   r   )r*   )rC   Z
labels_mapZ	label_varri   r.   r+   r+   r,   rI     s   	 
rI   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   rt   )r0   xr+   r+   r,   	<genexpr>  s    z'_tabulate.<locals>.<listcomp>.<genexpr>)max)r0   colr+   r+   r,   r2     r   z_tabulate.<locals>.<listcomp>   r   z{{:{}}} c                 S   s   g | ]}d | qS )-r+   )r0   wr+   r+   r,   r2     r   c                 S   s@   g | ]\}}t t||krt|d |d  d nt|qS )Nr   z...r,  )r0   r-  Z	col_widthr+   r+   r,   r2     s    .
)r   r/  shutilget_terminal_sizecolumnsr   sumindexr   r   r   )	rn   ro   Z
col_widthsterminal_widthZcol_to_minimizeZ
row_formatlinesrw   Zrow_format_argsr+   r+   r,   ru     s$    
ru   r   queuefunc.kwargsc                 C   s"   |di |D ]}|  | qd S )Nr+   )put)r<  r=  r>  r'  r+   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+   )Zapply_asyncr@  )r0   r>  r=  r   r<  r+   r,   r2     r   z&iflatmap_unordered.<locals>.<listcomp>T皙?r?   c                 s   s    | ]}|  V  qd S r+  )readyr0   Zasync_resultr+   r+   r,   r.    s    z%iflatmap_unordered.<locals>.<genexpr>c                 S   s   g | ]}|j d dqS )rB  rC  )rg   rE  r+   r+   r,   r2     s    N)r   rg   r   r   emptyKeyboardInterruptr   TimeoutError)r   r=  r   Zasync_resultsr+   rA  r,   r     s8   r   )NNNNNNNNFNN)FNNN)NNN)FNNNNF)r&   N)NN)NNNNNNNNNNFNNNN)NNNNNNNNNNNN)NNNNNNNNNNNNNNNN)s__doc__r   r   Zmultiprocessing.poolr5  r   collectionsr   dataclassesr   r   r<  r   r   typingr   r	   r
   r   r   r   r   ZtyperZhuggingface_hubr   r   Zhuggingface_hub._jobs_apir   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#   Z
get_logger__name__loggerrt   tupler-   ZSUGGESTED_FLAVORSr   ZArgumentZImageArgOptionZImageOptZ	FlavorOptr   Z	LabelsOptZ
TimeoutOptboolZ	DetachOptZNamespaceOptZWithOptZ	PythonOptZ
SuspendOptZConcurrencyOptZScheduleArgZ	ScriptArgZScriptArgsArgZ
VolumesOptZ
CommandArgZJobIdArgZ	JobIdsArgZScheduledJobIdArgZjobs_clir7   rV   intrb   r   rm   r|   r   r   r   r   r   r   r   Zuv_appZ	add_typerr  Zscheduled_appr  r  r  r  r  r  Zscheduled_uv_appr  rJ   rI   ru   r   r@  r   r   r   r+   r+   r+   r,   <module>   s  2$<
&
		
	



	
*
,1
 
V		


z	
	

/		
'		


i	
	
"0&e**