Spaces:
Paused
Paused
File size: 4,780 Bytes
6102083 |
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 171 172 173 174 175 176 177 |
# 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:
```bash
# 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ẻ:
```python
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
```bash
# 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
```python
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:
```json
[
{"label": "partner_intent_flirt", "score": 0.85},
{"label": "partner_intent_invite", "score": 0.12},
...
]
```
### Reply Suggestion API Response:
```json
{
"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
```python
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
|