
    gjN                     x   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	ee          Z G d
 dee          Zej        ej        ej        ej        fZe G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d d                      Ze G d  d!                      Zd"d"d"d#d$d%ed&e e         d'e!eef         d"z  d(e!eef         d"z  d)eez  d"z  d*e"e#z  ez  d"z  d+e!eef         d"z  d,e e
         d"z  d-e e"         d"z  d.e$d/e!eef         fd0Z%d"S )1    )	dataclass)datetime)Enum)Any)	constantsVolume)parse_datetimec                   z    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     ]/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/huggingface_hub/_jobs_api.pyr   r      s        	 	 IK'OF HIDDFFFJJ!L!LJFFDFFF!L%N%N%NNNr7   r   c                   *    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   r;   r<   r=   r>   r?   r@   r6   r7   r8   r:   r:   C   s8        	 	 IHEGJGGGr7   r:   c                   V    e Zd ZU eed<   edz  ed<   ee         dz  ed<   edz  ed<   dS )	JobStatusstageNmessageexpose_urlsssh_url)r   r   r   r:   __annotations__strlistr6   r7   r8   rB   rB   \   sP         OOO4ZcT!!!!4Zr7   rB   c                   .    e Zd ZU eed<   eed<   eed<   dS )JobOwneridnametypeN)r   r   r   rH   rG   r6   r7   r8   rK   rK   d   s+         GGG
III
IIIIIr7   rK   c                   L    e Zd ZU dZedz  ed<   edz  ed<   edz  e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                    |                     d|                     d                    | _        |                     d|                     d                    | _        |                     d|                     d                    | _        d S )NschedulingSecsrQ   runningSecsrR   	totalSecsrS   )getrQ   rR   rS   selfkwargss     r8   __init__zJobDurations.__init__   sm    %zz*:FJJGX<Y<YZZ"JJ}fjj6P6PQQ **[&**\2J2JKKr7   rT   N)r   r   r   r   intrG   r]   r6   r7   r8   rP   rP   k   sl           4Z*d
L L L L L Lr7   rP   c                   <    e Zd ZU dZeed<   eed<   dZedz  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.
    rN   rL   NrM   )r   r   r   r   rH   rG   rM   r6   r7   r8   ra   ra      sD           IIIGGGD#*r7   ra   c                      e Zd ZU dZeed<   edz  ed<   edz  ed<   edz  ed<   edz  ed<   edz  ed<   ee         dz  ed	<   ee         dz  ed
<   eee	f         dz  ed<   eee	f         dz  ed<   e
dz  ed<   eeef         dz  ed<   ee         dz  ed<   eed<   edz  ed<   eed<   edz  ed<   eed<   ee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.
        ssh_url (`str` or `None`):
            SSH endpoint of the Job, e.g. `"ssh://687fb701029421ae5549d998@ssh.hf.jobs"`. Only present when the Job
            was started with `ssh=True`. Connecting requires write access to the Job's namespace and an SSH public
            key registered on the Hub (https://huggingface.co/settings/keys).

    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'
    ```
    rL   N
created_at
started_atfinished_atdocker_imagespace_idcommand	argumentsenvironmentsecretsflavorlabelsvolumesstatus	durationsowner	initiatorendpointurlrT   c                    |d         | _         |                    d          p|                    d          }|rt          |          nd | _        |                    d          p|                    d          }|rt          |          n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          }|rd |D             nd | _        |                    di           }t#          |d         |                    d          |                    d          |                    d                    | _        |                    d          }|rt'          d$i |nd | _        |                    d          }	|	r1t+          |	d         |	d         |	                    d                     nd | _        |                    d!t.          j                  | _        | j         d"| j	        j         d#| j          | _        d S )%NrL   	createdAtrd   	startedAtre   
finishedAtrf   dockerImagerg   spaceIdrh   rr   rM   rN   rL   rM   rN   ri   rj   rk   rl   rm   rn   ro   c                 &    g | ]}t          d i |S r6   r   .0vs     r8   
<listcomp>z$JobInfo.__init__.<locals>.<listcomp>  "    555555r7   rp   rC   rD   
exposeUrlssshUrl)rC   rD   rE   rF   rq   rs   )rN   rL   rM   rt   z/jobs//r6   )rL   rY   r
   rd   re   rf   rg   rh   rK   rr   ri   rj   rk   rl   rm   rn   ro   rB   rp   rP   rq   ra   rs   r   ENDPOINTrt   rM   ru   )
r[   r\   rd   re   rf   rr   ro   rp   rq   rs   s
             r8   r]   zJobInfo.__init__   s   ,ZZ,,H

<0H0H
8BL.444ZZ,,H

<0H0H
8BL.444jj..K&**]2K2K:EO>+6664"JJ}55SN9S9S

9--GJ1G1G

7B''t5=uV}UUU
zz),,K00!::m44zz),,jj**jj****Y''9@J55W5555dHb))/JJy))

<00JJx((	
 
 
 JJ{++	6?I22	222TJJ{++	dmwLi/IdO)--X^J_J_````sw 	
 

:y/ABBmFF4:?FFTWFFr7   r^   )r   r   r   r   rH   rG   r   rI   dictr   r   r	   rB   rP   rK   ra   r]   r6   r7   r8   rc   rc      s~        D DL 	GGG44D   *Dj#YCy4c3h$&&&&#s(^d""""$cNT!!!!&\D    d""""OOOd"""" MMM	HHH$G $G $G $G $G $Gr7   rc   c                   8   e Zd ZU edz  ed<   edz  ed<   ee         dz  ed<   ee         dz  ed<   eeef         dz  ed<   eeef         dz  ed<   edz  ed<   e	dz  ed	<   ee         dz  ed
<   edz  ed<   eeef         dz  ed<   ee
         dz  ed<   ddZdS )JobSpecNrg   rh   ri   rj   rk   rl   rm   timeouttagsarchrn   ro   rT   c                    |                     d          p|                     d          | _        |                     d          p|                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d	          | _        |                     d
          | _        |                     d          | _	        |                     d          | _
        |                     d          | _        |                     d          }|rd |D             nd | _        d S )Nrz   rg   r{   rh   ri   rj   rk   rl   rm   r   r   r   rn   ro   c                 &    g | ]}t          d i |S r~   r   r   s     r8   r   z$JobSpec.__init__.<locals>.<listcomp>7  r   r7   )rY   rg   rh   ri   rj   rk   rl   rm   r   r   r   rn   ro   )r[   r\   ro   s      r8   r]   zJobSpec.__init__*  s,   "JJ}55SN9S9S

9--GJ1G1Gzz),,K00!::m44zz),,jj**zz),,JJv&&	JJv&&	jj****Y''9@J55W5555dr7   r^   )r   r   r   rH   rG   rI   r   r   r   r_   r	   r]   r6   r7   r8   r   r     s         *Dj#YCy4c3h$&&&&#s(^d""""$4Z
s)d

*cNT!!!!&\D    K K K K K Kr7   r   c                   ,    e Zd ZU eed<   eed<   ddZdS )LastJobInforL   atrT   Nc                 T    |d         | _         t          |d                   | _        d S )NrL   r   )rL   r
   r   rZ   s     r8   r]   zLastJobInfo.__init__?  s$    , ..r7   r^   )r   r   r   rH   rG   r   r]   r6   r7   r8   r   r   :  s<         GGGLLL/ / / / / /r7   r   c                   8    e Zd ZU edz  ed<   edz  ed<   ddZdS )ScheduledJobStatusNlast_jobnext_job_run_atrT   c                 "   |                     d          p|                     d          }|rt          di |nd | _        |                     d          p|                     d          }|rt          t	          |                    nd | _        d S )NlastJobr   nextJobRunAtr   r6   )rY   r   r   r
   rH   r   )r[   r\   r   r   s       r8   r]   zScheduledJobStatus.__init__I  s    ::i((BFJJz,B,B3;E//h/// **^44U

CT8U8UGV`~c/.B.BCCC\`r7   r^   )r   r   r   r   rG   r   r]   r6   r7   r8   r   r   D  sQ         D    _$$$a a a a a ar7   r   c                       e Zd ZU dZeed<   edz  ed<   eed<   edz  ed<   edz  ed<   edz  ed<   e	ed	<   e
e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)
    ```
    rL   Nrd   job_specschedulesuspendconcurrencyrp   rr   rT   c                 *   |d         | _         |                    d          p|                    d          }|rt          |          nd | _        t	          di |                    d          p|                    di           | _        |                    d          | _        |                    d          | _        |                    d          | _        |                    d	i           }t          |                    d
          p|                    d          |                    d          p|                    d                    | _
        |                    di           }t          |d         |d         |d                   | _        d S )NrL   rw   rd   r   jobSpecr   r   r   rp   r   r   r   r   )r   r   rr   rM   rN   r|   r6   )rL   rY   r
   rd   r   r   r   r   r   r   rp   rK   rr   )r[   r\   rd   rp   rr   s        r8   r]   zScheduledJobInfo.__init__  s\   ,ZZ,,H

<0H0H
8BL.444XX6::j#9#9#VVZZ	SU=V=VXX

:..zz),,!::m44Hb))(ZZ
++Dvzz)/D/D"JJ'899WVZZ=W=W
 
 
 

7B''t5=uV}UUU


r7   r^   )r   r   r   r   rH   rG   r   r   boolr   rK   r]   r6   r7   r8   r   r   P  s         & &P 	GGG4DjD[OOOV V V V V Vr7   r   c                   N    e Zd ZU dZeed<   eed<   eed<   eed<   ee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"`.
    rN   modelquantityvrammanufacturerrT   Nc                     |d         | _         |d         | _        |d         | _        |d         | _        |d         | _        d S )NrN   r   r   r   r   )rN   r   r   r   r   rZ   s     r8   r]   zJobAccelerator.__init__  sB    6N	G_
z*6N	">2r7   r^   )r   r   r   r   rH   rG   r]   r6   r7   r8   r   r     sf            IIIJJJMMM
III3 3 3 3 3 3r7   r   c                   |    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   edz  ed<   eed	<   eed
<   ee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'
    ```
    rM   pretty_namecpuramephemeral_storageNacceleratorunit_cost_micro_usdunit_cost_usd
unit_labelrT   c                 F   |d         | _         |d         | _        |d         | _        |d         | _        |                    dd          | _        |                    d          }|rt          di |nd | _        |d         | _        |d	         | _	        |d
         | _
        d S )NrM   
prettyNamer   r   ephemeralStoragezN/Ar   unitCostMicroUSDunitCostUSD	unitLabelr6   )rM   r   r   r   rY   r   r   r   r   r   r   )r[   r\   r   s      r8   r]   zJobHardwareInfo.__init__  s    6N	!,/%=%=!',>!F!Fjj//<GQ>88K888T#)*<#= #M2 -r7   r^   )
r   r   r   r   rH   rG   r   r_   floatr]   r6   r7   r8   r   r     s         ! !F III	HHH	HHH$&&&&OOO
. 
. 
. 
. 
. 
.r7   r   NF)rn   ro   exposesshimageri   envrl   rm   r   rn   ro   r   r   rT   c        
            |g |pi |pt           j        d}
|r||
d<   |roddddd}t          |t                    rA|d         |v r7t	          t          |d d                   ||d                  z            |
d	<   nt	          |          |
d	<   |r||
d
<   |rd |D             |
d<   |rd|i|
d<   |	rddi|
d<   dD ]3}|                     |          r| t          |          d          |
d<    n4| |
d<   |
S )N)ri   rj   rk   rm   rl      <   i  iQ )smhdtimeoutSecondsrn   c                 6    g | ]}|                                 S r6   )to_dict)r   vols     r8   r   z$_create_job_spec.<locals>.<listcomp>  s     @@@s{{}}@@@r7   ro   portsr   enabledTr   )zhttps://huggingface.co/spaces/zhttps://hf.co/spaces/zhuggingface.co/spaces/zhf.co/spaces/r{   rz   )r   r   
isinstancerH   r_   r   
startswithlen)r   ri   r   rl   rm   r   rn   ro   r   r   r   time_units_factorsprefixs                r8   _create_job_specr     s}    yb1K1	   H  &% 6#$2DyIIgs## 	67I(I(I),U73B3<-@-@CUV]^`VaCb-b)c)cH%&&),WH%& $# A@@@@@ /%v.
 ,$d+ 
( 
( F## 	"'F"6HYE	 #(Or7   )&dataclassesr   r   enumr   typingr   huggingface_hubr   huggingface_hub._space_apir	   huggingface_hub.utils._datetimer
   rH   r   r:   r;   r<   r=   r>   TERMINAL_JOB_STAGESrB   rK   rP   ra   rc   r   r   r   r   r   r   rI   r   r_   r   r   r   r6   r7   r8   <module>r      sp   " ! ! ! ! !                   % % % % % % - - - - - - : : : : : :(& (& (& (& (&#t (& (& (&V    sD   ,  )8+<hnhN^_                  L L L L L L L L4         AG AG AG AG AG AG AG AGH K K K K K K K K< / / / / / / / / a a a a a a a a @V @V @V @V @V @V @V @VF 3 3 3 3 3 3 3 3> 8. 8. 8. 8. 8. 8. 8. 8.F %)#'#6 6 66 #Y6 
c3h$		6
 #s(^d"6 #$6 5[3%6 cNT!6 &\D 6 I6 
6 
#s(^6 6 6 6 6 6r7   