Skip to content

[PLT-1355] Simplify labeling sevice handling for project #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions libs/labelbox/src/labelbox/schema/labeling_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ def request(self) -> 'LabelingService':
raise Exception("Failed to start labeling service")
return LabelingService.get(self.client, self.project_id)

@classmethod
def getOrCreate(cls, client, project_id: Cuid) -> 'LabelingService':
"""
Returns the labeling service associated with the project. If the project does not have a labeling service, it will create one.

Returns:
LabelingService: The labeling service for the project.
"""
try:
return cls.get(client, project_id)
except ResourceNotFoundError:
return cls.start(client, project_id)

@classmethod
def get(cls, client, project_id: Cuid) -> 'LabelingService':
"""
Expand Down
14 changes: 2 additions & 12 deletions libs/labelbox/src/labelbox/schema/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,13 +1920,12 @@ def clone(self) -> "Project":
def get_labeling_service(self) -> LabelingService:
"""Get the labeling service for this project.

Raises:
ResourceNotFoundError if the project does not have a labeling service.
Will automatically create a labeling service if one does not exist.

Returns:
LabelingService: The labeling service for this project.
"""
return LabelingService.get(self.client, self.uid)
return LabelingService.getOrCreate(self.client, self.uid)

@experimental
def get_labeling_service_status(self) -> LabelingServiceStatus:
Expand All @@ -1940,15 +1939,6 @@ def get_labeling_service_status(self) -> LabelingServiceStatus:
"""
return self.get_labeling_service().status

@experimental
def request_labeling_service(self) -> LabelingService:
"""Get the labeling service for this project.

Returns:
LabelingService: The labeling service for this project.
"""
return LabelingService.start(self.client, self.uid) # type: ignore


class ProjectMember(DbObject):
user = Relationship.ToOne("User", cache=True)
Expand Down
32 changes: 4 additions & 28 deletions libs/labelbox/tests/integration/test_labeling_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,16 @@
from labelbox.schema.labeling_service import LabelingServiceStatus


def test_get_labeling_service_throws_exception(project):
with pytest.raises(ResourceNotFoundError): # No labeling service by default
project.get_labeling_service()
with pytest.raises(ResourceNotFoundError): # No labeling service by default
project.get_labeling_service_status()


def test_start_labeling_service(project):
labeling_service = project.request_labeling_service()
assert labeling_service.status == LabelingServiceStatus.SetUp
assert labeling_service.project_id == project.uid

# Check that the labeling service is now available
labeling_service = project.get_labeling_service()
labeling_service = project.get_labeling_service() # creates and gets it
assert labeling_service.status == LabelingServiceStatus.SetUp
assert labeling_service.project_id == project.uid

labeling_service_status = project.get_labeling_service_status()
assert labeling_service_status == LabelingServiceStatus.SetUp


def test_request_labeling_service(
configured_batch_project_for_labeling_service):
project = configured_batch_project_for_labeling_service

project.upsert_instructions('tests/integration/media/sample_pdf.pdf')

labeling_service = project.request_labeling_service(
) # project fixture is an Image type project
labeling_service.request()
assert project.get_labeling_service_status(
) == LabelingServiceStatus.Requested


def test_request_labeling_service_moe_offline_project(
rand_gen, offline_chat_evaluation_project, chat_evaluation_ontology,
offline_conversational_data_row, model_config):
Expand All @@ -51,7 +27,7 @@ def test_request_labeling_service_moe_offline_project(

project.upsert_instructions('tests/integration/media/sample_pdf.pdf')

labeling_service = project.request_labeling_service()
labeling_service = project.get_labeling_service()
labeling_service.request()
assert project.get_labeling_service_status(
) == LabelingServiceStatus.Requested
Expand All @@ -65,7 +41,7 @@ def test_request_labeling_service_moe_project(

project.upsert_instructions('tests/integration/media/sample_pdf.pdf')

labeling_service = project.request_labeling_service()
labeling_service = project.get_labeling_service()
with pytest.raises(
LabelboxError,
match=
Expand All @@ -81,7 +57,7 @@ def test_request_labeling_service_moe_project(


def test_request_labeling_service_incomplete_requirements(project, ontology):
labeling_service = project.request_labeling_service(
labeling_service = project.get_labeling_service(
) # project fixture is an Image type project
with pytest.raises(ResourceNotFoundError,
match="Associated ontology id could not be found"
Expand Down
Loading