0% found this document useful (0 votes)
37 views

Instabase AI Hub - API to run apps

Instabase AI Hub introduces a private preview for running AI Hub apps via API, allowing users to upload files, process them, and download results programmatically. The document provides setup instructions, including obtaining an API token, running an app, checking job status, and downloading results. Additionally, it outlines how to upload files to an input folder and includes a raw code snippet for end-to-end execution.

Uploaded by

rop.e.nh.art.o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Instabase AI Hub - API to run apps

Instabase AI Hub introduces a private preview for running AI Hub apps via API, allowing users to upload files, process them, and download results programmatically. The document provides setup instructions, including obtaining an API token, running an app, checking job status, and downloading results. Additionally, it outlines how to upload files to an input folder and includes a raw code snippet for end-to-end execution.

Uploaded by

rop.e.nh.art.o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Run AI Hub Apps with API

Private Preview Only

Thanks for using Instabase AI Hub. We’re excited to extend a private preview of our new
capability to allow running AI Hub apps using an API. The APIs allow you to upload files to
Instabase, run an AI Hub app to process them, and download the processed results
programmatically.

We’ve put together some docs here to get you started. Please reach out to us at
aihub@instabase.com if you have any questions or feedback.

How to set up call AI Hub APIs


To get started, you will need to set up AI Hub APIs by defining your API token, the base URL for
your requests.

You should have received an API token for your account, and your username from us. In case
you haven’t, or you need to rotate it, please reach out to aihub@instabase.com. In the future,
we will allow you to manage API tokens in the AI Hub UI.

Python
import requests
import time

#Please enter your username and a unique folder name to upload your docs.
API_TOKEN = 'ADD-YOUR-API-TOKEN-HERE'

API_BASE_URL = 'https://aihub.instabase.com/api'
API_HEADERS = {
'Authorization': f'Bearer {API_TOKEN}'
}

How to start a new AI Hub app run


To run an AI Hub app, you can use the `/v2/zero-shot-idp/projects/app/run` endpoint. You
need to specify the following key pieces of information:
● An input directory → An Instabase directory of files that you want your app to process.
For more information about how to upload your files to Instabase, please see the below
section.
● App name → The name of your Instabase app.
● Owner (optional) → By default, the owner is considered to be the account which
generated the API key you’re using. If you’ve created your AI Hub app with this account,
you can leave this out. If you are trying to run a public AI Hub app, please specify the
owner as “instabase”.

Python
APP_NAME = 'EXAMPLE-APP-NAME' #Please enter your app name

# Step 1: Run App


run_app_payload = json.dumps({
"input_dir": input_file_dir,
"name": APP_NAME
# optional: "owner": "<owner-name", if not specified, assumes it is your
username
})

url = f'{API_BASE_URL}/v2/zero-shot-idp/projects/app/run'
run_app_resp = requests.post(url, headers=API_HEADERS, data=run_app_payload)

job_id = run_app_resp.json().get('job_id')
print(f'App Running: {job_id}')
return job_id

How to check run status


Once you’ve started your app run, you can use the `/v1/jobs/status` to check job status. You
should poll this endpoint until the job is complete. Once the job is successful, you can use the
following section to download your app run results. You can review our job status API docs for
more information.
Python
# Step 2: Poll Job until Complete
url = f'{API_BASE_URL}/v1/jobs/status?type=flow&job_id={job_id}'

job_status = ''
while job_status != 'DONE':
time.sleep(5)
job_status_resp = requests.get(url, headers=API_HEADERS)
job_status = job_status_resp.json().get('state')
print(f'Job Status: {job_status}')

How to download your app run results


Once the app run is complete, you can use the '{API_BASE_URL}/v1/flow_binary/results'
endpoint to download your app run results as a json. You can review our job results API docs for
more information.

Python
# Now the job is complete, get the results
output_folder_path = job_status_resp.json()['results'][0]['output_folder']

results_payload = json.dumps({
"file_offset": 0,
"ibresults_path": f'{output_folder_path}/batch.ibflowresults'
})

url = f'{API_BASE_URL}/v1/flow_binary/results'
results_resp = requests.post(url, headers=API_HEADERS, data=results_payload)

return results_resp.json()

We are working on extending support to export these app run results as a CSV, or an Excel file.

How to upload files to an input folder


In the above sections, we assumed that you had an input folder of files uploaded to Instabase.
To upload an input folder, you can either upload files to a Google Drive or S3 bucket that you
have mounted to Instabase.

Or you can use the below instructions to upload files to your Instabase drive. You can review
docs for our filesystem APIs for more information.
Python
FILENAMES = ["sample1.pdf", "sample2.docx", "sample3.png"] # Please enter the
paths of files to upload as a list
IB_INPUT_FILE_DIR = 'USERNAME/my-repo/fs/Instabase Drive/INPUT_FOLDER/' #
Please enter your username and a unique name for the input folder
# When you add input files to folder; a folder will be created if it does not
exist
def read_input_file(filepath):
try:
with open(filepath, 'rb') as input_file:
input_file_data = input_file.read()

return input_file_data
except:
print(f"Unable to find file at {filepath}")

def upload_input_file (input_file_dir, input_file_data, input_file_name):


print("Uploading File...")
url = f'{API_BASE_URL}/v2/files/{IB_INPUT_FILE_DIR}{input_file_name}'

upload_resp = requests.put(url, headers=API_HEADERS, data=input_file_data)


if upload_resp.status_code >= 200 and upload_resp.status_code < 300:
print(f'File successfully uploaded at {input_file_dir}')

return upload_resp

input_filepaths = FILENAMES
for input_filepath in input_filepaths:
input_file_data = read_input_file(input_filepath)
# TODO: parse out file name from input_filepath if necessary
upload_input_file(IB_INPUT_FILE_DIR, input_file_data, input_filepath)

Appendix
Raw snippet for e2e running of an AI Hub app

Python
import requests
import time
import json

APP_NAME = 'APP NAME' # Please enter your app name


IB_INPUT_FILE_DIR = 'USERNAME/my-repo/fs/Instabase Drive/INPUT_FOLDER/' # Please
enter your username and a unique name for the input folder
API_TOKEN = 'TOKEN' # Please enter your API token
FILENAMES = ["sample1.pdf", "sample2.docx", "sample3.png"] # Please enter the
paths of files to upload as a list

API_BASE_URL = 'https://aihub.instabase.com/api'
API_HEADERS = {
'Authorization': f'Bearer {API_TOKEN}'
}

def read_input_file(filepath):
try:
with open(filepath, 'rb') as input_file:
input_file_data = input_file.read()

return input_file_data
except:
print(f'Unable to find file at {filepath}')

def upload_input_file(input_file_dir, input_file_data, input_file_name):


# Add input files to folder; a folder will be created for you if it doesn't
exist already.
print('Uploading File...')
url = f'{API_BASE_URL}/v2/files/{IB_INPUT_FILE_DIR}{input_file_name}'

upload_resp = requests.put(url, headers=API_HEADERS, data=input_file_data)


if upload_resp.status_code >= 200 and upload_resp.status_code < 300:
print(f'File successfully uploaded at {input_file_dir}')

return upload_resp

def run_app(input_file_dir, app_name):


# Run App passing in the App Name and the directory to process.
print('Running App')
run_app_payload = json.dumps({
"input_dir": input_file_dir,
"name": APP_NAME,
# optional: "owner": "<owner-name", if not specified, assumes it is your
username
})

url = f'{API_BASE_URL}/v2/zero-shot-idp/projects/app/run'
run_app_resp = requests.post(url, headers=API_HEADERS, data=run_app_payload)
job_id = run_app_resp.json().get('job_id')
print(f'App Running: {job_id}')
return job_id

def get_results(job_id):
# Using the Job ID from the app run get the current status. If complete,
get the results.
url = f'{API_BASE_URL}/v1/jobs/status?type=flow&job_id={job_id}'

job_status = ''
while job_status != 'DONE':
time.sleep(5)
job_status_resp = requests.get(url, headers=API_HEADERS)
job_status = job_status_resp.json().get('state')
print(f'Job Status: {job_status}')

print('Downloading Results')

# Now the job is complete, get the results


output_folder_path = job_status_resp.json()['results'][0]['output_folder']

results_payload = json.dumps({
"file_offset": 0,
"ibresults_path": f'{output_folder_path}/batch.ibflowresults'
})

url = f'{API_BASE_URL}/v1/flow_binary/results'
results_resp = requests.post(url, headers=API_HEADERS, data=results_payload)

return results_resp.json()

input_filepaths = FILENAMES
for input_filepath in input_filepaths:
input_file_data = read_input_file(input_filepath)
upload_input_file(IB_INPUT_FILE_DIR, input_file_data, input_filepath)
job_id = run_app(IB_INPUT_FILE_DIR, APP_NAME)
results = get_results(job_id)
print(results)

You might also like