Spaces:
Runtime error
Runtime error
Download test_api.py from parthraninga/Sentiment-Classifier: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/spaces/parthraninga/Sentiment-Classifier/resolve/main/test_api.py
- Command line
-
hf download hf://spaces/parthraninga/Sentiment-Classifier/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/parthraninga/Sentiment-Classifier/resolve/main/test_api.py
2.39 kB
| 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() | |