Spaces:
Sleeping
Sleeping
Upload scripts/demo.py with huggingface_hub
Browse files- scripts/demo.py +52 -0
scripts/demo.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""One-file end-to-end demo: send a few sample captchas to the running API.
|
| 2 |
+
|
| 3 |
+
Usage:
|
| 4 |
+
python scripts/demo.py
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import base64
|
| 8 |
+
import json
|
| 9 |
+
import sys
|
| 10 |
+
import time
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import httpx
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
API = "http://localhost:8765"
|
| 17 |
+
SAMPLES = Path(__file__).resolve().parent.parent / "tests" / "samples"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def post_solve(payload: dict) -> dict:
|
| 21 |
+
r = httpx.post(f"{API}/solve", json=payload, timeout=30)
|
| 22 |
+
r.raise_for_status()
|
| 23 |
+
return r.json()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main() -> int:
|
| 27 |
+
print(f"POST {API}/health")
|
| 28 |
+
r = httpx.get(f"{API}/health", timeout=5)
|
| 29 |
+
print(json.dumps(r.json(), indent=2))
|
| 30 |
+
|
| 31 |
+
print()
|
| 32 |
+
print("--- Sample captchas ---")
|
| 33 |
+
if not SAMPLES.exists():
|
| 34 |
+
print(f"(no samples in {SAMPLES}; run tests/make_samples.py first)")
|
| 35 |
+
return 1
|
| 36 |
+
|
| 37 |
+
for img_path in sorted(SAMPLES.glob("*.png")):
|
| 38 |
+
b64 = base64.b64encode(img_path.read_bytes()).decode("ascii")
|
| 39 |
+
t0 = time.time()
|
| 40 |
+
result = post_solve({"type": "math", "image_base64": b64, "hint": img_path.stem.split("_", 1)[1].replace("_", " ")})
|
| 41 |
+
ms = int((time.time() - t0) * 1000)
|
| 42 |
+
print(f"{img_path.name:30} -> {result} (rtt {ms} ms)")
|
| 43 |
+
|
| 44 |
+
print()
|
| 45 |
+
print("--- /stats ---")
|
| 46 |
+
r = httpx.get(f"{API}/stats", timeout=5)
|
| 47 |
+
print(json.dumps(r.json(), indent=2))
|
| 48 |
+
return 0
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
sys.exit(main())
|