Panic_Detection / test_api.py
Shib-Sankar-Das's picture
Deploy Panic Detection API with FastAPI, CNN model, and Docker configuration
5de794c
"""
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)}")