File size: 4,348 Bytes
a831961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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)