Skip to main content

The official Python SDK for the kawn AI API

Project description

kawn.ai Python SDK

The official Python client for interacting with the kawn.ai API.

Installation

pip install kawn.ai

Setup

Set your API key as an environment variable (recommended); if you don't have a key you can get one from kawn consonle

export MISRAJ_API_KEY="your-api-key"

Architecture Usage

The kawn.ai SDK is designed with decoupled services. You create a core HTTP Client first, and pass it to whatever specific service you need. This keeps the library robust and easy to extend.

1. Synchronous Usage

from kawn import KawnClient
from kawn.services import EmbeddingService, OCRService

# The client automatically picks up the MISRAJ_API_KEY environment variable.
client = KawnClient()

# Initialize our decoupled services using the core client transport
embeddings_service = EmbeddingService(client)
ocr_service = OCRService(client)

# Text Embeddings
response = embeddings_service.create(inputs="Hello, kawn.ai!")
print(response.data[0].embedding)

# Batch Embeddings
batch_response = embeddings_service.create(inputs=[
    "This is the first sentence.",
    "This is the second sentence."
])
print(batch_response.data)

# Basser OCR: Single File Processing (Supports Images and PDFs)
ocr_res = ocr_service.process_file(file_path="document.pdf")
print(ocr_res)

# Basser OCR: Batch File Processing
batch_ocr_res = ocr_service.process_batch(file_paths=[
    "document1.png",
    "document2.pdf"
])

for res in batch_ocr_res.successful_results:
    print(res)

2. Asynchronous Usage

For high-performance applications, use the AsyncClient and async services.

import asyncio
from kawn import AsyncKawnClient
from kawn.services import AsyncEmbeddingService, AsyncOCRService

async def main():
    # You can also pass credentials explicitly if bypassing env variables
    async with AsyncKawnClient(api_key="your-api-key") as client:
        
        # Initialize Async services
        embed_service = AsyncEmbeddingService(client)
        ocr_service = AsyncOCRService(client)

        batch_embed_res = await embed_service.create(
            inputs=["Concurrency is fast!", "Async batching is optimal."]
        )
        print(batch_embed_res)

        # Basser OCR Async Batch Processing
        batch_ocr_res = await ocr_service.process_batch(
            file_paths=["receipt_amount.png", "invoice.pdf"]
        )
        
        for r in batch_ocr_res.successful_results:
            print(r)

if __name__ == "__main__":
    asyncio.run(main())

Utilities & PDF Processing

The OCRService inherently supports uploading both images and multi-page PDF files directly. There is generally no need to manually convert or split your PDFs before using the OCR API.

However, if you require more granular control—such as processing individual pages, setting a specific zoom factor, or rendering files to images offline before sending them—you can use the SDK's built-in PDF helper:

from misraj.utils.pdf import convert_pdf_to_images

# Processes the PDF into separate images in parallel and saves them locally
image_files = convert_pdf_to_images(
    pdf_content="report.pdf", 
    zoom=4, 
    output_folder="./processed_pages"
)

Error Handling

from kawn import KawnClient
from kawn import KawnAPIError, AuthenticationError, RateLimitError
from kawn.exceptions import ProcessingFailedError
from kawn.services import OCRService

try:
    client = KawnClient()
    ocr = OCRService(client)
    ocr.process_file("broken_file.jpg")
except AuthenticationError as e:
    print("Invalid API Key!", e)
except RateLimitError as e:
    print("Too many requests, slow down!", e)
except ProcessingFailedError as e:
    print("OCR Processing failed on the server!", e)
except KawnAPIError as e:
    print(f"Generic server error: {e}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kawn_ai-0.1.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kawn_ai-0.1.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file kawn_ai-0.1.2.tar.gz.

File metadata

  • Download URL: kawn_ai-0.1.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kawn_ai-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0ea25adc817d50488bfe3b3ec3c6958818a46d876bdb16aa5b1a6f2c96697c4d
MD5 be57ed87c00b6fbab79eaa1a2f9af199
BLAKE2b-256 aa5bd6a2580fe2190ac38422131b09247cb1d87c0bad56ab4cb38c1878742457

See more details on using hashes here.

File details

Details for the file kawn_ai-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: kawn_ai-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kawn_ai-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3510ddd13adea05d3a39698e0777fb6ddf6459d2cd4154b11c6ffe72b714b198
MD5 5fb4771205601de71ab059cf185af326
BLAKE2b-256 93d4c834abdd4c4307507e4e53cd9eecbecab0d8d22b5330cdeb61bc59021830

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page