| | """ |
| | Script untuk test Python API secara lokal |
| | """ |
| |
|
| | import requests |
| | import base64 |
| | import sys |
| | import json |
| |
|
| | def test_health(api_url): |
| | """Test health check endpoint""" |
| | print(f"\n{'='*50}") |
| | print("Testing /health endpoint...") |
| | print(f"{'='*50}") |
| |
|
| | try: |
| | response = requests.get(f"{api_url}/health", timeout=10) |
| | print(f"Status Code: {response.status_code}") |
| | print(f"Response:\n{json.dumps(response.json(), indent=2)}") |
| | return response.status_code == 200 |
| | except Exception as e: |
| | print(f"Error: {e}") |
| | return False |
| |
|
| | def test_classify(api_url, image_path): |
| | """Test classification endpoint""" |
| | print(f"\n{'='*50}") |
| | print("Testing /classify endpoint...") |
| | print(f"{'='*50}") |
| |
|
| | try: |
| | |
| | with open(image_path, 'rb') as f: |
| | img_data = base64.b64encode(f.read()).decode() |
| |
|
| | |
| | payload = { |
| | 'image': f'data:image/jpeg;base64,{img_data}' |
| | } |
| |
|
| | |
| | print(f"Sending image: {image_path}") |
| | response = requests.post( |
| | f"{api_url}/classify", |
| | json=payload, |
| | timeout=30 |
| | ) |
| |
|
| | print(f"Status Code: {response.status_code}") |
| | result = response.json() |
| | print(f"\nPrediction Result:") |
| | print(f" Class: {result.get('predicted_class')}") |
| | print(f" Confidence: {result.get('confidence', 0)*100:.2f}%") |
| | print(f" Mode: {result.get('mode', 'unknown')}") |
| | print(f"\n Probabilities:") |
| | for cls, prob in result.get('probabilities', {}).items(): |
| | print(f" {cls}: {prob*100:.2f}%") |
| |
|
| | return response.status_code == 200 |
| |
|
| | except FileNotFoundError: |
| | print(f"Error: Image file not found: {image_path}") |
| | return False |
| | except Exception as e: |
| | print(f"Error: {e}") |
| | return False |
| |
|
| | def main(): |
| | |
| | API_URL = "http://localhost:5000" |
| |
|
| | if len(sys.argv) > 1: |
| | API_URL = sys.argv[1] |
| |
|
| | print(f"\n{'#'*50}") |
| | print(f"# Testing Palm Seedling Classifier API") |
| | print(f"# API URL: {API_URL}") |
| | print(f"{'#'*50}") |
| |
|
| | |
| | health_ok = test_health(API_URL) |
| |
|
| | if not health_ok: |
| | print("\n❌ Health check failed! Please check if the API is running.") |
| | return |
| |
|
| | print("\n✓ Health check passed!") |
| |
|
| | |
| | if len(sys.argv) > 2: |
| | image_path = sys.argv[2] |
| | classify_ok = test_classify(API_URL, image_path) |
| |
|
| | if classify_ok: |
| | print("\n✓ Classification test passed!") |
| | else: |
| | print("\n❌ Classification test failed!") |
| | else: |
| | print("\n⚠ No image provided for classification test.") |
| | print("Usage: python test_local.py [API_URL] [IMAGE_PATH]") |
| | print("Example: python test_local.py http://localhost:5000 bibit.jpg") |
| |
|
| | print("\n" + "="*50) |
| | print("Test completed!") |
| | print("="*50 + "\n") |
| |
|
| | if __name__ == "__main__": |
| | main() |
| |
|