|
| 1 | +import json |
| 2 | +from typing import TYPE_CHECKING, Callable, List, Optional, Dict, Any |
| 3 | + |
| 4 | +from labelbox.orm.model import Entity |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from labelbox import User |
| 8 | + |
| 9 | + def lru_cache() -> Callable[..., Callable[..., Dict[str, Any]]]: |
| 10 | + pass |
| 11 | +else: |
| 12 | + from functools import lru_cache |
| 13 | + |
| 14 | + |
| 15 | +class CreateBatchesTask: |
| 16 | + |
| 17 | + def __init__(self, client, project_id: str, batch_ids: List[str], |
| 18 | + task_ids: List[str]): |
| 19 | + self.client = client |
| 20 | + self.project_id = project_id |
| 21 | + self.batches = batch_ids |
| 22 | + self.tasks = [ |
| 23 | + Entity.Task.get_task(self.client, task_id) for task_id in task_ids |
| 24 | + ] |
| 25 | + |
| 26 | + def wait_till_done(self, timeout_seconds: int = 300) -> None: |
| 27 | + """ |
| 28 | + Waits for the task to complete. |
| 29 | +
|
| 30 | + Args: |
| 31 | + timeout_seconds: the number of seconds to wait before timing out |
| 32 | +
|
| 33 | + Returns: None |
| 34 | + """ |
| 35 | + |
| 36 | + for task in self.tasks: |
| 37 | + task.wait_till_done(timeout_seconds) |
| 38 | + |
| 39 | + def errors(self) -> Optional[Dict[str, Any]]: |
| 40 | + """ |
| 41 | + Returns the errors from the task, if any. |
| 42 | +
|
| 43 | + Returns: a dictionary of errors, keyed by task id |
| 44 | + """ |
| 45 | + |
| 46 | + errors = {} |
| 47 | + for task in self.tasks: |
| 48 | + if task.status == "FAILED": |
| 49 | + errors[task.uid] = json.loads(task.result_url) |
| 50 | + |
| 51 | + if len(errors) == 0: |
| 52 | + return None |
| 53 | + |
| 54 | + return errors |
| 55 | + |
| 56 | + @lru_cache() |
| 57 | + def result(self): |
| 58 | + """ |
| 59 | + Returns the batches created by the task. |
| 60 | +
|
| 61 | + Returns: the list of batches created by the task |
| 62 | + """ |
| 63 | + |
| 64 | + return [ |
| 65 | + self.client.get_batch(self.project_id, batch_id) |
| 66 | + for batch_id in self.batches |
| 67 | + ] |
0 commit comments