sentinel-face-recognition / test_semantic_search.py
StillWorking's picture
Upload test_semantic_search.py with huggingface_hub
2fa4f99 verified
Raw History Blame Contribute Delete
5.33 kB
"""
End-to-end smoke test for the Sentinel-AI API.
Make sure the backend is running before executing this script.
Usage:
python test_semantic_search.py
"""
import json
import os
import sys
from typing import Any
import requests
API_BASE = os.getenv("SENTINEL_API_BASE_URL", "http://localhost:8000").rstrip("/")
# Optional shared-deployment token (start the backend with SENTINEL_API_KEY).
_API_KEY = os.getenv("SENTINEL_API_KEY", "").strip()
_HEADERS = {"Authorization": f"Bearer {_API_KEY}"} if _API_KEY else {}
def _print_header(title: str) -> None:
print("\n" + "=" * 70)
print(title)
print("=" * 70)
def test_health() -> bool:
"""Test if the backend is running."""
_print_header("TEST: Health Check")
try:
response = requests.get(f"{API_BASE}/health", headers=_HEADERS, timeout=5)
response.raise_for_status()
data = response.json()
print("Backend is running")
print(f" Status: {data['status']}")
print(f" Index ready: {data['index_ready']}")
print(f" Model: {data['model']}")
return bool(data.get("status") == "ok")
except requests.exceptions.ConnectionError:
print(f"Cannot connect to {API_BASE}")
print(" Start the backend with:")
print(" cd backend && python -m uvicorn main:app --host 127.0.0.1 --port 8000 --reload")
return False
except Exception as exc:
print(f"Health check failed: {exc}")
return False
def test_semantic_search(query: str, top_k: int = 5) -> dict[str, Any] | None:
"""Run one semantic-search query and return the response (or None)."""
_print_header("TEST: Semantic Search")
print(f"Query: '{query}'")
print(f"Top K: {top_k}")
try:
response = requests.post(
f"{API_BASE}/api/v1/semantic-search",
json={"query": query, "top_k": top_k},
headers=_HEADERS,
timeout=30,
)
response.raise_for_status()
data = response.json()
attrs = data.get("extracted_attrs") or []
excluded = data.get("excluded_attrs") or []
matches = data.get("matches") or []
print("Request successful")
print(f" Status: {data.get('status')}")
print(f" Extracted attributes: {', '.join(attrs) if attrs else 'None'}")
print(f" Excluded attributes: {', '.join(excluded) if excluded else 'None'}")
print(f" Matches found: {len(matches)}")
metrics = data.get("cascadeflow_metrics")
if metrics:
print(f" Latency: {metrics.get('latency_ms', 0):.1f} ms")
print(f" Cost: ${metrics.get('cost_usd', 0):.6f}")
if matches:
print(f"\nTop {min(3, len(matches))} matches:")
for i, match in enumerate(matches[:3], 1):
shown = ", ".join(match["attributes"][:5])
print(f" {i}. Image ID: {match['image_id']} (faiss_id={match['faiss_id']})")
print(f" Attributes: {shown}...")
return data
except requests.exceptions.Timeout:
print("Request timed out (>30 seconds)")
return None
except requests.exceptions.HTTPError as exc:
print(f"HTTP Error: {exc}")
try:
print(f" Detail: {json.dumps(response.json(), indent=2)}")
except Exception:
print(f" Response: {response.text}")
return None
except Exception as exc:
print(f"Request failed: {exc}")
return None
def main() -> int:
"""Run all tests and return a process exit code."""
_print_header("SENTINEL-AI SEMANTIC SEARCH TEST SUITE")
if not test_health():
print("\nBackend is not running. Please start it first.")
return 1
results: list[tuple[str, bool]] = []
# A response is only a pass if the request succeeded *and* the extracted
# attributes are sensible for the query.
data = test_semantic_search("smiling woman with blond hair", top_k=5)
ok = bool(data) and "Male" in data.get("excluded_attrs", [])
if ok:
ok = not any("Male" in m.get("attributes", []) for m in data.get("matches", []))
results.append(("Smiling woman query (excludes Male from results)", ok))
data = test_semantic_search("young male with wavy hair and eyeglasses", top_k=5)
ok = bool(data) and {"Male", "Young"} <= set(data.get("extracted_attrs", []))
results.append(("Young male with glasses query", ok))
data = test_semantic_search("person with beard and mustache", top_k=5)
ok = bool(data) and "No_Beard" not in data.get("extracted_attrs", [])
results.append(("Beard/mustache query (no No_Beard inversion)", ok))
data = test_semantic_search("alien with three eyes", top_k=5)
ok = bool(data) and not data.get("matches")
results.append(("Edge-case query returns no matches", ok))
_print_header("SUMMARY")
for label, passed in results:
print(f" [{'PASS' if passed else 'FAIL'}] {label}")
passed_count = sum(1 for _, passed in results if passed)
print(f"\nTests passed: {passed_count}/{len(results)}")
if passed_count == len(results):
print("\nALL TESTS PASSED")
return 0
print(f"\n{len(results) - passed_count} test(s) failed.")
return 1
if __name__ == "__main__":
sys.exit(main())