Media Gen SolutionManaging custom assets with Asset Library

Asset Library in the Python SDK

Manage assets using the Python SDK.

The AssetLibrary 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

Overview of AssetLibrary API

Python
1from octoai.client import OctoAI
2
3if __name__ == "__main__":
4 # If you have an OCTOAI_TOKEN set as an environment variable, you do not need to pass a token.
5 # If one is not set, you can use:
6 # asset_library = AssetLibrary(token="your OctoAI API Token goes here")
7 client = OctoAI()
8 asset_library = client.asset_library
9 # You can get a list of the public OctoAI assets
10 print(asset_library.list(is_public=True, owner="octoai"))
11 # You can get a specific asset, either one you created or in this example an OctoAI asset
12 asset = asset_library.get("octoai:product_photography_v1")
13 # And also create, delete, or use those assets to generate images as in the below example.
14 asset_library.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.

Python
1from octoai.client import OctoAI
2from octoai.asset_library import Data_Lora
3from octoai.util import to_file
4
5if __name__ == "__main__":
6 # OCTOAI_TOKEN set as an environment variable so do not need to pass a token.
7 client = OctoAI()
8 asset_library = client.asset_library
9 image_gen = client.image_gen
10
11 asset_name = "origami-paper-test"
12 # There is also TextualInversionData, VAEData, and CheckpointData.
13 lora_data = Data_Lora(
14 data_type="fp16",
15 engine="image/stable-diffusion-v1-5",
16 file_format="safetensors",
17 )
18
19 asset = asset_library.create_from_file(
20 file="origami-paper.safetensors",
21 data=lora_data,
22 name=asset_name,
23 description="origami-paper stable diffusion 1.5",
24 )
25
26 image_gen_resp = image_gen.generate_sd(
27 prompt="rainbow origami tailong dragon",
28 num_images=4,
29 loras={"asset": 0.8}
30 )
31
32 # Some images can be removed for safety.
33 # Please see the ImageGenerator client docs for more information.
34 for i, image in enumerate(image_gen_resp.images):
35 to_file(image, f"result{i}.jpg")
36
37 # You can clean up your asset with the following:
38 asset_library.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:

./assets/images/
result0.jpg
result1.jpg
result2.jpg
result3.jpg
save_the_other_paper_poodle.png
save_the_paper_poodle.png
Python
1import os
2from octoai.client import OctoAI
3from octoai.asset_library import Data_File
4
5if __name__ == "__main__":
6 # OCTOAI_TOKEN set as an environment variable so do not need to pass a token.
7 client = OctoAI()
8
9 dir_path = "./assets/images/" # Set your dir_path here to your file assets.
10 files = []
11 # Get a list of files in the folder
12 for file_path in os.listdir(dir_path):
13 if os.path.isfile(os.path.join(dir_path, file_path)):
14 files.append(file_path)
15 for file in files:
16 # Use the file names to get file_format and the asset_name.
17 split_file_name = file.split(".")
18 asset_name = split_file_name[0]
19 file_format = split_file_name[1]
20 file_data = Data_File(
21 file_format=file_format,
22 )
23 asset = client.asset_library.create_from_file(
24 file=dir_path + file,
25 data=file_data,
26 name=asset_name,
27 )

You can then use octoai.asset_library.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.