o
    ï’2jÓK  ã                   @   sÒ  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ G dd	„ d	eeƒZG d
d„ deeƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG dd„ dƒƒZeG d d!„ d!ƒƒZd"d"d"d#œd$ed%ee d&eeef d"B d'eeef d"B d(eeB d"B d)eeB eB d"B d*eeef d"B d+ee
 d"B d,ee d"B d-eeef fd.d/„Zd"S )0é    )Ú	dataclass)Údatetime)ÚEnum)ÚAny)Ú	constants©ÚVolume)Úparse_datetimec                   @   sx   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdS )ÚJobHardwarea  
    Enumeration of hardware flavors available to run Jobs on the Hub.

    Value can be compared to a string:
    ```py
    assert JobHardware.CPU_BASIC == "cpu-basic"
    ```

    Both enums are kept in sync with the Hub API by `utils/check_hardware_flavors.py`.
    z	cpu-basiczcpu-upgradezcpu-performancezcpu-xlzt4-smallz	t4-mediumÚl4x1Úl4x4Úl40sx1Úl40sx4Úl40sx8z
a10g-smallz
a10g-largeza10g-largex2za10g-largex4z
a100-largeÚa100x4Úa100x8Úh200Úh200x2Úh200x4Úh200x8zrtx-pro-6000zrtx-pro-6000x2zrtx-pro-6000x4zrtx-pro-6000x8N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	CPU_BASICÚCPU_UPGRADEÚCPU_PERFORMANCEÚCPU_XLÚT4_SMALLÚ	T4_MEDIUMÚL4X1ÚL4X4ÚL40SX1ÚL40SX4ÚL40SX8Ú
A10G_SMALLÚ
A10G_LARGEÚA10G_LARGEX2ÚA10G_LARGEX4Ú
A100_LARGEÚA100X4ÚA100X8ÚH200ÚH200X2ÚH200X4ÚH200X8ÚRTX_PRO_6000ÚRTX_PRO_6000X2ÚRTX_PRO_6000X4ÚRTX_PRO_6000X8© r4   r4   úT/var/www/html/whisper/venv/lib/python3.10/site-packages/huggingface_hub/_jobs_api.pyr
      s8    r
   c                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	ÚJobStageav  
    Enumeration of possible stage of a Job on the Hub.

    Value can be compared to a string:
    ```py
    assert JobStage.COMPLETED == "COMPLETED"
    ```
    Possible values are: `COMPLETED`, `CANCELED`, `ERROR`, `DELETED`, `SCHEDULING`, `RUNNING`.
    Taken from https://github.com/huggingface/moon-landing/blob/main/server/job_types/JobInfo.ts#L61 (private url).
    Ú	COMPLETEDÚCANCELEDÚERRORÚDELETEDÚ
SCHEDULINGÚRUNNINGN)
r   r   r   r   r7   r8   r9   r:   r;   r<   r4   r4   r4   r5   r6   C   s    r6   c                   @   s2   e Zd ZU eed< edB ed< ee dB ed< dS )Ú	JobStatusÚstageNÚmessageÚexpose_urls)r   r   r   r6   Ú__annotations__ÚstrÚlistr4   r4   r4   r5   r=   X   s   
 r=   c                   @   s&   e Zd ZU eed< eed< eed< dS )ÚJobOwnerÚidÚnameÚtypeN)r   r   r   rB   rA   r4   r4   r4   r5   rD   _   s   
 rD   c                   @   s@   e Zd ZU dZedB ed< edB ed< edB ed< d	dd„ZdS )
ÚJobDurationsau  
    Timing breakdown for a Job, computed server-side.

    Args:
        scheduling_secs (`int` or `None`):
            Seconds the job spent in the scheduling stage before starting to run.
            `None` if the job never reached the running stage.
        running_secs (`int` or `None`):
            Seconds the job has been or was running. Recomputed on each request
            while the job is in progress. `None` if the job never started running.
        total_secs (`int` or `None`):
            Total seconds elapsed since the job was created. Recomputed on each
            request while the job is in progress.
    NÚscheduling_secsÚrunning_secsÚ
total_secsÚreturnc                 K   s@   |  d|  d¡¡| _|  d|  d¡¡| _|  d|  d¡¡| _d S )NÚschedulingSecsrI   ÚrunningSecsrJ   Ú	totalSecsrK   )ÚgetrI   rJ   rK   ©ÚselfÚkwargsr4   r4   r5   Ú__init__{   s   zJobDurations.__init__©rL   N)r   r   r   r   ÚintrA   rT   r4   r4   r4   r5   rH   f   s   
 rH   c                   @   s2   e Zd ZU dZeed< eed< dZedB ed< dS )ÚJobInitiatoraJ  
    Contains information about what triggered a Job.

    Args:
        type (`str`): Initiator kind, for example `"user"`, `"org"`, `"scheduled-job"`, or `"duplicated-job"`.
        id (`str`): Identifier of the initiator.
        name (`str` or `None`): Human-readable name when available, usually for user/org initiators.
    rG   rE   NrF   )r   r   r   r   rB   rA   rF   r4   r4   r4   r5   rW      s
   
 	rW   c                   @   s  e Zd ZU dZeed< edB ed< edB ed< edB ed< edB ed< edB ed< ee dB ed	< ee dB ed
< eee	f dB ed< eee	f dB ed< e
dB ed< eeef dB ed< ee dB ed< eed< edB ed< eed< edB ed< eed< eed< ddd„ZdS )ÚJobInfoa‘  
    Contains information about a Job.

    Args:
        id (`str`):
            Job ID.
        created_at (`datetime` or `None`):
            When the Job was created.
        started_at (`datetime` or `None`):
            When the Job started running. None while the Job is still scheduling.
        finished_at (`datetime` or `None`):
            When the Job finished. None while the Job is still scheduling or running.
        docker_image (`str` or `None`):
            The Docker image from Docker Hub used for the Job.
            Can be None if space_id is present instead.
        space_id (`str` or `None`):
            The Docker image from Hugging Face Spaces used for the Job.
            Can be None if docker_image is present instead.
        command (`list[str]` or `None`):
            Command of the Job, e.g. `["python", "-c", "print('hello world')"]`
        arguments (`list[str]` or `None`):
            Arguments passed to the command
        environment (`dict[str]` or `None`):
            Environment variables of the Job as a dictionary.
        secrets (`dict[str]` or `None`):
            Secret environment variables of the Job (encrypted).
        flavor (`str` or `None`):
            Flavor for the hardware. See [`JobHardware`] for possible values.
            E.g. `"cpu-basic"`.
        labels (`dict[str, str]` or `None`):
            Labels to attach to the job (key-value pairs).
        volumes (`list[Volume]` or `None`):
            Volumes mounted in the job container (buckets, models, datasets, spaces).
        status: (`JobStatus` or `None`):
            Status of the Job, e.g. `JobStatus(stage="RUNNING", message=None)`
            See [`JobStage`] for possible stage values.
        durations (`JobDurations` or `None`):
            Timing breakdown of the Job. Present for all job states including SCHEDULING.
        owner: (`JobOwner` or `None`):
            Owner of the Job, e.g. `JobOwner(id="5e9ecfc04957053f60648a3e", name="lhoestq", type="user")`
        initiator (`JobInitiator` or `None`):
            What triggered the Job, e.g. `JobInitiator(type="scheduled-job", id="...")` for a cron-triggered run.
        expose_urls (`list[str]` or `None`):
            Public URLs through which the Job's exposed ports are reachable (one per port exposed via `expose=`),
            e.g. `["https://687fb701029421ae5549d998--8000.hf.jobs"]`. `None` when no port is exposed.
            Accessing a URL requires an HF token with read access to the Job's namespace.

    Example:

    ```python
    >>> from huggingface_hub import run_job
    >>> job = run_job(
    ...     image="python:3.12",
    ...     command=["python", "-c", "print('Hello from the cloud!')"]
    ... )
    >>> job
    JobInfo(id='687fb701029421ae5549d998', created_at=datetime.datetime(2025, 7, 22, 16, 6, 25, 79000, tzinfo=datetime.timezone.utc), started_at=datetime.datetime(2025, 7, 22, 16, 6, 31, 79000, tzinfo=datetime.timezone.utc), finished_at=None, docker_image='python:3.12', space_id=None, command=['python', '-c', "print('Hello from the cloud!')"], arguments=[], environment={}, secrets={}, flavor='cpu-basic', labels=None, status=JobStatus(stage='RUNNING', message=None), durations=JobDurations(scheduling_secs=6, running_secs=2, total_secs=8), owner=JobOwner(id='5e9ecfc04957053f60648a3e', name='lhoestq', type='user'), initiator=JobInitiator(type='user', id='5e9ecfc04957053f60648a3e', name='lhoestq'), endpoint='https://huggingface.co', url='https://huggingface.co/jobs/lhoestq/687fb701029421ae5549d998')
    >>> job.id
    '687fb701029421ae5549d998'
    >>> job.url
    'https://huggingface.co/jobs/lhoestq/687fb701029421ae5549d998'
    >>> job.status.stage
    'RUNNING'
    ```
    rE   NÚ
created_atÚ
started_atÚfinished_atÚdocker_imageÚspace_idÚcommandÚ	argumentsÚenvironmentÚsecretsÚflavorÚlabelsÚvolumesÚstatusÚ	durationsÚownerÚ	initiatorÚendpointÚurlrL   c           
      K   sê  |d | _ | d¡p| d¡}|rt|ƒnd | _| d¡p!| d¡}|r(t|ƒnd | _| d¡p4| d¡}|r;t|ƒnd | _| d¡pG| d	¡| _| d
¡pR| d¡| _| di ¡}t|d |d |d d| _	| d¡| _
| d¡| _| d¡| _| d¡| _| d¡| _| d¡| _| d¡}|ršdd„ |D ƒnd | _| di ¡}t|d | d¡| d¡d| _| d¡}|rÁtd$i |¤Žnd | _| d¡}	|	rÙt|	d |	d |	 d¡d nd | _| d!tj¡| _| j› d"| j	j› d#| j › | _d S )%NrE   Ú	createdAtrY   Ú	startedAtrZ   Ú
finishedAtr[   ÚdockerImager\   ÚspaceIdr]   rg   rF   rG   ©rE   rF   rG   r^   r_   r`   ra   rb   rc   rd   c                 S   ó   g | ]	}t d i |¤Ž‘qS ©r4   r   ©Ú.0Úvr4   r4   r5   Ú
<listcomp>þ   ó    z$JobInfo.__init__.<locals>.<listcomp>re   r>   r?   Ú
exposeUrls)r>   r?   r@   rf   rh   )rG   rE   rF   ri   z/jobs/ú/r4   )rE   rP   r	   rY   rZ   r[   r\   r]   rD   rg   r^   r_   r`   ra   rb   rc   rd   r=   re   rH   rf   rW   rh   r   ÚENDPOINTri   rF   rj   )
rR   rS   rY   rZ   r[   rg   rd   re   rf   rh   r4   r4   r5   rT   ë   s<   

ÿ

"ÿ"zJobInfo.__init__rU   )r   r   r   r   rB   rA   r   rC   Údictr   r
   r   r=   rH   rD   rW   rT   r4   r4   r4   r5   rX   ‘   s,   
 BrX   c                   @   sÐ   e Zd ZU edB ed< edB ed< ee dB ed< ee dB ed< eeef dB ed< eeef dB ed< edB ed< e	dB ed	< ee dB ed
< edB ed< eeef dB ed< ee
 dB ed< ddd„ZdS )ÚJobSpecNr\   r]   r^   r_   r`   ra   rb   ÚtimeoutÚtagsÚarchrc   rd   rL   c                 K   sÄ   |  d¡p	|  d¡| _|  d¡p|  d¡| _|  d¡| _|  d¡| _|  d¡| _|  d¡| _|  d	¡| _|  d
¡| _|  d¡| _	|  d¡| _
|  d¡| _|  d¡}|r]dd„ |D ƒ| _d S d | _d S )Nrn   r\   ro   r]   r^   r_   r`   ra   rb   r}   r~   r   rc   rd   c                 S   rq   rr   r   rs   r4   r4   r5   rv   +  rw   z$JobSpec.__init__.<locals>.<listcomp>)rP   r\   r]   r^   r_   r`   ra   rb   r}   r~   r   rc   rd   )rR   rS   rd   r4   r4   r5   rT     s   
"zJobSpec.__init__rU   )r   r   r   rB   rA   rC   r{   r   r
   rV   r   rT   r4   r4   r4   r5   r|     s   
 r|   c                   @   s(   e Zd ZU eed< eed< ddd„ZdS )ÚLastJobInforE   ÚatrL   Nc                 K   s   |d | _ t|d ƒ| _d S )NrE   r   )rE   r	   r   rQ   r4   r4   r5   rT   3  s   
zLastJobInfo.__init__rU   )r   r   r   rB   rA   r   rT   r4   r4   r4   r5   r€   .  s   
 r€   c                   @   s0   e Zd ZU edB ed< edB ed< ddd„ZdS )ÚScheduledJobStatusNÚlast_jobÚnext_job_run_atrL   c                 K   s`   |  d¡p	|  d¡}|rtdi |¤Žnd | _|  d¡p|  d¡}|r+tt|ƒƒ| _d S d | _d S )NÚlastJobrƒ   ÚnextJobRunAtr„   r4   )rP   r€   rƒ   r	   rB   r„   )rR   rS   rƒ   r„   r4   r4   r5   rT   =  s    zScheduledJobStatus.__init__rU   )r   r   r   r€   rA   r   rT   r4   r4   r4   r5   r‚   8  s   
 r‚   c                   @   sl   e Zd ZU dZeed< edB ed< eed< edB ed< edB ed< edB ed< e	ed	< e
ed
< ddd„ZdS )ÚScheduledJobInfoaÅ  
    Contains information about a Job.

    Args:
        id (`str`):
            Scheduled Job ID.
        created_at (`datetime` or `None`):
            When the scheduled Job was created.
        tags (`list[str]` or `None`):
            The tags of the scheduled Job.
        schedule (`str` or `None`):
            One of "@annually", "@yearly", "@monthly", "@weekly", "@daily", "@hourly", or a
            CRON schedule expression (e.g., '0 9 * * 1' for 9 AM every Monday).
        suspend (`bool` or `None`):
            Whether the scheduled job is suspended (paused).
        concurrency (`bool` or `None`):
            Whether multiple instances of this Job can run concurrently.
        status (`ScheduledJobStatus` or `None`):
            Status of the scheduled Job.
        owner: (`JobOwner` or `None`):
            Owner of the scheduled Job, e.g. `JobOwner(id="5e9ecfc04957053f60648a3e", name="lhoestq", type="user")`
        job_spec: (`JobSpec` or `None`):
            Specifications of the Job.

    Example:

    ```python
    >>> from huggingface_hub import run_job
    >>> scheduled_job = create_scheduled_job(
    ...     image="python:3.12",
    ...     command=["python", "-c", "print('Hello from the cloud!')"],
    ...     schedule="@hourly",
    ... )
    >>> scheduled_job.id
    '687fb701029421ae5549d999'
    >>> scheduled_job.status.next_job_run_at
    datetime.datetime(2025, 7, 22, 17, 6, 25, 79000, tzinfo=datetime.timezone.utc)
    ```
    rE   NrY   Újob_specÚscheduleÚsuspendÚconcurrencyre   rg   rL   c                 K   sÜ   |d | _ | d¡p| d¡}|rt|ƒnd | _tdi | d¡p%| di ¡¤Ž| _| d¡| _| d¡| _| d¡| _| d	i ¡}t	| d
¡pK| d¡| d¡pT| d¡d| _
| di ¡}t|d |d |d d| _d S )NrE   rk   rY   rˆ   ÚjobSpecr‰   rŠ   r‹   re   rƒ   r…   r„   r†   )rƒ   r„   rg   rF   rG   rp   r4   )rE   rP   r	   rY   r|   rˆ   r‰   rŠ   r‹   r‚   re   rD   rg   )rR   rS   rY   re   rg   r4   r4   r5   rT   w  s   
"þ zScheduledJobInfo.__init__rU   )r   r   r   r   rB   rA   r   r|   Úboolr‚   rD   rT   r4   r4   r4   r5   r‡   D  s   
 (r‡   c                   @   sD   e Zd ZU dZeed< eed< eed< eed< eed< dd	d
„ZdS )ÚJobAcceleratoraý  
    Contains information about a Job accelerator (GPU).

    Args:
        type (`str`):
            Type of accelerator, e.g. `"gpu"`.
        model (`str`):
            Model of accelerator, e.g. `"T4"`, `"A10G"`, `"A100"`, `"L4"`, `"L40S"`.
        quantity (`str`):
            Number of accelerators, e.g. `"1"`, `"2"`, `"4"`, `"8"`.
        vram (`str`):
            Total VRAM, e.g. `"16 GB"`, `"24 GB"`.
        manufacturer (`str`):
            Manufacturer of the accelerator, e.g. `"Nvidia"`.
    rG   ÚmodelÚquantityÚvramÚmanufacturerrL   Nc                 K   s6   |d | _ |d | _|d | _|d | _|d | _d S )NrG   r   r   r‘   r’   )rG   r   r   r‘   r’   rQ   r4   r4   r5   rT      s
   



zJobAccelerator.__init__rU   )r   r   r   r   rB   rA   rT   r4   r4   r4   r5   rŽ   ˆ  s   
 rŽ   c                   @   sh   e Zd ZU dZeed< eed< eed< eed< eed< edB ed< eed	< eed
< eed< ddd„Z	dS )ÚJobHardwareInfoa$  
    Contains information about available Job hardware.

    Args:
        name (`str`):
            Machine identifier, e.g. `"cpu-basic"`, `"a10g-large"`.
        pretty_name (`str`):
            Human-readable name, e.g. `"CPU Basic"`, `"Nvidia A10G - large"`.
        cpu (`str`):
            CPU specification, e.g. `"2 vCPU"`, `"12 vCPU"`.
        ram (`str`):
            RAM specification, e.g. `"16 GB"`, `"46 GB"`.
        ephemeral_storage (`str`):
            Ephemeral storage specification, e.g. `"20 GB"`, `"100 GB"`.
        accelerator (`JobAccelerator` or `None`):
            GPU/accelerator details if available.
        unit_cost_micro_usd (`int`):
            Cost in micro-dollars per unit, e.g. `167` (= $0.000167).
        unit_cost_usd (`float`):
            Cost in USD per unit, e.g. `0.000167`.
        unit_label (`str`):
            Cost unit period, e.g. `"minute"`.

    Example:

    ```python
    >>> from huggingface_hub import list_jobs_hardware
    >>> hardware_list = list_jobs_hardware()
    >>> hardware_list[0]
    JobHardwareInfo(name='cpu-basic', pretty_name='CPU Basic', cpu='2 vCPU', ram='16 GB', ephemeral_storage='20 GB', accelerator=None, unit_cost_micro_usd=167, unit_cost_usd=0.000167, unit_label='minute')
    >>> hardware_list[0].name
    'cpu-basic'
    ```
    rF   Úpretty_nameÚcpuÚramÚephemeral_storageNÚacceleratorÚunit_cost_micro_usdÚunit_cost_usdÚ
unit_labelrL   c                 K   sz   |d | _ |d | _|d | _|d | _| dd¡| _| d¡}|r)tdi |¤Žnd | _|d | _|d	 | _	|d
 | _
d S )NrF   Ú
prettyNamer•   r–   ÚephemeralStoragezN/Ar˜   ÚunitCostMicroUSDÚunitCostUSDÚ	unitLabelr4   )rF   r”   r•   r–   rP   r—   rŽ   r˜   r™   rš   r›   )rR   rS   r˜   r4   r4   r5   rT   ×  s   






zJobHardwareInfo.__init__rU   )
r   r   r   r   rB   rA   rŽ   rV   ÚfloatrT   r4   r4   r4   r5   r“   ¨  s   
 #r“   N)rc   rd   ÚexposeÚimager^   Úenvra   rb   r}   rc   rd   r¢   rL   c        	         C   sê   |g |pi |p	t jdœ}	|r||	d< |r?dddddœ}
t|tƒr9|d |
v r9tt|d d… ƒ|
|d   ƒ|	d	< nt|ƒ|	d	< |rE||	d
< |rPdd„ |D ƒ|	d< |rXd|i|	d< dD ]}|  |¡rn| t|ƒd … |	d<  |	S qZ| |	d< |	S )N)r^   r_   r`   rb   ra   é   é<   i  i€Q )ÚsÚmÚhÚdéÿÿÿÿÚtimeoutSecondsrc   c                 S   s   g | ]}|  ¡ ‘qS r4   )Úto_dict)rt   Úvolr4   r4   r5   rv     s    z$_create_job_spec.<locals>.<listcomp>rd   Úportsr¢   )zhttps://huggingface.co/spaces/zhttps://hf.co/spaces/zhuggingface.co/spaces/zhf.co/spaces/ro   rn   )r
   r   Ú
isinstancerB   rV   r¡   Ú
startswithÚlen)r£   r^   r¤   ra   rb   r}   rc   rd   r¢   rˆ   Útime_units_factorsÚprefixr4   r4   r5   Ú_create_job_specä  s4   ü&
ûrµ   ) Údataclassesr   r   Úenumr   Útypingr   Úhuggingface_hubr   Úhuggingface_hub._space_apir   Úhuggingface_hub.utils._datetimer	   rB   r
   r6   r=   rD   rH   rW   rX   r|   r€   r‚   r‡   rŽ   r“   rC   r{   rV   r¡   rµ   r4   r4   r4   r5   Ú<module>   sn   +}	CCöþýüû
úùø
	÷

ö
õ