Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -1,4 +1,3 @@
# coding=utf-8
# Copyright 2025-present, the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@ -15,13 +14,56 @@
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any, Optional, Union
from typing import Any
from huggingface_hub import constants
from huggingface_hub._space_api import SpaceHardware
from huggingface_hub._space_api import Volume
from huggingface_hub.utils._datetime import parse_datetime
class JobHardware(str, Enum):
"""
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`.
"""
# CPU
CPU_BASIC = "cpu-basic"
CPU_UPGRADE = "cpu-upgrade"
CPU_PERFORMANCE = "cpu-performance"
CPU_XL = "cpu-xl"
# GPU
T4_SMALL = "t4-small"
T4_MEDIUM = "t4-medium"
L4X1 = "l4x1"
L4X4 = "l4x4"
L40SX1 = "l40sx1"
L40SX4 = "l40sx4"
L40SX8 = "l40sx8"
A10G_SMALL = "a10g-small"
A10G_LARGE = "a10g-large"
A10G_LARGEX2 = "a10g-largex2"
A10G_LARGEX4 = "a10g-largex4"
A100_LARGE = "a100-large"
A100X4 = "a100x4"
A100X8 = "a100x8"
H200 = "h200"
H200X2 = "h200x2"
H200X4 = "h200x4"
H200X8 = "h200x8"
RTX_PRO_6000 = "rtx-pro-6000"
RTX_PRO_6000X2 = "rtx-pro-6000x2"
RTX_PRO_6000X4 = "rtx-pro-6000x4"
RTX_PRO_6000X8 = "rtx-pro-6000x8"
class JobStage(str, Enum):
"""
Enumeration of possible stage of a Job on the Hub.
@ -30,7 +72,7 @@ class JobStage(str, Enum):
```py
assert JobStage.COMPLETED == "COMPLETED"
```
Possible values are: `COMPLETED`, `CANCELED`, `ERROR`, `DELETED`, `RUNNING`.
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).
"""
@ -39,13 +81,15 @@ class JobStage(str, Enum):
CANCELED = "CANCELED"
ERROR = "ERROR"
DELETED = "DELETED"
SCHEDULING = "SCHEDULING"
RUNNING = "RUNNING"
@dataclass
class JobStatus:
stage: JobStage
message: Optional[str]
message: str | None
expose_urls: list[str] | None
@dataclass
@ -55,6 +99,49 @@ class JobOwner:
type: str
@dataclass
class JobDurations:
"""
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.
"""
scheduling_secs: int | None
running_secs: int | None
total_secs: int | None
def __init__(self, **kwargs) -> None:
self.scheduling_secs = kwargs.get("schedulingSecs", kwargs.get("scheduling_secs"))
self.running_secs = kwargs.get("runningSecs", kwargs.get("running_secs"))
self.total_secs = kwargs.get("totalSecs", kwargs.get("total_secs"))
@dataclass
class JobInitiator:
"""
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.
"""
type: str
id: str
name: str | None = None
@dataclass
class JobInfo:
"""
@ -65,6 +152,10 @@ class JobInfo:
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.
@ -80,13 +171,25 @@ class JobInfo:
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.
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:
@ -97,7 +200,7 @@ class JobInfo:
... 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', 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')
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
@ -108,16 +211,22 @@ class JobInfo:
"""
id: str
created_at: Optional[datetime]
docker_image: Optional[str]
space_id: Optional[str]
command: Optional[list[str]]
arguments: Optional[list[str]]
environment: Optional[dict[str, Any]]
secrets: Optional[dict[str, Any]]
flavor: Optional[SpaceHardware]
created_at: datetime | None
started_at: datetime | None
finished_at: datetime | None
docker_image: str | None
space_id: str | None
command: list[str] | None
arguments: list[str] | None
environment: dict[str, Any] | None
secrets: dict[str, Any] | None
flavor: JobHardware | None
labels: dict[str, str] | None
volumes: list[Volume] | None
status: JobStatus
durations: JobDurations | None
owner: JobOwner
initiator: JobInitiator | None
# Inferred fields
endpoint: str
@ -127,6 +236,10 @@ class JobInfo:
self.id = kwargs["id"]
created_at = kwargs.get("createdAt") or kwargs.get("created_at")
self.created_at = parse_datetime(created_at) if created_at else None
started_at = kwargs.get("startedAt") or kwargs.get("started_at")
self.started_at = parse_datetime(started_at) if started_at else None
finished_at = kwargs.get("finishedAt") or kwargs.get("finished_at")
self.finished_at = parse_datetime(finished_at) if finished_at else None
self.docker_image = kwargs.get("dockerImage") or kwargs.get("docker_image")
self.space_id = kwargs.get("spaceId") or kwargs.get("space_id")
owner = kwargs.get("owner", {})
@ -136,8 +249,19 @@ class JobInfo:
self.environment = kwargs.get("environment")
self.secrets = kwargs.get("secrets")
self.flavor = kwargs.get("flavor")
self.labels = kwargs.get("labels")
volumes = kwargs.get("volumes")
self.volumes = [Volume(**v) for v in volumes] if volumes else None
status = kwargs.get("status", {})
self.status = JobStatus(stage=status["stage"], message=status.get("message"))
self.status = JobStatus(
stage=status["stage"], message=status.get("message"), expose_urls=status.get("exposeUrls")
)
durations = kwargs.get("durations")
self.durations = JobDurations(**durations) if durations else None
initiator = kwargs.get("initiator")
self.initiator = (
JobInitiator(type=initiator["type"], id=initiator["id"], name=initiator.get("name")) if initiator else None
)
# Inferred fields
self.endpoint = kwargs.get("endpoint", constants.ENDPOINT)
@ -146,16 +270,18 @@ class JobInfo:
@dataclass
class JobSpec:
docker_image: Optional[str]
space_id: Optional[str]
command: Optional[list[str]]
arguments: Optional[list[str]]
environment: Optional[dict[str, Any]]
secrets: Optional[dict[str, Any]]
flavor: Optional[SpaceHardware]
timeout: Optional[int]
tags: Optional[list[str]]
arch: Optional[str]
docker_image: str | None
space_id: str | None
command: list[str] | None
arguments: list[str] | None
environment: dict[str, Any] | None
secrets: dict[str, Any] | None
flavor: JobHardware | None
timeout: int | None
tags: list[str] | None
arch: str | None
labels: dict[str, str] | None
volumes: list[Volume] | None
def __init__(self, **kwargs) -> None:
self.docker_image = kwargs.get("dockerImage") or kwargs.get("docker_image")
@ -168,6 +294,9 @@ class JobSpec:
self.timeout = kwargs.get("timeout")
self.tags = kwargs.get("tags")
self.arch = kwargs.get("arch")
self.labels = kwargs.get("labels")
volumes = kwargs.get("volumes")
self.volumes = [Volume(**v) for v in volumes] if volumes else None
@dataclass
@ -182,8 +311,8 @@ class LastJobInfo:
@dataclass
class ScheduledJobStatus:
last_job: Optional[LastJobInfo]
next_job_run_at: Optional[datetime]
last_job: LastJobInfo | None
next_job_run_at: datetime | None
def __init__(self, **kwargs) -> None:
last_job = kwargs.get("lastJob") or kwargs.get("last_job")
@ -235,11 +364,11 @@ class ScheduledJobInfo:
"""
id: str
created_at: Optional[datetime]
created_at: datetime | None
job_spec: JobSpec
schedule: Optional[str]
suspend: Optional[bool]
concurrency: Optional[bool]
schedule: str | None
suspend: bool | None
concurrency: bool | None
status: ScheduledJobStatus
owner: JobOwner
@ -293,7 +422,7 @@ class JobAccelerator:
@dataclass
class JobHardware:
class JobHardwareInfo:
"""
Contains information about available Job hardware.
@ -306,6 +435,8 @@ class JobHardware:
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`):
@ -321,7 +452,7 @@ class JobHardware:
>>> 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')
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'
```
@ -331,7 +462,8 @@ class JobHardware:
pretty_name: str
cpu: str
ram: str
accelerator: Optional[JobAccelerator]
ephemeral_storage: str
accelerator: JobAccelerator | None
unit_cost_micro_usd: int
unit_cost_usd: float
unit_label: str
@ -341,6 +473,7 @@ class JobHardware:
self.pretty_name = kwargs["prettyName"]
self.cpu = kwargs["cpu"]
self.ram = kwargs["ram"]
self.ephemeral_storage = kwargs.get("ephemeralStorage", "N/A")
accelerator = kwargs.get("accelerator")
self.accelerator = JobAccelerator(**accelerator) if accelerator else None
self.unit_cost_micro_usd = kwargs["unitCostMicroUSD"]
@ -352,17 +485,20 @@ def _create_job_spec(
*,
image: str,
command: list[str],
env: Optional[dict[str, Any]],
secrets: Optional[dict[str, Any]],
flavor: Optional[SpaceHardware],
timeout: Optional[Union[int, float, str]],
env: dict[str, Any] | None,
secrets: dict[str, Any] | None,
flavor: JobHardware | str | None,
timeout: int | float | str | None,
labels: dict[str, str] | None = None,
volumes: list[Volume] | None = None,
expose: list[int] | None = None,
) -> dict[str, Any]:
# prepare job spec to send to HF Jobs API
job_spec: dict[str, Any] = {
"command": command,
"arguments": [],
"environment": env or {},
"flavor": flavor or SpaceHardware.CPU_BASIC,
"flavor": flavor or JobHardware.CPU_BASIC,
}
# secrets are optional
if secrets:
@ -374,6 +510,15 @@ def _create_job_spec(
job_spec["timeoutSeconds"] = int(float(timeout[:-1]) * time_units_factors[timeout[-1]])
else:
job_spec["timeoutSeconds"] = int(timeout)
# labels are optional
if labels:
job_spec["labels"] = labels
# volumes are optional
if volumes:
job_spec["volumes"] = [vol.to_dict() for vol in volumes]
# expose ports through the jobs proxy
if expose:
job_spec["expose"] = {"ports": expose}
# input is either from docker hub or from HF spaces
for prefix in (
"https://huggingface.co/spaces/",