AssetOrchestrator Python Client

Create, List, Delete, and Get assets using the Python SDK

For a quick glance at features for the AssetOrchestrator Client, please see the AssetOrchestrator reference docs.

The AssetOrchestrator client in the Python SDK allows create, list, get, and delete actions of assets. These assets allow integration with the ImageGenerator Client to generate more customized images.

This guide will walk you through using this API to see a list of our public assets, create your own asset, and use your asset to generate an image.

Requirements

Quick Overview of AssetOrchestrator API

from octoai.clients.asset_orch import AssetOrchestrator

if __name__ == "__main__":
  # If you have an OCTOAI_TOKEN set as an environment variable, you do not need to pass a token.
  # If one is not set, you can use:
  # asset_orch = AssetOrchestrator(token="your OctoAI API Token goes here")
  asset_orch = AssetOrchestrator()
  # You can get a list of the public OctoAI assets
  print(asset_orch.list(is_public=True, owner="octoai"))
  # You can get a specific asset, either one you created or in this example an OctoAI asset
  asset = asset_orch.get(is_public=True, owner="octoai", name="product_photography_v1")
  # And also create, delete, or use those assets to generate images as in the below example.
  asset_orch.delete("asset_id_goes_here")

Creating a LoRA and Generating an Image

You will need a safetensors file in order to use this example, and in our case one is named origami-paper.safetensors. I'll be using a lora trained on origami that I can use with the words "origami" and "paper".

In this example, we will be adding a LoRA then using it to generate an image. You can also add checkpoints, vae, and textual inversions.

You can check the reference guide for more information about the returned Asset object or options for the create method.

from octoai.clients.asset_orch import LoraData, AssetOrchestrator
from octoai.clients.image_gen import ImageGenerator

if __name__ == "__main__":
    # OCTOAI_TOKEN set as an environment variable so do not need to pass a token.
    asset_orch = AssetOrchestrator()
    image_gen = ImageGenerator()
    
    asset_name = "origami-paper-test"
    # There is also TextualInversionData, VAEData, and CheckpointData.
    lora_data = LoraData(
        data_type="fp16",
        engine="image/stable-diffusion-v1-5",
        file_format="safetensors",
    )

    asset = asset_orch.create(
        file="origami-paper.safetensors",
        data=lora_data,
        name=asset_name,
        description="origami-paper stable diffusion 1.5",
    )

    image_gen_resp = image_gen.generate(
        engine="sd",
        prompt="rainbow origami tailong dragon",
        num_images=4,
        loras={asset: 0.8}
    )
    
    # Some images can be removed for safety.
    # Please see the ImageGenerator client docs for more information.
    for image in image_gen_resp.images:
      image.to_pil().show()

    # You can clean up your asset with the following:
    asset_orch.delete(asset.id)
astropus.png

rainbow-origami-tailong-dragon.png

Creating File Assets from a Folder of Images

Let's say you have a folder of images assets you would like to upload for using the FineTuning service. You can do so using the below code snippet to get all the files in your folder named images, and then splitting on the . to get your file_format extension (jpg, jpeg, or png), and use the file name as the asset name.

In this example, there is a directory named images that contains files with a _, -, and alphanumeric file names, jpg, jpeg, or png suffixes. In this example, the folder contains the following:

./images/
	result0.jpg
	result1.jpg
	result2.jpg
	result3.jpg
	save_the_other_paper_poodle.png
	save_the_paper_poodle.png
import os
from octoai.clients.asset_orch import AssetOrchestrator, FileData

if __name__ == "__main__":
    # OCTOAI_TOKEN set as an environment variable so do not need to pass a token.
    asset_orch = AssetOrchestrator()

    dir_path = "./images/"  # Set your dir_path here to your file assets.
    files = []
    # Get a list of files in the folder
    for file_path in os.listdir(dir_path):
        if os.path.isfile(os.path.join(dir_path, file_path)):
            files.append(file_path)
    for file in files:
        # Use the file names to get file_format and the asset_name.
        split_file_name = file.split(".")
        asset_name = split_file_name[0]
        file_format = split_file_name[1]
        file_data = FileData(
            file_format=file_format,
        )
        asset = asset_orch.create(
            file=dir_path + file,
            data=file_data,
            name=asset_name,
        )

You can then use asset_orch.list() to see the assets have been created and uploaded and a result that looks something like:

[
id: asset_01234567891011121314151617, name: save_the_paper_poodle, status: ready, 
id: asset_01234567891011121314151618, name: save_the_other_paper_poodle, status: ready, 
id: asset_01234567891011121314151619, name: result2, status: ready, 
id: asset_01234567891011121314151620, name: result3, status: ready, 
id: asset_01234567891011121314151621, name: result1, status: ready, 
id: asset_01234567891011121314151622, name: result0, status: ready, 
id: asset_01234567891011121314151600, name: origami-paper-test, status: uploaded]

In this example, an already existing lora created in the previous example also exists. The lora has uploaded however will likely need a few more seconds before being ready for use.