File size: 1,337 Bytes
3fccf40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One-file end-to-end demo: send a few sample captchas to the running API.

Usage:
  python scripts/demo.py
"""

import base64
import json
import sys
import time
from pathlib import Path

import httpx


API = "http://localhost:8765"
SAMPLES = Path(__file__).resolve().parent.parent / "tests" / "samples"


def post_solve(payload: dict) -> dict:
    r = httpx.post(f"{API}/solve", json=payload, timeout=30)
    r.raise_for_status()
    return r.json()


def main() -> int:
    print(f"POST {API}/health")
    r = httpx.get(f"{API}/health", timeout=5)
    print(json.dumps(r.json(), indent=2))

    print()
    print("--- Sample captchas ---")
    if not SAMPLES.exists():
        print(f"(no samples in {SAMPLES}; run tests/make_samples.py first)")
        return 1

    for img_path in sorted(SAMPLES.glob("*.png")):
        b64 = base64.b64encode(img_path.read_bytes()).decode("ascii")
        t0 = time.time()
        result = post_solve({"type": "math", "image_base64": b64, "hint": img_path.stem.split("_", 1)[1].replace("_", " ")})
        ms = int((time.time() - t0) * 1000)
        print(f"{img_path.name:30} -> {result}  (rtt {ms} ms)")

    print()
    print("--- /stats ---")
    r = httpx.get(f"{API}/stats", timeout=5)
    print(json.dumps(r.json(), indent=2))
    return 0


if __name__ == "__main__":
    sys.exit(main())