Skip to content

HiddenLayer's SDK documentation

The HiddenLayer platform enhances the developer experience for protecting artificial intelligence (AI) and machine learning (ML) models without needing to write complex code or manage the underlying infrastructure.

The HiddenLayer SDK uses Python to provide a simple and efficient way to interact with the HiddenLayer API. This guide will walk you through how to install and use the HiddenLayer Python SDK to retrieve AI Detect and Response (AIDR) and Model Scanner information.

This SDK can be used to interact with the following HiddenLayer services:

  • Model Scanner
  • AIDR for Predictive Models
Active Development

This project is under active development.

Before You Begin

The following are required for using the HiddenLayer Python SDK:

  • Python (latest version is recommended; this should include pip)
  • HiddenLayer API key and secret; see Create API Key

Install SDK

Install the hiddenlayer-sdk package with pip.

pip install hiddenlayer-sdk

The HiddenLayer Python SDK offers functionality to interact with other services, such as HuggingFace, AWS, etc.

  • To scan models from HuggingFace, install the necessary HuggingFace dependencies via:

    pip install hiddenlayer-sdk[hf]
  • To scan models from AWS, install the necessary AWS dependencies via:

    pip install hiddenlayer-sdk[aws]

Usage Overview

The main client that gets exposed is hiddenlayer.HiddenlayerServiceClient which can be used to interact with all HiddenLayer services exposed via API.

To use the SDK to call an API, first find the API in the Developer Portal.

  • For example, to scan a model, run:

    from hiddenlayer import HiddenlayerServiceClient
    
    hl_client = HiddenlayerServiceClient(
        host="https://api.hiddenlayer.ai",
        api_id=..., # Your Hiddenlayer API Client ID
        api_key=... # Your Hiddenlayer API Secret Key
    )
    
    hl_client.model_scanner.scan_file(
        model_name="name_of_the_model",
        model_path="path/to/model/file.pkl"
    )
  • Then, on the appropriate client, call the corresponding method. All API calls have the form:

    hl_client.<SERVICE>.<METHOD>(<parameters>)

Authentication

To authenticate to HiddenLayer, you have to generate a client ID and secret from the platform UI. See Create API Key.

Once you have those, you can authenticate using the SDK like so:

hl_client = HiddenlayerServiceClient(
    host="https://api.hiddenlayer.ai",
    api_id=..., # Your Hiddenlayer API Client ID
    api_key=... # Your Hiddenalyer API Secret Key
)

Data Models

The Hiddenlayer Python SDK uses Pydantic to represent data for APIs making the code more readable and type-safe, while also making it easier to work with the code.

Specific data models are organized under hiddenlayer.sdk.rest.models.

For more information, consult the Data Models API Reference <api/hiddenlayer.sdk.rest.models.html>_.

Example Usage

The HiddenLayer Python SDK comes with a number of examples demonstrating how to use the library for various common use-cases.

These examples and more are located in the examples directory of the Github repository.

Initiate Client

from hiddenlayer import HiddenlayerServiceClient

hl_client = HiddenlayerServiceClient(
    host="https://api.hiddenlayer.ai",
    api_id=..., # Your Hiddenlayer API Client ID
    api_key=... # Your Hiddenalyer API Secret Key
)

Scanning Models

Scanning a model on disk

from hiddenlayer import HiddenlayerServiceClient

hl_client = HiddenlayerServiceClient(
  host="https://api.hiddenlayer.ai",
  api_id=..., # Your Hiddenlayer API Client ID
  api_key=... # Your Hiddenalyer API Secret Key
)

Scanning a HuggingFace model

Scanning a model on disk

# Scan a model saved locally on disk
scan_results = hl_client.model_scanner.scan_file(
    model_path="./models/example_model.xgb", model_name="sdk_example_model"
)

# View scan results
print(scan_results)

Scanning a Hugging Face model

# Scan a HuggingFace model
huggingface_scan_results = hl_client.model_scanner.scan_huggingface_model(
    repo_id="drhyrum/bert-tiny-torch-vuln",
)

# See if there were any detections
for result in huggingface_scan_results:
    print(result)

AIDR

Submitting vectors to AIDR

.. literalinclude:: ../../examples/mldr.py
  :start-after: [docs_submit_vectors_start]
  :end-before: [docs_submit_vectors_end]