Instabase AI Hub - API to run apps
Instabase AI Hub - API to run apps
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.
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}'
}
Python
APP_NAME = 'EXAMPLE-APP-NAME' #Please enter your app name
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
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}')
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.
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}")
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
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}')
return upload_resp
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')
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)