@@ -330,7 +330,7 @@ def create_from_file(cls, client: "labelbox.Client", project_id: str,
330
330
"""
331
331
if os .path .exists (path ):
332
332
with open (path , 'rb' ) as f :
333
- return cls ._create_mea_import_from_bytes (
333
+ return cls ._create_mal_import_from_bytes (
334
334
client , project_id , name , f ,
335
335
os .stat (path ).st_size )
336
336
else :
@@ -355,7 +355,7 @@ def create_from_objects(
355
355
if not data_str :
356
356
raise ValueError ('annotations cannot be empty' )
357
357
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 ,
359
359
len (data ))
360
360
361
361
@classmethod
@@ -440,7 +440,7 @@ def _get_file_mutation(cls) -> str:
440
440
}""" % query .results_query_part (cls )
441
441
442
442
@classmethod
443
- def _create_mea_import_from_bytes (
443
+ def _create_mal_import_from_bytes (
444
444
cls , client : "labelbox.Client" , project_id : str , name : str ,
445
445
bytes_data : BinaryIO , content_len : int ) -> "MALPredictionImport" :
446
446
file_name = f"{ project_id } __{ name } .ndjson"
@@ -454,3 +454,155 @@ def _create_mea_import_from_bytes(
454
454
res = cls ._create_from_bytes (client , variables , query_str , file_name ,
455
455
bytes_data )
456
456
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 (
493
+ cls , client : "labelbox.Client" , project_id : str , name : str ,
494
+ labels : List [Dict [str , Any ]]) -> "LabelImport" :
495
+ """
496
+ Create an 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 , data ,
511
+ 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 an 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 (
533
+ query_str ,
534
+ params = {
535
+ "fileUrl" : url ,
536
+ "projectId" : project_id ,
537
+ 'name' : name
538
+ })["createLabelImport" ])
539
+ else :
540
+ raise ValueError (f"Url { url } is not reachable" )
541
+
542
+ @classmethod
543
+ def from_name (cls ,
544
+ client : "labelbox.Client" ,
545
+ project_id : str ,
546
+ name : str ,
547
+ as_json : bool = False ) -> "LabelImport" :
548
+ """
549
+ Retrieves an label import job.
550
+
551
+ Args:
552
+ client: Labelbox Client for executing queries
553
+ project_id: ID used for querying import jobs
554
+ name: Name of the import job.
555
+ Returns:
556
+ LabelImport
557
+ """
558
+ query_str = """query getLabelImportPyApi($projectId : ID!, $name: String!) {
559
+ labelImport(
560
+ where: {projectId: $projectId, name: $name}){
561
+ %s
562
+ }}""" % query .results_query_part (cls )
563
+ params = {
564
+ "projectId" : project_id ,
565
+ "name" : name ,
566
+ }
567
+ response = client .execute (query_str , params )
568
+ if response is None :
569
+ raise labelbox .exceptions .ResourceNotFoundError (
570
+ LabelImport , params )
571
+ response = response ["labelImport" ]
572
+ if as_json :
573
+ return response
574
+ return cls (client , response )
575
+
576
+ @classmethod
577
+ def _get_url_mutation (cls ) -> str :
578
+ return """mutation createLabelImportPyApi($projectId : ID!, $name: String!, $fileUrl: String!) {
579
+ createLabelImport(data: {
580
+ projectId: $projectId
581
+ name: $name
582
+ fileUrl: $fileUrl
583
+ }) {%s}
584
+ }""" % query .results_query_part (cls )
585
+
586
+ @classmethod
587
+ def _get_file_mutation (cls ) -> str :
588
+ return """mutation createLabelImportPyApi($projectId : ID!, $name: String!, $file: Upload!, $contentLength: Int!) {
589
+ createLabelImport(data: {
590
+ projectId: $projectId name: $name filePayload: { file: $file, contentLength: $contentLength}
591
+ }) {%s}
592
+ }""" % query .results_query_part (cls )
593
+
594
+ @classmethod
595
+ def _create_label_import_from_bytes (
596
+ cls , client : "labelbox.Client" , project_id : str , name : str ,
597
+ bytes_data : BinaryIO , content_len : int ) -> "LabelImport" :
598
+ file_name = f"{ project_id } __{ name } .ndjson"
599
+ variables = {
600
+ "file" : None ,
601
+ "contentLength" : content_len ,
602
+ "projectId" : project_id ,
603
+ "name" : name
604
+ }
605
+ query_str = cls ._get_file_mutation ()
606
+ res = cls ._create_from_bytes (client , variables , query_str , file_name ,
607
+ bytes_data )
608
+ return cls (client , res ["createLabelImport" ])
0 commit comments