Update test script for HF Space with full validation
Browse files- Point to HF Space URL with X-API-Key header
- Test all 7 images with expected result validation
- Print PASS/FAIL per image with class, probability, elapsed time
- Summary line at end
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- test/test.py +56 -29
test/test.py
CHANGED
|
@@ -1,37 +1,64 @@
|
|
| 1 |
import requests
|
| 2 |
import time
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# BASE_URL = "http://localhost:8000"
|
| 9 |
|
| 10 |
-
# inference
|
| 11 |
def test_predict(image_path):
|
| 12 |
url = f"{BASE_URL}/predict/"
|
| 13 |
-
|
| 14 |
-
with open(image_path, "rb") as
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
print("
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
|
| 35 |
-
IMAGE_PATH = "/mnt/d/pig/api/test/data/nr4.jpg"
|
| 36 |
-
|
| 37 |
-
test_predict(IMAGE_PATH)
|
|
|
|
| 1 |
import requests
|
| 2 |
import time
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
BASE_URL = "https://cjr61-papi-cc.hf.space"
|
| 6 |
+
API_KEY = "5175231e76de498f2a7455c19dc92a551b9b9c8a"
|
| 7 |
+
HEADERS = {"X-API-Key": API_KEY}
|
| 8 |
+
|
| 9 |
+
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
| 10 |
+
|
| 11 |
+
# Expected results for validation
|
| 12 |
+
EXPECTED = {
|
| 13 |
+
"ec.JPG": "bacteria",
|
| 14 |
+
"ped.JPG": "virus",
|
| 15 |
+
"nr1.png": "None",
|
| 16 |
+
"nr2.jpg": "None",
|
| 17 |
+
"nr3.jpg": "None",
|
| 18 |
+
"nr4.jpg": "None",
|
| 19 |
+
"rw.jpg": "virus",
|
| 20 |
+
}
|
| 21 |
|
|
|
|
| 22 |
|
|
|
|
| 23 |
def test_predict(image_path):
|
| 24 |
url = f"{BASE_URL}/predict/"
|
| 25 |
+
t0 = time.time()
|
| 26 |
+
with open(image_path, "rb") as f:
|
| 27 |
+
response = requests.post(url, headers=HEADERS, files={"file": f})
|
| 28 |
+
elapsed = round(time.time() - t0, 2)
|
| 29 |
+
return response, elapsed
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def run_all():
|
| 33 |
+
print(f"Target: {BASE_URL}\n{'='*55}")
|
| 34 |
+
passed = failed = 0
|
| 35 |
+
|
| 36 |
+
for fname, expected in EXPECTED.items():
|
| 37 |
+
path = os.path.join(DATA_DIR, fname)
|
| 38 |
+
if not os.path.exists(path):
|
| 39 |
+
print(f" [SKIP] {fname} — file not found")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
resp, elapsed = test_predict(path)
|
| 43 |
+
if resp.status_code != 200:
|
| 44 |
+
print(f" [FAIL] {fname} — HTTP {resp.status_code} ({elapsed}s)")
|
| 45 |
+
failed += 1
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
result = resp.json()
|
| 49 |
+
cls = result.get("class", "?")
|
| 50 |
+
prob = result.get("probability", "?")
|
| 51 |
+
ok = cls == expected
|
| 52 |
+
mark = "PASS" if ok else "FAIL"
|
| 53 |
+
if ok:
|
| 54 |
+
passed += 1
|
| 55 |
+
else:
|
| 56 |
+
failed += 1
|
| 57 |
+
print(f" [{mark}] {fname:<12} got={cls:<10} prob={prob} expected={expected} ({elapsed}s)")
|
| 58 |
+
|
| 59 |
+
print(f"{'='*55}")
|
| 60 |
+
print(f" Result: {passed} passed, {failed} failed / {passed+failed} total")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
run_all()
|
|
|
|
|
|
|
|
|