#!/usr/bin/env python3
import requests
import json
import time
import sys
# Configuration
TOKEN = "your_bearer_token"
API = "https://api.artosai.com"
ORG_ID = "your_org_id"
headers = {
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
}
def log_step(step, message):
print(f"\n{'='*50}")
print(f"Step {step}: {message}")
print('='*50)
def log_success(message):
print(f"✓ {message}")
def log_error(message):
print(f"✗ {message}")
sys.exit(1)
def upload_file(filename, filepath, container):
"""Upload a file to S3."""
url = f"{API}/api/v1/files/upload"
upload_headers = {"Authorization": f"Bearer {TOKEN}"}
files = {
"file_name": (None, filename),
"container": (None, container),
"file_content": open(filepath, "rb")
}
response = requests.post(url, headers=upload_headers, files=files)
if response.status_code == 200:
log_success(f"{filename} uploaded")
else:
log_error(f"Failed to upload {filename}: {response.json().get('detail')}")
# Step 1: Upload Documents
log_step(1, "Uploading Documents")
upload_file("csr_template.docx", "csr_template.docx", "templates")
upload_file("example_csr_2024.docx", "example_csr_2024.docx", "documents")
upload_file("example_csr_2023.docx", "example_csr_2023.docx", "documents")
# Step 2: Generate Template from Examples
log_step(2, "Generating Template from Examples")
template_data = {
"template_filename": "csr_template.docx",
"example_filenames": ["example_csr_2024.docx", "example_csr_2023.docx"],
"name": "CSR Template 2024",
"description": "Auto-generated from example documents",
"document_type": "CSR",
}
response = requests.post(f"{API}/api/v1/templates/generate", headers=headers, json=template_data)
if response.status_code not in [200, 202]:
log_error(f"Template generation failed: {response.json().get('detail')}")
result = response.json()
template_id = result.get('template_id') or result.get('id')
log_success(f"Template generated: {template_id}")
# Step 3: Upload Source Documents
log_step(3, "Uploading Source Documents")
upload_file("protocol.pdf", "protocol.pdf", "documents")
upload_file("safety_report.pdf", "safety_report.pdf", "documents")
# Step 4: Create Document Set
log_step(4, "Creating Document Set")
docset_data = {"document_set_name": "Q1 2024 CSR"}
response = requests.post(f"{API}/api/v1/document-sets/", headers=headers, json=docset_data)
if response.status_code != 201:
log_error(f"Document set creation failed: {response.json().get('detail')}")
docset_id = response.json()['document_set_id']
log_success(f"Document set created: {docset_id}")
# Step 5: Request Document Generation
log_step(5, "Requesting Document Generation")
gen_data = {
"document_type": "CSR",
"file_paths": [
f"input/protocol.pdf",
f"input/safety_report.pdf"
],
"document_set_key": "random-uuid",
"document_set_name": "Q1 2024 CSR",
"template_id": template_id,
"output_name": "CSR_Final.docx"
}
response = requests.post(f"{API}/api/v1/documents/generate", headers=headers, json=gen_data)
if response.status_code != 202:
log_error(f"Generation request failed: {response.json().get('detail')}")
task_id = response.json()['task_id']
log_success(f"Generation request accepted: {task_id}")
# Step 6: Poll Status Until Complete
log_step(6, "Monitoring Generation Progress")
while True:
response = requests.get(f"{API}/api/v1/documents/status/{task_id}", headers=headers)
status = response.json()['status']
print(f"Status: {status}")
if status == "Complete":
log_success("Generation complete!")
doc_id = task_id
break
elif status == "Failed":
log_error(f"Generation failed: {response.json().get('error')}")
time.sleep(5)
# Step 7: Retrieve Document Details
log_step(7, "Retrieving Document Details")
response = requests.get(f"{API}/api/v1/documents/{doc_id}", headers=headers)
doc = response.json()['document']
log_success("Document retrieved")
print(f" Document ID: {doc['document_id']}")
print(f" Type: {doc['document_type']}")
print(f" Status: {doc['status']}")
print(f" Output: {doc['output_name']}")
log_step("Complete", "Workflow Complete")
print(f"Document ID: {doc_id}")
print("Ready for download or further processing")