lovebird25 / API_TESTING.md
Paul
update code
6102083

A newer version of the Gradio SDK is available: 6.2.0

Upgrade

API Testing Guide

Sau khi deploy thành công lên Hugging Face Spaces, bạn có thể test API bằng các cách sau:

Cách 1: Test trực tiếp qua Browser

  1. Vào URL của Space: https://your-space-name.hf.space
  2. Test trực tiếp trên UI
  3. Hoặc xem API docs tại: https://your-space-name.hf.space/docs

Cách 2: Test bằng Script Python

Sử dụng script test_api.py:

# Test với conversation mặc định
python test_api.py --space_url https://your-space-name.hf.space

# Test với conversation tùy chỉnh
python test_api.py \
    --space_url https://your-space-name.hf.space \
    --test_conversation "Anh ơi tối nay có rảnh không? ||| Em cũng rảnh nè"

Test từng API riêng lẻ:

import requests

SPACE_URL = "https://your-space-name.hf.space"

# 1. Test Intent API
response = requests.post(
    f"{SPACE_URL}/api/intent",
    json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
)
print("Intent:", response.json())

# 2. Test Tone API
response = requests.post(
    f"{SPACE_URL}/api/tone",
    json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
)
print("Tone:", response.json())

# 3. Test Reply Suggestion API
response = requests.post(
    f"{SPACE_URL}/api/reply",
    json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
)
print("Reply:", response.json())

Cách 3: Test bằng cURL

# Test Intent
curl -X POST "https://your-space-name.hf.space/api/intent" \
  -H "Content-Type: application/json" \
  -d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'

# Test Tone
curl -X POST "https://your-space-name.hf.space/api/tone" \
  -H "Content-Type: application/json" \
  -d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'

# Test Reply Suggestion
curl -X POST "https://your-space-name.hf.space/api/reply" \
  -H "Content-Type: application/json" \
  -d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'

Cách 4: Test bằng Gradio API

import requests

SPACE_URL = "https://your-space-name.hf.space"

# Reply Suggestion tab (fn_index=2)
response = requests.post(
    f"{SPACE_URL}/api/predict",
    json={
        "data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"],
        "fn_index": 2  # Reply Suggestion tab
    }
)

result = response.json()
print("Result:", result)

# Extract suggestion
if "data" in result:
    data = result["data"]
    if isinstance(data, list) and len(data) >= 2:
        suggestion = data[1]  # Second element is the suggestion text
        print(f"Generated Reply: {suggestion}")

Response Format

Intent/Tone API Response:

[
  {"label": "partner_intent_flirt", "score": 0.85},
  {"label": "partner_intent_invite", "score": 0.12},
  ...
]

Reply Suggestion API Response:

{
  "data": [
    {
      "suggestion": "Mai được nha, em sắp xếp lại lịch cho.",
      "intent": "flirt",
      "intent_score": 0.85,
      "tone": "warm",
      "tone_score": 0.92,
      "male": "Tối nay anh có lịch đột xuất.",
      "female": "Thế mai được không?",
      "model_type": "finetuned"
    },
    "Mai được nha, em sắp xếp lại lịch cho."  # Suggestion text only
  ]
}

Example Test Cases

test_cases = [
    {
        "conversation": "Tối nay anh có lịch đột xuất. ||| Thế mai được không?",
        "expected_intent": "invite_propose",
        "expected_tone": "warm"
    },
    {
        "conversation": "Em dễ thương quá. ||| Cảm ơn anh nha",
        "expected_intent": "flirt_charm",
        "expected_tone": "warm"
    },
    {
        "conversation": "Anh đang làm gì đấy? ||| Đang làm việc",
        "expected_intent": "rapport",
        "expected_tone": "plain"
    }
]

for test in test_cases:
    response = requests.post(
        f"{SPACE_URL}/api/reply",
        json={"data": [test["conversation"]]}
    )
    print(f"\nInput: {test['conversation']}")
    print(f"Response: {response.json()}")

Troubleshooting

API không response?

  • Kiểm tra Space đã deploy xong chưa
  • Check Logs tab trong Space để xem lỗi
  • Đảm bảo URL đúng format: https://your-space-name.hf.space

Rate limiting?

  • Hugging Face Spaces có rate limit
  • Đợi vài giây giữa các requests
  • Hoặc upgrade Space tier

Model not found error?

  • Check xem fine-tuning đã hoàn thành chưa (xem Logs)
  • App sẽ tự động fallback về base model/Inference API
  • Check model_type trong response để biết đang dùng model nào