File size: 3,489 Bytes
3785df9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
test_api.py  —  Verify your deployed Space handles both images and PDFs.

Usage:
    python test_api.py --url https://<your-space>.hf.space --image note.jpg
    python test_api.py --url https://<your-space>.hf.space --image note.jpg --pdf doc.pdf
"""

import argparse
import base64
import json
import requests


def pretty(data: dict):
    print(json.dumps(data, indent=2))


def test_health(base_url: str):
    print("\n── Health check ──────────────────────────────────")
    r = requests.get(f"{base_url}/health", timeout=30)
    print(f"Status: {r.status_code}")
    pretty(r.json())
    assert r.json()["status"] == "ok"
    print("✓ PASS")


def test_image_file(base_url: str, path: str):
    print("\n── /ocr/file  [image upload] ─────────────────────")
    with open(path, "rb") as f:
        r = requests.post(f"{base_url}/ocr/file", files={"file": f}, timeout=120)
    print(f"Status: {r.status_code}")
    data = r.json()
    pretty(data)
    assert data.get("success") and data.get("file_type") == "image"
    print(f"✓ PASS  →  '{data['text']}'")


def test_image_base64(base_url: str, path: str):
    print("\n── /ocr/base64  [image base64] ───────────────────")
    with open(path, "rb") as f:
        b64 = base64.b64encode(f.read()).decode()
    r = requests.post(f"{base_url}/ocr/base64",
                      json={"file": b64, "filename": path}, timeout=120)
    print(f"Status: {r.status_code}")
    data = r.json()
    pretty(data)
    assert data.get("success") and data.get("file_type") == "image"
    print(f"✓ PASS  →  '{data['text']}'")


def test_pdf_file(base_url: str, path: str):
    print("\n── /ocr/file  [PDF upload] ───────────────────────")
    with open(path, "rb") as f:
        r = requests.post(f"{base_url}/ocr/file", files={"file": f}, timeout=300)
    print(f"Status: {r.status_code}")
    data = r.json()
    pretty(data)
    assert data.get("success") and data.get("file_type") == "pdf"
    print(f"✓ PASS  →  {data['page_count']} page(s)")


def test_pdf_base64(base_url: str, path: str):
    print("\n── /ocr/base64  [PDF base64] ─────────────────────")
    with open(path, "rb") as f:
        b64 = base64.b64encode(f.read()).decode()
    r = requests.post(f"{base_url}/ocr/base64",
                      json={"file": b64, "filename": path}, timeout=300)
    print(f"Status: {r.status_code}")
    data = r.json()
    pretty(data)
    assert data.get("success") and data.get("file_type") == "pdf"
    print(f"✓ PASS  →  {data['page_count']} page(s)")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--url",   default="http://localhost:7860")
    parser.add_argument("--image", required=True, help="Path to a handwritten image")
    parser.add_argument("--pdf",   default=None,  help="Path to a handwritten PDF (optional)")
    args = parser.parse_args()

    base = args.url.rstrip("/")

    test_health(base)
    test_image_file(base, args.image)
    test_image_base64(base, args.image)

    if args.pdf:
        test_pdf_file(base, args.pdf)
        test_pdf_base64(base, args.pdf)
    else:
        print("\n(Skipping PDF tests — pass --pdf <file> to test PDF support)")

    print("\n\nAll tests passed ✓")