Spaces:
Sleeping
Sleeping
| 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") | |