File size: 1,726 Bytes
5bf30ef
dead143
f4d119e
5bf30ef
f4d119e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dead143
5bf30ef
 
 
f4d119e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf30ef
f4d119e
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
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import time
import os

BASE_URL = "https://cjr61-papi-cc.hf.space"
API_KEY  = "5175231e76de498f2a7455c19dc92a551b9b9c8a"
HEADERS  = {"X-API-Key": API_KEY}

DATA_DIR = os.path.join(os.path.dirname(__file__), "data")

# Expected results for validation
EXPECTED = {
    "ec.JPG":  "bacteria",
    "ped.JPG": "virus",
    "nr1.png": "None",
    "nr2.jpg": "None",
    "nr3.jpg": "None",
    "nr4.jpg": "None",
    "rw.jpg":  "virus",
}


def test_predict(image_path):
    url = f"{BASE_URL}/predict/"
    t0 = time.time()
    with open(image_path, "rb") as f:
        response = requests.post(url, headers=HEADERS, files={"file": f})
    elapsed = round(time.time() - t0, 2)
    return response, elapsed


def run_all():
    print(f"Target: {BASE_URL}\n{'='*55}")
    passed = failed = 0

    for fname, expected in EXPECTED.items():
        path = os.path.join(DATA_DIR, fname)
        if not os.path.exists(path):
            print(f"  [SKIP] {fname} — file not found")
            continue

        resp, elapsed = test_predict(path)
        if resp.status_code != 200:
            print(f"  [FAIL] {fname} — HTTP {resp.status_code} ({elapsed}s)")
            failed += 1
            continue

        result = resp.json()
        cls  = result.get("class", "?")
        prob = result.get("probability", "?")
        ok   = cls == expected
        mark = "PASS" if ok else "FAIL"
        if ok:
            passed += 1
        else:
            failed += 1
        print(f"  [{mark}] {fname:<12} got={cls:<10} prob={prob}  expected={expected}  ({elapsed}s)")

    print(f"{'='*55}")
    print(f"  Result: {passed} passed, {failed} failed / {passed+failed} total")


if __name__ == "__main__":
    run_all()