Run apps by SDK

With the AI Hub software development kit (SDK), you can write a Python script to create an end-to-end solution and integrate it with existing upstream and downstream workflows. From uploading files to running an app, you can use the SDK to create a complete AI Hub document-processing solution.

This guide includes a complete script to use as a model for creating your own workflow.

The script in this guide uses the AI Hub SDK, but you can use the AI Hub API instead. For guidance, see the batches and runs endpoints, or try out the API runner tool in the AI Hub interface.

Before you begin

Learn about the API and SDK and install the SDK.

1

Import modules and initialize the client

Import key Python modules.

1import os
2import time
3
4from aihub import AIHub

Initialize a client that lets you interact with the API through Python objects and methods.

1# initialize the SDK client
2client = AIHub(api_root=<API-ROOT>,
3 api_key=<API-TOKEN>,
4 ib_context=<IB-CONTEXT>)

Parameter reference

ParameterTypeRequiredDescription
api_rootstrNoThe root URL of your AI Hub API.

For community accounts
• Omit setting api_root.

For organization accounts
• If your organization has a custom AI Hub domain, use your organization’s root API URL, such as https://my-org.instabase.com/api.
• If your organization doesn’t have a custom AI Hub domain, omit setting api_root.
api_keystrYesYour API token.
ib_contextstrNo, but recommendedThe value for the IB-Context header that the SDK includes with all API requests.

For community accounts
• Omit setting ib_context.

For commercial accounts
• To use your personal account, set to your user ID.
• To use your organization account, set to your organization ID.
If ib_context isn’t explicitly set, requests use consumption units from your community account.
2

Upload files to a batch

You can create a batch of files to use as input for your app run. You can add and remove files from the batch, but the batch ID remains constant so you can repeatedly use all files in the batch as app input. For more information, see the batches endpoint.

Below the code you wrote to initialize the client, create a batch with client.batches.create() and upload files to it with client.batches.add_file().

1# create a batch
2batch = client.batches.create(name=<BATCH-NAME>)
3
4# add files to the batch
5file_paths = [<PATH/TO/FILE1>, <PATH/TO/FILE2>]
6for path in file_paths:
7 with open(file_path, 'rb') as file:
8 client.batches.add_file(batch_id=batch.id,
9 file_name=os.path.basename(path),
10 file=file)

Parameter reference

ParameterTypeRequiredDescription
namestrYesUser-generated name of the batch. Maximum length is 255 characters.
file_pathslist[str]YesPaths to one or more local files to upload to the batch. Each path is a separate string. All paths are collected in a list, even if there’s only one path.
3

Process files through an AI Hub app

After uploading your files to a batch, you can process them using an AI Hub app. With the SDK, you can run the app, check the status of your app run, and when the run is complete, get the results. For more information, see the runs endpoint.

This section of the script runs the app with the batch you created as input, checks the status of the app run, and gets the results.

1run = client.apps.runs.create(app_name=<APP-NAME>,
2 owner=<OWNER>,
3 batch_id=batch.id)
4
5# repeatedly check run status until run is complete
6status = client.apps.runs.status(run.id)
7while status.status == 'RUNNING':
8 time.sleep(5) # polling interval in seconds
9 status = client.apps.runs.status(run.id)
10
11# get app run results
12results = client.apps.runs.results(run.id)

Parameter reference

ParameterTypeRequiredDescription
app_namestrYesThe name of the AI Hub app to process the files in the batch.
ownerstrNoThe account that generated the app.

For apps you created: omit setting owner.
For apps published by Instabase: specify instabase.

For more information about customizing an app run, including using webhooks, see the runs endpoint.

Complete script

This script combines all the snippets from above and contains all the code needed to run an app using the SDK.

The script performs these tasks:

  1. Initialize the API client.

  2. Create a batch object and upload files to the batch.

  3. Run an app using the batch as input.

  4. Poll the app run status until the run is complete.

  5. Return app run results as a Python dictionary. See the get run results endpoint for an example of app run results.

1import os
2import time
3
4from aihub import AIHub
5
6# initialize the SDK client
7client = AIHub(api_root=<API-ROOT>,
8 api_key=<API-TOKEN>,
9 ib_context=<IB-CONTEXT>)
10
11# create a batch
12batch = client.batches.create(name=<BATCH-NAME>)
13
14# add files to the batch
15file_paths = [<PATH/TO/FILE1>, <PATH/TO/FILE2>]
16for path in file_paths:
17 with open(file_path, 'rb') as file:
18 client.batches.add_file(batch_id=batch.id,
19 file_name=os.path.basename(path),
20 file=file)
21
22run = client.apps.runs.create(app_name=<APP-NAME>,
23 owner=<OWNER>,
24 batch_id=batch.id)
25
26# repeatedly check run status until run is complete
27status = client.apps.runs.status(run.id)
28while status.status == 'RUNNING':
29 time.sleep(5) # polling interval in seconds
30 status = client.apps.runs.status(run.id)
31
32# get app run results
33results = client.apps.runs.results(run.id)

Parameter reference

ValueTypeRequiredDescription
api_rootstrNoThe root URL of your AI Hub API.

For community accounts
• Omit setting api_root.

For organization accounts
• If your organization has a custom AI Hub domain, use your organization’s root API URL, such as https://my-org.instabase.com/api.
• If your organization doesn’t have a custom AI Hub domain, omit setting api_root.
api_keystrYesYour API token.
ib_contextstrNo, but recommendedThe value for the IB-Context header that the SDK includes with all API requests.

For community accounts
• Omit setting ib_context.

For commercial accounts
• To use your personal account, set to your user ID.
• To use your organization account, set to your organization ID.
If ib_context isn’t explicitly set, requests use consumption units from your community account.
namestrYesUser-generated name of the batch. Maximum length is 255 characters.
file_pathslist[str]YesPaths to one or more local files to upload to the batch. Each path is a separate string. All paths are collected in a list, even if there’s only one path.
app_namestrYesThe name of the AI Hub app to process the files in the batch.
ownerstrNoThe account that generated the app.

For apps you created: omit setting owner.
For apps published by Instabase: specify instabase.
Was this page helpful?