PDF AI
Ask, fill, detect fields, suggest comments, check numbers, classify and edit a PDF with the Indox SDK. Every call spends credits, so they run only with RUN_PAID_CALLS = True; each returns a job you poll with pdf.ai.job.
Config
Add your settings to the repo .env (local) or Colab Secrets — this cell reads them with get(...):
INDOX_API_KEY (your ak_… key) and INDOX_BASE_URL (https://indox.org; the SDK adds /api/v1/… itself). A missing key stops here with its name.
import json, logging, re, sys, tempfile
from pathlib import Path
REPO = Path.cwd().parents[1]
sys.path[:0] = [str(REPO), str(REPO / "languages" / "python")]
try:
from google.colab import userdata
get = userdata.get
except ImportError:
from tools.env import require_env as get
from indox_client import Indox
logging.basicConfig(format="→ %(message)s", force=True)
logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG)
def _mask(record):
record.msg = re.sub(r"/(public|sign)/[^/ ]+", r"/\1/***", record.getMessage())
record.args = ()
return True
logging.getLogger("urllib3.connectionpool").addFilter(_mask)
def show(data, limit=2500):
print(json.dumps(data, indent=2, default=str)[:limit])
client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_BASE_URL"))
start_credits = client.billing.credits_balance()["credit_amount"]
SAMPLES = REPO / "colab"
out = Path(tempfile.mkdtemp())
pdf = SAMPLES / "pdf" / "samples" / "bitcoin.pdf"
RUN_PAID_CALLS = Falseimport time
def finish(job):
while job["status"] not in ("done", "failed"):
time.sleep(2)
job = client.pdf.ai.job(job["job_id"])
show(job)
assert job["status"] == "done", job.get("error")
return jobif RUN_PAID_CALLS:
answer = finish(client.pdf.ai.ask("What problem does this paper solve?", file_path=pdf))
else:
print("skipped: pdf.ai.ask (RUN_PAID_CALLS = False)")if RUN_PAID_CALLS:
form = client.pdf.forms.fill("w9", {}, out / "w9.pdf")
filled = finish(client.pdf.ai.fill(form))
detected = finish(client.pdf.ai.detect_fields(form, page=1))
else:
print("skipped: pdf.ai.fill / detect_fields (RUN_PAID_CALLS = False)")if RUN_PAID_CALLS:
comments = finish(client.pdf.ai.suggest_comments(pdf))
numbers = finish(client.pdf.ai.check_numbers(pdf))
kind = finish(client.pdf.ai.classify(pdf))
else:
print("skipped: pdf.ai.suggest_comments / check_numbers / classify (RUN_PAID_CALLS = False)")if RUN_PAID_CALLS:
edited = finish(client.pdf.ai.edit(pdf, instruction="Fix any spelling mistakes"))
else:
print("skipped: pdf.ai.edit (RUN_PAID_CALLS = False)")Usage
now = client.billing.credits_balance()["credit_amount"]
print(f"credits used by this notebook: {start_credits - now} (balance {now})")
show(client.billing.usage_by_feature())