Skip to main content

The official Python SDK for the kawn AI API

Project description

misraj.ai Python SDK

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

Installation

pip install kawn.ai

Setup

Set your API key as an environment variable (recommended):

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 = Client()

# 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 AsyncClient(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.0.tar.gz (11.2 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.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kawn_ai-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 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.0.tar.gz
Algorithm Hash digest
SHA256 ac422a06e691c3a0bc173888b62ae858f8e3c2f630b9e0b6a22ed5eb36acf756
MD5 645e38fbd811b60e5335c1e56fa56d00
BLAKE2b-256 50f0c47247dd9d2d941484a93780c71c8c53e8a812e98bfe869d78d2e54d99a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kawn_ai-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43e44cfc8b0870bbe6d5f264cfac2dc95259e384d3f68a0a944bd1597fa1f429
MD5 83c30412d7bd027a3fb65e6727c97ed6
BLAKE2b-256 56970d80b597086a3f72ea628255123dc3481db83480dc1d0ee5774b8077f825

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