import requests import json import time # Configuration BASE_URL = "http://localhost:7860" def test_api(): """Test all API endpoints""" print("๐Ÿงช Testing Content Classifier API") print("=" * 50) # Test 1: Root endpoint print("\n๐Ÿ“ Testing root endpoint...") try: response = requests.get(f"{BASE_URL}/") print(f"โœ… Status: {response.status_code}") print(f"๐Ÿ“„ Response: {json.dumps(response.json(), indent=2)}") except Exception as e: print(f"โŒ Error: {e}") # Test 2: Health check print("\n๐Ÿฅ Testing health endpoint...") try: response = requests.get(f"{BASE_URL}/health") print(f"โœ… Status: {response.status_code}") print(f"๐Ÿ“„ Response: {json.dumps(response.json(), indent=2)}") except Exception as e: print(f"โŒ Error: {e}") # Test 3: Model info print("\n๐Ÿค– Testing model info endpoint...") try: response = requests.get(f"{BASE_URL}/model-info") print(f"โœ… Status: {response.status_code}") if response.status_code == 200: print(f"๐Ÿ“„ Response: {json.dumps(response.json(), indent=2)}") else: print(f"๐Ÿ“„ Response: {response.text}") except Exception as e: print(f"โŒ Error: {e}") # Test 4: Prediction endpoint print("\n๐Ÿ” Testing prediction endpoint...") test_cases = [ { "text": "Hello, how are you today? I hope you're doing well!", "expected": "positive sentiment" }, { "text": "I will destroy everything and cause harm!", "expected": "threat detection" }, { "text": "This product is amazing, I love it so much!", "expected": "positive sentiment" }, { "text": "I hate this, it's terrible and awful!", "expected": "negative sentiment" }, { "text": "Neutral statement about weather conditions today.", "expected": "neutral/low confidence" } ] for i, case in enumerate(test_cases, 1): print(f"\n๐Ÿ“ Test Case {i}: {case['expected']}") print(f"๐Ÿ’ฌ Text: '{case['text'][:50]}{'...' if len(case['text']) > 50 else ''}'") try: payload = { "text": case["text"], "max_length": 512 } response = requests.post(f"{BASE_URL}/predict", json=payload) if response.status_code == 200: result = response.json() print(f"โœ… Status: {response.status_code}") print(f"๐ŸŽฏ Is Threat: {result['is_threat']}") print(f"๐Ÿ“Š Confidence: {result['final_confidence']:.3f}") print(f"โš ๏ธ Threat Prediction: {result['threat_prediction']:.3f}") print(f"๐Ÿ˜Š Sentiment: {result['sentiment_analysis']['label']} ({result['sentiment_analysis']['score']:.3f})") print(f"๐Ÿ”ง Models Used: {result['models_used']}") else: print(f"โŒ Status: {response.status_code}") print(f"๐Ÿ“„ Response: {response.text}") except Exception as e: print(f"โŒ Error: {e}") print("\n" + "=" * 50) print("๐Ÿ API Testing Complete!") def test_edge_cases(): """Test edge cases and error conditions""" print("\n๐Ÿšจ Testing Edge Cases") print("=" * 30) edge_cases = [ {"text": "", "description": "Empty string"}, {"text": " ", "description": "Whitespace only"}, {"text": "A" * 1000, "description": "Very long text"}, {"text": "๐Ÿ”ฅ๐Ÿ’ฏ๐Ÿš€", "description": "Emoji only"}, {"text": "12345 67890", "description": "Numbers only"} ] for case in edge_cases: print(f"\n๐Ÿ“ Testing: {case['description']}") try: payload = {"text": case["text"]} response = requests.post(f"{BASE_URL}/predict", json=payload) if response.status_code == 200: result = response.json() print(f"โœ… Success: Threat={result['is_threat']}, Confidence={result['final_confidence']:.3f}") else: print(f"โš ๏ธ Status {response.status_code}: {response.json().get('detail', 'Unknown error')}") except Exception as e: print(f"โŒ Error: {e}") if __name__ == "__main__": print("๐Ÿ” Content Classifier API Test Suite") print(f"๐ŸŒ Base URL: {BASE_URL}") # Wait a moment for server to be ready print("\nโฑ๏ธ Waiting for server to be ready...") time.sleep(2) # Run tests test_api() test_edge_cases() print(f"\n๐Ÿ“š Visit {BASE_URL}/docs for interactive API documentation")