ml_service / TESTING.md
bldeaw's picture
Deploy to Hugging Face Spaces: Fix model loading and add documentation
c72db2d
|
Raw
History Blame Contribute Delete
8.34 kB

API Testing Guide

คู่มือการทดสอบ API สำหรับ Job Failure Prediction & Anomaly Detection

🚀 วิธีที่ 1: ใช้ Test Script (แนะนำ)

Local Testing

# Test API ที่รันบน localhost:8000 (default)
python test_api.py

# Test API ที่รันบน localhost:7860 (Hugging Face Spaces port)
python test_api.py --port 7860

# Test API ที่ URL เฉพาะ
python test_api.py --url http://localhost:8000

Hugging Face Spaces Testing

# Test API บน Hugging Face Spaces
python test_api.py --url https://your-username-your-space.hf.space

🧪 วิธีที่ 2: ใช้ cURL

Health Check

# Local
curl http://localhost:8000/health

# Hugging Face Spaces
curl https://your-username-your-space.hf.space/health

Job Failure Prediction

# Local
curl -X POST http://localhost:8000/predict/job-fail \
  -H "Content-Type: application/json" \
  -d '{
    "zone": "prod",
    "job_nm": "daily_export_customer",
    "job_start_time": "2026-01-21T01:00:00",
    "duration_sec": 5400,
    "status": "SUCCESS",
    "err_msg": "",
    "explain": true
  }'

# Hugging Face Spaces
curl -X POST https://your-username-your-space.hf.space/predict/job-fail \
  -H "Content-Type: application/json" \
  -d '{
    "zone": "prod",
    "job_nm": "daily_export_customer",
    "job_start_time": "2026-01-21T01:00:00",
    "duration_sec": 5400,
    "status": "SUCCESS",
    "explain": true
  }'

Anomaly Detection

# Local
curl -X POST http://localhost:8000/detect/anomaly \
  -H "Content-Type: application/json" \
  -d '{
    "features": {
      "duration_sec": 5400,
      "duration_zscore": 1.6,
      "avg_duration_7": 3000,
      "failure_rate_7": 0.15,
      "err_msg_len": 0,
      "hour_sin": 0.2588,
      "hour_cos": 0.9659
    },
    "threshold": 0.01
  }'

# Hugging Face Spaces
curl -X POST https://your-username-your-space.hf.space/detect/anomaly \
  -H "Content-Type: application/json" \
  -d '{
    "features": {
      "duration_sec": 5400,
      "duration_zscore": 1.6,
      "avg_duration_7": 3000,
      "failure_rate_7": 0.15,
      "err_msg_len": 0
    }
  }'

🌐 วิธีที่ 3: ใช้ Swagger UI (Interactive)

Local

  1. เริ่ม API server:

    uvicorn app:app --host 0.0.0.0 --port 8000
    
  2. เปิด browser ไปที่:

    http://localhost:8000/docs
    
  3. ทดสอบ endpoints ผ่าน interactive UI

Hugging Face Spaces

  1. ไปที่ Space page
  2. คลิก "API" tab หรือไปที่:
    https://your-username-your-space.hf.space/docs
    
  3. ทดสอบ endpoints ผ่าน interactive UI

📝 วิธีที่ 4: ใช้ Python Requests

Example Script

import requests
import json

# Base URL
BASE_URL = "http://localhost:8000"  # หรือ URL ของ Hugging Face Space

# Test Health
response = requests.get(f"{BASE_URL}/health")
print("Health Check:", response.json())

# Test Prediction
payload = {
    "zone": "prod",
    "job_nm": "daily_export",
    "job_start_time": "2026-01-21T01:00:00",
    "duration_sec": 5400,
    "status": "SUCCESS",
    "explain": True
}
response = requests.post(
    f"{BASE_URL}/predict/job-fail",
    json=payload
)
print("Prediction:", response.json())

# Test Anomaly Detection
payload = {
    "features": {
        "duration_sec": 5400,
        "duration_zscore": 1.6,
        "err_msg_len": 0
    }
}
response = requests.post(
    f"{BASE_URL}/detect/anomaly",
    json=payload
)
print("Anomaly Detection:", response.json())

🔍 วิธีที่ 5: ใช้ Postman / Insomnia

Import Collection

  1. สร้าง new collection
  2. เพิ่ม requests:

Health Check

  • Method: GET
  • URL: http://localhost:8000/health

Job Failure Prediction

  • Method: POST
  • URL: http://localhost:8000/predict/job-fail
  • Headers: Content-Type: application/json
  • Body (JSON):
{
  "zone": "prod",
  "job_nm": "daily_export",
  "job_start_time": "2026-01-21T01:00:00",
  "duration_sec": 5400,
  "status": "SUCCESS",
  "explain": true
}

Anomaly Detection

  • Method: POST
  • URL: http://localhost:8000/detect/anomaly
  • Headers: Content-Type: application/json
  • Body (JSON):
{
  "features": {
    "duration_sec": 5400,
    "duration_zscore": 1.6,
    "err_msg_len": 0
  }
}

✅ Expected Responses

Health Check Response

{
  "status": "healthy",
  "service": "job-failure-prediction",
  "models_loaded": {
    "predictor": true,
    "anomaly_detector": true
  }
}

Job Failure Prediction Response

{
  "fail_probability": 0.79,
  "risk_level": "MEDIUM",
  "top_drivers": [
    {
      "feature": "failure_rate_7",
      "shap_value": 0.30,
      "effect": "increase"
    }
  ],
  "recommended_actions": [
    "Monitor upstream dependencies and recent job history"
  ]
}

Anomaly Detection Response

{
  "reconstruction_error": 0.0235,
  "is_anomaly": true,
  "threshold": 0.01,
  "top_drivers": [
    {
      "feature": "duration_zscore",
      "error": 0.0142
    }
  ]
}

🐛 Troubleshooting

Connection Error

ปัญหา: ConnectionError: Could not connect to API

แก้ไข:

  • ตรวจสอบว่า API server กำลังรันอยู่
  • ตรวจสอบ URL และ port
  • ตรวจสอบ firewall settings

404 Not Found

ปัญหา: 404 Not Found

แก้ไข:

  • ตรวจสอบ endpoint path (/health, /predict/job-fail, /detect/anomaly)
  • ตรวจสอบว่า API server รันอยู่

500 Internal Server Error

ปัญหา: 500 Internal Server Error

แก้ไข:

  • ตรวจสอบ logs ของ API server
  • ตรวจสอบว่า models ถูก load ถูกต้อง
  • ตรวจสอบ request payload format

Models Not Loaded

ปัญหา: models_loaded: {"predictor": false, "anomaly_detector": false}

แก้ไข:

  • ตรวจสอบว่า models อยู่ใน models/ directory
  • ตรวจสอบว่า model files มีครบถ้วน
  • ตรวจสอบ logs สำหรับ error messages

📊 Test Cases

Test Case 1: Basic Health Check

curl http://localhost:8000/health

Expected: Status 200, models_loaded = true

Test Case 2: Prediction with Minimal Data

curl -X POST http://localhost:8000/predict/job-fail \
  -H "Content-Type: application/json" \
  -d '{"job_nm": "test_job", "job_start_time": "2026-01-21T01:00:00"}'

Expected: Status 200, fail_probability between 0-1

Test Case 3: Prediction with Full Data

curl -X POST http://localhost:8000/predict/job-fail \
  -H "Content-Type: application/json" \
  -d '{
    "zone": "prod",
    "job_nm": "daily_export",
    "job_start_time": "2026-01-21T01:00:00",
    "duration_sec": 5400,
    "status": "SUCCESS",
    "explain": true
  }'

Expected: Status 200, includes top_drivers and recommended_actions

Test Case 4: Anomaly Detection

curl -X POST http://localhost:8000/detect/anomaly \
  -H "Content-Type: application/json" \
  -d '{
    "features": {
      "duration_sec": 10000,
      "duration_zscore": 3.0,
      "err_msg_len": 0
    }
  }'

Expected: Status 200, is_anomaly = true (for high zscore)


🎯 Quick Test Checklist

  • Health endpoint returns 200
  • Models are loaded (check health response)
  • Prediction endpoint accepts requests
  • Prediction returns valid probability (0-1)
  • Risk level is one of: MINIMAL, LOW, MEDIUM, CRITICAL
  • SHAP explanations work (when explain=true)
  • Anomaly detection accepts feature dict
  • Anomaly detection returns is_anomaly boolean
  • Error handling works (invalid requests return 422/500)
  • CORS works (if testing from browser)

📚 Additional Resources

  • Swagger UI: /docs - Interactive API documentation
  • ReDoc: /redoc - Alternative API documentation
  • OpenAPI Schema: /openapi.json - Machine-readable API schema