Upload 9 files
Browse files- src/jobs.py +28 -3
src/jobs.py
CHANGED
|
@@ -41,15 +41,40 @@ def _launch_job(*, token: str, env: dict[str, str], bucket_source: str, flavor:
|
|
| 41 |
)
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def _job_result(job: Any, *, run_id: str, kind: str, bucket_source: str, extra: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
payload: dict[str, Any] = {
|
| 46 |
"run_id": run_id,
|
| 47 |
"kind": kind,
|
| 48 |
-
"job_id":
|
| 49 |
-
"job_url":
|
| 50 |
-
"status":
|
| 51 |
"bucket_source": bucket_source,
|
| 52 |
"bucket_uri": bucket_uri_from_source(bucket_source),
|
|
|
|
| 53 |
}
|
| 54 |
if extra:
|
| 55 |
payload.update(extra)
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
|
| 44 |
+
def _job_field(job: Any, name: str, default: Any = None) -> Any:
|
| 45 |
+
if isinstance(job, dict):
|
| 46 |
+
return job.get(name, default)
|
| 47 |
+
return getattr(job, name, default)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _job_status_stage(job: Any) -> Any:
|
| 51 |
+
status = _job_field(job, "status")
|
| 52 |
+
if isinstance(status, dict):
|
| 53 |
+
return status.get("stage") or status.get("status") or status.get("state")
|
| 54 |
+
return getattr(status, "stage", None) or getattr(status, "status", None) or getattr(status, "state", None)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _job_url_from_id(*, job_id: str | None, bucket_source: str) -> str:
|
| 58 |
+
if not job_id:
|
| 59 |
+
return ""
|
| 60 |
+
owner = str(bucket_source or "").split("/", 1)[0].strip()
|
| 61 |
+
return f"https://huggingface.co/jobs/{owner}/{job_id}" if owner else ""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
def _job_result(job: Any, *, run_id: str, kind: str, bucket_source: str, extra: dict[str, Any] | None = None) -> dict[str, Any]:
|
| 65 |
+
job_id = str(_job_field(job, "id") or _job_field(job, "job_id") or _job_field(job, "jobId") or "").strip()
|
| 66 |
+
job_url = str(_job_field(job, "url") or _job_field(job, "job_url") or "").strip()
|
| 67 |
+
if not job_url:
|
| 68 |
+
job_url = _job_url_from_id(job_id=job_id, bucket_source=bucket_source)
|
| 69 |
payload: dict[str, Any] = {
|
| 70 |
"run_id": run_id,
|
| 71 |
"kind": kind,
|
| 72 |
+
"job_id": job_id,
|
| 73 |
+
"job_url": job_url,
|
| 74 |
+
"status": _job_status_stage(job),
|
| 75 |
"bucket_source": bucket_source,
|
| 76 |
"bucket_uri": bucket_uri_from_source(bucket_source),
|
| 77 |
+
"created_by": str(bucket_source or "").split("/", 1)[0].strip(),
|
| 78 |
}
|
| 79 |
if extra:
|
| 80 |
payload.update(extra)
|