@@ -266,7 +266,7 @@ def from_name(cls,
266266
267267 @classmethod
268268 def _get_url_mutation (cls ) -> str :
269- return """mutation createMEAPredictionImportPyApi ($modelRunId : ID!, $name: String!, $fileUrl: String!) {
269+ return """mutation createMEAPredictionImportByUrlPyApi ($modelRunId : ID!, $name: String!, $fileUrl: String!) {
270270 createModelErrorAnalysisPredictionImport(data: {
271271 modelRunId: $modelRunId
272272 name: $name
@@ -276,7 +276,7 @@ def _get_url_mutation(cls) -> str:
276276
277277 @classmethod
278278 def _get_file_mutation (cls ) -> str :
279- return """mutation createMEAPredictionImportPyApi ($modelRunId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
279+ return """mutation createMEAPredictionImportByFilePyApi ($modelRunId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
280280 createModelErrorAnalysisPredictionImport(data: {
281281 modelRunId: $modelRunId name: $name filePayload: { file: $file, contentLength: $contentLength}
282282 }) {%s}
@@ -330,7 +330,7 @@ def create_from_file(cls, client: "labelbox.Client", project_id: str,
330330 """
331331 if os .path .exists (path ):
332332 with open (path , 'rb' ) as f :
333- return cls ._create_mea_import_from_bytes (
333+ return cls ._create_mal_import_from_bytes (
334334 client , project_id , name , f ,
335335 os .stat (path ).st_size )
336336 else :
@@ -355,7 +355,7 @@ def create_from_objects(
355355 if not data_str :
356356 raise ValueError ('annotations cannot be empty' )
357357 data = data_str .encode ('utf-8' )
358- return cls ._create_mea_import_from_bytes (client , project_id , name , data ,
358+ return cls ._create_mal_import_from_bytes (client , project_id , name , data ,
359359 len (data ))
360360
361361 @classmethod
@@ -423,7 +423,7 @@ def from_name(cls,
423423
424424 @classmethod
425425 def _get_url_mutation (cls ) -> str :
426- return """mutation createMALPredictionImportPyApi ($projectId : ID!, $name: String!, $fileUrl: String!) {
426+ return """mutation createMALPredictionImportByUrlPyApi ($projectId : ID!, $name: String!, $fileUrl: String!) {
427427 createModelAssistedLabelingPredictionImport(data: {
428428 projectId: $projectId
429429 name: $name
@@ -433,14 +433,14 @@ def _get_url_mutation(cls) -> str:
433433
434434 @classmethod
435435 def _get_file_mutation (cls ) -> str :
436- return """mutation createMALPredictionImportPyApi ($projectId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
436+ return """mutation createMALPredictionImportByFilePyApi ($projectId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
437437 createModelAssistedLabelingPredictionImport(data: {
438438 projectId: $projectId name: $name filePayload: { file: $file, contentLength: $contentLength}
439439 }) {%s}
440440 }""" % query .results_query_part (cls )
441441
442442 @classmethod
443- def _create_mea_import_from_bytes (
443+ def _create_mal_import_from_bytes (
444444 cls , client : "labelbox.Client" , project_id : str , name : str ,
445445 bytes_data : BinaryIO , content_len : int ) -> "MALPredictionImport" :
446446 file_name = f"{ project_id } __{ name } .ndjson"
@@ -454,3 +454,154 @@ def _create_mea_import_from_bytes(
454454 res = cls ._create_from_bytes (client , variables , query_str , file_name ,
455455 bytes_data )
456456 return cls (client , res ["createModelAssistedLabelingPredictionImport" ])
457+
458+
459+ class LabelImport (AnnotationImport ):
460+ project = Relationship .ToOne ("Project" , cache = True )
461+
462+ @property
463+ def parent_id (self ) -> str :
464+ """
465+ Identifier for this import. Used to refresh the status
466+ """
467+ return self .project ().uid
468+
469+ @classmethod
470+ def create_from_file (cls , client : "labelbox.Client" , project_id : str ,
471+ name : str , path : str ) -> "LabelImport" :
472+ """
473+ Create a label import job from a file of annotations
474+
475+ Args:
476+ client: Labelbox Client for executing queries
477+ project_id: Project to import labels into
478+ name: Name of the import job. Can be used to reference the task later
479+ path: Path to ndjson file containing annotations
480+ Returns:
481+ LabelImport
482+ """
483+ if os .path .exists (path ):
484+ with open (path , 'rb' ) as f :
485+ return cls ._create_label_import_from_bytes (
486+ client , project_id , name , f ,
487+ os .stat (path ).st_size )
488+ else :
489+ raise ValueError (f"File { path } is not accessible" )
490+
491+ @classmethod
492+ def create_from_objects (cls , client : "labelbox.Client" , project_id : str ,
493+ name : str ,
494+ labels : List [Dict [str , Any ]]) -> "LabelImport" :
495+ """
496+ Create a label import job from an in memory dictionary
497+
498+ Args:
499+ client: Labelbox Client for executing queries
500+ project_id: Project to import labels into
501+ name: Name of the import job. Can be used to reference the task later
502+ labels: List of labels
503+ Returns:
504+ LabelImport
505+ """
506+ data_str = ndjson .dumps (labels )
507+ if not data_str :
508+ raise ValueError ('labels cannot be empty' )
509+ data = data_str .encode ('utf-8' )
510+ return cls ._create_label_import_from_bytes (client , project_id , name ,
511+ data , len (data ))
512+
513+ @classmethod
514+ def create_from_url (cls , client : "labelbox.Client" , project_id : str ,
515+ name : str , url : str ) -> "LabelImport" :
516+ """
517+ Create a label annotation import job from a url
518+ The url must point to a file containing label annotations.
519+
520+ Args:
521+ client: Labelbox Client for executing queries
522+ project_id: Project to import labels into
523+ name: Name of the import job. Can be used to reference the task later
524+ url: Url pointing to file to upload
525+ Returns:
526+ LabelImport
527+ """
528+ if requests .head (url ):
529+ query_str = cls ._get_url_mutation ()
530+ return cls (
531+ client ,
532+ client .execute (query_str ,
533+ params = {
534+ "fileUrl" : url ,
535+ "projectId" : project_id ,
536+ 'name' : name
537+ })["createLabelImport" ])
538+ else :
539+ raise ValueError (f"Url { url } is not reachable" )
540+
541+ @classmethod
542+ def from_name (cls ,
543+ client : "labelbox.Client" ,
544+ project_id : str ,
545+ name : str ,
546+ as_json : bool = False ) -> "LabelImport" :
547+ """
548+ Retrieves an label import job.
549+
550+ Args:
551+ client: Labelbox Client for executing queries
552+ project_id: ID used for querying import jobs
553+ name: Name of the import job.
554+ Returns:
555+ LabelImport
556+ """
557+ query_str = """query getLabelImportPyApi($projectId : ID!, $name: String!) {
558+ labelImport(
559+ where: {projectId: $projectId, name: $name}){
560+ %s
561+ }}""" % query .results_query_part (cls )
562+ params = {
563+ "projectId" : project_id ,
564+ "name" : name ,
565+ }
566+ response = client .execute (query_str , params )
567+ if response is None :
568+ raise labelbox .exceptions .ResourceNotFoundError (LabelImport , params )
569+ response = response ["labelImport" ]
570+ if as_json :
571+ return response
572+ return cls (client , response )
573+
574+ @classmethod
575+ def _get_url_mutation (cls ) -> str :
576+ return """mutation createLabelImportByUrlPyApi($projectId : ID!, $name: String!, $fileUrl: String!) {
577+ createLabelImport(data: {
578+ projectId: $projectId
579+ name: $name
580+ fileUrl: $fileUrl
581+ }) {%s}
582+ }""" % query .results_query_part (cls )
583+
584+ @classmethod
585+ def _get_file_mutation (cls ) -> str :
586+ return """mutation createLabelImportByFilePyApi($projectId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
587+ createLabelImport(data: {
588+ projectId: $projectId name: $name filePayload: { file: $file, contentLength: $contentLength}
589+ }) {%s}
590+ }""" % query .results_query_part (cls )
591+
592+ @classmethod
593+ def _create_label_import_from_bytes (cls , client : "labelbox.Client" ,
594+ project_id : str , name : str ,
595+ bytes_data : BinaryIO ,
596+ content_len : int ) -> "LabelImport" :
597+ file_name = f"{ project_id } __{ name } .ndjson"
598+ variables = {
599+ "file" : None ,
600+ "contentLength" : content_len ,
601+ "projectId" : project_id ,
602+ "name" : name
603+ }
604+ query_str = cls ._get_file_mutation ()
605+ res = cls ._create_from_bytes (client , variables , query_str , file_name ,
606+ bytes_data )
607+ return cls (client , res ["createLabelImport" ])
0 commit comments