OpenXLab Python SDK

0.1.3 · active · verified Sun Apr 05

OpenXLab is a Python library providing tools to interact with the OpenXLab platform, focusing on managing datasets and models. It includes a command-line interface (CLI) and a Python SDK to programmatically access platform features such as dataset information, upload, and download. The library is currently at version 0.1.3 and appears to have a regular release cadence, with updates often reflecting platform-level changes within the broader OpenXLab ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with OpenXLab using environment variables for Access Key (AK) and Secret Key (SK), and then how to use the `openxlab.dataset.get` function to download a public dataset. Replace 'YOUR_AK_HERE' and 'YOUR_SK_HERE' with your actual credentials, or ideally, set them as environment variables (OPENXLAB_AK, OPENXLAB_SK). The example attempts to download the 'OpenDataLab/MNIST' dataset, which is a common public dataset.

import os
from openxlab.xlab.handler.user_token import get_jwt
from openxlab.dataset import get

# Set your Access Key and Secret Key as environment variables
# These can be obtained from your OpenXLab user center.
# For example: export OPENXLAB_AK="YOUR_AK_HERE"
# For example: export OPENXLAB_SK="YOUR_SK_HERE"
ak = os.environ.get('OPENXLAB_AK', 'YOUR_AK_HERE')
sk = os.environ.get('OPENXLAB_SK', 'YOUR_SK_HERE')

if ak == 'YOUR_AK_HERE' or sk == 'YOUR_SK_HERE':
    print("Please set OPENXLAB_AK and OPENXLAB_SK environment variables or replace placeholders.")
    # Attempt to use CLI config if env vars are not set for demonstration purposes
    # In a real scenario, user would run 'openxlab config' or set env vars.
    # For programmatic access, env vars are preferred.
else:
    try:
        # Authenticate and get a JWT token
        token = get_jwt(ak=ak, sk=sk)
        print(f"Successfully obtained JWT token (first 10 chars): {token[:10]}...")

        # Example: Get (download) a public dataset
        # Replace 'OpenDataLab/MNIST' with a valid dataset repository and adjust target_path
        # Ensure the target_path exists or can be created.
        dataset_repo_name = 'OpenDataLab/MNIST'
        target_download_path = './MNIST_dataset'
        print(f"\nAttempting to download dataset: {dataset_repo_name} to {target_download_path}")
        
        # Create a dummy directory for download if it doesn't exist
        os.makedirs(target_download_path, exist_ok=True)

        # Note: The 'get' function might automatically use configured AK/SK,
        # but explicit authentication often precedes other SDK calls.
        get(dataset_repo=dataset_repo_name, target_path=target_download_path)
        print(f"Dataset '{dataset_repo_name}' downloaded to '{target_download_path}' successfully.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure your AK/SK are correct and you have network access to OpenXLab.")

view raw JSON →