Spaces:
Sleeping
Sleeping
feat: Remove test_api
Browse files- test_api.py +0 -148
test_api.py
DELETED
|
@@ -1,148 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
|
| 5 |
-
BASE_URL = "http://localhost:7860"
|
| 6 |
-
|
| 7 |
-
def test_health_check():
|
| 8 |
-
"""Test health check endpoint"""
|
| 9 |
-
print("Testing health check...")
|
| 10 |
-
try:
|
| 11 |
-
response = requests.get(f"{BASE_URL}/health")
|
| 12 |
-
print(f"Status: {response.status_code}")
|
| 13 |
-
print(f"Response: {response.json()}")
|
| 14 |
-
return response.status_code == 200
|
| 15 |
-
except Exception as e:
|
| 16 |
-
print(f"Error: {e}")
|
| 17 |
-
return False
|
| 18 |
-
|
| 19 |
-
def test_model_info():
|
| 20 |
-
"""Test model info endpoint"""
|
| 21 |
-
print("\nTesting model info...")
|
| 22 |
-
try:
|
| 23 |
-
response = requests.get(f"{BASE_URL}/model_info")
|
| 24 |
-
print(f"Status: {response.status_code}")
|
| 25 |
-
print(f"Response: {response.json()}")
|
| 26 |
-
return response.status_code == 200
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"Error: {e}")
|
| 29 |
-
return False
|
| 30 |
-
|
| 31 |
-
def test_single_prediction():
|
| 32 |
-
"""Test single prediction endpoint"""
|
| 33 |
-
print("\nTesting single prediction...")
|
| 34 |
-
|
| 35 |
-
user_data = {
|
| 36 |
-
"Umur": 25,
|
| 37 |
-
"Jenis Kelamin": "Laki-laki",
|
| 38 |
-
"Kota": "Jakarta",
|
| 39 |
-
"Frekuensi": "Sekali sebulan",
|
| 40 |
-
"Anggaran/Kunjungan (IDR)": 2000000,
|
| 41 |
-
"Kemauan Bepergian": "Domestik",
|
| 42 |
-
"Metode Pemesanan": "Aplikasi Seluler",
|
| 43 |
-
"Tujuan Utama": "Relaksasi",
|
| 44 |
-
"Yang Dicari": "Kecantikan"
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
response = requests.post(
|
| 49 |
-
f"{BASE_URL}/predict",
|
| 50 |
-
headers={"Content-Type": "application/json"},
|
| 51 |
-
data=json.dumps(user_data)
|
| 52 |
-
)
|
| 53 |
-
print(f"Status: {response.status_code}")
|
| 54 |
-
if response.status_code == 200:
|
| 55 |
-
result = response.json()
|
| 56 |
-
print(f"Prediction: {result['prediction']}")
|
| 57 |
-
print(f"Confidence: {result['confidence']:.4f}")
|
| 58 |
-
print("All probabilities:")
|
| 59 |
-
for activity, prob in result['all_probabilities'].items():
|
| 60 |
-
print(f" {activity}: {prob:.4f}")
|
| 61 |
-
else:
|
| 62 |
-
print(f"Error: {response.json()}")
|
| 63 |
-
return response.status_code == 200
|
| 64 |
-
except Exception as e:
|
| 65 |
-
print(f"Error: {e}")
|
| 66 |
-
return False
|
| 67 |
-
|
| 68 |
-
def test_invalid_request():
|
| 69 |
-
"""Test API with invalid data"""
|
| 70 |
-
print("\nTesting invalid request...")
|
| 71 |
-
|
| 72 |
-
# Missing required fields
|
| 73 |
-
invalid_data = {
|
| 74 |
-
"Umur": 25,
|
| 75 |
-
"Jenis Kelamin": "Laki-laki"
|
| 76 |
-
# Missing other required fields
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
try:
|
| 80 |
-
response = requests.post(
|
| 81 |
-
f"{BASE_URL}/predict",
|
| 82 |
-
headers={"Content-Type": "application/json"},
|
| 83 |
-
data=json.dumps(invalid_data)
|
| 84 |
-
)
|
| 85 |
-
print(f"Status: {response.status_code}")
|
| 86 |
-
print(f"Response: {response.json()}")
|
| 87 |
-
return response.status_code == 400
|
| 88 |
-
except Exception as e:
|
| 89 |
-
print(f"Error: {e}")
|
| 90 |
-
return False
|
| 91 |
-
|
| 92 |
-
def run_all_tests():
|
| 93 |
-
"""Run all tests"""
|
| 94 |
-
print("=" * 60)
|
| 95 |
-
print("WELLNESS RECOMMENDATION API TESTS")
|
| 96 |
-
print("=" * 60)
|
| 97 |
-
|
| 98 |
-
tests = [
|
| 99 |
-
("Health Check", test_health_check),
|
| 100 |
-
("Model Info", test_model_info),
|
| 101 |
-
("Single Prediction", test_single_prediction),
|
| 102 |
-
("Invalid Request", test_invalid_request)
|
| 103 |
-
]
|
| 104 |
-
|
| 105 |
-
results = []
|
| 106 |
-
for test_name, test_func in tests:
|
| 107 |
-
print(f"\n{'-' * 40}")
|
| 108 |
-
result = test_func()
|
| 109 |
-
results.append((test_name, result))
|
| 110 |
-
time.sleep(1)
|
| 111 |
-
|
| 112 |
-
print(f"\n{'=' * 60}")
|
| 113 |
-
print("TEST RESULTS SUMMARY")
|
| 114 |
-
print(f"{'=' * 60}")
|
| 115 |
-
|
| 116 |
-
for test_name, result in results:
|
| 117 |
-
status = "Passed" if result else "Failed"
|
| 118 |
-
print(f"{test_name:25} : {status}")
|
| 119 |
-
|
| 120 |
-
passed = sum(1 for _, result in results if result)
|
| 121 |
-
total = len(results)
|
| 122 |
-
print(f"\nOverall: {passed}/{total} tests passed")
|
| 123 |
-
|
| 124 |
-
if __name__ == "__main__":
|
| 125 |
-
print("Waiting for server to start...")
|
| 126 |
-
time.sleep(3)
|
| 127 |
-
|
| 128 |
-
run_all_tests()
|
| 129 |
-
|
| 130 |
-
print(f"\n{'=' * 60}")
|
| 131 |
-
print("POSTMAN TESTING GUIDE")
|
| 132 |
-
print(f"{'=' * 60}")
|
| 133 |
-
print(f"Base URL: {BASE_URL}")
|
| 134 |
-
print("\n1. GET /health - Check API health")
|
| 135 |
-
print("2. GET /model_info - Get model information")
|
| 136 |
-
print("3. POST /predict - Single prediction")
|
| 137 |
-
print(" Body (JSON):")
|
| 138 |
-
print(json.dumps({
|
| 139 |
-
"Umur": 25,
|
| 140 |
-
"Jenis Kelamin": "Laki-laki",
|
| 141 |
-
"Kota": "Jakarta",
|
| 142 |
-
"Frekuensi": "Sekali sebulan",
|
| 143 |
-
"Anggaran/Kunjungan (IDR)": 2000000,
|
| 144 |
-
"Kemauan Bepergian": "Domestik",
|
| 145 |
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