o
    0iR@                     @   s  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	 d dl
mZ d dlmZ d dlmZ eG dd	 d	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dddd ed!ee d"eeeef  d#eeeef  d$ee d%ee	eeef  d&eeeef  d'eee  d(eeef fd)d*Z dS )+    )	dataclass)datetime)Enum)AnyLiteralOptionalUnion)	constants)SpaceHardware)parse_datetimec                   @   sh   e Zd ZU dZed ed< eed< eed< dZee ed< dZ	ee
 ed< dZee ed	< dddZdS )Volumea  
    Describes a volume to mount in a Job container.

    Args:
        type (`str`):
            Type of volume: `"bucket"`, `"model"`, `"dataset"`, or `"space"`.
        source (`str`):
            Source identifier, e.g. `"username/my-bucket"` or `"username/my-model"`.
        mount_path (`str`):
            Mount path inside the container, e.g. `"/data"`. Must start with `/`.
        revision (`str` or `None`):
            Git revision (only for repos, defaults to `"main"`).
        read_only (`bool` or `None`):
            Read-only mount. Forced `True` for repos, defaults to `False` for buckets.
        path (`str` or `None`):
            Subfolder prefix inside the bucket/repo to mount, e.g. `"path/to/dir"`.
    )ZbucketmodelZdatasetspacetypesource
mount_pathNrevision	read_onlypathreturnc                 K   sv   | dd| _|d | _| d}|d ur|n|d | _| d| _| d}|d ur-|n| d| _| d	| _d S )
Nr   r   r   	mountPathr   r   readOnlyr   r   )getr   r   r   r   r   r   )selfkwargsr   r    r   S/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/_jobs_api.py__init__4   s   


zVolume.__init__r   N)__name__
__module____qualname____doc__r   __annotations__strr   r   r   boolr   r   r   r   r   r   r      s   
 r   c                   @   s$   e Zd ZdZdZdZdZdZdZdS )JobStageah  
    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`, `RUNNING`.
    Taken from https://github.com/huggingface/moon-landing/blob/main/server/job_types/JobInfo.ts#L61 (private url).
    	COMPLETEDCANCELEDERRORDELETEDRUNNINGN)	r   r    r!   r"   r'   r(   r)   r*   r+   r   r   r   r   r&   ?   s    r&   c                   @   s"   e Zd ZU eed< ee ed< dS )	JobStatusstagemessageN)r   r    r!   r&   r#   r   r$   r   r   r   r   r,   S   s   
 r,   c                   @   s&   e Zd ZU eed< eed< eed< dS )JobOwneridnamer   N)r   r    r!   r$   r#   r   r   r   r   r/   Y   s   
 r/   c                   @   s   e Zd ZU dZeed< ee ed< ee ed< ee ed< eee  ed< eee  ed< ee	ee
f  ed< ee	ee
f  ed	< ee ed
< ee	eef  ed< eee  ed< eed< e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.
        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, as in Hugging Face Spaces. See [`SpaceHardware`] 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.
        owner: (`JobOwner` or `None`):
            Owner of the Job, e.g. `JobOwner(id="5e9ecfc04957053f60648a3e", name="lhoestq", type="user")`

    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), 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), owner=JobOwner(id='5e9ecfc04957053f60648a3e', name='lhoestq', type='user'), 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'
    ```
    r0   
created_atdocker_imagespace_idcommand	argumentsenvironmentsecretsflavorlabelsvolumesstatusownerendpointurlr   Nc                 K   sD  |d | _ |dp|d}|rt|nd | _|dp!|d| _|dp,|d| _|di }t|d |d	 |d
 d| _|d| _|d| _	|d| _
|d| _|d| _|d| _|d}|rtdd |D nd | _|di }t|d |dd| _|dtj| _| j d| jj d| j  | _d S )Nr0   	createdAtr3   dockerImager4   spaceIdr5   r>   r1   r   r0   r1   r   r6   r7   r8   r9   r:   r;   r<   c                 S      g | ]	}t d i |qS r   r   .0vr   r   r   
<listcomp>       z$JobInfo.__init__.<locals>.<listcomp>r=   r-   r.   )r-   r.   r?   z/jobs//)r0   r   r   r3   r4   r5   r/   r>   r6   r7   r8   r9   r:   r;   r<   r,   r=   r	   ZENDPOINTr?   r1   r@   )r   r   r3   r>   r<   r=   r   r   r   r      s&   

"zJobInfo.__init__r   )r   r    r!   r"   r$   r#   r   r   listdictr   r
   r   r,   r/   r   r   r   r   r   r2   `   s$   
 6r2   c                   @   s   e Zd ZU ee ed< ee ed< eee  ed< eee  ed< eeeef  ed< eeeef  ed< ee	 ed< ee
 ed< eee  ed	< ee ed
< eeeef  ed< eee  ed< dddZdS )JobSpecr4   r5   r6   r7   r8   r9   r:   timeouttagsarchr;   r<   r   Nc                 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 )NrB   r4   rC   r5   r6   r7   r8   r9   r:   rQ   rR   rS   r;   r<   c                 S   rE   rF   rG   rH   r   r   r   rK      rL   z$JobSpec.__init__.<locals>.<listcomp>)r   r4   r5   r6   r7   r8   r9   r:   rQ   rR   rS   r;   r<   )r   r   r<   r   r   r   r      s   
"zJobSpec.__init__r   )r   r    r!   r   r$   r#   rN   rO   r   r
   intr   r   r   r   r   r   rP      s   
 rP   c                   @   s(   e Zd ZU eed< eed< dddZdS )LastJobInfor0   atr   Nc                 K   s   |d | _ t|d | _d S )Nr0   rV   )r0   r   rV   r   r   r   r   r   r      s   
zLastJobInfo.__init__r   )r   r    r!   r$   r#   r   r   r   r   r   r   rU      s   
 rU   c                   @   s0   e Zd ZU ee ed< ee ed< dddZdS )ScheduledJobStatuslast_jobnext_job_run_atr   Nc                 K   s`   | dp	| d}|rtdi |nd | _| dp| d}|r+tt|| _d S d | _d S )NlastJobrY   nextJobRunAtrZ   r   )r   rU   rY   r   r$   rZ   )r   r   rY   rZ   r   r   r   r      s    zScheduledJobStatus.__init__r   )r   r    r!   r   rU   r#   r   r   r   r   r   r   rX      s   
 rX   c                   @   sl   e Zd ZU dZeed< ee ed< eed< ee ed< ee	 ed< ee	 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)
    ```
    r0   r3   job_specschedulesuspendconcurrencyr=   r>   r   Nc                 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 )Nr0   rA   r3   r^   ZjobSpecr_   r`   ra   r=   rY   r[   rZ   r\   )rY   rZ   r>   r1   r   rD   r   )r0   r   r   r3   rP   r^   r_   r`   ra   rX   r=   r/   r>   )r   r   r3   r=   r>   r   r   r   r   *  s   
" zScheduledJobInfo.__init__r   )r   r    r!   r"   r$   r#   r   r   rP   r%   rX   r/   r   r   r   r   r   r]      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"`.
    r   r   quantityvrammanufacturerr   Nc                 K   s6   |d | _ |d | _|d | _|d | _|d | _d S )Nr   r   rc   rd   re   )r   r   rc   rd   re   rW   r   r   r   r   S  s
   



zJobAccelerator.__init__r   )r   r    r!   r"   r$   r#   r   r   r   r   r   rb   ;  s   
 rb   c                   @   s`   e Zd ZU dZeed< eed< eed< eed< ee ed< eed< e	ed< eed	< dddZ
dS )JobHardwarea  
    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"`.
        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]
    JobHardware(name='cpu-basic', pretty_name='CPU Basic', cpu='2 vCPU', ram='16 GB', accelerator=None, unit_cost_micro_usd=167, unit_cost_usd=0.000167, unit_label='minute')
    >>> hardware_list[0].name
    'cpu-basic'
    ```
    r1   pretty_namecpuramacceleratorunit_cost_micro_usdunit_cost_usd
unit_labelr   Nc                 K   sl   |d | _ |d | _|d | _|d | _|d}|r"td	i |nd | _|d | _|d | _|d | _	d S )
Nr1   Z
prettyNamerh   ri   rj   ZunitCostMicroUSDZunitCostUSDZ	unitLabelr   )
r1   rg   rh   ri   r   rb   rj   rk   rl   rm   )r   r   rj   r   r   r   r     s   






zJobHardware.__init__r   )r   r    r!   r"   r$   r#   r   rb   rT   floatr   r   r   r   r   rf   [  s   
 !rf   N)r;   r<   imager6   envr9   r:   rQ   r;   r<   r   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< dD ]}
| |
rf| t|
d  |d<  |S qR| |d< |S )N)r6   r7   r8   r:   r9      <   i  iQ )smhdZtimeoutSecondsr;   c                 S   sf   g | ]/}|j |j|jd |jdurd|jini |jdur"d|jini |jdur.d|jini qS ))r   r   r   Nr   r   r   )r   r   r   r   r   r   )rI   Zvolr   r   r   rK     s    	z$_create_job_spec.<locals>.<listcomp>r<   )zhttps://huggingface.co/spaces/zhttps://hf.co/spaces/zhuggingface.co/spaces/zhf.co/spaces/rC   rB   )r
   Z	CPU_BASIC
isinstancer$   rT   rn   
startswithlen)ro   r6   rp   r9   r:   rQ   r;   r<   r^   Ztime_units_factorsprefixr   r   r   _create_job_spec  s4   &	

r|   )!dataclassesr   r   enumr   typingr   r   r   r   Zhuggingface_hubr	   Zhuggingface_hub._space_apir
   Zhuggingface_hub.utils._datetimer   r   r$   r&   r,   r/   r2   rP   rU   rX   r]   rb   rf   rN   rO   rT   rn   r|   r   r   r   r   <module>   sb   %a	C?
	

