""" Example usage of the DocGenie API. Demonstrates how to call the API and save generated documents. """ import asyncio import base64 import json from pathlib import Path import httpx async def generate_documents_example(): """ Example: Generate documents from seed images. """ # API endpoint api_url = "http://localhost:8000/generate" # Example seed image URLs (replace with your actual URLs) seed_image_urls = [ "https://example.com/receipt1.jpg", "https://example.com/receipt2.jpg", # Add more seed image URLs here ] # Request payload payload = { "seed_images": seed_image_urls, "prompt_params": { "language": "English", "doc_type": "business and administrative documents", "gt_type": "Multiple questions about each document, with their answers taken **verbatim** from the document.", "gt_format": '{"": "", "": "", ...}', "num_solutions": 3 }, "model": "claude-sonnet-4-5-20250929" # "api_key": "your-api-key" # Optional if ANTHROPIC_API_KEY env var is set } print("Sending request to DocGenie API...") print(f"Seed images: {len(seed_image_urls)}") print(f"Requested solutions: {payload['prompt_params']['num_solutions']}") async with httpx.AsyncClient(timeout=300.0) as client: response = await client.post(api_url, json=payload) if response.status_code != 200: print(f"Error: {response.status_code}") print(response.text) return result = response.json() print(f"\nSuccess! Generated {result['total_documents']} documents") # Create output directory output_dir = Path("api_output") output_dir.mkdir(exist_ok=True) # Process each generated document for idx, doc in enumerate(result["documents"]): doc_id = doc["document_id"] print(f"\n--- Document {idx + 1} (ID: {doc_id}) ---") # Save PDF pdf_path = output_dir / f"{doc_id}.pdf" pdf_bytes = base64.b64decode(doc["pdf_base64"]) with open(pdf_path, "wb") as f: f.write(pdf_bytes) print(f" PDF saved: {pdf_path}") # Save HTML html_path = output_dir / f"{doc_id}.html" with open(html_path, "w", encoding="utf-8") as f: f.write(doc["html"]) print(f" HTML saved: {html_path}") # Save CSS css_path = output_dir / f"{doc_id}.css" with open(css_path, "w", encoding="utf-8") as f: f.write(doc["css"]) print(f" CSS saved: {css_path}") # Save ground truth if doc["ground_truth"]: gt_path = output_dir / f"{doc_id}_gt.json" with open(gt_path, "w", encoding="utf-8") as f: json.dump(doc["ground_truth"], f, indent=2, ensure_ascii=False) print(f" Ground truth saved: {gt_path}") print(f" GT entries: {len(doc['ground_truth'])}") # Save bounding boxes bbox_path = output_dir / f"{doc_id}_bboxes.json" bboxes_data = [bbox for bbox in doc["bboxes"]] with open(bbox_path, "w", encoding="utf-8") as f: json.dump(bboxes_data, f, indent=2) print(f" Bounding boxes saved: {bbox_path}") print(f" BBox count: {len(doc['bboxes'])}") # Print document info print(f" Dimensions: {doc['page_width_mm']:.1f}mm x {doc['page_height_mm']:.1f}mm") print(f"\n✅ All files saved to: {output_dir.absolute()}") async def health_check_example(): """ Example: Check if the API is running. """ api_url = "http://localhost:8000/health" print("Checking API health...") async with httpx.AsyncClient() as client: response = await client.get(api_url) if response.status_code == 200: result = response.json() print(f"✅ API is healthy!") print(f" Status: {result['status']}") print(f" Version: {result['version']}") else: print(f"❌ API is not responding: {response.status_code}") if __name__ == "__main__": print("DocGenie API - Example Usage\n") # Run health check asyncio.run(health_check_example()) print("\n" + "="*60 + "\n") # Run document generation example # NOTE: Replace seed_image_urls in the function with actual URLs # asyncio.run(generate_documents_example()) print("\n⚠️ To run document generation:") print(" 1. Make sure the API is running (python api/main.py)") print(" 2. Replace seed_image_urls in this script with actual image URLs") print(" 3. Set ANTHROPIC_API_KEY environment variable") print(" 4. Uncomment the generate_documents_example() line above")