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.
Import modules and initialize the client
Import key Python modules.
Initialize a client that lets you interact with the API through Python objects and methods.
Parameter reference
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()
.
Parameter reference
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.
Parameter reference
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:
-
Create a batch object and upload files to the batch.
-
Run an app using the batch as input.
-
Poll the app run status until the run is complete.
-
Return app run results as a Python dictionary. See the get run results endpoint for an example of app run results.