Useful SDK Snippets
End-to-end project preparation via SDK
import os
import json
import random
from dotenv import load_dotenv
from imerit_ango.sdk import SDK
from imerit_ango.models.export_options import ExportOptions
from imerit_ango.models.invite import Invitation, ProjectAssignment
from imerit_ango.models.enums import OrganizationRoles, ProjectRoles
from imerit_ango.models.label_category import LabelOption, ClassificationCategory, Classification
# Organization Configurations
load_dotenv('variables.env')
api_key = os.getenv('API_KEY')
# Project Configurations
project_name = "Sample Text Classification Project"
project_description = "Created by end-to-end project preparation script"
labeler_email_list = ["[email protected]", "[email protected]"]
reviewer_email_list = ["[email protected]"]
# Initialize SDK
ango_sdk = SDK(api_key)
print("SDK initialized with API key.")
# Create Project
response = ango_sdk.create_project(name=project_name, description=project_description)
organization_id = response.get("data", {}).get("project", {}).get("organization")
project_id = response.get("data", {}).get("project", {}).get("_id")
print(f"Project '{project_name}' created successfully.")
print(f"Project ID: {project_id}, Organization ID: {organization_id}")
# Add Members to the Project
# Add Labelers
labeler_project_assignments = [ProjectAssignment(project_id=project_id, project_role=ProjectRoles.Labeler)]
labeler_invitation = Invitation(to=labeler_email_list, organization_role=OrganizationRoles.Member, project_assignments=labeler_project_assignments)
labeler_invite_response = ango_sdk.invite_members_to_org(organization_id=organization_id, invite_data=labeler_invitation)
# Add Reviewers
reviewer_project_assignments = [ProjectAssignment(project_id=project_id, project_role=ProjectRoles.Reviewer)]
reviewer_invitation = Invitation(to=reviewer_email_list, organization_role=OrganizationRoles.Member, project_assignments=reviewer_project_assignments)
reviewer_invite_response = ango_sdk.invite_members_to_org(organization_id=organization_id, invite_data=reviewer_invitation)
# The add_members_to_project function can also be used if the members are already part of the organization:
# ango_sdk.add_members_to_project(project_id=project_id, members=labeler_email_list, role=ProjectRoles.Labeler)
# ango_sdk.add_members_to_project(project_id=project_id, members=reviewer_email_list, role=ProjectRoles.Reviewer)
print(f"Added {len(labeler_email_list)} labelers to the project.")
print(f"Added {len(reviewer_email_list)} reviewers to the project.")
# Update Workflow Stages
start_stage = {"id": "Start",
"type": "Start",
"name": "Start",
"next": ["Label"],
"autoForward": False,
"position": {"x": 0, "y": 0}}
label_stage = {"id": "Label",
"type": "Label",
"name": "Label",
"next": ["Review"],
"assignedTo": [],
"position": {"x": 400, "y": 0}}
review_stage = {"id": "Review",
"type": "Review",
"name": "Review",
"next": ["Complete", "Label"],
"assignedTo": [],
"position": {"x": 800, "y": 0}}
complete_stage = {"id": "Complete",
"type": "Complete",
"name": "Complete",
"next": [],
"position": {"x": 1200, "y": 0},
"preventRequeue": False}
stages = [start_stage, label_stage, review_stage, complete_stage]
sdk_response = ango_sdk.update_workflow_stages(project_id, stages)
print("Workflow stages updated successfully.")
# Create Category Schema
schema_id = "123456"
category = ClassificationCategory(classification=Classification.Single_dropdown,
title="Choice",
options=[LabelOption("First"), LabelOption("Second"), LabelOption("Third")],
schemaId=schema_id,
shortcutKey="1")
response = ango_sdk.create_label_set(project_id=project_id, classifications=[category])
print("Label set with classification category created successfully.")
# Create Batches
for index in range(3):
batch_name = "Batch-" + str(index+1)
ango_sdk.create_batch(project_id=project_id, batch_name=batch_name)
print("Batches created successfully.")
# Prepare Files for Upload
file_paths = []
external_id_list = []
for asset_index in range(30):
external_id = str(asset_index + 1).zfill(5) + ".txt"
content = "The quick brown fox jumps over the lazy dog."
batch_name = "Batch-" + str(1 + asset_index // 10)
external_id_list.append(external_id)
file_paths.append({"data": content, "externalId": external_id, "batches": [batch_name]})
# Upload Files
ango_sdk.upload_files_cloud(project_id, file_paths)
print("Files uploaded successfully to the cloud.")
# Prepare Pre-labels
annotations = []
options = ["First", "Second", "Third"]
for external_id in external_id_list:
answer = random.choice(options)
annotation = {"externalId": external_id, "classifications": [{"schemaId": schema_id, "answer": answer}]}
annotations.append(annotation)
# Import Pre-labels
ango_sdk.import_labels(project_id, annotations)
print("Pre-labels imported successfully.")
# Export Project
export_options = ExportOptions(stage=["Complete"])
export_response = ango_sdk.export(project_id=project_id, options=export_options)
# Save the export as a JSON file
with open('project_export.json', 'w') as file:
json.dump(export_response, file, indent=4)
print("Export saved successfully.")Clone an Existing Project’s Category Schema and Workflow
Last updated