Skip to content

Commit 5a9b34c

Browse files
committed
Add upsert_predictions_and_send_to_project method
1 parent 36c56b3 commit 5a9b34c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

labelbox/schema/model_run.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
from labelbox.orm.query import results_query_part
1313
from labelbox.orm.model import Field, Relationship, Entity
1414
from labelbox.orm.db_object import DbObject, experimental
15+
from labelbox.data.annotation_types import LabelList
1516

1617
if TYPE_CHECKING:
1718
from labelbox import MEAPredictionImport
1819

1920
logger = logging.getLogger(__name__)
2021

22+
DATAROWS_IMPORT_LIMIT = 25000
23+
2124

2225
class DataSplit(Enum):
2326
TRAINING = "TRAINING"
@@ -122,6 +125,34 @@ def _wait_until_done(self, status_fn, timeout_seconds=120, sleep_time=5):
122125
)
123126
time.sleep(sleep_time)
124127

128+
def upsert_predictions_and_send_to_project(
129+
self,
130+
name: str,
131+
predictions: LabelList,
132+
project_id: str,
133+
priority: Optional[int] = 5,
134+
) -> 'MEAPredictionImport': # type: ignore
135+
""" Upload predictions and creates a batch import to project.
136+
Args:
137+
name (str): name of the AnnotationImport job as well as the name of the batch import
138+
predictions (Iterable):
139+
iterable of annotation rows
140+
project_id (str): id of the project to import into
141+
priority (int): priority of the job
142+
Returns:
143+
(AnnotationImport, Project)
144+
"""
145+
data_rows_set = set(
146+
map(lambda x: x.data_row.id, predictions)[:DATAROWS_IMPORT_LIMIT])
147+
data_rows = list(data_rows_set)
148+
project = self.client.get_project(project_id)
149+
batch = project.create_batch(name, data_rows, priority)
150+
151+
predictions_for_data_rows = filter(
152+
lambda x: x.data_row.id in data_rows_set, predictions)
153+
154+
return (self.add_predictions(name, predictions_for_data_rows), batch)
155+
125156
def add_predictions(
126157
self,
127158
name: str,

0 commit comments

Comments
 (0)