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.
Before you begin
Review the developer quickstart and install the AI Hub SDK.
Import modules and initialize the client
The first step of using the SDK is to import key Python modules.
Next, initialize a client that lets you interact with the AI Hub API. For information on what values to use for the parameters, see the Developer quickstart.
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()
.
User-defined values
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 run 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.
User-defined values
batch.id
or run.id
values in this section of the script, because they’re passed through from the results of the previous step.For more information about customizing an app run, including using webhooks, see the runs endpoint.
Complete script
This script demonstrates a complete, end-to-end workflow using the AI Hub SDK. It combines all the snippets from above.
The script performs these tasks:
-
Create a batch object and upload files to the batch.
-
Run the specified app using the batch as input.
-
Poll the app run status until the run is complete.
-
Return app run results as an object that can be converted into a Python dictionary. See the get run results endpoint for an example of app run results.