import os
import time
from chunkr_ai import Chunkr
# Initialize the client
client = Chunkr(api_key=os.environ["CHUNKR_API_KEY"])
# 1. Upload a local file
with open("path/to/doc.pdf", "rb") as f:
uploaded = client.files.create(file=f)
# 2. Create a parse task using the uploaded file URL
parse_task = client.tasks.parse.create(file=uploaded.url)
print(f"Task created with ID: {parse_task.task_id}")
# 3. Wait for the task to complete
while not parse_task.completed:
print(f"Task status: {parse_task.status}")
time.sleep(3)
parse_task = client.tasks.parse.get(task_id=parse_task.task_id)
# 4. Access the results
if parse_task.status == "Succeeded" and parse_task.output is not None:
print("Task completed successfully!")
print(f"Document has {len(parse_task.output.chunks)} chunks")
else: # Could be "Failed" or "Cancelled"
print(f"Task status: {parse_task.status}")