@@ -141,6 +141,20 @@ 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 , objects : List [Dict [str , Any ]],
146+ object_name : str ) -> BinaryIO :
147+ if not isinstance (objects , list ):
148+ raise TypeError (
149+ f"{ object_name } must be in a form of list. Found { type (objects )} "
150+ )
151+
152+ data_str = ndjson .dumps (objects )
153+ if not data_str :
154+ raise ValueError (f"{ object_name } cannot be empty" )
155+
156+ return data_str .encode ('utf-8' )
157+
144158 def refresh (self ) -> None :
145159 """Synchronizes values of all fields with the database.
146160 """
@@ -197,8 +211,9 @@ def create_from_file(cls, client: "labelbox.Client", model_run_id: str,
197211 raise ValueError (f"File { path } is not accessible" )
198212
199213 @classmethod
200- def create_from_objects (cls , client : "labelbox.Client" , model_run_id : str ,
201- name , predictions ) -> "MEAPredictionImport" :
214+ def create_from_objects (
215+ cls , client : "labelbox.Client" , model_run_id : str , name ,
216+ predictions : List [Dict [str , Any ]]) -> "MEAPredictionImport" :
202217 """
203218 Create an MEA prediction import job from an in memory dictionary
204219
@@ -210,12 +225,10 @@ def create_from_objects(cls, client: "labelbox.Client", model_run_id: str,
210225 Returns:
211226 MEAPredictionImport
212227 """
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' )
228+ data = cls ._get_ndjson_from_objects (predictions , 'annotations' )
229+
217230 return cls ._create_mea_import_from_bytes (client , model_run_id , name ,
218- data , len (data ))
231+ data , len (str ( data ) ))
219232
220233 @classmethod
221234 def create_from_url (cls , client : "labelbox.Client" , model_run_id : str ,
@@ -448,19 +461,16 @@ def create_from_objects(
448461 Returns:
449462 MALPredictionImport
450463 """
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' )
464+ data = cls ._get_ndjson_from_objects (predictions , 'annotations' )
455465
456466 has_confidence = LabelsConfidencePresenceChecker .check (predictions )
457467 if has_confidence :
458468 logger .warning ("""
459- Confidence scores are not supported in MAL Prediction Import.
460- Corresponding confidence score values will be ingored .
469+ Confidence scores are not supported in MAL Prediction Import.
470+ Corresponding confidence score values will be ignored .
461471 """ )
462472 return cls ._create_mal_import_from_bytes (client , project_id , name , data ,
463- len (data ))
473+ len (str ( data ) ))
464474
465475 @classmethod
466476 def create_from_url (cls , client : "labelbox.Client" , project_id : str ,
@@ -607,19 +617,16 @@ def create_from_objects(cls, client: "labelbox.Client", project_id: str,
607617 Returns:
608618 LabelImport
609619 """
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' )
620+ data = cls ._get_ndjson_from_objects (labels , 'labels' )
614621
615622 has_confidence = LabelsConfidencePresenceChecker .check (labels )
616623 if has_confidence :
617624 logger .warning ("""
618- Confidence scores are not supported in Label Import.
625+ Confidence scores are not supported in Label Import.
619626 Corresponding confidence score values will be ignored.
620627 """ )
621628 return cls ._create_label_import_from_bytes (client , project_id , name ,
622- data , len (data ))
629+ data , len (str ( data ) ))
623630
624631 @classmethod
625632 def create_from_url (cls , client : "labelbox.Client" , project_id : str ,
0 commit comments