|
| 1 | +from datetime import datetime |
| 2 | +from enum import Enum |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from labelbox.exceptions import ResourceNotFoundError |
| 6 | + |
| 7 | +from labelbox.data.annotation_types.types import Cuid |
| 8 | +from labelbox.pydantic_compat import BaseModel |
| 9 | +from labelbox.utils import _CamelCaseMixin |
| 10 | + |
| 11 | + |
| 12 | +class LabelingServiceStatus(Enum): |
| 13 | + Accepted = 'ACCEPTED', |
| 14 | + Calibration = 'CALIBRATION', |
| 15 | + Complete = 'COMPLETE', |
| 16 | + Production = 'PRODUCTION', |
| 17 | + Requested = 'REQUESTED', |
| 18 | + SetUp = 'SET_UP' |
| 19 | + |
| 20 | + |
| 21 | +class LabelingService(BaseModel): |
| 22 | + id: Cuid |
| 23 | + project_id: Cuid |
| 24 | + created_at: datetime |
| 25 | + updated_at: datetime |
| 26 | + created_by_id: Cuid |
| 27 | + status: LabelingServiceStatus |
| 28 | + client: Any # type Any to avoid circular import from client |
| 29 | + |
| 30 | + def __init__(self, **kwargs): |
| 31 | + super().__init__(**kwargs) |
| 32 | + if not self.client.enable_experimental: |
| 33 | + raise RuntimeError( |
| 34 | + "Please enable experimental in client to use LabelingService") |
| 35 | + |
| 36 | + class Config(_CamelCaseMixin.Config): |
| 37 | + ... |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def start(cls, client, project_id: Cuid) -> 'LabelingService': |
| 41 | + """ |
| 42 | + Starts the labeling service for the project. This is equivalent to a UI acction to Request Specialized Labelers |
| 43 | +
|
| 44 | + Returns: |
| 45 | + LabelingService: The labeling service for the project. |
| 46 | + Raises: |
| 47 | + Exception: If the service fails to start. |
| 48 | + """ |
| 49 | + query_str = """mutation CreateProjectBoostWorkforcePyApi($projectId: ID!) { |
| 50 | + upsertProjectBoostWorkforce(data: { projectId: $projectId }) { |
| 51 | + success |
| 52 | + } |
| 53 | + }""" |
| 54 | + result = client.execute(query_str, {"projectId": project_id}) |
| 55 | + success = result["upsertProjectBoostWorkforce"]["success"] |
| 56 | + if not success: |
| 57 | + raise Exception("Failed to start labeling service") |
| 58 | + return cls.get(client, project_id) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def get(cls, client, project_id: Cuid) -> 'LabelingService': |
| 62 | + """ |
| 63 | + Returns the labeling service associated with the project. |
| 64 | +
|
| 65 | + Raises: |
| 66 | + ResourceNotFoundError: If the project does not have a labeling service. |
| 67 | + """ |
| 68 | + query = """ |
| 69 | + query GetProjectBoostWorkforcePyApi($projectId: ID!) { |
| 70 | + projectBoostWorkforce(data: { projectId: $projectId }) { |
| 71 | + id |
| 72 | + projectId |
| 73 | + createdAt |
| 74 | + updatedAt |
| 75 | + createdById |
| 76 | + status |
| 77 | + } |
| 78 | + } |
| 79 | + """ |
| 80 | + result = client.execute(query, {"projectId": project_id}) |
| 81 | + if result["projectBoostWorkforce"] is None: |
| 82 | + raise ResourceNotFoundError( |
| 83 | + message="The project does not have a labeling service.") |
| 84 | + data = result["projectBoostWorkforce"] |
| 85 | + data["client"] = client |
| 86 | + return LabelingService(**data) |
0 commit comments