Spaces:
Sleeping
Sleeping
File size: 4,504 Bytes
5de794c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
"""
Test script for the Panic Detection API
Run this locally to test your API before deployment
"""
import requests
import json
# Change this to your API URL
# For local testing: "http://localhost:7860"
# For Hugging Face: "https://your-space-name.hf.space"
API_URL = "http://localhost:7860"
def test_root():
"""Test root endpoint"""
print("\n" + "="*50)
print("Testing Root Endpoint")
print("="*50)
response = requests.get(f"{API_URL}/")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_health():
"""Test health check endpoint"""
print("\n" + "="*50)
print("Testing Health Check Endpoint")
print("="*50)
response = requests.get(f"{API_URL}/health")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_model_info():
"""Test model info endpoint"""
print("\n" + "="*50)
print("Testing Model Info Endpoint")
print("="*50)
response = requests.get(f"{API_URL}/model-info")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_predict_no_panic():
"""Test prediction with no panic scenario"""
print("\n" + "="*50)
print("Testing Prediction - No Panic Scenario")
print("="*50)
data = {
"gender": "Female",
"age": 30,
"weight": 60.0,
"heartrate": 75,
"stepcount": 3000,
"activity": "Walking"
}
print(f"Input Data: {json.dumps(data, indent=2)}")
response = requests.post(f"{API_URL}/predict", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_predict_panic():
"""Test prediction with panic scenario"""
print("\n" + "="*50)
print("Testing Prediction - Potential Panic Scenario")
print("="*50)
data = {
"gender": "Male",
"age": 25,
"weight": 75.0,
"heartrate": 150,
"stepcount": 1000,
"activity": "Standing"
}
print(f"Input Data: {json.dumps(data, indent=2)}")
response = requests.post(f"{API_URL}/predict", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_batch_predict():
"""Test batch prediction"""
print("\n" + "="*50)
print("Testing Batch Prediction")
print("="*50)
data = [
{
"gender": "Male",
"age": 25,
"weight": 70.5,
"heartrate": 120,
"stepcount": 5000,
"activity": "Running"
},
{
"gender": "Female",
"age": 30,
"weight": 60.0,
"heartrate": 75,
"stepcount": 3000,
"activity": "Walking"
}
]
print(f"Input Data: {json.dumps(data, indent=2)}")
response = requests.post(f"{API_URL}/batch-predict", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def test_invalid_input():
"""Test with invalid input"""
print("\n" + "="*50)
print("Testing Invalid Input")
print("="*50)
data = {
"gender": "InvalidGender",
"age": 25,
"weight": 70.5,
"heartrate": 120,
"stepcount": 5000,
"activity": "Running"
}
print(f"Input Data: {json.dumps(data, indent=2)}")
response = requests.post(f"{API_URL}/predict", json=data)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
if __name__ == "__main__":
print("\n" + "="*50)
print("PANIC DETECTION API TEST SUITE")
print("="*50)
print(f"Testing API at: {API_URL}")
try:
test_root()
test_health()
test_model_info()
test_predict_no_panic()
test_predict_panic()
test_batch_predict()
test_invalid_input()
print("\n" + "="*50)
print("ALL TESTS COMPLETED")
print("="*50)
except requests.exceptions.ConnectionError:
print("\n❌ ERROR: Could not connect to API")
print("Make sure the API is running!")
print("To run locally: uvicorn app:app --host 0.0.0.0 --port 7860")
except Exception as e:
print(f"\n❌ ERROR: {str(e)}")
|