Spaces:
Runtime error
Runtime error
| 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() | |