File size: 2,388 Bytes
bcfd653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json

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

def test_api():
    # Test data
    test_texts = [
        "I love this product! It's amazing!",
        "This is terrible and disappointing.",
        "It's okay, nothing special.",
        "Best purchase ever! Highly recommend!"
    ]
    
    print("🧪 Testing Sentiment Analysis API")
    print("=" * 50)
    
    # Test single prediction
    print("\n📊 Testing /predict endpoint:")
    for text in test_texts[:2]:
        try:
            response = requests.post(
                f"{base_url}/predict",
                json={"text": text}
            )
            result = response.json()
            print(f"Text: '{text}'")
            print(f"Prediction: {result['prediction']} ({result['sentiment']})")
            print(f"Confidence: {result['confidence']:.2%}")
            print("-" * 30)
        except Exception as e:
            print(f"Error testing '{text}': {e}")
    
    # Test probability prediction
    print("\n📈 Testing /predict_proba endpoint:")
    test_text = test_texts[0]
    try:
        response = requests.post(
            f"{base_url}/predict_proba",
            json={"text": test_text}
        )
        result = response.json()
        print(f"Text: '{test_text}'")
        print(f"Probabilities: {result['probabilities']}")
        print(f"Prediction: {result['prediction']} ({result['sentiment']})")
        print("-" * 30)
    except Exception as e:
        print(f"Error testing probabilities: {e}")
    
    # Test batch prediction
    print("\n📦 Testing /batch_predict endpoint:")
    try:
        response = requests.post(
            f"{base_url}/batch_predict",
            json=test_texts
        )
        results = response.json()
        for result in results['results']:
            print(f"Text: '{result['text']}'")
            print(f"Sentiment: {result['sentiment']} (confidence: {result['confidence']:.2%})")
            print("-" * 30)
    except Exception as e:
        print(f"Error testing batch prediction: {e}")

if __name__ == "__main__":
    print("Make sure to start the API server first with: python app.py")
    print("Then run this test script.")
    
    # Uncomment the line below to run tests (make sure API is running first)
    # test_api()