21
21
from labelbox .adv_client import AdvClient
22
22
from labelbox .orm import query
23
23
from labelbox .orm .db_object import DbObject
24
- from labelbox .orm .model import Entity
24
+ from labelbox .orm .model import Entity , Field
25
25
from labelbox .pagination import PaginatedCollection
26
26
from labelbox .schema import role
27
27
from labelbox .schema .conflict_resolution_strategy import ConflictResolutionStrategy
52
52
from labelbox .schema .slice import CatalogSlice , ModelSlice
53
53
from labelbox .schema .task import Task
54
54
from labelbox .schema .user import User
55
- from labelbox .schema .editor_task_type import EditorTaskType
55
+ from labelbox .schema .ontology_kind import (OntologyKind , EditorTaskTypeMapper ,
56
+ EditorTaskType )
56
57
57
58
logger = logging .getLogger (__name__ )
58
59
@@ -564,7 +565,7 @@ def get_labeling_frontends(self, where=None) -> List[LabelingFrontend]:
564
565
"""
565
566
return self ._get_all (Entity .LabelingFrontend , where )
566
567
567
- def _create (self , db_object_type , data ):
568
+ def _create (self , db_object_type , data , extra_params = {} ):
568
569
""" Creates an object on the server. Attribute values are
569
570
passed as keyword arguments:
570
571
@@ -580,12 +581,14 @@ def _create(self, db_object_type, data):
580
581
"""
581
582
# Convert string attribute names to Field or Relationship objects.
582
583
# Also convert Labelbox object values to their UIDs.
584
+
583
585
data = {
584
586
db_object_type .attribute (attr ) if isinstance (attr , str ) else attr :
585
587
value .uid if isinstance (value , DbObject ) else value
586
588
for attr , value in data .items ()
587
589
}
588
590
591
+ data = {** data , ** extra_params }
589
592
query_string , params = query .create (db_object_type , data )
590
593
res = self .execute (query_string , params )
591
594
res = res ["create%s" % db_object_type .type_name ()]
@@ -700,18 +703,26 @@ def create_project(self, **kwargs) -> Project:
700
703
)
701
704
702
705
media_type = kwargs .get ("media_type" )
703
- if media_type :
704
- if MediaType .is_supported (media_type ):
705
- media_type = media_type .value
706
- else :
707
- raise TypeError (f"{ media_type } is not a valid media type. Use"
708
- f" any of { MediaType .get_supported_members ()} "
709
- " from MediaType. Example: MediaType.Image." )
706
+ if media_type and MediaType .is_supported (media_type ):
707
+ media_type_value = media_type .value
708
+ elif media_type :
709
+ raise TypeError (f"{ media_type } is not a valid media type. Use"
710
+ f" any of { MediaType .get_supported_members ()} "
711
+ " from MediaType. Example: MediaType.Image." )
710
712
else :
711
713
logger .warning (
712
714
"Creating a project without specifying media_type"
713
715
" through this method will soon no longer be supported." )
714
716
717
+ ontology_kind = kwargs .pop ("ontology_kind" , None )
718
+ if ontology_kind and OntologyKind .is_supported (ontology_kind ):
719
+ editor_task_type_value = EditorTaskTypeMapper .to_editor_task_type (
720
+ ontology_kind , media_type ).value
721
+ elif ontology_kind :
722
+ raise OntologyKind .get_ontology_kind_validation_error (ontology_kind )
723
+ else :
724
+ editor_task_type_value = None
725
+
715
726
quality_mode = kwargs .get ("quality_mode" )
716
727
if not quality_mode :
717
728
logger .info ("Defaulting quality mode to Benchmark." )
@@ -729,16 +740,34 @@ def create_project(self, **kwargs) -> Project:
729
740
else :
730
741
raise ValueError (f"{ quality_mode } is not a valid quality mode." )
731
742
732
- return self ._create (Entity .Project , {
733
- ** data ,
734
- ** ({
735
- "media_type" : media_type
736
- } if media_type else {})
737
- })
743
+ params = {** data }
744
+ if media_type_value :
745
+ params ["media_type" ] = media_type_value
746
+ if editor_task_type_value :
747
+ params ["editor_task_type" ] = editor_task_type_value
748
+
749
+ extra_params = {
750
+ Field .String ("dataset_name_or_id" ):
751
+ params .pop ("dataset_name_or_id" , None ),
752
+ Field .Boolean ("append_to_existing_dataset" ):
753
+ params .pop ("append_to_existing_dataset" , None ),
754
+ Field .Int ("data_row_count" ):
755
+ params .pop ("data_row_count" , None ),
756
+ }
757
+ extra_params = {k : v for k , v in extra_params .items () if v is not None }
738
758
739
- def create_model_chat_project (self , ** kwargs ) -> Project :
740
- kwargs ["media_type" ] = media_type .MediaType .Conversational
741
- kwargs ["editor_task_type" ] = editor_task_type .EditorTaskType .ModelChatEvaluation
759
+ return self ._create (Entity .Project , params , extra_params )
760
+
761
+ def create_model_evalution_project (self ,
762
+ dataset_name_or_id : str ,
763
+ append_to_existing_dataset : bool = False ,
764
+ data_row_count : int = 100 ,
765
+ ** kwargs ) -> Project :
766
+ kwargs ["media_type" ] = MediaType .Conversational
767
+ kwargs ["ontology_kind" ] = OntologyKind .ModelEvaluation
768
+ kwargs ["dataset_name_or_id" ] = dataset_name_or_id
769
+ kwargs ["append_to_existing_dataset" ] = append_to_existing_dataset
770
+ kwargs ["data_row_count" ] = data_row_count
742
771
743
772
return self .create_project (** kwargs )
744
773
@@ -950,7 +979,7 @@ def create_ontology_from_feature_schemas(
950
979
name ,
951
980
feature_schema_ids ,
952
981
media_type : MediaType = None ,
953
- editor_task_type : EditorTaskType = None ) -> Ontology :
982
+ ontology_kind : OntologyKind = None ) -> Ontology :
954
983
"""
955
984
Creates an ontology from a list of feature schema ids
956
985
@@ -988,10 +1017,11 @@ def create_ontology_from_feature_schemas(
988
1017
"Neither `tool` or `classification` found in the normalized feature schema"
989
1018
)
990
1019
normalized = {'tools' : tools , 'classifications' : classifications }
1020
+
991
1021
return self .create_ontology (name = name ,
992
1022
normalized = normalized ,
993
1023
media_type = media_type ,
994
- editor_task_type = editor_task_type )
1024
+ ontology_kind = ontology_kind )
995
1025
996
1026
def delete_unused_feature_schema (self , feature_schema_id : str ) -> None :
997
1027
"""
@@ -1182,7 +1212,7 @@ def create_ontology(self,
1182
1212
name ,
1183
1213
normalized ,
1184
1214
media_type : MediaType = None ,
1185
- editor_task_type : EditorTaskType = None ) -> Ontology :
1215
+ ontology_kind : OntologyKind = None ) -> Ontology :
1186
1216
"""
1187
1217
Creates an ontology from normalized data
1188
1218
>>> normalized = {"tools" : [{'tool': 'polygon', 'name': 'cat', 'color': 'black'}], "classifications" : []}
@@ -1206,16 +1236,17 @@ def create_ontology(self,
1206
1236
1207
1237
if media_type :
1208
1238
if MediaType .is_supported (media_type ):
1209
- media_type = media_type .value
1239
+ media_type_value = media_type .value
1210
1240
else :
1211
1241
raise get_media_type_validation_error (media_type )
1212
1242
1213
- if editor_task_type :
1214
- if EditorTaskType .is_supported (editor_task_type ):
1215
- editor_task_type = editor_task_type .value
1216
- else :
1217
- raise EditorTaskType .get_editor_task_type_validation_error (
1218
- editor_task_type )
1243
+ if ontology_kind and OntologyKind .is_supported (ontology_kind ):
1244
+ editor_task_type_value = EditorTaskTypeMapper .to_editor_task_type (
1245
+ ontology_kind , media_type ).value
1246
+ elif ontology_kind :
1247
+ raise OntologyKind .get_ontology_kind_validation_error (ontology_kind )
1248
+ else :
1249
+ editor_task_type_value = None
1219
1250
1220
1251
query_str = """mutation upsertRootSchemaNodePyApi($data: UpsertOntologyInput!){
1221
1252
upsertOntology(data: $data){ %s }
@@ -1224,68 +1255,15 @@ def create_ontology(self,
1224
1255
'data' : {
1225
1256
'name' : name ,
1226
1257
'normalized' : json .dumps (normalized ),
1227
- 'mediaType' : media_type
1258
+ 'mediaType' : media_type_value
1228
1259
}
1229
1260
}
1230
- if editor_task_type :
1231
- params ['data' ]['editorTaskType' ] = editor_task_type
1261
+ if editor_task_type_value :
1262
+ params ['data' ]['editorTaskType' ] = editor_task_type_value
1232
1263
1233
1264
res = self .execute (query_str , params )
1234
1265
return Entity .Ontology (self , res ['upsertOntology' ])
1235
1266
1236
- def create_model_chat_evaluation_ontology (self , name , normalized ):
1237
- """
1238
- Creates a model chat evalutation ontology from normalized data
1239
- >>> normalized = {"tools" : [{'tool': 'message-single-selection', 'name': 'model output single selection', 'color': '#ff0000',},
1240
- {'tool': 'message-multi-selection', 'name': 'model output multi selection', 'color': '#00ff00',},
1241
- {'tool': 'message-ranking', 'name': 'model output multi ranking', 'color': '#0000ff',}]
1242
- }
1243
- >>> ontology = client.create_ontology("ontology-name", normalized)
1244
-
1245
- Or use the ontology builder
1246
- >>> ontology_builder = OntologyBuilder(tools=[
1247
- Tool(tool=Tool.Type.MESSAGE_SINGLE_SELECTION,
1248
- name="model output single selection"),
1249
- Tool(tool=Tool.Type.MESSAGE_MULTI_SELECTION,
1250
- name="model output multi selection"),
1251
- Tool(tool=Tool.Type.MESSAGE_RANKING,
1252
- name="model output multi ranking"),
1253
- ],)
1254
-
1255
- >>> ontology = client.create_model_chat_evaluation_ontology("Multi-chat ontology", ontology_builder.asdict())
1256
-
1257
- Args:
1258
- name (str): Name of the ontology
1259
- normalized (dict): A normalized ontology payload. See above for details.
1260
- Returns:
1261
- The created Ontology
1262
- """
1263
-
1264
- return self .create_ontology (
1265
- name = name ,
1266
- normalized = normalized ,
1267
- media_type = MediaType .Conversational ,
1268
- editor_task_type = EditorTaskType .ModelChatEvaluation )
1269
-
1270
- def create_model_chat_evaluation_ontology_from_feature_schemas (
1271
- self , name , feature_schema_ids ):
1272
- """
1273
- Creates an ontology from a list of feature schema ids
1274
-
1275
- Args:
1276
- name (str): Name of the ontology
1277
- feature_schema_ids (List[str]): List of feature schema ids corresponding to
1278
- top level tools and classifications to include in the ontology
1279
- Returns:
1280
- The created Ontology
1281
- """
1282
-
1283
- return self .create_ontology_from_feature_schemas (
1284
- name = name ,
1285
- feature_schema_ids = feature_schema_ids ,
1286
- media_type = MediaType .Conversational ,
1287
- editor_task_type = EditorTaskType .ModelChatEvaluation )
1288
-
1289
1267
def create_feature_schema (self , normalized ):
1290
1268
"""
1291
1269
Creates a feature schema from normalized data.
0 commit comments