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.

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. The AI Hub interface also includes an API runner tool you can use to generate end-to-end code samples for running an app or deployment by API.

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.

1# Creates a batch
2batch = client.batches.create(name='<BATCH-NAME>')
3
4# Adds files to the batch
5file_paths = ['<inner/folder/sample1.pdf>', '<.../sample2.docx>', '<.../sample3.png>']
6for file_path in file_paths:
7 with open(file_path, "rb") as file:
8 client.batches.add_file(batch_id=batch.id, file_name=os.path.basename(file_path), file=file)

Define the following values in the script:

Parameter or variableTypeRequiredDescription
<BATCH-NAME>stringYesName of the batch. Maximum length is 255 characters.
file_pathslistYesA 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.

1run = client.apps.runs.create(app_name='<APP-NAME>', owner=None, batch_id=batch.id)
2
3# Get status
4status = client.apps.runs.status(run.id)
5
6# Continuously checks the run status until it's done.
7while status.status == 'RUNNING':
8 time.sleep(5) # Polling interval
9 status = client.apps.runs.status(run.id)
10
11# Gets the results of the app run
12results = client.apps.runs.results(run.id)

Define the following values in the script:

Parameter or variableTypeRequiredDescription
<APP-NAME>stringYesThe name of a prebuilt or custom AI Hub app through which you want to process the uploaded files.
ownerstringNoThe 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.
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

1from aihub import AIHub
2import os
3import time
4
5# Initializes the client. Define your API token, API root URL, and IB-Context header value.
6client = AIHub(api_key="<API-TOKEN>", api_root="<API-ROOT>", ib_context="<IB-CONTEXT>")
7
8# Creates a batch and adds files.
9batch = client.batches.create(name='<BATCH-NAME>')
10file_paths = ['<inner/folder/sample1.pdf>', '<.../sample2.docx>', '<.../sample3.png>']
11for file_path in file_paths:
12 with open(file_path, "rb") as file:
13 client.batches.add_file(batch_id=batch.id, file_name=os.path.basename(file_path), file=file)
14
15# Runs an app and gets the results when the app run is complete.
16run = client.apps.runs.create(app_name='<APP-NAME>', owner=None, batch_id=batch.id)
17
18# Get status
19status = client.apps.runs.status(run.id)
20
21# Continuously checks the run status until it's done.
22while status.status == 'RUNNING':
23 time.sleep(5) # Polling interval
24 status = client.apps.runs.status(run.id)
25
26results = 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 variableTypeRequiredDescription
<API-TOKEN>stringYesYour API token. Used when initializing the API client.
<API-ROOT>stringNoDefaults to https://aihub.instabase.com/api. Used when initializing the API client.
<IB-CONTEXT>stringNoDefaults to your user ID. If undefined, requests are made with your community account. Used when initializing the API client.
<BATCH-NAME>stringYesName of the batch. Maximum length is 255 characters.
file_pathslistYesA list of local file paths of files to upload to the batch.
<APP-NAME>stringYesThe name of a prebuilt or custom AI Hub app through which you want to process the uploaded files.
ownerstringNoThe 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.
Was this page helpful?