StillWorking commited on
Commit
2fa4f99
·
verified ·
1 Parent(s): 133fa92

Upload test_semantic_search.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_semantic_search.py +152 -0
test_semantic_search.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ End-to-end smoke test for the Sentinel-AI API.
3
+ Make sure the backend is running before executing this script.
4
+
5
+ Usage:
6
+ python test_semantic_search.py
7
+ """
8
+
9
+ import json
10
+ import os
11
+ import sys
12
+ from typing import Any
13
+
14
+ import requests
15
+
16
+ API_BASE = os.getenv("SENTINEL_API_BASE_URL", "http://localhost:8000").rstrip("/")
17
+
18
+ # Optional shared-deployment token (start the backend with SENTINEL_API_KEY).
19
+ _API_KEY = os.getenv("SENTINEL_API_KEY", "").strip()
20
+ _HEADERS = {"Authorization": f"Bearer {_API_KEY}"} if _API_KEY else {}
21
+
22
+
23
+ def _print_header(title: str) -> None:
24
+ print("\n" + "=" * 70)
25
+ print(title)
26
+ print("=" * 70)
27
+
28
+
29
+ def test_health() -> bool:
30
+ """Test if the backend is running."""
31
+ _print_header("TEST: Health Check")
32
+ try:
33
+ response = requests.get(f"{API_BASE}/health", headers=_HEADERS, timeout=5)
34
+ response.raise_for_status()
35
+ data = response.json()
36
+
37
+ print("Backend is running")
38
+ print(f" Status: {data['status']}")
39
+ print(f" Index ready: {data['index_ready']}")
40
+ print(f" Model: {data['model']}")
41
+ return bool(data.get("status") == "ok")
42
+ except requests.exceptions.ConnectionError:
43
+ print(f"Cannot connect to {API_BASE}")
44
+ print(" Start the backend with:")
45
+ print(" cd backend && python -m uvicorn main:app --host 127.0.0.1 --port 8000 --reload")
46
+ return False
47
+ except Exception as exc:
48
+ print(f"Health check failed: {exc}")
49
+ return False
50
+
51
+
52
+ def test_semantic_search(query: str, top_k: int = 5) -> dict[str, Any] | None:
53
+ """Run one semantic-search query and return the response (or None)."""
54
+ _print_header("TEST: Semantic Search")
55
+ print(f"Query: '{query}'")
56
+ print(f"Top K: {top_k}")
57
+
58
+ try:
59
+ response = requests.post(
60
+ f"{API_BASE}/api/v1/semantic-search",
61
+ json={"query": query, "top_k": top_k},
62
+ headers=_HEADERS,
63
+ timeout=30,
64
+ )
65
+ response.raise_for_status()
66
+ data = response.json()
67
+
68
+ attrs = data.get("extracted_attrs") or []
69
+ excluded = data.get("excluded_attrs") or []
70
+ matches = data.get("matches") or []
71
+
72
+ print("Request successful")
73
+ print(f" Status: {data.get('status')}")
74
+ print(f" Extracted attributes: {', '.join(attrs) if attrs else 'None'}")
75
+ print(f" Excluded attributes: {', '.join(excluded) if excluded else 'None'}")
76
+ print(f" Matches found: {len(matches)}")
77
+
78
+ metrics = data.get("cascadeflow_metrics")
79
+ if metrics:
80
+ print(f" Latency: {metrics.get('latency_ms', 0):.1f} ms")
81
+ print(f" Cost: ${metrics.get('cost_usd', 0):.6f}")
82
+
83
+ if matches:
84
+ print(f"\nTop {min(3, len(matches))} matches:")
85
+ for i, match in enumerate(matches[:3], 1):
86
+ shown = ", ".join(match["attributes"][:5])
87
+ print(f" {i}. Image ID: {match['image_id']} (faiss_id={match['faiss_id']})")
88
+ print(f" Attributes: {shown}...")
89
+
90
+ return data
91
+ except requests.exceptions.Timeout:
92
+ print("Request timed out (>30 seconds)")
93
+ return None
94
+ except requests.exceptions.HTTPError as exc:
95
+ print(f"HTTP Error: {exc}")
96
+ try:
97
+ print(f" Detail: {json.dumps(response.json(), indent=2)}")
98
+ except Exception:
99
+ print(f" Response: {response.text}")
100
+ return None
101
+ except Exception as exc:
102
+ print(f"Request failed: {exc}")
103
+ return None
104
+
105
+
106
+ def main() -> int:
107
+ """Run all tests and return a process exit code."""
108
+ _print_header("SENTINEL-AI SEMANTIC SEARCH TEST SUITE")
109
+
110
+ if not test_health():
111
+ print("\nBackend is not running. Please start it first.")
112
+ return 1
113
+
114
+ results: list[tuple[str, bool]] = []
115
+
116
+ # A response is only a pass if the request succeeded *and* the extracted
117
+ # attributes are sensible for the query.
118
+ data = test_semantic_search("smiling woman with blond hair", top_k=5)
119
+ ok = bool(data) and "Male" in data.get("excluded_attrs", [])
120
+ if ok:
121
+ ok = not any("Male" in m.get("attributes", []) for m in data.get("matches", []))
122
+ results.append(("Smiling woman query (excludes Male from results)", ok))
123
+
124
+ data = test_semantic_search("young male with wavy hair and eyeglasses", top_k=5)
125
+ ok = bool(data) and {"Male", "Young"} <= set(data.get("extracted_attrs", []))
126
+ results.append(("Young male with glasses query", ok))
127
+
128
+ data = test_semantic_search("person with beard and mustache", top_k=5)
129
+ ok = bool(data) and "No_Beard" not in data.get("extracted_attrs", [])
130
+ results.append(("Beard/mustache query (no No_Beard inversion)", ok))
131
+
132
+ data = test_semantic_search("alien with three eyes", top_k=5)
133
+ ok = bool(data) and not data.get("matches")
134
+ results.append(("Edge-case query returns no matches", ok))
135
+
136
+ _print_header("SUMMARY")
137
+ for label, passed in results:
138
+ print(f" [{'PASS' if passed else 'FAIL'}] {label}")
139
+
140
+ passed_count = sum(1 for _, passed in results if passed)
141
+ print(f"\nTests passed: {passed_count}/{len(results)}")
142
+
143
+ if passed_count == len(results):
144
+ print("\nALL TESTS PASSED")
145
+ return 0
146
+
147
+ print(f"\n{len(results) - passed_count} test(s) failed.")
148
+ return 1
149
+
150
+
151
+ if __name__ == "__main__":
152
+ sys.exit(main())