File size: 1,984 Bytes
c1f3888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json

# Test the API
base_url = "http://localhost:7860"

def test_api():
    # Test root endpoint
    print("Testing root endpoint...")
    try:
        response = requests.get(f"{base_url}/")
        print(f"Root: {response.status_code} - {response.json()}")
    except Exception as e:
        print(f"Root endpoint error: {e}")
    
    # Test health endpoint
    print("\nTesting health endpoint...")
    try:
        response = requests.get(f"{base_url}/health")
        print(f"Health: {response.status_code} - {response.json()}")
    except Exception as e:
        print(f"Health endpoint error: {e}")
    
    # Test model info endpoint
    print("\nTesting model info endpoint...")
    try:
        response = requests.get(f"{base_url}/model-info")
        print(f"Model Info: {response.status_code} - {response.json()}")
    except Exception as e:
        print(f"Model info endpoint error: {e}")
    
    # Test prediction endpoint
    print("\nTesting prediction endpoint...")
    test_texts = [
        "This is a normal, safe message.",
        "I will harm you and your family!",
        "Hello, how are you doing today?",
        "This product is amazing, I love it!"
    ]
    
    for text in test_texts:
        try:
            payload = {"text": text}
            response = requests.post(f"{base_url}/predict", json=payload)
            if response.status_code == 200:
                result = response.json()
                print(f"\nText: '{text}'")
                print(f"Is Threat: {result['is_threat']}")
                print(f"Confidence: {result['final_confidence']:.3f}")
                print(f"Threat Prediction: {result['threat_prediction']:.3f}")
            else:
                print(f"Error {response.status_code}: {response.text}")
        except Exception as e:
            print(f"Prediction error for '{text}': {e}")

if __name__ == "__main__":
    test_api()