Skip to content

Commit b2e2e15

Browse files
author
Val Brodsky
committed
Add LabelingService start()
1 parent 29545c7 commit b2e2e15

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed

libs/labelbox/src/labelbox/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@
4343
from labelbox.schema.identifiable import UniqueId, GlobalKey
4444
from labelbox.schema.ontology_kind import OntologyKind
4545
from labelbox.schema.project_overview import ProjectOverview, ProjectOverviewDetailed
46+
from labelbox.schema.labeling_service import LabelingService, LabelingServiceStatus
Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from datetime import datetime
22
from enum import Enum
3+
from typing import Any
4+
5+
from labelbox.exceptions import ResourceNotFoundError
36

4-
from ..client import Client
57
from labelbox.data.annotation_types.types import Cuid
6-
from labelbox.orm.db_object import experimental
78
from labelbox.pydantic_compat import BaseModel
89
from labelbox.utils import _CamelCaseMixin
910

@@ -17,43 +18,69 @@ class LabelingServiceStatus(Enum):
1718
SetUp = 'SET_UP'
1819

1920

20-
@experimental
21-
class LabelingService(_CamelCaseMixin, BaseModel):
21+
class LabelingService(BaseModel):
2222
id: Cuid
2323
project_id: Cuid
2424
created_at: datetime
2525
updated_at: datetime
2626
created_by_id: Cuid
2727
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+
...
2838

2939
@classmethod
30-
def start(cls, client: Client, project_id: Cuid) -> 'LabelingService':
31-
return cls._create(client=client, project_id=project_id)
32-
33-
"""
34-
mutation CreateProjectBoostWorkforce($projectId: ID!) {
35-
upsertProjectBoostWorkforce(data: { projectId: $projectId }) {
36-
success
37-
__typename
38-
}
39-
}
40-
{
41-
"projectId": "clz0b7jg901fh07zic3u67b7g"
42-
}
43-
44-
45-
{
46-
"data": {
47-
"upsertProjectBoostWorkforce": {
48-
"success": true,
49-
"__typename": "ProjectBoostWorkforceResult"
50-
}
51-
}
52-
}
53-
"""
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+
5460
@classmethod
55-
def _create(cls, client: Client, project_id: Cuid) -> 'LabelingService':
56-
...
57-
58-
def status_as_string(self):
59-
return self.status.value
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)

libs/labelbox/src/labelbox/schema/project.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def is_chat_evaluation(self) -> bool:
150150
True if this project is a live chat evaluation project, False otherwise
151151
"""
152152
return self.media_type == MediaType.Conversational and self.editor_task_type == EditorTaskType.ModelChatEvaluation
153-
153+
154154
def is_prompt_response(self) -> bool:
155155
"""
156156
Returns:
@@ -1918,32 +1918,13 @@ def clone(self) -> "Project":
19181918

19191919
@experimental
19201920
def get_labeling_service(self) -> LabelingService:
1921-
"""
1922-
Returns the labeling service associated with the project.
1921+
"""Get the labeling service for this project.
19231922
19241923
Returns:
1925-
LabelingService: The labeling service associated with the project.
1926-
1927-
Raises:
1928-
ResourceNotFoundError: If the project does not have a labeling service.
1924+
LabelingService: The labeling service for this project.
19291925
"""
1930-
query = """
1931-
query GetProjectBoostWorkforcePyApi($projectId: ID!) {
1932-
projectBoostWorkforce(data: { projectId: $projectId }) {
1933-
id
1934-
projectId
1935-
createdAt
1936-
updatedAt
1937-
createdById
1938-
status
1939-
}
1940-
}
1941-
"""
1942-
result = self.client.execute(query, {"projectId": self.uid})
1943-
if result["projectBoostWorkforce"] is None:
1944-
raise ResourceNotFoundError(
1945-
message="The project does not have a labeling service.")
1946-
return LabelingService(**result["projectBoostWorkforce"])
1926+
return LabelingService.get(self.client, self.uid) # type: ignore
1927+
19471928

19481929
class ProjectMember(DbObject):
19491930
user = Relationship.ToOne("User", cache=True)
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
from labelbox.exceptions import ResourceNotFoundError
21
import pytest
32

3+
from labelbox.exceptions import ResourceNotFoundError
4+
from labelbox.schema.labeling_service import LabelingService, LabelingServiceStatus
5+
46

57
def test_get_labeling_service_throws_exception(project):
68
with pytest.raises(ResourceNotFoundError): # No labeling service by default
79
project.get_labeling_service()
10+
11+
12+
def test_start_labeling_service(project):
13+
labeling_service = LabelingService.start(project.client, project.uid)
14+
assert labeling_service.status == LabelingServiceStatus.SetUp
15+
assert labeling_service.project_id == project.uid
16+
17+
# Check that the labeling service is now available
18+
labeling_service = project.get_labeling_service()
19+
assert labeling_service.status == LabelingServiceStatus.SetUp
20+
assert labeling_service.project_id == project.uid

0 commit comments

Comments
 (0)