|
12 | 12 |
|
13 | 13 | from labelbox import utils |
14 | 14 | from labelbox.exceptions import (InvalidQueryError, LabelboxError, |
15 | | - ProcessingWaitTimeout, ResourceConflict) |
| 15 | + ProcessingWaitTimeout, ResourceConflict, |
| 16 | + ResourceNotFoundError) |
16 | 17 | from labelbox.orm import query |
17 | 18 | from labelbox.orm.db_object import DbObject, Deletable, Updateable |
18 | 19 | from labelbox.orm.model import Entity, Field, Relationship |
19 | 20 | from labelbox.pagination import PaginatedCollection |
20 | 21 | from labelbox.schema.consensus_settings import ConsensusSettings |
21 | 22 | from labelbox.schema.data_row import DataRow |
| 23 | +from labelbox.schema.export_params import ModelRunExportParams |
22 | 24 | from labelbox.schema.media_type import MediaType |
23 | 25 | from labelbox.schema.queue_mode import QueueMode |
24 | 26 | from labelbox.schema.resource_tag import ResourceTag |
| 27 | +from labelbox.schema.task import Task |
| 28 | +from labelbox.schema.user import User |
25 | 29 |
|
26 | 30 | if TYPE_CHECKING: |
27 | 31 | from labelbox import BulkImportRequest |
@@ -283,10 +287,11 @@ def label_generator(self, timeout_seconds=600, **kwargs): |
283 | 287 | return LBV1Converter.deserialize_video(json_data, self.client) |
284 | 288 | return LBV1Converter.deserialize(json_data) |
285 | 289 |
|
286 | | - def export_labels(self, |
287 | | - download=False, |
288 | | - timeout_seconds=1800, |
289 | | - **kwargs) -> Optional[Union[str, List[Dict[Any, Any]]]]: |
| 290 | + def export_labels_v2( |
| 291 | + self, |
| 292 | + download=False, |
| 293 | + timeout_seconds=1800, |
| 294 | + **kwargs) -> Optional[Union[str, List[Dict[Any, Any]]]]: |
290 | 295 | """ Calls the server-side Label exporting that generates a JSON |
291 | 296 | payload, and returns the URL to that payload. |
292 | 297 |
|
@@ -371,6 +376,66 @@ def _validate_datetime(string_date: str) -> bool: |
371 | 376 | self.uid) |
372 | 377 | time.sleep(sleep_time) |
373 | 378 |
|
| 379 | + """ |
| 380 | + Creates a project run export task with the given params and returns the task. |
| 381 | + |
| 382 | + >>> export_task = export_labels_v2("my_export_task", filter={"media_attributes": True}) |
| 383 | + |
| 384 | + """ |
| 385 | + |
| 386 | + def export_labels_v2(self, task_name: str, |
| 387 | + params: Optional[ModelRunExportParams]) -> Task: |
| 388 | + _params = params or {} |
| 389 | + mutation_name = "exportDataRows" |
| 390 | + create_task_query_str = """mutation exportDataRowsPyApi($input: ExportDataRowsInput!){ |
| 391 | + %s(input: $input) {taskId} } |
| 392 | + """ % (mutation_name) |
| 393 | + params = { |
| 394 | + "input": { |
| 395 | + "taskName": task_name, |
| 396 | + "filters": { |
| 397 | + "modelRunIds": [], |
| 398 | + "projectIds": [self.uid] |
| 399 | + }, |
| 400 | + "params": { |
| 401 | + "includeAttachments": |
| 402 | + _params.get('include_attachments', False), |
| 403 | + "includeMediaAttributes": |
| 404 | + _params.get('include_media_attributes', False), |
| 405 | + "includeMetadata": |
| 406 | + _params.get('include_metadata_fields', False), |
| 407 | + "includeDataRowDetails": |
| 408 | + _params.get('include_data_row_details', False), |
| 409 | + "includeProjectDetails": |
| 410 | + _params.get('include_project_details', False), |
| 411 | + "includeLabels": |
| 412 | + _params.get('include_labels', False), |
| 413 | + "includePerformanceDetails": |
| 414 | + _params.get('include_performance_details', False), |
| 415 | + # Arguments locked based on exectuion context |
| 416 | + "includeModelRuns": |
| 417 | + False, |
| 418 | + }, |
| 419 | + } |
| 420 | + } |
| 421 | + res = self.client.execute( |
| 422 | + create_task_query_str, |
| 423 | + params, |
| 424 | + ) |
| 425 | + res = res[mutation_name] |
| 426 | + task_id = res["taskId"] |
| 427 | + user: User = self.client.get_user() |
| 428 | + tasks: List[Task] = list( |
| 429 | + user.created_tasks(where=Entity.Task.uid == task_id)) |
| 430 | + # Cache user in a private variable as the relationship can't be |
| 431 | + # resolved due to server-side limitations (see Task.created_by) |
| 432 | + # for more info. |
| 433 | + if len(tasks) != 1: |
| 434 | + raise ResourceNotFoundError(Entity.Task, task_id) |
| 435 | + task: Task = tasks[0] |
| 436 | + task._user = user |
| 437 | + return task |
| 438 | + |
374 | 439 | def export_issues(self, status=None) -> str: |
375 | 440 | """ Calls the server-side Issues exporting that |
376 | 441 | returns the URL to that payload. |
|
0 commit comments