Skip to content

Commit 245a572

Browse files
author
Bihan Jiang
committed
add from json function
1 parent 01f1e93 commit 245a572

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

nucleus/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
ITEM_METADATA_SCHEMA_KEY,
9696
ITEMS_KEY,
9797
KEEP_HISTORY_KEY,
98+
MESSAGE_KEY,
9899
MODEL_RUN_ID_KEY,
99100
NAME_KEY,
100101
NUCLEUS_ENDPOINT,
@@ -216,7 +217,7 @@ def list_jobs(
216217
return [
217218
AsyncJob(
218219
job_id=job[JOB_ID_KEY],
219-
job_status=job[JOB_STATUS_KEY],
220+
job_last_known_status=job[JOB_STATUS_KEY],
220221
job_type=job[JOB_TYPE_KEY],
221222
job_creation_time=job[JOB_CREATION_TIME_KEY],
222223
client=self,
@@ -1162,7 +1163,7 @@ def create_custom_index(
11621163
embeddings_urls: list of urls, each of which being a json mapping dataset_item_id -> embedding vector
11631164
embedding_dim: the dimension of the embedding vectors, must be consistent for all embedding vectors in the index.
11641165
"""
1165-
return self.make_request(
1166+
response_objects = self.make_request(
11661167
{
11671168
EMBEDDINGS_URL_KEY: embeddings_urls,
11681169
EMBEDDING_DIMENSION_KEY: embedding_dim,
@@ -1171,6 +1172,18 @@ def create_custom_index(
11711172
requests_command=requests.post,
11721173
)
11731174

1175+
job = AsyncJob(
1176+
job_id=response_objects[JOB_ID_KEY],
1177+
job_last_known_status=response_objects[JOB_STATUS_KEY],
1178+
job_type=response_objects[JOB_TYPE_KEY],
1179+
job_creation_time=response_objects[JOB_CREATION_TIME_KEY],
1180+
client=self,
1181+
)
1182+
dataset_id = response_objects[DATASET_ID_KEY]
1183+
message = response_objects[MESSAGE_KEY]
1184+
1185+
return dataset_id, job, message
1186+
11741187
def check_index_status(self, job_id: str):
11751188
return self.make_request(
11761189
{},

nucleus/dataset.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
DATASET_SLICES_KEY,
2424
DEFAULT_ANNOTATION_UPDATE_MODE,
2525
EXPORTED_ROWS,
26-
JOB_ID_KEY,
27-
JOB_STATUS_KEY,
28-
JOB_TYPE_KEY,
29-
JOB_CREATION_TIME_KEY,
3026
NAME_KEY,
3127
REFERENCE_IDS_KEY,
3228
REQUEST_ID_KEY,
@@ -184,14 +180,7 @@ def annotate(
184180
payload={REQUEST_ID_KEY: request_id, UPDATE_KEY: update},
185181
route=f"dataset/{self.id}/annotate?async=1",
186182
)
187-
188-
return AsyncJob(
189-
response[JOB_ID_KEY],
190-
response[JOB_STATUS_KEY],
191-
response[JOB_TYPE_KEY],
192-
response[JOB_CREATION_TIME_KEY],
193-
self._client,
194-
)
183+
return AsyncJob.from_json(response, self._client)
195184

196185
return self._client.annotate_dataset(
197186
self.id, annotations, update=update, batch_size=batch_size
@@ -250,13 +239,7 @@ def append(
250239
payload={REQUEST_ID_KEY: request_id, UPDATE_KEY: update},
251240
route=f"dataset/{self.id}/append?async=1",
252241
)
253-
return AsyncJob(
254-
response[JOB_ID_KEY],
255-
response[JOB_STATUS_KEY],
256-
response[JOB_TYPE_KEY],
257-
response[JOB_CREATION_TIME_KEY],
258-
self._client,
259-
)
242+
return AsyncJob.from_json(response, self._client)
260243

261244
return self._client.populate_dataset(
262245
self.id,

nucleus/job.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from dataclasses import dataclass
22
import time
33
from typing import Dict, List
4-
54
import requests
5+
from nucleus.constants import (
6+
JOB_CREATION_TIME_KEY,
7+
JOB_ID_KEY,
8+
JOB_STATUS_KEY,
9+
JOB_TYPE_KEY,
10+
)
611

712
JOB_POLLING_INTERVAL = 5
813

914

1015
@dataclass
1116
class AsyncJob:
1217
job_id: str
13-
job_status: str
18+
job_last_known_status: str
1419
job_type: str
1520
job_creation_time: str
1621
client: "NucleusClient" # type: ignore # noqa: F821
@@ -45,6 +50,16 @@ def sleep_until_complete(self, verbose_std_out=True):
4550
if final_status["status"] == "Errored":
4651
raise JobError(final_status, self)
4752

53+
@classmethod
54+
def from_json(cls, payload: dict, client):
55+
return cls(
56+
job_id=payload[JOB_ID_KEY],
57+
job_last_known_status=payload[JOB_STATUS_KEY],
58+
job_type=payload[JOB_TYPE_KEY],
59+
job_creation_time=payload[JOB_CREATION_TIME_KEY],
60+
client=client,
61+
)
62+
4863

4964
class JobError(Exception):
5065
def __init__(self, job_status: Dict[str, str], job: AsyncJob):

nucleus/model_run.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
ANNOTATIONS_KEY,
99
BOX_TYPE,
1010
DEFAULT_ANNOTATION_UPDATE_MODE,
11-
JOB_ID_KEY,
12-
JOB_STATUS_KEY,
13-
JOB_TYPE_KEY,
14-
JOB_CREATION_TIME_KEY,
1511
POLYGON_TYPE,
1612
REQUEST_ID_KEY,
1713
SEGMENTATION_TYPE,
@@ -118,14 +114,7 @@ def predict(
118114
payload={REQUEST_ID_KEY: request_id, UPDATE_KEY: update},
119115
route=f"modelRun/{self.model_run_id}/predict?async=1",
120116
)
121-
122-
return AsyncJob(
123-
response[JOB_ID_KEY],
124-
response[JOB_STATUS_KEY],
125-
response[JOB_TYPE_KEY],
126-
response[JOB_CREATION_TIME_KEY],
127-
self._client,
128-
)
117+
return AsyncJob.from_json(response, self._client)
129118
else:
130119
return self._client.predict(self.model_run_id, annotations, update)
131120

nucleus/slice.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
from nucleus.utils import convert_export_payload, format_dataset_item_response
99
from nucleus.constants import (
1010
EXPORTED_ROWS,
11-
JOB_ID_KEY,
12-
JOB_STATUS_KEY,
13-
JOB_TYPE_KEY,
14-
JOB_CREATION_TIME_KEY,
1511
)
1612

1713

@@ -128,13 +124,7 @@ def send_to_labeling(self, project_id: str):
128124
response = self._client.make_request(
129125
{}, f"slice/{self.slice_id}/{project_id}/send_to_labeling"
130126
)
131-
return AsyncJob(
132-
response[JOB_ID_KEY],
133-
response[JOB_STATUS_KEY],
134-
response[JOB_TYPE_KEY],
135-
response[JOB_CREATION_TIME_KEY],
136-
self._client,
137-
)
127+
return AsyncJob.from_json(response, self._client)
138128

139129

140130
def check_annotations_are_in_slice(

0 commit comments

Comments
 (0)