Import HTML annotations

How to import annotations on HTML data and sample import formats.

Open this Colab for an interactive tutorial on importing annotations on HTML data.

Supported annotations

To import annotations in Labelbox, you need to create the annotations payload. In this section, we provide this payload for every annotation type.

Labelbox supports two formats for the annotations payload:

  • Python Annotation types (recommended)
  • NDJSON

Both are described below.

Classification: Radio (Single-choice)

text_annotation = lb_types.ClassificationAnnotation(
    name="text_html",
    value=lb_types.Text(answer="sample text")
)
text_annotation_ndjson = {
    'name': 'text_html',
    'answer': 'sample text',
}

Classification: Checklist (Multi-choice)

checklist_annotation= lb_types.ClassificationAnnotation(
  name="checklist_html", # must match your ontology feature's name
  value=lb_types.Checklist(
      answer = [
        lb_types.ClassificationAnswer(
            name = "first_checklist_answer"
        ), 
        lb_types.ClassificationAnswer(
            name = "second_checklist_answer"
        )
      ]
    )
 )
checklist_annotation_ndjson = {
    'name': 'checklist_html',
    'answers': [
        {'name': 'first_checklist_answer'},
        {'name': 'second_checklist_answer'}
    ]
}

Classification: Free-form text

text_annotation = lb_types.ClassificationAnnotation(
    name="text_html",
    value=lb_types.Text(answer="sample text")
)
text_annotation_ndjson = {
    'name': 'text_html',
    'answer': 'sample text',
}

End-to-end example: Import pre-labels or ground truth

Whether you are importing annotations as pre-labels or as ground truth, the steps are very similar. Steps 5 and 6 (creating and importing the annotation payload) is where the process becomes slightly different and is explained below in detail.

Before you start

You will need to import these libraries to use the code examples in this section.

import labelbox as lb
import uuid
import labelbox.types as lb_types

Replace with your API key

API_KEY = ""
client = lb.Client(API_KEY)

Step 1: Import data rows

The data row must be uploaded to Catalog before attaching annotations. Here, we create an example HTML data row.

global_key = "sample_html_1.html"

asset = {
    "row_data": "https://storage.googleapis.com/labelbox-datasets/html_sample_data/sample_html_1.html",
    "global_key": global_key
}

dataset = client.create_dataset(
    name="html_annotation_import_demo_dataset", 
    iam_integration=None # Removing this argument will default to the organziation's default iam integration
) 
task = dataset.create_data_rows([asset])
task.wait_till_done()
print("Errors:", task.errors)
print("Failed data rows: ", task.failed_data_rows)

Step 2: Create an ontology

Your project should have the correct ontology setup with all the tools and classifications supported for your annotations, and the tool names and classification instructions should match the name fields in your annotations to ensure the correct feature schemas are matched.

For example, when we create the text annotation, we provided the name as text_html. Now, when we set up our ontology, we must ensure that the name of the tool is also text_html. The same alignment must hold true for the other tools and classifications we create in our ontology.

ontology_builder = lb.OntologyBuilder(
  classifications=[ 
    lb.Classification( 
      class_type=lb.Classification.Type.TEXT,
      name="text_html"), 
    lb.Classification( 
      class_type=lb.Classification.Type.CHECKLIST,                   
      name="checklist_html", 
      options=[
        lb.Option(value="first_checklist_answer"),
        lb.Option(value="second_checklist_answer")            
      ]
    ), 
    lb.Classification( 
      class_type=lb.Classification.Type.RADIO, 
      name="radio_html", 
      options=[
        lb.Option(value="first_radio_answer"),
        lb.Option(value="second_radio_answer")
      ]
    )
  ]
)

ontology = client.create_ontology("Ontology HTML Annotations", ontology_builder.asdict(), media_type=lb.MediaType.Html)

Step 3: Create a labeling project

Connect the ontology to the labeling project.

project = client.create_project(name="html_project", 
                                    media_type=lb.MediaType.Html)

# Setup your ontology 
project.setup_editor(ontology)

Step 4: Send a batch of data rows to the project

batch = project.create_batch(
  "first-batch-html-demo", # Each batch in a project must have a unique name
  global_keys=[global_key], # Paginated collection of data row objects, list of data row ids or global keys
  priority=5 # priority between 1(highest) - 5(lowest)
)

print("Batch: ", batch)

Step 5: Create the annotations payload

Create the annotations payload using the snippets of code shown above.

Labelbox supports two formats for the annotations payload: NDJSON and Python annotation types. Both approaches are described below with instructions to compose annotations into Labels attached to the data rows.

The resulting label and label_ndjson from each approach will include every annotation (created above) supported by the respective method.

label = []
label.append(
  lb_types.Label(
    data=lb_types.HTMLData(
      global_key=global_key
    ),
    annotations=[
      text_annotation,
      checklist_annotation,
      radio_annotation
    ]
  )
)
label_ndjson = []
for annotations in [text_annotation_ndjson,
                    checklist_annotation_ndjson,
                    radio_annotation_ndjson]:
  annotations.update({
      'dataRow': {
          'globalKey': global_key
      }
  })
  label_ndjson.append(annotations)

Step 6: Import the annotation payload

For both options, you can pass either the label or label_ndjson payload as the value for the predictions or labels parameter.

Option A: Upload to a labeling project as pre-labels (Model-assisted labeling)

upload_job = lb.MALPredictionImport.create_from_objects(
    client = client, 
    project_id = project.uid, 
    name=f"mal_job-{str(uuid.uuid4())}", 
    predictions=label)

upload_job.wait_until_done();
print("Errors:", upload_job.errors)
print("Status of uploads: ", upload_job.statuses)

Option B: Upload to a labeling project as ground truth

upload_job = lb.LabelImport.create_from_objects(
    client = client, 
    project_id = project.uid, 
    name="label_import_job"+str(uuid.uuid4()),  
    labels=label_ndjson)

print("Errors:", upload_job.errors)