CJR61 Claude Sonnet 4.6 commited on
Commit
f4d119e
·
1 Parent(s): 41ef3fd

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>

Files changed (1) hide show
  1. test/test.py +56 -29
test/test.py CHANGED
@@ -1,37 +1,64 @@
1
  import requests
2
  import time
 
3
 
4
- # define API URL
5
- # BASE_URL = "http://104.199.172.213:8000"
6
- BASE_URL = "http://35.185.152.182:8000"
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # BASE_URL = "http://localhost:8000"
9
 
10
- # inference
11
  def test_predict(image_path):
12
  url = f"{BASE_URL}/predict/"
13
- start_time = time.time()
14
- with open(image_path, "rb") as file:
15
- files = {"file": file}
16
- response = requests.post(url, files=files)
17
- end_time = time.time()
18
- print("Inference time:", end_time - start_time, "seconds")
19
-
20
- # Print the response status code and content
21
- print("Response status code:", response.status_code)
22
- print("Response content:", response.text)
23
-
24
- # # Check if the response is JSON
25
- # try:
26
- # print("Inference response:", response.json())
27
- # except requests.exceptions.JSONDecodeError as e:
28
- # print("JSON decode error:", e)
29
- # print("Response content is not in JSON format")
30
-
31
-
32
- # testing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  if __name__ == "__main__":
34
- # IMAGE_PATH = "/mnt/d/pig/api/test/data/ec.jpg"
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()