Spaces:
Sleeping
Sleeping
| """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()) | |