Spaces:
Sleeping
Sleeping
| #!/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) | |