| """ |
| 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_url = "http://localhost:8000/generate" |
| |
| |
| seed_image_urls = [ |
| "https://example.com/receipt1.jpg", |
| "https://example.com/receipt2.jpg", |
| |
| ] |
| |
| |
| 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": '{"<Text of question 1>": "<Answer to question 1>", "<Text of question 2>": "<Answer to question 2>", ...}', |
| "num_solutions": 3 |
| }, |
| "model": "claude-sonnet-4-5-20250929" |
| |
| } |
| |
| 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") |
| |
| |
| output_dir = Path("api_output") |
| output_dir.mkdir(exist_ok=True) |
| |
| |
| for idx, doc in enumerate(result["documents"]): |
| doc_id = doc["document_id"] |
| print(f"\n--- Document {idx + 1} (ID: {doc_id}) ---") |
| |
| |
| 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}") |
| |
| |
| 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}") |
| |
| |
| 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}") |
| |
| |
| 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'])}") |
| |
| |
| 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(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") |
| |
| |
| asyncio.run(health_check_example()) |
| |
| print("\n" + "="*60 + "\n") |
| |
| |
| |
| |
| |
| 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") |
|
|