1
1
from datetime import datetime
2
2
from enum import Enum
3
+ from typing import Any
4
+
5
+ from labelbox .exceptions import ResourceNotFoundError
3
6
4
- from ..client import Client
5
7
from labelbox .data .annotation_types .types import Cuid
6
- from labelbox .orm .db_object import experimental
7
8
from labelbox .pydantic_compat import BaseModel
8
9
from labelbox .utils import _CamelCaseMixin
9
10
@@ -17,43 +18,69 @@ class LabelingServiceStatus(Enum):
17
18
SetUp = 'SET_UP'
18
19
19
20
20
- @experimental
21
- class LabelingService (_CamelCaseMixin , BaseModel ):
21
+ class LabelingService (BaseModel ):
22
22
id : Cuid
23
23
project_id : Cuid
24
24
created_at : datetime
25
25
updated_at : datetime
26
26
created_by_id : Cuid
27
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
+ ...
28
38
29
39
@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
+
54
60
@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 )
0 commit comments