|
| 1 | +from collections import OrderedDict |
| 2 | +from time import time |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from .constants import WAIT_INTERVAL_SEC |
| 6 | +from .hasty_object import HastyObject |
| 7 | +from .requester import Requester |
| 8 | + |
| 9 | + |
| 10 | +class AutomatedLabelingJob(HastyObject): |
| 11 | + endpoint = '/v1/projects/{project_id}/automated_labeling' |
| 12 | + endpoint_job_id = '/v1/projects/{project_id}/automated_labeling/{job_id}' |
| 13 | + |
| 14 | + def __repr__(self): |
| 15 | + return self.get__repr__(OrderedDict({"id": self._id, |
| 16 | + "status": self._status, |
| 17 | + "progress": self._progress})) |
| 18 | + |
| 19 | + @property |
| 20 | + def id(self): |
| 21 | + """ |
| 22 | + :type: string |
| 23 | + """ |
| 24 | + return self._id |
| 25 | + |
| 26 | + @property |
| 27 | + def status(self): |
| 28 | + """ |
| 29 | + :type: string |
| 30 | + """ |
| 31 | + return self._status |
| 32 | + |
| 33 | + @property |
| 34 | + def project_id(self): |
| 35 | + """ |
| 36 | + :type: string |
| 37 | + """ |
| 38 | + return self._project_id |
| 39 | + |
| 40 | + @property |
| 41 | + def progress(self): |
| 42 | + """ |
| 43 | + :type: float |
| 44 | + """ |
| 45 | + return self._progress |
| 46 | + |
| 47 | + @property |
| 48 | + def started_on(self): |
| 49 | + """ |
| 50 | + :type: string |
| 51 | + """ |
| 52 | + return self._started_on |
| 53 | + |
| 54 | + @property |
| 55 | + def completed_on(self): |
| 56 | + """ |
| 57 | + :type: string |
| 58 | + """ |
| 59 | + return self._completed_on |
| 60 | + |
| 61 | + @property |
| 62 | + def experiment_id(self): |
| 63 | + """ |
| 64 | + :type: string |
| 65 | + """ |
| 66 | + return self._experiment_id |
| 67 | + |
| 68 | + def _init_properties(self): |
| 69 | + self._job_id = None |
| 70 | + self._status = None |
| 71 | + self._project_id = None |
| 72 | + self._started_on = None |
| 73 | + self._completed_on = None |
| 74 | + self._last_check = None |
| 75 | + self._experiment_id = None |
| 76 | + self._progress = None |
| 77 | + |
| 78 | + def _set_prop_values(self, data): |
| 79 | + if "id" in data: |
| 80 | + self._id = data["id"] |
| 81 | + if "status" in data: |
| 82 | + self._status = data["status"] |
| 83 | + if "started_on" in data: |
| 84 | + self._started_on = data["started_on"] |
| 85 | + if "completed_on" in data: |
| 86 | + self._completed_on = data["completed_on"] |
| 87 | + if "project_id" in data: |
| 88 | + self._project_id = data["project_id"] |
| 89 | + if "experiment_id" in data: |
| 90 | + self._experiment_id = data["experiment_id"] |
| 91 | + if "progress" in data: |
| 92 | + self._progress = data["progress"] |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def _create(requester: Requester, project_id: str, experiment_id: str, confidence_threshold: float = 0.8, |
| 96 | + max_detections_per_image: int = 100, num_images: int = 0, masker_threshold: float = 0.5, |
| 97 | + dataset_id: Optional[str] = None): |
| 98 | + res = requester.post(AutomatedLabelingJob.endpoint.format(project_id=project_id), |
| 99 | + json_data={"experiment_id": experiment_id, |
| 100 | + "confidence_threshold": confidence_threshold, |
| 101 | + "max_detections_per_image": max_detections_per_image, |
| 102 | + "num_images": num_images, |
| 103 | + "masker_threshold": masker_threshold, |
| 104 | + "dataset_id": dataset_id}) |
| 105 | + return AutomatedLabelingJob(requester, res, {"project_id": project_id}) |
| 106 | + |
| 107 | + def check_status(self): |
| 108 | + """ |
| 109 | + Returns current status and progress |
| 110 | + """ |
| 111 | + if self._last_check is None or self._last_check > WAIT_INTERVAL_SEC: |
| 112 | + res = self._requester.get(AutomatedLabelingJob.endpoint_job_id.format(project_id=self._project_id, |
| 113 | + job_id=self._id)) |
| 114 | + self._set_prop_values(res) |
| 115 | + self._last_check = time() |
| 116 | + return {"status": self._status, |
| 117 | + "progress": self._progress} |
0 commit comments