Running apps with the AI Hub SDK

With the AI Hub software development kit (SDK), you can create and integrate an end-to-end solution. From uploading files to running an app, you can chain these API calls together using a script for a complete workflow. This guide includes a complete script you can reference to create your own complete workflow.

Note

While the script in this guide uses the AI Hub Python SDK, you can use the AI Hub API’s Batches and App runs endpoints to do the same in any scripting language.

Before you begin

Uploading files to a batch

You can create a batch of files to use as input for your app run. In AI Hub, a batch is a user-defined group of files. You can add and remove files from the batch, but the batch itself has a constant ID, so you can easily and repeatedly use all files in the batch as an app’s input.

For more information, see the Batches endpoint.

Script excerpt

This section of the script creates a batch and uploads the listed files to it.

# Creates a batch
batch = client.batches.create(name='<BATCH-NAME>')

# Adds files to the batch
file_paths = ['<inner/folder/sample1.pdf>', '<.../sample2.docx>', '<.../sample3.png>']
for file_path in file_paths:
    with open(file_path, "rb") as file:
        client.batches.add_file(id=batch.id, file_name=os.path.basename(file_path), file=file)

Define the following values in the script:

Parameter or variable Type Required Description
<BATCH-NAME> string Yes Name of the batch. Maximum length is 255 characters.
file_paths list Yes A list of local file paths of files to upload to the batch.

Processing 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 run results.

For more information, see the App runs endpoint.

Script excerpt

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

run = client.apps.runs.create(app_name='<APP-NAME>', owner=None, batch_id=batch.id)

# Continuously checks the app run status until it's done
while True:
    status = client.apps.runs.status(run.id)
    if status.status == 'COMPLETE':
        break
    time.sleep(5)  # Polling interval

# Gets the results of the app run
results = client.apps.runs.results(run.id)

Define the following values in the script:

Parameter or variable Type Required Description
<APP-NAME> string Yes The name of a prebuilt or custom AI Hub app through which you want to process the uploaded files.
owner string No The account that generated the app. If left as None, defaults to your AI Hub username. For custom AI Hub apps created by you, accept the default. For public AI Hub apps published by Instabase, specify instabase.
Note

You don’t need to define the batch.id or run.id values in this section of the script, as they’re passed through from the results of the previous step.

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

Complete workflow

This example Python script shows a complete, end-to-end workflow based on calls to AI Hub API using the AI Hub SDK. When run, this script performs the following tasks:

  • Initializes the API client.

  • Creates a batch object and uploads files to the batch.

  • Runs the specified app using the batch as input.

  • Automatically polls the app run’s status for completion.

  • Returns the app run’s results in JSON format. See the Run results endpoint for an example of app run results.

Complete script

from aihub import AIHub
import os
import time

# Initializes the client. Define your API token, API root URL, and IB-Context header value.
client = AIHub(api_key="<API-TOKEN>", api_root="<API-ROOT>", ib_context="<IB-CONTEXT>")

# Creates a batch and adds files.
batch = client.batches.create(name='<BATCH-NAME>')
file_paths = ['<inner/folder/sample1.pdf>', '<.../sample2.docx>', '<.../sample3.png>']
for file_path in file_paths:
    with open(file_path, "rb") as file:
        client.batches.add_file(id=batch.id, file_name=os.path.basename(file_path), file=file)

# Runs an app and gets the results when the app run is complete.
run = client.apps.runs.create(app_name='<APP-NAME>', owner=None, batch_id=batch.id)

# Continuously checks the run status until it's done.
while True:
    status = client.apps.runs.status(run.id)
    if status.status == 'COMPLETE':
        break
    time.sleep(5)  # Polling interval

results = client.apps.runs.results(run.id)

User-defined values

To recap, this table outlines all values in the complete script that can or must be defined.

Parameter or variable Type Required Description
<API-TOKEN> string Yes Your API token. Used when initializing the API client.
<API-ROOT> string No Defaults to https://aihub.instabase.com/api. Used when initializing the API client.
<IB-CONTEXT> string No Defaults to your user ID. If undefined, requests are made with your community account. Used when initializing the API client.
<BATCH-NAME> string Yes Name of the batch. Maximum length is 255 characters.
file_paths list Yes A list of local file paths of files to upload to the batch.
<APP-NAME> string Yes The name of a prebuilt or custom AI Hub app through which you want to process the uploaded files.
owner string No The account that generated the app. If left as None, defaults to your AI Hub username. For custom AI Hub apps created by you, accept the default. For public AI Hub apps published by Instabase, specify instabase.