safe-space2 / test_deployment.py
parthraninga's picture
Upload 66 files
a831961 verified
#!/usr/bin/env python3
"""
Test script for SafeSpace AI API deployment
This script tests all the main endpoints to ensure they work correctly
"""
import requests
import json
import time
def test_api(base_url="http://localhost:7860"):
"""Test all API endpoints"""
print(f"πŸ§ͺ Testing SafeSpace AI API at {base_url}")
print("=" * 50)
# Test 1: Root endpoint
print("\n1. Testing root endpoint...")
try:
response = requests.get(f"{base_url}/")
if response.status_code == 200:
print("βœ… Root endpoint working")
data = response.json()
print(f" Version: {data.get('version', 'Unknown')}")
print(f" Available endpoints: {len(data.get('endpoints', {}))}")
else:
print(f"❌ Root endpoint failed: {response.status_code}")
except Exception as e:
print(f"❌ Root endpoint error: {e}")
# Test 2: Health check
print("\n2. Testing health endpoint...")
try:
response = requests.get(f"{base_url}/health")
if response.status_code == 200:
print("βœ… Health endpoint working")
data = response.json()
print(f" Status: {data.get('status', 'Unknown')}")
else:
print(f"❌ Health endpoint failed: {response.status_code}")
except Exception as e:
print(f"❌ Health endpoint error: {e}")
# Test 3: Model status
print("\n3. Testing model status...")
try:
response = requests.get(f"{base_url}/api/models/status")
if response.status_code == 200:
print("βœ… Model status endpoint working")
data = response.json()
print(f" Models loaded: {data.get('summary', {}).get('loaded_models', 'Unknown')}")
else:
print(f"❌ Model status failed: {response.status_code}")
except Exception as e:
print(f"❌ Model status error: {e}")
# Test 4: Threat analysis
print("\n4. Testing threat analysis...")
try:
test_data = {
"text": "Emergency services are responding to an incident downtown",
"city": "Delhi"
}
response = requests.post(f"{base_url}/api/threats/analyze",
json=test_data, timeout=30)
if response.status_code == 200:
print("βœ… Threat analysis working")
data = response.json()
print(f" Threat detected: {data.get('is_threat', 'Unknown')}")
print(f" Confidence: {data.get('confidence', 'Unknown')}")
else:
print(f"❌ Threat analysis failed: {response.status_code}")
print(f" Response: {response.text}")
except Exception as e:
print(f"❌ Threat analysis error: {e}")
# Test 5: City threats
print("\n5. Testing city threats...")
try:
response = requests.get(f"{base_url}/api/threats/?city=Delhi&limit=3", timeout=30)
if response.status_code == 200:
print("βœ… City threats working")
data = response.json()
print(f" Threats found: {data.get('total_threats', 'Unknown')}")
print(f" City: {data.get('city', 'Unknown')}")
else:
print(f"❌ City threats failed: {response.status_code}")
except Exception as e:
print(f"❌ City threats error: {e}")
# Test 6: Model info
print("\n6. Testing model info...")
try:
response = requests.get(f"{base_url}/api/models/info")
if response.status_code == 200:
print("βœ… Model info working")
data = response.json()
models_info = data.get('models_info', {})
print(f" Models available: {len(models_info)}")
else:
print(f"❌ Model info failed: {response.status_code}")
except Exception as e:
print(f"❌ Model info error: {e}")
print("\n" + "=" * 50)
print("πŸŽ‰ Testing complete! Check results above.")
print("πŸ“š Visit /docs for interactive API documentation")
if __name__ == "__main__":
import sys
# Allow custom base URL
base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:7860"
test_api(base_url)