@@ -141,6 +141,17 @@ def _create_from_bytes(cls, client, variables, query_str, file_name,
141141 files = {file_name : file_data }
142142 return client .execute (data = data , files = files )
143143
144+ @classmethod
145+ def _get_ndjson_from_objects (cls , data , slug ):
146+ if not isinstance (data , list ):
147+ raise TypeError (f"{ slug } must be in a form of list. Found { type (data )} " )
148+
149+ data_str = ndjson .dumps (data )
150+ if not data_str :
151+ raise ValueError (f"{ slug } cannot be empty" )
152+
153+ return data_str .encode ('utf-8' )
154+
144155 def refresh (self ) -> None :
145156 """Synchronizes values of all fields with the database.
146157 """
@@ -198,7 +209,7 @@ def create_from_file(cls, client: "labelbox.Client", model_run_id: str,
198209
199210 @classmethod
200211 def create_from_objects (cls , client : "labelbox.Client" , model_run_id : str ,
201- name , predictions ) -> "MEAPredictionImport" :
212+ name , predictions : List [ Dict [ str , Any ]] ) -> "MEAPredictionImport" :
202213 """
203214 Create an MEA prediction import job from an in memory dictionary
204215
@@ -210,10 +221,8 @@ def create_from_objects(cls, client: "labelbox.Client", model_run_id: str,
210221 Returns:
211222 MEAPredictionImport
212223 """
213- data_str = ndjson .dumps (predictions )
214- if not data_str :
215- raise ValueError ('annotations cannot be empty' )
216- data = data_str .encode ('utf-8' )
224+ data = cls ._get_ndjson_from_objects (predictions , 'annotations' )
225+
217226 return cls ._create_mea_import_from_bytes (client , model_run_id , name ,
218227 data , len (data ))
219228
@@ -448,16 +457,13 @@ def create_from_objects(
448457 Returns:
449458 MALPredictionImport
450459 """
451- data_str = ndjson .dumps (predictions )
452- if not data_str :
453- raise ValueError ('annotations cannot be empty' )
454- data = data_str .encode ('utf-8' )
460+ data = cls ._get_ndjson_from_objects (predictions , 'annotations' )
455461
456462 has_confidence = LabelsConfidencePresenceChecker .check (predictions )
457463 if has_confidence :
458464 logger .warning ("""
459- Confidence scores are not supported in MAL Prediction Import.
460- Corresponding confidence score values will be ingored .
465+ Confidence scores are not supported in MAL Prediction Import.
466+ Corresponding confidence score values will be ignored .
461467 """ )
462468 return cls ._create_mal_import_from_bytes (client , project_id , name , data ,
463469 len (data ))
@@ -607,15 +613,12 @@ def create_from_objects(cls, client: "labelbox.Client", project_id: str,
607613 Returns:
608614 LabelImport
609615 """
610- data_str = ndjson .dumps (labels )
611- if not data_str :
612- raise ValueError ('labels cannot be empty' )
613- data = data_str .encode ('utf-8' )
616+ data = cls ._get_ndjson_from_objects (labels , 'labels' )
614617
615618 has_confidence = LabelsConfidencePresenceChecker .check (labels )
616619 if has_confidence :
617620 logger .warning ("""
618- Confidence scores are not supported in Label Import.
621+ Confidence scores are not supported in Label Import.
619622 Corresponding confidence score values will be ignored.
620623 """ )
621624 return cls ._create_label_import_from_bytes (client , project_id , name ,
0 commit comments