Skip to content

Commit bf7e7f7

Browse files
JWittmeyerJWittmeyer
andauthored
Adds task + label & multi label mutations (#93)
Co-authored-by: JWittmeyer <[email protected]>
1 parent d880212 commit bf7e7f7

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

controller/labeling_task_label/manager.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
from typing import List, Any, Dict
1717
import re
1818

19+
LABEL_COLORS = [
20+
"red",
21+
"orange",
22+
"amber",
23+
"yellow",
24+
"lime",
25+
"green",
26+
"emerald",
27+
"teal",
28+
"cyan",
29+
"sky",
30+
"blue",
31+
"indigo",
32+
"violet",
33+
"purple",
34+
"fuchsia",
35+
"pink",
36+
"rose",
37+
]
38+
1939

2040
def get_label(project_id: str, label_id: str) -> LabelingTaskLabel:
2141
return labeling_task_label.get(project_id, label_id)
@@ -73,6 +93,25 @@ def create_label(
7393
return label
7494

7595

96+
def create_labels(
97+
project_id: str, labeling_task_id: str, names: List[str]
98+
) -> List[LabelingTaskLabel]:
99+
task = labeling_task.get(project_id, labeling_task_id)
100+
labels = []
101+
102+
available_colors = len(LABEL_COLORS)
103+
for idx, name in enumerate(names):
104+
labels.append(
105+
labeling_task_label.create(
106+
project_id, name, labeling_task_id, LABEL_COLORS[idx % available_colors]
107+
)
108+
)
109+
if task.task_type == LabelingTaskType.INFORMATION_EXTRACTION.value:
110+
create_knowledge_base_if_not_existing(name, project_id)
111+
general.commit()
112+
return labels
113+
114+
76115
def delete_label(project_id: str, label_id: str) -> None:
77116
labeling_task_label.delete(project_id, label_id, with_commit=True)
78117

graphql_api/mutation/labeling_task.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Optional
1+
from typing import Optional, List
22

33
from controller.auth import manager as auth
44
from controller.labeling_task import manager
5+
from controller.labeling_task_label import manager as label_manager
56
from controller.project import manager as project_manager
67
from submodules.model import events
78
from util import doc_ock, notification
@@ -46,6 +47,51 @@ def mutate(
4647
return CreateLabelingTask(ok=True)
4748

4849

50+
class CreateTaskAndLabels(graphene.Mutation):
51+
class Arguments:
52+
project_id = graphene.ID(required=True)
53+
labeling_task_name = graphene.String(required=True)
54+
labeling_task_type = graphene.String(required=True)
55+
labeling_task_target_id = graphene.ID(required=False)
56+
labels = graphene.List(graphene.String, required=False)
57+
58+
ok = graphene.Boolean()
59+
task_id = graphene.ID()
60+
61+
def mutate(
62+
self,
63+
info,
64+
project_id: str,
65+
labeling_task_name: str,
66+
labeling_task_type: str,
67+
labeling_task_target_id: Optional[str] = None,
68+
labels: Optional[List[str]] = None,
69+
):
70+
auth.check_demo_access(info)
71+
auth.check_project_access(info, project_id)
72+
user = auth.get_user_by_info(info)
73+
project = project_manager.get_project(project_id)
74+
item = manager.create_labeling_task(
75+
project_id, labeling_task_name, labeling_task_type, labeling_task_target_id
76+
)
77+
78+
if labels is not None:
79+
label_manager.create_labels(project_id, str(item.id), labels)
80+
81+
doc_ock.post_event(
82+
user,
83+
events.AddLabelingTask(
84+
ProjectName=f"{project.name}-{project.id}",
85+
Name=labeling_task_name,
86+
Type=labeling_task_type,
87+
),
88+
)
89+
notification.send_organization_update(
90+
project_id, f"labeling_task_created:{str(item.id)}"
91+
)
92+
return CreateTaskAndLabels(ok=True, task_id=str(item.id))
93+
94+
4995
class UpdateLabelingTask(graphene.Mutation):
5096
class Arguments:
5197
project_id = graphene.ID(required=True)
@@ -103,3 +149,4 @@ class LabelingTaskMutation(graphene.ObjectType):
103149
create_labeling_task = CreateLabelingTask.Field()
104150
update_labeling_task = UpdateLabelingTask.Field()
105151
delete_labeling_task = DeleteLabelingTask.Field()
152+
create_task_and_labels = CreateTaskAndLabels.Field()

graphql_api/mutation/labeling_task_label.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from controller.auth import manager as auth
88
from controller.project import manager as project_manager
99
from util import doc_ock, notification
10-
from typing import Dict
10+
from typing import Dict, List
1111

1212

1313
class CreateLabelingTaskLabel(graphene.Mutation):
@@ -50,6 +50,37 @@ def mutate(
5050
return CreateLabelingTaskLabel(label=label)
5151

5252

53+
class CreateLabelingTaskLabels(graphene.Mutation):
54+
class Arguments:
55+
project_id = graphene.ID(required=True)
56+
labeling_task_id = graphene.ID(required=True)
57+
labels = graphene.List(graphene.String, required=True)
58+
59+
ok = graphene.Boolean()
60+
61+
def mutate(self, info, project_id: str, labeling_task_id: str, labels: List[str]):
62+
auth.check_demo_access(info)
63+
auth.check_project_access(info, project_id)
64+
user = auth.get_user_by_info(info)
65+
created_labels = manager.create_labels(project_id, labeling_task_id, labels)
66+
task = task_manager.get_labeling_task(project_id, labeling_task_id)
67+
project = project_manager.get_project(project_id)
68+
for label in created_labels:
69+
doc_ock.post_event(
70+
user,
71+
events.AddLabel(
72+
ProjectName=f"{project.name}-{project_id}",
73+
Name=label.name,
74+
LabelingTaskName=task.name,
75+
),
76+
)
77+
notification.send_organization_update(
78+
project_id, f"label_created:{label.id}:labeling_task:{labeling_task_id}"
79+
)
80+
81+
return CreateLabelingTaskLabels(ok=True)
82+
83+
5384
class UpdateLabelingTaskLabelColor(graphene.Mutation):
5485
class Arguments:
5586
project_id = graphene.ID(required=True)
@@ -134,6 +165,7 @@ def mutate(self, info, project_id: str, label_id: str):
134165

135166
class LabelingTaskLabelMutation(graphene.ObjectType):
136167
create_label = CreateLabelingTaskLabel.Field()
168+
create_labels = CreateLabelingTaskLabels.Field()
137169
delete_label = DeleteLabelingTaskLabel.Field()
138170
update_label_color = UpdateLabelingTaskLabelColor.Field()
139171
update_label_hotkey = UpdateLabelingTaskLabelHotkey.Field()

0 commit comments

Comments
 (0)