File size: 4,996 Bytes
5c36ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
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": '{"<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"
        # "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")