### Create Example Dataset and Data Rows Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/exporting_to_csv.ipynb Creates a new dataset named 'image-demo-dataset' and adds a sample image data row with a unique global key. This setup is for demonstration purposes. ```python # Create dataset with image data row global_key = str(uuid.uuid4()) test_img_url = { "row_data": "https://storage.googleapis.com/labelbox-datasets/image_sample_data/2560px-Kitano_Street_Kobe01s5s4110.jpeg", "global_key": global_key, } dataset = client.create_dataset(name="image-demo-dataset") task = dataset.create_data_rows([test_img_url]) task.wait_till_done() print("Errors:", task.errors) print("Failed data rows:", task.failed_data_rows) ``` -------------------------------- ### Install Labelbox Python SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/README.md Use pip to install the SDK. For enhanced functionality including data processing, install with the 'data' extra. ```bash pip install labelbox ``` ```bash pip install "labelbox[data]" ``` -------------------------------- ### Verify Project Setup and Retrieve Information Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/project_setup.ipynb Check if the project setup is complete and retrieve the associated ontology and batch names. This helps confirm the project is ready for labeling. ```python # Note setup_complete will be None if it fails. print(batch_project.setup_complete) print(batch_project.ontology()) print([ds.name for ds in batch_project.batches()]) ``` -------------------------------- ### Install dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/libs/labelbox/README.md Synchronize the environment with all necessary features for the SDK. ```bash rye sync --all-features # to install labelbox[data] dependencies ``` -------------------------------- ### Install Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/huggingface/huggingface_custom_embeddings.ipynb Install the required Labelbox and HuggingFace libraries. ```python %pip install -q "labelbox[data]" %pip install -q transformers ``` -------------------------------- ### Install Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/sam/meta_sam.ipynb Install the necessary libraries for Labelbox, YOLOv8, and the Segment Anything model. ```python %pip install -q "labelbox[data]" %pip install -q ultralytics==8.0.20 %pip install -q "git+https://github.com/facebookresearch/segment-anything.git" ``` -------------------------------- ### Install Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/queue_management.ipynb Install the required Labelbox and NumPy packages. ```python %pip install -q "labelbox[data]" %pip install -q numpy ``` -------------------------------- ### Create Project and Setup Editor Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/project_setup.ipynb Create a new project, specifying quality modes and media type. Then, set up the editor by associating it with the created ontology. ```python batch_project = client.create_project( name="Project Setup Demo", quality_modes=[QualityMode.Consensus ], # For benchmarks use quality_mode = QualityMode.Benchmark media_type=lb.MediaType.Image, ) batch_project.setup_editor(ontology) ``` -------------------------------- ### Start ngrok Tunnel (Optional) Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/webhooks.ipynb If a public IP address is not available, this command starts ngrok to create a public URL that forwards to the local webhook server. Requires ngrok to be installed and authenticated. ```python if not COLAB: os.system(f"ngrok http {WH_PORT} &") ``` -------------------------------- ### Install SDK Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_data.ipynb Install the required Labelbox data package and urllib3 for the export environment. ```python %pip install -q "labelbox[data]" %pip install -q urllib3 ``` -------------------------------- ### Install Labelbox and Pandas Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/exporting_to_csv.ipynb Installs the Labelbox SDK with data support and the Pandas library. Use this to set up your environment for data export. ```python %pip install -q --upgrade "Labelbox[data]" %pip install -q pandas ``` -------------------------------- ### Install Labelbox SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/data_rows.ipynb Install the Labelbox Python SDK using pip. The '-q' flag suppresses verbose output. ```python %pip install labelbox -q ``` -------------------------------- ### Create and Setup Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/image.ipynb Create a new labeling project with a specified name and media type. Then, configure the project's editor by setting up the previously defined ontology. ```python # Project defaults to batch mode with benchmark quality settings if this argument is not provided # Queue mode will be deprecated once dataset mode is deprecated project = client.create_project(name="Image Annotation Import Demo", media_type=lb.MediaType.Image) project.setup_editor(ontology) ``` -------------------------------- ### Build Distribution with Rye Source: https://github.com/labelbox/labelbox-python/blob/develop/CONTRIBUTING.md Build a distribution package in the 'dist/' directory for installation. Example: 'pip install dist/labelbox-build.tar.gz'. ```bash rye build ``` -------------------------------- ### Install Labelbox SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/user_management.ipynb Install the required Labelbox data package. ```python %pip install "labelbox[data]" ``` -------------------------------- ### Install Labelbox SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/custom_embeddings.ipynb Install the required Labelbox data package. ```python %pip install -q --upgrade "labelbox[data]" ``` -------------------------------- ### Install Labelbox SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/conversational_LLM_data_generation.ipynb Installs the Labelbox Python SDK with data handling capabilities. Use this command before running any Labelbox-related scripts. ```python %pip install -q "labelbox[data]" ``` -------------------------------- ### Create and Setup Video Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/video.ipynb Create a new project for video annotation and connect the previously defined ontology to it using `setup_editor`. This step links your annotation schema to the project's labeling interface. ```python project = client.create_project(name="Video Annotation Import Demo", media_type=lb.MediaType.Video) ## connect ontology to your project project.setup_editor(ontology) ``` -------------------------------- ### Mark Project Setup as Completed Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/multimodal_chat_project.ipynb Call this method after finalizing project setup and model configurations. Once marked, project model configurations cannot be added, modified, or deleted. ```python project.set_project_model_setup_complete() ``` -------------------------------- ### Install Required Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/langchain/langchain.ipynb Install necessary packages for LangChain, LangSmith, and Labelbox integration. ```python %pip install --upgrade --quiet langchain langsmith langchainhub %pip install --upgrade --quiet langchain-openai tiktoken pandas duckduckgo-search %pip install --upgrade --quiet "labelbox[data]" ``` -------------------------------- ### Setup Project Ontology Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/model_experiments/model_predictions_to_project.ipynb Configure the editor for a project with a given ontology. The `project` object must be previously created. ```python # Setup Ontology project.setup_editor(ontology) ``` -------------------------------- ### Create a Labelbox Text Project and Setup Editor Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/text.ipynb Initiate a new text-based labeling project and configure its editor with a previously defined ontology. This step is crucial for preparing the project for data import. ```python # Project defaults to batch mode with benchmark quality settings if this argument is not provided # Queue mode will be deprecated once dataset mode is deprecated project = client.create_project(name="Text Annotation Import Demo", media_type=lb.MediaType.Text) project.setup_editor(ontology) ``` -------------------------------- ### Install pre-commit hooks Source: https://github.com/labelbox/labelbox-python/blob/develop/libs/labelbox/README.md Set up pre-commit to automatically enforce formatting standards during git commits. ```shell pip install pre-commit pre-commit install ``` -------------------------------- ### Retrieve project instance Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/exporting_to_csv.ipynb Example of fetching a project object using a project ID. ```python # PROJECT_ID = None # project = client.get_project(PROJECT_ID) ``` -------------------------------- ### Inspect project metadata Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/projects.ipynb Displays basic project configuration details, including setup status, media type, batches, and ontology. ```python # Note the project is not fully setup many of the fields will be empty. print("Project is not setup yet:", project.setup_complete is None) print("Project name:", project.name) print("Project description:", project.description) print("Media Type:", project.media_type) batches = [b for b in project.batches()] print("Project Batches", batches) print("Ontology:", project.ontology()) ``` -------------------------------- ### Flask Webhook Server Setup Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/webhooks.ipynb Initializes a Flask application and defines routes for handling incoming webhook requests. Includes a root route and a specific endpoint for processing POST requests. ```python app = Flask(__name__) @app.route("/") def hello_world(): return "Hello, World!" @app.route("/webhook-endpoint", methods=["POST"]) def print_webhook_info(): payload = request.data computed_signature = hmac.new(secret, msg=payload, digestmod=hashlib.sha1).hexdigest() if request.headers["X-Hub-Signature"] != "sha1=" + computed_signature: print( "Error: computed_signature does not match signature provided in the headers" ) return "Error", 500, 200 print("=========== New Webhook Delivery ============") print("Delivery ID: %s" % request.headers["X-Labelbox-Id"]) print("Event: %s" % request.headers["X-Labelbox-Event"]) print("Payload: %s" % json.dumps(json.loads(payload.decode("utf8")), indent=4)) return "Success" thread = threading.Thread(target=lambda: run_simple("0.0.0.0", WH_PORT, app)) thread.start() ``` -------------------------------- ### Sync Dependencies with Rye Source: https://github.com/labelbox/labelbox-python/blob/develop/CONTRIBUTING.md Use 'rye sync' to install project dependencies. Use '--all-features' for the main 'labelbox' module. ```bash rye sync rye sync --all-features ``` -------------------------------- ### Create a Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/projects.ipynb Initialize a new project with a specified media type. ```python # Creates an empty project project = client.create_project( name="my-test-project", description="a description", media_type=lb.MediaType.Image, ) ``` -------------------------------- ### Create and Set Up Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/yolo/import_yolov8_annotations.ipynb Creates a Labelbox project, sets up a batch with the data row's global key, and configures the project editor with the defined ontology. ```python project = client.create_project(name="YOLOv8 Demo Project", media_type=lb.MediaType.Image) project.create_batch(name="batch 1", global_keys=[global_key]) project.setup_editor(ontology) ``` -------------------------------- ### Validate Labelbox SDK Installation and API Key Source: https://github.com/labelbox/labelbox-python/blob/develop/README.md This Python script connects to Labelbox using an API key, creates a dataset, and adds a data row to verify the setup. Ensure you replace API_KEY with your actual key. ```python import labelbox as lb client = lb.Client(API_KEY) # API_KEY = API Key generated from labelbox.com dataset = client.create_dataset(name="Test Dataset") data_rows = [{"row_data": "My First Data Row", "global_key": "first-data-row"}] task = dataset.create_data_rows(data_rows) task.wait_till_done() ``` -------------------------------- ### Install Labelbox SDK Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/foundry/object_detection.ipynb Install the required Python package for Labelbox. ```python %pip install -q labelbox ``` -------------------------------- ### Set Up Project and Connect Ontology Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/exporting_to_csv.ipynb Creates a new project named 'Image Annotation Import Demo' and configures its editor with the previously defined ontology. This prepares the project for image annotation tasks. ```python # Set up project and connect ontology project = client.create_project(name="Image Annotation Import Demo", media_type=lb.MediaType.Image) project.setup_editor(ontology) ``` -------------------------------- ### Initialize Labelbox project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/sam/meta_sam_video.ipynb Creates a new video project or retrieves an existing one, then links it to an ontology. ```python # Create a new project if you don't have one # Project defaults to batch mode with benchmark quality settings if this argument is not provided # Queue mode will be deprecated once dataset mode is deprecated project = client.create_project(name="yolo-sam-video-masks-project", media_type=lb.MediaType.Video) # Or get an existing project by ID (uncomment the below) # project = get_project("fill_in_project_id") # If the project already has an ontology set up, comment out this line project.setup_editor(ontology) ``` -------------------------------- ### Initialize Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/tiled.ipynb Creates a project specifically for geospatial tiled media and associates it with an ontology. ```python # Project defaults to batch mode with benchmark quality settings if this argument is not provided # Queue mode will be deprecated once dataset mode is deprecated project = client.create_project(name="Geospatial Project Demo", media_type=lb.MediaType.Geospatial_Tile) project.setup_editor(ontology) ``` -------------------------------- ### Create a Labelbox project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/conversational_LLM_predictions.ipynb Initializes a new project with conversational media type and sets up the editor with a predefined ontology. ```python project = client.create_project( name="Conversational Human Evaluation Demo", media_type=lb.MediaType.Conversational, ) project.setup_editor(ontology) ``` -------------------------------- ### Create Project and Dataset Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/queue_management.ipynb Initialize a new project and dataset for queue management. ```python # Create Labelbox project project = client.create_project( name="batch-test-project", description="a description", quality_modes=[ QualityMode.Benchmark ], # For Consensus projects use quality_mode = QualityMode.Consensus media_type=lb.MediaType.Image, ) dataset = client.create_dataset(name="queue_dataset") ``` -------------------------------- ### Install Labelbox and Ultralytics Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/yolo/import_yolov8_annotations.ipynb Installs the necessary Python packages for Labelbox data integration and the Ultralytics library. ```python %pip install -q --upgrade "labelbox[data]" %pip install -q --upgrade ultralytics ``` -------------------------------- ### Create a Labelbox project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/model_experiments/custom_metrics_demo.ipynb Initializes a new project with a specified media type and configures the editor with an existing ontology. ```python # Create a Labelbox project project = client.create_project(name="image_prediction_many_kinds", media_type=lb.MediaType.Image) project.setup_editor(ontology) ``` -------------------------------- ### Create a destination project for migration Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/projects.ipynb Initializes a new project and attaches an ontology to prepare for data and label migration. ```python # Create an empty destination project destination_project = client.create_project( name="destination-test-project", description="a description", media_type=lb.MediaType.Image, ) # Create ontology and attach to destination project destination_ontology_builder = lb.OntologyBuilder( classifications=[ # List of Classification objects lb.Classification( class_type=lb.Classification.Type.RADIO, name="destination_radio_question", options=[ lb.Option(value="destination_first_radio_answer"), lb.Option(value="destination_second_radio_answer"), ], ), ]) destination_ontology = client.create_ontology("dest-test-ontology", ontology_builder.asdict()) destination_project.setup_editor(destination_ontology) ``` -------------------------------- ### Install Dependencies Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/webhooks.ipynb Installs necessary Python packages for webhook handling, including Labelbox SDK, Flask, and cryptographic libraries. ```python %pip install -q "labelbox[data]" %pip install -q requests %pip install -q hmac %pip install -q hashlib %pip install -q flask %pip install -q Werkzeug ``` -------------------------------- ### Data Row Generator Output Example Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_v1_to_v2_migration_support.ipynb Example of a single data row object returned by deprecated export methods. ```python # Single entry from the output of dataset.export_data_rows() (deprecated) # Data row objects will not be deprecated. single_output_from_data_row_generator = """ """ ``` ```python # Single output from batch.export_data_rows() method (deprecated) # Data row objects will not be deprecated single_output_from_data_row_generator = """ """ ``` -------------------------------- ### Initialize a labeling project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/conversational_LLM.ipynb Creates a new project and associates the previously defined ontology with the editor. ```python # Create Labelbox project project = client.create_project( name="Conversational Text Annotation Import Demo (Pairwise comparison)", media_type=lb.MediaType.Conversational, ) # Setup your ontology project.setup_editor( ontology) # Connect your ontology and editor to your project ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/sam/meta_sam.ipynb Import required libraries and configure the environment for image processing and Labelbox interaction. ```python from IPython import display display.clear_output() import ultralytics ultralytics.checks() import cv2 import numpy as np from ultralytics import YOLO from IPython.display import display, Image import torch import matplotlib.pyplot as plt from segment_anything import ( sam_model_registry, SamAutomaticMaskGenerator, SamPredictor, ) import os import urllib.request import uuid import labelbox as lb import labelbox.types as lb_types HOME = os.getcwd() if IN_COLAB: from google.colab.patches import cv2_imshow ``` -------------------------------- ### Initialize Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/audio.ipynb Creates a new project and associates the previously defined ontology with it. ```python # Create Labelbox project project = client.create_project(name="audio_project", media_type=lb.MediaType.Audio) # Setup your ontology project.setup_editor( ontology) # Connect your ontology and editor to your project ``` -------------------------------- ### Initialize LangChain Tools and LLM Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/langchain/langchain.ipynb Configure DuckDuckGo search tools and the ChatOpenAI model for evaluation. ```python tools = [ DuckDuckGoSearchResults( name="duck_duck_go"), # General internet search using DuckDuckGo ] llm = ChatOpenAI( model="gpt-3.5-turbo-16k", temperature=0, ) ``` -------------------------------- ### Radio Classification Prediction (NDJSON - Different Example) Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/conversational_predictions.ipynb Formats a radio classification prediction as a Python dictionary for NDJSON export, corresponding to the second Python object example. ```python radio_prediction_ndjson = { "name": "radio_convo", "answer": { "name": "first_radio_answer", "confidence": 0.5 }, "messageId": "0", } ``` -------------------------------- ### Create Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/foundry/object_detection.ipynb Initialize a project and associate it with the defined ontology. ```python project = client.create_project(name="Foundry Image Demo", media_type=lb.MediaType.Image) project.setup_editor(ontology) ``` -------------------------------- ### Example Output from Export Queued Data Rows (Deprecated) Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_v1_to_v2_migration_support.ipynb A single entry example of the output from the deprecated `project.export_queued_data_rows()` method, showing the structure of a data row object. ```json [ {'id': 'clpouak6nap2g0783ajd1d6pf', 'createdAt': '2023-12-03T02:04:34.062Z', 'updatedAt': '2023-12-03T02:05:33.797Z', 'externalId': None, 'globalKey': 'b57c9ab2-304f-4c17-ba5f-c536f39a6a46', 'metadataFields': [], 'customMetadata': [], 'rowData': 'https://storage.googleapis.com/labelbox-developer-testing-assets/image/data_files/santa.jpeg', 'mediaAttributes': {'assetType': 'image', 'contentLength': 305973, 'height': 1333, 'mimeType': 'image/jpeg', 'subType': 'jpeg', 'superType': 'image', 'width': 2000}} ] ``` -------------------------------- ### Configure API Keys and Environment Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/langchain/langchain.ipynb Set up API keys and environment variables for Labelbox, LangSmith, and OpenAI. ```python LB_API_KEY = "" LS_API_KEY = "" OPENAI_API_KEY = "" unique_id = uuid4().hex[0:8] os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}" os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" os.environ["LANGCHAIN_API_KEY"] = LS_API_KEY os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY lb_client = lb.Client(LB_API_KEY) client = Client() ``` -------------------------------- ### Radio Classification Prediction (Python Object - Different Example) Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/conversational_predictions.ipynb Another example of defining a radio classification prediction using the Labelbox SDK. This demonstrates a different answer choice. ```python radio_prediction = lb_types.ClassificationAnnotation( name="radio_convo", value=lb_types.Radio(answer=lb_types.ClassificationAnswer( name="first_radio_answer", confidence=0.5)), message_id="0", ) ``` -------------------------------- ### Example Label Object from Deprecated Generator Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_v1_to_v2_migration_support.ipynb This example shows the structure of a single label object that could be obtained from the deprecated `project.label_generator()` method. Note that Label objects themselves are not deprecated. ```python Label( uid='clrf5csho2ihx07ilffgp2fzj', data=ImageData( im_bytes=None, file_path=None, url='https://storage.googleapis.com/labelbox-datasets/image_sample_data/2560px-Kitano_Street_Kobe01s5s4110.jpeg', arr=None ), annotations=[ ObjectAnnotation( confidence=None, name='bounding_box', feature_schema_id='clrf5ck4a0b9b071paa9ncu15', extra={ 'instanceURI': 'https://api.labelbox.com/masks/feature/clrf5csvi6ofm07lsf9pygwvi?token=' 'color': '#ff0000', 'feature_id': 'clrf5csvi6ofm07lsf9pygwvi', 'value': 'bounding_box', 'page': None, 'unit': None }, value=Rectangle( extra={}, start=Point(extra={}, x=2096.0, y=1264.0), end=Point(extra={}, x=2240.0, y=1689.0) ), classifications=[] ), # Add more annotations as needed # ... ], extra={ 'Created By': 'aovalle@labelbox.com', 'Project Name': 'Image Annotation Import Demo', 'Created At': '2024-01-15T16:35:59.000Z', 'Updated At': '2024-01-15T16:51:56.000Z', 'Seconds to Label': 66.0, 'Agreement': -1.0, 'Benchmark Agreement': -1.0, 'Benchmark ID': None, 'Dataset Name': 'image-demo-dataset', 'Reviews': [], 'View Label': 'https://editor.labelbox.com?project=clrf5ckex09m9070x1te223u5&label=clrf5csho2ihx07ilffgp2fzj', 'Has Open Issues': 0.0, 'Skipped': False, 'media_type': 'image', 'Data Split': None, 'Global Key': '2560px-Kitano_Street_Kobe01s5s41102.jpeg' } ) ``` -------------------------------- ### Initialize Labeling Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/html.ipynb Creates a new Labelbox project configured for HTML media and attaches the previously defined ontology. ```python # Create Labelbox project project = client.create_project(name="HTML Import Annotation Demo", media_type=lb.MediaType.Html) # Setup your ontology project.setup_editor( ontology) # Connect your ontology and editor to your project ``` -------------------------------- ### Define Text Classification Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Example of defining a free text classification annotation. ```python # Python annotation text_annotation = lb_types.ClassificationAnnotation(name="free_text", value=lb_types.Text( answer="sample text", confidence=0.5)) ``` ```json # NDJSON text_annotation_ndjson = { "name": "free_text", "answer": "sample text", "confidence": 0.5, } ``` -------------------------------- ### Define Checklist Prediction Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Example of defining a checklist question and its answers in NDJSON format. ```json checklist_prediction_ndjson = { "name": "checklist_question", "answer": [ { "name": "first_checklist_answer", "confidence": 0.5 }, { "name": "second_checklist_answer", "confidence": 0.5 }, ], } ``` -------------------------------- ### Create a Labelbox project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Initializes a new Labelbox project with a specified media type and ontology. ```python # Create a Labelbox project project = client.create_project(name="Image Prediction Demo", media_type=lb.MediaType.Image) project.setup_editor(ontology) ``` -------------------------------- ### Get Project for Export Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/composite_mask_export.ipynb Retrieve a project object by its ID to prepare for data export. ```python # Insert the project ID of the project from which you wish to export data rows. PROJECT_ID = "" project = client.get_project(PROJECT_ID) ``` -------------------------------- ### Create a New Labelbox Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/model_experiments/model_predictions_to_project.ipynb Create a new project in Labelbox with a specified name and media type. Ensure the `client` object is initialized. ```python # Create a new project project = client.create_project(name="Model Run Import Demo Project", media_type=lb.MediaType.Image) ``` -------------------------------- ### Create LangSmith Dataset Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/langchain/langchain.ipynb Initialize a chat dataset and add an example run with message history. ```python dataset_name = f"Sample Dataset - {str(uuid4())}" dataset = client.create_dataset( dataset_name, description="A sample dataset in LangSmith.", data_type=DataType.chat, ) client.create_chat_example( messages=[ { "type": "ai", "data": { "content": "hi how are you" } }, { "type": "human", "data": { "content": "Im doing great how about you" } }, ], generations={ "type": "ai", "data": { "content": "Im doing great" }, }, # Custom model output dataset_id=dataset.id, ) ``` -------------------------------- ### Composite Mask Annotation Entry Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/composite_mask_export.ipynb Example JSON entry for an annotation containing a composite mask. ```json { "feature_id": "clpk3ow9u006f14vs2w5qa9l3", "feature_schema_id": "clpk3nvrv05bh08ua8fwqavng", "name": "mask", "value": "mask", "annotation_kind": "ImageSegmentationMask", "classifications": [], "composite_mask": { "url": "https://api.labelbox.com/api/v1/tasks/{task_id}/masks/{composite_mask_id}/index/1", "color_rgb": [ 123, 103, 152 ] } } ``` -------------------------------- ### Test Local Server Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/project_configuration/webhooks.ipynb Sends a GET request to the local server to verify it is running and accessible. ```python print(requests.get("http://localhost:3001").text) ``` -------------------------------- ### Initialize Labelbox Client Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/user_management.ipynb Import the library and initialize the client with experimental features enabled. ```python import labelbox as lb import os ``` ```python # Add your api key API_KEY = None client = lb.Client(api_key=API_KEY, enable_experimental=True) organization = client.get_organization() ``` -------------------------------- ### Create a project batch Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/exporting_to_csv.ipynb Initializes a batch for a project using global keys to identify data rows. ```python batch = project.create_batch( "image-demo-batch", global_keys=[ global_key ], # paginated collection of data row objects, list of data row ids or global keys priority=1, ) print(f"Batch: {batch}") ``` -------------------------------- ### Define Polygon Prediction Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Example of defining a polygon annotation using a list of points in Python and NDJSON. ```python # Python Anotation polygon_prediction = lb_types.ObjectAnnotation( name="polygon", confidence=0.5, value=lb_types.Polygon(points=[ lb_types.Point(x=1489.581, y=183.934), lb_types.Point(x=2278.306, y=256.885), lb_types.Point(x=2428.197, y=200.437), lb_types.Point(x=2560.0, y=335.419), lb_types.Point(x=2557.386, y=503.165), lb_types.Point(x=2320.596, y=503.103), lb_types.Point(x=2156.083, y=628.943), lb_types.Point(x=2161.111, y=785.519), lb_types.Point(x=2002.115, y=894.647), lb_types.Point(x=1838.456, y=877.874), lb_types.Point(x=1436.53, y=874.636), lb_types.Point(x=1411.403, y=758.579), lb_types.Point(x=1353.853, y=751.74), lb_types.Point(x=1345.264, y=453.461), lb_types.Point(x=1426.011, y=421.129), ]), ) ``` ```json # NDJSON polygon_prediction_ndjson = { "name": "polygon", "confidence": 0.5, "polygon": [ { "x": 1489.581, "y": 183.934 }, { "x": 2278.306, "y": 256.885 }, { "x": 2428.197, "y": 200.437 }, { "x": 2560.0, "y": 335.419 }, { "x": 2557.386, "y": 503.165 }, { "x": 2320.596, "y": 503.103 }, { "x": 2156.083, "y": 628.943 }, { "x": 2161.111, "y": 785.519 }, { "x": 2002.115, "y": 894.647 }, { "x": 1838.456, "y": 877.874 }, { "x": 1436.53, "y": 874.636 }, { "x": 1411.403, "y": 758.579 }, { "x": 1353.853, "y": 751.74 }, { "x": 1345.264, "y": 453.461 }, { "x": 1426.011, "y": 421.129 }, { "x": 1489.581, "y": 183.934 }, ], } ``` -------------------------------- ### Create Polyline Annotation Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Example of creating a polyline annotation with multiple points. This code is for Python object creation. ```python # Python Annotation polyline_prediction = lb_types.ObjectAnnotation( name="polyline", confidence=0.5, value=lb_types.Line(points=[ lb_types.Point(x=2534.353, y=249.471), lb_types.Point(x=2429.492, y=182.092), lb_types.Point(x=2294.322, y=221.962), lb_types.Point(x=2224.491, y=180.463), lb_types.Point(x=2136.123, y=204.716), lb_types.Point(x=1712.247, y=173.949), lb_types.Point(x=1703.838, y=84.438), lb_types.Point(x=1579.772, y=82.61), lb_types.Point(x=1583.442, y=167.552), lb_types.Point(x=1478.869, y=164.903), lb_types.Point(x=1418.941, y=318.149), lb_types.Point(x=1243.128, y=400.815), lb_types.Point(x=1022.067, y=319.007), lb_types.Point(x=892.367, y=379.216), lb_types.Point(x=670.273, y=364.408), lb_types.Point(x=613.114, y=288.16), lb_types.Point(x=377.559, y=238.251), lb_types.Point(x=368.087, y=185.064), lb_types.Point(x=246.557, y=167.286), lb_types.Point(x=236.648, y=285.61), lb_types.Point(x=90.929, y=326.412), ]), ) ``` -------------------------------- ### Create or Retrieve Labelbox Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/integrations/sam/meta_sam.ipynb Set up a new project or retrieve an existing one. If creating a new project, you can optionally set default batching and quality settings. The project's editor must be set up with the ontology. ```python # Create a new project if you don't have one # Project defaults to batch mode with benchmark quality settings if this argument is not provided # Queue mode will be deprecated once dataset mode is deprecated project = client.create_project(name="auto-mask-classification-project", media_type=lb.MediaType.Image) # Or get an existing project by ID (uncomment the below) # project = get_project("fill_in_project_id") # If the project already has an ontology set up, comment out this line project.setup_editor(ontology) ``` -------------------------------- ### Create Labelbox Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/pdf.ipynb Initializes a new project and associates it with a previously created ontology. ```python # Create a Labelbox project project = client.create_project(name="PDF_annotation_demo", media_type=lb.MediaType.Document) project.setup_editor(ontology) ``` -------------------------------- ### Define Bounding Box with Nested Classification Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/image_predictions.ipynb Example of a bounding box annotation that includes a nested radio classification. ```python ####### Bounding box with nested classification ####### bbox_with_radio_subclass_prediction = lb_types.ObjectAnnotation( name="bbox_with_radio_subclass", confidence=0.5, value=lb_types.Rectangle( start=lb_types.Point(x=541, y=933), # x = left, y = top end=lb_types.Point(x=871, y=1124), # x= left + width , y = top + height ), classifications=[ lb_types.ClassificationAnnotation( name="sub_radio_question", value=lb_types.Radio(answer=lb_types.ClassificationAnswer( name="first_sub_radio_answer", confidence=0.5)), ) ], ) ``` ```json ## NDJSON bbox_with_radio_subclass_prediction_ndjson = { "name": "bbox_with_radio_subclass", "confidence": 0.5, "classifications": [{ "name": "sub_radio_question", "answer": { "name": "first_sub_radio_answer", "confidence": 0.5 }, }], "bbox": { "top": 933, "left": 541, "height": 191, "width": 330 }, } ``` -------------------------------- ### Create a Model and Model Run Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/conversational_predictions.ipynb Use this code to initialize a new model and its first run within Labelbox. Ensure you have a valid ontology ID. ```python model = client.create_model( name="Conversational_model_run_" + str(uuid.uuid4()), ontology_id=ontology.uid, ) model_run = model.create_model_run("iteration 1") ``` -------------------------------- ### Upload Ground Truth Labels Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/image.ipynb Example of using LabelImport for ground truth uploads, noting that relationships are not supported in this mode. ```python # Relationships are not supported with LabelImport # For this demo either run MAL or Ground Truth, not both # Upload label for this data row in project # upload_job = lb.LabelImport.create_from_objects( # client = client, # project_id = project.uid, # name="label_import_job"+str(uuid.uuid4()), # labels=label) # print("Errors:", upload_job.errors) # print("Status of uploads: ", upload_job.statuses) ``` -------------------------------- ### Get Dataset for Export Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_data.ipynb Retrieve a specific dataset using its ID. This dataset object will then be used to initiate the export process. ```python # Insert the dataset ID of the dataset from which you wish to export data rows. DATASET_ID = "" dataset = client.get_dataset(DATASET_ID) ``` -------------------------------- ### Create a project batch Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/tiled.ipynb Initializes a new batch within a project using global keys and a specified priority level. ```python batch = project.create_batch( "first-batch-geo-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) ``` -------------------------------- ### Define conversational row_data with model outputs Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/conversational_LLM.ipynb Example of a full conversational data row string containing messages and model outputs. ```python pairwise_shopping_2 = """ { "type": "application/vnd.labelbox.conversational", "version": 1, "messages": [ { "messageId": "message-0", "timestampUsec": 1530718491, "content": "Hi! How can I help?", "user": { "userId": "Bot 002", "name": "Bot" }, "align": "left", "canLabel": false }, { "messageId": "message-1", "timestampUsec": 1530718503, "content": "I just bought a vacuum cleaner from you guys like a week ago and it's already broken!!", "user": { "userId": "User 00686", "name": "User" }, "align": "right", "canLabel": true } ], "modelOutputs": [ { "title": "Response A", "content": "I'm really sorry to hear that your vacuum cleaner is not working as expected. We certainly understand how frustrating this can be, especially with a recent purchase. I assure you, we're here to help!\n\n To proceed with resolving this issue, could you please provide some details about the problem you're experiencing with the vacuum cleaner? Additionally, if you could share your order number or receipt details, that would be very helpful. We will definitely look into the available options for you, which may include a replacement or a refund, depending on the specific circumstances and our return policy.\n\n Your satisfaction is our priority, and we'll do our best to resolve this matter promptly and to your satisfaction.", "modelConfigName": "GPT-3.5 with temperature 0" }, { "title": "Response B", "content": "I'm sorry about the issue with your vacuum cleaner. Please send us the order number or receipt details so we can quickly assist you with a replacement. Your satisfaction is important to us!", "modelConfigName": "Fine Tuned GPT-3.5 with demo data" } ] } """ ``` -------------------------------- ### Initialize Video Project for Export Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/composite_mask_export.ipynb Retrieves a specific video project from the Labelbox client. Ensure the VIDEO_PROJECT_ID is correctly set. ```python VIDEO_PROJECT_ID = "" project_video = client.get_project(VIDEO_PROJECT_ID) ``` -------------------------------- ### Create a Labelbox Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/text_predictions.ipynb Initializes a new Labelbox project with a specified name and media type. It also sets up the editor with the provided ontology, which is crucial for defining how annotations are applied. ```python # Create a Labelbox project project = client.create_project(name="Text Prediction Import Demo", media_type=lb.MediaType.Text) project.setup_editor(ontology) ``` -------------------------------- ### Get Data Rows from Project Batches Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/basics/data_rows.ipynb Retrieve all batches associated with a specific project. This is a prerequisite for exporting data rows from those batches. ```python # Pick a project with batches that have data rows with global keys PROJECT_ID = "" project = client.get_project(PROJECT_ID) batches = list(project.batches()) print(batches) # This is the same as # -> dataset = client.get_dataset(dataset_id) ``` -------------------------------- ### Create a Labelbox Project for HTML Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/html_predictions.ipynb Initializes a new Labelbox project specifically for HTML media type. The project must be set up with an ontology before annotations can be imported. ```python # Create a Labelbox project project = client.create_project(name="HTML prediction import demo", media_type=lb.MediaType.Html) project.setup_editor(ontology) ``` -------------------------------- ### Get Project Object Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/annotation_import/conversational_LLM_data_generation.ipynb Retrieves a specific Labelbox project using its ID. This project object is required for subsequent upload operations. ```python project = client.get_project(project_id=project_id) ``` -------------------------------- ### Get Batch ID for Project Export Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/exports/export_v1_to_v2_migration_support.ipynb This snippet shows how to define a placeholder for a batch ID, which is required for exporting specific batches from a project. ```python # Find the batch ID by navigating to "Batches" --> "Manage batches" --> "Copy Batch ID" BATCH_ID = "" ``` -------------------------------- ### Create a Labelbox Project Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/conversational_predictions.ipynb Use this to create a new project in Labelbox, specifying the media type. Ensure the ontology is set up correctly for the project. ```python project = client.create_project( name="Conversational Text Prediction Import Demo", media_type=lb.MediaType.Conversational, ) project.setup_editor(ontology) ``` -------------------------------- ### Define Video and Text Annotations Source: https://github.com/labelbox/labelbox-python/blob/develop/examples/prediction_upload/video_predictions.ipynb Examples of defining video object annotations with keyframes and classification subclasses, as well as simple text classification annotations. ```python start=lb_types.Point(x=bbox_dm2["left"], y=bbox_dm2["top"]), # x = left, y = top end=lb_types.Point( x=bbox_dm2["left"] + bbox_dm2["width"], y=bbox_dm2["top"] + bbox_dm2["height"], ), # x= left + width , y = top + height ), ), lb_types.VideoObjectAnnotation( name="bbox_class", keyframe=True, frame=11, segment_index=0, value=lb_types.Rectangle( start=lb_types.Point(x=bbox_dm2["left"], y=bbox_dm2["top"]), # x = left, y = top end=lb_types.Point( x=bbox_dm2["left"] + bbox_dm2["width"], y=bbox_dm2["top"] + bbox_dm2["height"], ), # x= left + width , y = top + height ), classifications=[ lb_types.ClassificationAnnotation( name="checklist_class", value=lb_types.Checklist(answer=[ lb_types.ClassificationAnswer(name="first_checklist_answer") ]), ) ], ), lb_types.VideoObjectAnnotation( name="bbox_class", keyframe=True, frame=13, segment_index=0, value=lb_types.Rectangle( start=lb_types.Point(x=bbox_dm2["left"], y=bbox_dm2["top"]), # x = left, y = top end=lb_types.Point( x=bbox_dm2["left"] + bbox_dm2["width"], y=bbox_dm2["top"] + bbox_dm2["height"], ), # x= left + width , y = top + height ), classifications=[ lb_types.ClassificationAnnotation( name="checklist_class", value=lb_types.Checklist(answer=[ lb_types.ClassificationAnswer( name="second_checklist_answer") ]), ) ], ), ] ``` ```python bbox_dm = {"top": 617, "left": 1371, "height": 419, "width": 505} bbox_annotation = [ lb_types.VideoObjectAnnotation( name="bbox_video", keyframe=True, frame=13, segment_index=0, value=lb_types.Rectangle( start=lb_types.Point(x=bbox_dm["left"], y=bbox_dm["top"]), # x = left, y = top end=lb_types.Point( x=bbox_dm["left"] + bbox_dm["width"], y=bbox_dm["top"] + bbox_dm["height"], ), # x= left + width , y = top + height ), ), lb_types.VideoObjectAnnotation( name="bbox_video", keyframe=True, frame=15, segment_index=0, value=lb_types.Rectangle( start=lb_types.Point(x=bbox_dm["left"], y=bbox_dm["top"]), end=lb_types.Point( x=bbox_dm["left"] + bbox_dm["width"], y=bbox_dm["top"] + bbox_dm["height"], ), ), ), lb_types.VideoObjectAnnotation( name="bbox_video", keyframe=True, frame=19, segment_index=0, value=lb_types.Rectangle( start=lb_types.Point(x=bbox_dm["left"], y=bbox_dm["top"]), end=lb_types.Point( x=bbox_dm["left"] + bbox_dm["width"], y=bbox_dm["top"] + bbox_dm["height"], ), ), ), ] ``` ```python text_annotation = [ lb_types.ClassificationAnnotation( name="free_text", # must match your ontology feature's name value=lb_types.Text(answer="sample text"), ) ] ```