Spaces:
Paused
Paused
Paul
commited on
Commit
·
6102083
1
Parent(s):
cfeaf50
update code
Browse files- API_TESTING.md +176 -0
- README.md +1 -0
- UI_TESTING.md +135 -0
- app.py +93 -17
- requirements.txt +2 -1
- test_api.py +162 -0
API_TESTING.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# API Testing Guide
|
| 2 |
+
|
| 3 |
+
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:
|
| 4 |
+
|
| 5 |
+
## Cách 1: Test trực tiếp qua Browser
|
| 6 |
+
|
| 7 |
+
1. Vào URL của Space: `https://your-space-name.hf.space`
|
| 8 |
+
2. Test trực tiếp trên UI
|
| 9 |
+
3. Hoặc xem API docs tại: `https://your-space-name.hf.space/docs`
|
| 10 |
+
|
| 11 |
+
## Cách 2: Test bằng Script Python
|
| 12 |
+
|
| 13 |
+
### Sử dụng script test_api.py:
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
# Test với conversation mặc định
|
| 17 |
+
python test_api.py --space_url https://your-space-name.hf.space
|
| 18 |
+
|
| 19 |
+
# Test với conversation tùy chỉnh
|
| 20 |
+
python test_api.py \
|
| 21 |
+
--space_url https://your-space-name.hf.space \
|
| 22 |
+
--test_conversation "Anh ơi tối nay có rảnh không? ||| Em cũng rảnh nè"
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
### Test từng API riêng lẻ:
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
import requests
|
| 29 |
+
|
| 30 |
+
SPACE_URL = "https://your-space-name.hf.space"
|
| 31 |
+
|
| 32 |
+
# 1. Test Intent API
|
| 33 |
+
response = requests.post(
|
| 34 |
+
f"{SPACE_URL}/api/intent",
|
| 35 |
+
json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
|
| 36 |
+
)
|
| 37 |
+
print("Intent:", response.json())
|
| 38 |
+
|
| 39 |
+
# 2. Test Tone API
|
| 40 |
+
response = requests.post(
|
| 41 |
+
f"{SPACE_URL}/api/tone",
|
| 42 |
+
json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
|
| 43 |
+
)
|
| 44 |
+
print("Tone:", response.json())
|
| 45 |
+
|
| 46 |
+
# 3. Test Reply Suggestion API
|
| 47 |
+
response = requests.post(
|
| 48 |
+
f"{SPACE_URL}/api/reply",
|
| 49 |
+
json={"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}
|
| 50 |
+
)
|
| 51 |
+
print("Reply:", response.json())
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
## Cách 3: Test bằng cURL
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
# Test Intent
|
| 58 |
+
curl -X POST "https://your-space-name.hf.space/api/intent" \
|
| 59 |
+
-H "Content-Type: application/json" \
|
| 60 |
+
-d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'
|
| 61 |
+
|
| 62 |
+
# Test Tone
|
| 63 |
+
curl -X POST "https://your-space-name.hf.space/api/tone" \
|
| 64 |
+
-H "Content-Type: application/json" \
|
| 65 |
+
-d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'
|
| 66 |
+
|
| 67 |
+
# Test Reply Suggestion
|
| 68 |
+
curl -X POST "https://your-space-name.hf.space/api/reply" \
|
| 69 |
+
-H "Content-Type: application/json" \
|
| 70 |
+
-d '{"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"]}'
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## Cách 4: Test bằng Gradio API
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
import requests
|
| 77 |
+
|
| 78 |
+
SPACE_URL = "https://your-space-name.hf.space"
|
| 79 |
+
|
| 80 |
+
# Reply Suggestion tab (fn_index=2)
|
| 81 |
+
response = requests.post(
|
| 82 |
+
f"{SPACE_URL}/api/predict",
|
| 83 |
+
json={
|
| 84 |
+
"data": ["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"],
|
| 85 |
+
"fn_index": 2 # Reply Suggestion tab
|
| 86 |
+
}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
result = response.json()
|
| 90 |
+
print("Result:", result)
|
| 91 |
+
|
| 92 |
+
# Extract suggestion
|
| 93 |
+
if "data" in result:
|
| 94 |
+
data = result["data"]
|
| 95 |
+
if isinstance(data, list) and len(data) >= 2:
|
| 96 |
+
suggestion = data[1] # Second element is the suggestion text
|
| 97 |
+
print(f"Generated Reply: {suggestion}")
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## Response Format
|
| 101 |
+
|
| 102 |
+
### Intent/Tone API Response:
|
| 103 |
+
```json
|
| 104 |
+
[
|
| 105 |
+
{"label": "partner_intent_flirt", "score": 0.85},
|
| 106 |
+
{"label": "partner_intent_invite", "score": 0.12},
|
| 107 |
+
...
|
| 108 |
+
]
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
### Reply Suggestion API Response:
|
| 112 |
+
```json
|
| 113 |
+
{
|
| 114 |
+
"data": [
|
| 115 |
+
{
|
| 116 |
+
"suggestion": "Mai được nha, em sắp xếp lại lịch cho.",
|
| 117 |
+
"intent": "flirt",
|
| 118 |
+
"intent_score": 0.85,
|
| 119 |
+
"tone": "warm",
|
| 120 |
+
"tone_score": 0.92,
|
| 121 |
+
"male": "Tối nay anh có lịch đột xuất.",
|
| 122 |
+
"female": "Thế mai được không?",
|
| 123 |
+
"model_type": "finetuned"
|
| 124 |
+
},
|
| 125 |
+
"Mai được nha, em sắp xếp lại lịch cho." # Suggestion text only
|
| 126 |
+
]
|
| 127 |
+
}
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Example Test Cases
|
| 131 |
+
|
| 132 |
+
```python
|
| 133 |
+
test_cases = [
|
| 134 |
+
{
|
| 135 |
+
"conversation": "Tối nay anh có lịch đột xuất. ||| Thế mai được không?",
|
| 136 |
+
"expected_intent": "invite_propose",
|
| 137 |
+
"expected_tone": "warm"
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"conversation": "Em dễ thương quá. ||| Cảm ơn anh nha",
|
| 141 |
+
"expected_intent": "flirt_charm",
|
| 142 |
+
"expected_tone": "warm"
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"conversation": "Anh đang làm gì đấy? ||| Đang làm việc",
|
| 146 |
+
"expected_intent": "rapport",
|
| 147 |
+
"expected_tone": "plain"
|
| 148 |
+
}
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
for test in test_cases:
|
| 152 |
+
response = requests.post(
|
| 153 |
+
f"{SPACE_URL}/api/reply",
|
| 154 |
+
json={"data": [test["conversation"]]}
|
| 155 |
+
)
|
| 156 |
+
print(f"\nInput: {test['conversation']}")
|
| 157 |
+
print(f"Response: {response.json()}")
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
## Troubleshooting
|
| 161 |
+
|
| 162 |
+
### API không response?
|
| 163 |
+
- Kiểm tra Space đã deploy xong chưa
|
| 164 |
+
- Check Logs tab trong Space để xem lỗi
|
| 165 |
+
- Đảm bảo URL đúng format: `https://your-space-name.hf.space`
|
| 166 |
+
|
| 167 |
+
### Rate limiting?
|
| 168 |
+
- Hugging Face Spaces có rate limit
|
| 169 |
+
- Đợi vài giây giữa các requests
|
| 170 |
+
- Hoặc upgrade Space tier
|
| 171 |
+
|
| 172 |
+
### Model not found error?
|
| 173 |
+
- Check xem fine-tuning đã hoàn thành chưa (xem Logs)
|
| 174 |
+
- App sẽ tự động fallback về base model/Inference API
|
| 175 |
+
- Check `model_type` trong response để biết đang dùng model nào
|
| 176 |
+
|
README.md
CHANGED
|
@@ -8,6 +8,7 @@ sdk_version: 4.0.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
# Lovebird - AI Conversation Reply Generator
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
duplicated_from: null
|
| 12 |
---
|
| 13 |
|
| 14 |
# Lovebird - AI Conversation Reply Generator
|
UI_TESTING.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# UI Testing Guide
|
| 2 |
+
|
| 3 |
+
Sau khi deploy thành công lên Hugging Face Spaces, bạn có thể test trực tiếp trên UI.
|
| 4 |
+
|
| 5 |
+
## 🚀 Truy cập UI
|
| 6 |
+
|
| 7 |
+
1. Vào URL của Space: `https://your-space-name.hf.space`
|
| 8 |
+
2. UI sẽ hiển thị với 3 tabs: **Intent**, **Tone**, và **Reply Suggestion**
|
| 9 |
+
|
| 10 |
+
## 📝 Cách Test trên UI
|
| 11 |
+
|
| 12 |
+
### Tab 1: Intent Classification
|
| 13 |
+
|
| 14 |
+
1. **Chọn tab "Intent"**
|
| 15 |
+
2. **Input có sẵn default value** hoặc click vào **Examples** để chọn conversation mẫu
|
| 16 |
+
3. **Click "Predict Intent"** hoặc nhấn **Enter**
|
| 17 |
+
4. **Xem kết quả** trong JSON format với các intent được sắp xếp theo score
|
| 18 |
+
|
| 19 |
+
**Example input:**
|
| 20 |
+
```
|
| 21 |
+
Tối nay anh có lịch đột xuất. ||| Thế mai được không?
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
### Tab 2: Tone Detection
|
| 25 |
+
|
| 26 |
+
1. **Chọn tab "Tone"**
|
| 27 |
+
2. **Input có sẵn default value** hoặc click vào **Examples**
|
| 28 |
+
3. **Click "Predict Tone"** hoặc nhấn **Enter**
|
| 29 |
+
4. **Xem kết quả** trong JSON format với các tone được sắp xếp theo score
|
| 30 |
+
|
| 31 |
+
**Example input:**
|
| 32 |
+
```
|
| 33 |
+
Em dễ thương quá. ||| Cảm ơn anh nha
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
### Tab 3: Reply Suggestion (Main Feature)
|
| 37 |
+
|
| 38 |
+
1. **Chọn tab "Reply Suggestion"**
|
| 39 |
+
2. **Input conversation** theo format: `"male message ||| female message"`
|
| 40 |
+
- Hoặc click vào **Examples** để chọn conversation mẫu
|
| 41 |
+
- Hoặc dùng default value có sẵn
|
| 42 |
+
3. **Click "Generate Reply Suggestion"** hoặc nhấn **Enter**
|
| 43 |
+
4. **Xem kết quả**:
|
| 44 |
+
- **Suggested Reply**: Câu trả lời được generate
|
| 45 |
+
- **Detailed Result**: JSON với đầy đủ thông tin (intent, tone, scores, model type)
|
| 46 |
+
|
| 47 |
+
**Example input:**
|
| 48 |
+
```
|
| 49 |
+
Tối nay anh có lịch đột xuất. ||| Thế mai được không?
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
**Expected output:**
|
| 53 |
+
- **Suggested Reply**: "Mai được nha, em sắp xếp lại lịch cho."
|
| 54 |
+
- **Detailed Result**: Intent, tone, scores, model type (finetuned/base)
|
| 55 |
+
|
| 56 |
+
## 🎯 Quick Test Examples
|
| 57 |
+
|
| 58 |
+
### Test Case 1: Invite/Schedule
|
| 59 |
+
```
|
| 60 |
+
Input: Tối nay anh có lịch đột xuất. ||| Thế mai được không?
|
| 61 |
+
Expected Intent: invite_propose hoặc rapport
|
| 62 |
+
Expected Tone: warm hoặc plain
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### Test Case 2: Flirt/Compliment
|
| 66 |
+
```
|
| 67 |
+
Input: Em dễ thương quá. ||| Cảm ơn anh nha
|
| 68 |
+
Expected Intent: flirt_charm
|
| 69 |
+
Expected Tone: warm hoặc playful
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
### Test Case 3: Small Talk
|
| 73 |
+
```
|
| 74 |
+
Input: Anh đang làm gì đấy? ||| Đang làm việc
|
| 75 |
+
Expected Intent: rapport hoặc smalltalk
|
| 76 |
+
Expected Tone: plain hoặc curious
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## ✅ Checklist Test
|
| 80 |
+
|
| 81 |
+
- [ ] Intent tab hiển thị và chạy được
|
| 82 |
+
- [ ] Tone tab hiển thị và chạy được
|
| 83 |
+
- [ ] Reply Suggestion tab hiển thị và chạy được
|
| 84 |
+
- [ ] Examples click được và load vào input
|
| 85 |
+
- [ ] Default values có sẵn trong input boxes
|
| 86 |
+
- [ ] Enter key trigger generate
|
| 87 |
+
- [ ] Output hiển thị đúng format
|
| 88 |
+
- [ ] Generated reply hợp lý và contextually appropriate
|
| 89 |
+
- [ ] Detailed result JSON có đầy đủ thông tin
|
| 90 |
+
- [ ] Model type được hiển thị (finetuned/base)
|
| 91 |
+
|
| 92 |
+
## 🔍 Kiểm tra kết quả
|
| 93 |
+
|
| 94 |
+
### Intent/Tone Response:
|
| 95 |
+
- ✅ Có ít nhất 1 result với score > 0
|
| 96 |
+
- ✅ Results được sắp xếp theo score giảm dần
|
| 97 |
+
- ✅ Labels không có prefix `partner_` (đã được clean)
|
| 98 |
+
|
| 99 |
+
### Reply Suggestion Response:
|
| 100 |
+
- ✅ Có suggestion text (không rỗng)
|
| 101 |
+
- ✅ Suggestion ≤ 25 từ
|
| 102 |
+
- ✅ Intent và tone đã được detect
|
| 103 |
+
- ✅ Model type được hiển thị
|
| 104 |
+
- ✅ Suggestion phù hợp với context
|
| 105 |
+
|
| 106 |
+
## 🐛 Troubleshooting
|
| 107 |
+
|
| 108 |
+
### UI không load?
|
| 109 |
+
- Check Space đã deploy xong chưa
|
| 110 |
+
- Xem Logs tab trong Space
|
| 111 |
+
|
| 112 |
+
### Examples không hoạt động?
|
| 113 |
+
- Click vào example sẽ tự động load vào input box
|
| 114 |
+
- Sau đó click button để generate
|
| 115 |
+
|
| 116 |
+
### Không có output?
|
| 117 |
+
- Check format input: `"male ||| female"` (có dấu |||)
|
| 118 |
+
- Xem Logs tab để check lỗi
|
| 119 |
+
- App sẽ tự động fallback nếu có lỗi
|
| 120 |
+
|
| 121 |
+
### Generated reply không tốt?
|
| 122 |
+
- Check xem model đã fine-tune xong chưa (xem Logs)
|
| 123 |
+
- Model type trong output sẽ cho biết đang dùng model nào
|
| 124 |
+
- Fine-tuned model sẽ cho kết quả tốt hơn
|
| 125 |
+
|
| 126 |
+
## 📸 Screenshot Tips
|
| 127 |
+
|
| 128 |
+
Khi test, có thể screenshot:
|
| 129 |
+
1. Input conversation
|
| 130 |
+
2. Generated reply
|
| 131 |
+
3. Detailed result JSON
|
| 132 |
+
4. Model type indicator
|
| 133 |
+
|
| 134 |
+
Để document và review sau.
|
| 135 |
+
|
app.py
CHANGED
|
@@ -212,39 +212,107 @@ with gr.Blocks(title=title) as demo:
|
|
| 212 |
gr.Markdown(f"# {title}\n{description}")
|
| 213 |
with gr.Tabs():
|
| 214 |
with gr.TabItem("Intent"):
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
intent_btn.click(predict_intent, inputs=intent_in, outputs=intent_out, api_name="intent")
|
|
|
|
| 219 |
|
| 220 |
with gr.TabItem("Tone"):
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
tone_btn.click(predict_tone, inputs=tone_in, outputs=tone_out, api_name="tone")
|
|
|
|
| 225 |
|
| 226 |
with gr.TabItem("Reply Suggestion"):
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
lines=4,
|
| 229 |
-
label="
|
| 230 |
-
|
|
|
|
| 231 |
)
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
interactive=False
|
| 237 |
)
|
| 238 |
-
reply_btn = gr.Button("Generate Reply Suggestion")
|
| 239 |
|
| 240 |
def generate_reply_with_extraction(conversation: str) -> Tuple[Dict[str, Any], str]:
|
| 241 |
"""Generate reply and extract suggestion text."""
|
| 242 |
result = predict_reply(conversation)
|
| 243 |
suggestion_text = ""
|
| 244 |
if "error" in result:
|
| 245 |
-
suggestion_text = f"Error: {result['error']}"
|
| 246 |
else:
|
| 247 |
suggestion_text = result.get("suggestion", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
return result, suggestion_text
|
| 249 |
|
| 250 |
reply_btn.click(
|
|
@@ -253,6 +321,13 @@ with gr.Blocks(title=title) as demo:
|
|
| 253 |
outputs=[reply_out, reply_suggestion_text],
|
| 254 |
api_name="reply"
|
| 255 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
|
@@ -266,4 +341,5 @@ if __name__ == "__main__":
|
|
| 266 |
except Exception as e:
|
| 267 |
print(f"Setup script error (non-critical, app will continue): {e}")
|
| 268 |
|
| 269 |
-
|
|
|
|
|
|
| 212 |
gr.Markdown(f"# {title}\n{description}")
|
| 213 |
with gr.Tabs():
|
| 214 |
with gr.TabItem("Intent"):
|
| 215 |
+
gr.Markdown("### 🎯 Intent Classification\nClassify the intent of a conversation (flirt, invite, rapport, etc.)")
|
| 216 |
+
|
| 217 |
+
intent_in = gr.Textbox(
|
| 218 |
+
lines=5,
|
| 219 |
+
label="Input Text",
|
| 220 |
+
placeholder='Enter conversation in format: "male message ||| female message"\n\nExample:\n"Tối nay anh có lịch đột xuất. ||| Thế mai được không?"',
|
| 221 |
+
value="Tối nay anh có lịch đột xuất. ||| Thế mai được không?"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
intent_examples = gr.Examples(
|
| 225 |
+
examples=[
|
| 226 |
+
["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"],
|
| 227 |
+
["Em dễ thương quá. ||| Cảm ơn anh nha"],
|
| 228 |
+
["Anh đang làm gì đấy? ||| Đang làm việc"],
|
| 229 |
+
],
|
| 230 |
+
inputs=intent_in,
|
| 231 |
+
label="Example Conversations (Click to use)"
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
intent_btn = gr.Button("Predict Intent", variant="primary")
|
| 235 |
+
intent_out = gr.JSON(label="Intent Predictions (sorted by score)")
|
| 236 |
+
|
| 237 |
intent_btn.click(predict_intent, inputs=intent_in, outputs=intent_out, api_name="intent")
|
| 238 |
+
intent_in.submit(predict_intent, inputs=intent_in, outputs=intent_out)
|
| 239 |
|
| 240 |
with gr.TabItem("Tone"):
|
| 241 |
+
gr.Markdown("### 🎭 Tone Detection\nDetect the tone of a conversation (warm, playful, assertive, etc.)")
|
| 242 |
+
|
| 243 |
+
tone_in = gr.Textbox(
|
| 244 |
+
lines=5,
|
| 245 |
+
label="Input Text",
|
| 246 |
+
placeholder='Enter conversation in format: "male message ||| female message"\n\nExample:\n"Tối nay anh có lịch đột xuất. ||| Thế mai được không?"',
|
| 247 |
+
value="Tối nay anh có lịch đột xuất. ||| Thế mai được không?"
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
tone_examples = gr.Examples(
|
| 251 |
+
examples=[
|
| 252 |
+
["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"],
|
| 253 |
+
["Em dễ thương quá. ||| Cảm ơn anh nha"],
|
| 254 |
+
["Anh đang làm gì đấy? ||| Đang làm việc"],
|
| 255 |
+
],
|
| 256 |
+
inputs=tone_in,
|
| 257 |
+
label="Example Conversations (Click to use)"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
tone_btn = gr.Button("Predict Tone", variant="primary")
|
| 261 |
+
tone_out = gr.JSON(label="Tone Predictions (sorted by score)")
|
| 262 |
+
|
| 263 |
tone_btn.click(predict_tone, inputs=tone_in, outputs=tone_out, api_name="tone")
|
| 264 |
+
tone_in.submit(predict_tone, inputs=tone_in, outputs=tone_out)
|
| 265 |
|
| 266 |
with gr.TabItem("Reply Suggestion"):
|
| 267 |
+
gr.Markdown("### 🎯 Generate AI Reply Suggestion\nEnter a conversation and get an AI-generated reply suggestion based on intent and tone analysis.")
|
| 268 |
+
|
| 269 |
+
with gr.Row():
|
| 270 |
+
with gr.Column(scale=2):
|
| 271 |
+
reply_in = gr.Textbox(
|
| 272 |
+
lines=5,
|
| 273 |
+
label="Conversation",
|
| 274 |
+
placeholder='Enter conversation in format: "male message ||| female message"\n\nExample:\n"Tối nay anh có lịch đột xuất. ||| Thế mai được không?"',
|
| 275 |
+
value="Tối nay anh có lịch đột xuất. ||| Thế mai được không?"
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
reply_examples = gr.Examples(
|
| 279 |
+
examples=[
|
| 280 |
+
["Tối nay anh có lịch đột xuất. ||| Thế mai được không?"],
|
| 281 |
+
["Em dễ thương quá. ||| Cảm ơn anh nha"],
|
| 282 |
+
["Anh đang làm gì đấy? ||| Đang làm việc"],
|
| 283 |
+
["Cuối tuần em có rảnh không? ||| Có nè, sao anh?"],
|
| 284 |
+
],
|
| 285 |
+
inputs=reply_in,
|
| 286 |
+
label="Example Conversations (Click to use)"
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
with gr.Column(scale=1):
|
| 290 |
+
reply_btn = gr.Button("Generate Reply Suggestion", variant="primary", size="lg")
|
| 291 |
+
|
| 292 |
+
reply_suggestion_text = gr.Textbox(
|
| 293 |
lines=4,
|
| 294 |
+
label="💬 Suggested Reply",
|
| 295 |
+
interactive=False,
|
| 296 |
+
placeholder="Generated reply will appear here..."
|
| 297 |
)
|
| 298 |
+
|
| 299 |
+
reply_out = gr.JSON(
|
| 300 |
+
label="📊 Detailed Result (Intent, Tone, Scores, etc.)",
|
| 301 |
+
visible=True
|
|
|
|
| 302 |
)
|
|
|
|
| 303 |
|
| 304 |
def generate_reply_with_extraction(conversation: str) -> Tuple[Dict[str, Any], str]:
|
| 305 |
"""Generate reply and extract suggestion text."""
|
| 306 |
result = predict_reply(conversation)
|
| 307 |
suggestion_text = ""
|
| 308 |
if "error" in result:
|
| 309 |
+
suggestion_text = f"❌ Error: {result['error']}"
|
| 310 |
else:
|
| 311 |
suggestion_text = result.get("suggestion", "")
|
| 312 |
+
# Add model type info if available
|
| 313 |
+
model_type = result.get("model_type", "")
|
| 314 |
+
if model_type:
|
| 315 |
+
suggestion_text += f"\n\n(Using: {model_type} model)"
|
| 316 |
return result, suggestion_text
|
| 317 |
|
| 318 |
reply_btn.click(
|
|
|
|
| 321 |
outputs=[reply_out, reply_suggestion_text],
|
| 322 |
api_name="reply"
|
| 323 |
)
|
| 324 |
+
|
| 325 |
+
# Allow Enter key to submit
|
| 326 |
+
reply_in.submit(
|
| 327 |
+
generate_reply_with_extraction,
|
| 328 |
+
inputs=reply_in,
|
| 329 |
+
outputs=[reply_out, reply_suggestion_text]
|
| 330 |
+
)
|
| 331 |
|
| 332 |
|
| 333 |
if __name__ == "__main__":
|
|
|
|
| 341 |
except Exception as e:
|
| 342 |
print(f"Setup script error (non-critical, app will continue): {e}")
|
| 343 |
|
| 344 |
+
# Launch with sharing enabled for easy access
|
| 345 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
gradio
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
huggingface_hub
|
|
@@ -15,4 +15,5 @@ peft
|
|
| 15 |
bitsandbytes
|
| 16 |
datasets
|
| 17 |
pandas
|
|
|
|
| 18 |
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
huggingface_hub
|
|
|
|
| 15 |
bitsandbytes
|
| 16 |
datasets
|
| 17 |
pandas
|
| 18 |
+
einops
|
| 19 |
|
test_api.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
API Testing Script for Lovebird Reply Generator
|
| 3 |
+
|
| 4 |
+
This script helps you test the deployed Hugging Face Space API.
|
| 5 |
+
|
| 6 |
+
Usage:
|
| 7 |
+
python test_api.py --space_url https://your-space-name.hf.space
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
import requests
|
| 12 |
+
import json
|
| 13 |
+
from typing import Dict, Any
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_intent_api(space_url: str, text: str):
|
| 17 |
+
"""Test Intent classification API"""
|
| 18 |
+
api_url = f"{space_url}/api/intent"
|
| 19 |
+
|
| 20 |
+
response = requests.post(api_url, json={"data": [text]})
|
| 21 |
+
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
result = response.json()
|
| 24 |
+
print("\n✅ Intent API Response:")
|
| 25 |
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
| 26 |
+
return result
|
| 27 |
+
else:
|
| 28 |
+
print(f"\n❌ Intent API Error: {response.status_code}")
|
| 29 |
+
print(response.text)
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_tone_api(space_url: str, text: str):
|
| 34 |
+
"""Test Tone classification API"""
|
| 35 |
+
api_url = f"{space_url}/api/tone"
|
| 36 |
+
|
| 37 |
+
response = requests.post(api_url, json={"data": [text]})
|
| 38 |
+
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
result = response.json()
|
| 41 |
+
print("\n✅ Tone API Response:")
|
| 42 |
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
| 43 |
+
return result
|
| 44 |
+
else:
|
| 45 |
+
print(f"\n❌ Tone API Error: {response.status_code}")
|
| 46 |
+
print(response.text)
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_reply_api(space_url: str, conversation: str):
|
| 51 |
+
"""Test Reply Suggestion API"""
|
| 52 |
+
api_url = f"{space_url}/api/reply"
|
| 53 |
+
|
| 54 |
+
response = requests.post(api_url, json={"data": [conversation]})
|
| 55 |
+
|
| 56 |
+
if response.status_code == 200:
|
| 57 |
+
result = response.json()
|
| 58 |
+
print("\n✅ Reply Suggestion API Response:")
|
| 59 |
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
| 60 |
+
return result
|
| 61 |
+
else:
|
| 62 |
+
print(f"\n❌ Reply API Error: {response.status_code}")
|
| 63 |
+
print(response.text)
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_gradio_api(space_url: str, conversation: str, fn_index: int = 2):
|
| 68 |
+
"""Test using Gradio API endpoint"""
|
| 69 |
+
api_url = f"{space_url}/api/predict"
|
| 70 |
+
|
| 71 |
+
payload = {
|
| 72 |
+
"data": [conversation],
|
| 73 |
+
"fn_index": fn_index # 2 = Reply Suggestion tab
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
response = requests.post(api_url, json=payload)
|
| 77 |
+
|
| 78 |
+
if response.status_code == 200:
|
| 79 |
+
result = response.json()
|
| 80 |
+
print("\n✅ Gradio API Response:")
|
| 81 |
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
| 82 |
+
return result
|
| 83 |
+
else:
|
| 84 |
+
print(f"\n❌ Gradio API Error: {response.status_code}")
|
| 85 |
+
print(response.text)
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
parser = argparse.ArgumentParser(description="Test Lovebird API endpoints")
|
| 91 |
+
parser.add_argument(
|
| 92 |
+
"--space_url",
|
| 93 |
+
type=str,
|
| 94 |
+
required=True,
|
| 95 |
+
help="URL of your Hugging Face Space (e.g., https://your-space-name.hf.space)"
|
| 96 |
+
)
|
| 97 |
+
parser.add_argument(
|
| 98 |
+
"--test_conversation",
|
| 99 |
+
type=str,
|
| 100 |
+
default="Tối nay anh có lịch đột xuất. ||| Thế mai được không?",
|
| 101 |
+
help="Conversation to test (format: 'male ||| female')"
|
| 102 |
+
)
|
| 103 |
+
parser.add_argument(
|
| 104 |
+
"--test_text",
|
| 105 |
+
type=str,
|
| 106 |
+
default="Tối nay anh có lịch đột xuất. ||| Thế mai được không?",
|
| 107 |
+
help="Text to test for intent/tone"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
args = parser.parse_args()
|
| 111 |
+
|
| 112 |
+
print("=" * 60)
|
| 113 |
+
print("Testing Lovebird API")
|
| 114 |
+
print("=" * 60)
|
| 115 |
+
print(f"Space URL: {args.space_url}")
|
| 116 |
+
print(f"Test conversation: {args.test_conversation}")
|
| 117 |
+
print()
|
| 118 |
+
|
| 119 |
+
# Test 1: Intent API
|
| 120 |
+
print("=" * 60)
|
| 121 |
+
print("Test 1: Intent Classification")
|
| 122 |
+
print("=" * 60)
|
| 123 |
+
intent_result = test_intent_api(args.space_url, args.test_text)
|
| 124 |
+
|
| 125 |
+
# Test 2: Tone API
|
| 126 |
+
print("\n" + "=" * 60)
|
| 127 |
+
print("Test 2: Tone Classification")
|
| 128 |
+
print("=" * 60)
|
| 129 |
+
tone_result = test_tone_api(args.space_url, args.test_text)
|
| 130 |
+
|
| 131 |
+
# Test 3: Reply Suggestion API
|
| 132 |
+
print("\n" + "=" * 60)
|
| 133 |
+
print("Test 3: Reply Suggestion (Direct API)")
|
| 134 |
+
print("=" * 60)
|
| 135 |
+
reply_result = test_reply_api(args.space_url, args.test_conversation)
|
| 136 |
+
|
| 137 |
+
# Test 4: Gradio API (alternative)
|
| 138 |
+
print("\n" + "=" * 60)
|
| 139 |
+
print("Test 4: Reply Suggestion (Gradio API)")
|
| 140 |
+
print("=" * 60)
|
| 141 |
+
gradio_result = test_gradio_api(args.space_url, args.test_conversation)
|
| 142 |
+
|
| 143 |
+
# Summary
|
| 144 |
+
print("\n" + "=" * 60)
|
| 145 |
+
print("Test Summary")
|
| 146 |
+
print("=" * 60)
|
| 147 |
+
print(f"Intent API: {'✅ PASS' if intent_result else '❌ FAIL'}")
|
| 148 |
+
print(f"Tone API: {'✅ PASS' if tone_result else '❌ FAIL'}")
|
| 149 |
+
print(f"Reply API: {'✅ PASS' if reply_result else '❌ FAIL'}")
|
| 150 |
+
print(f"Gradio API: {'✅ PASS' if gradio_result else '❌ FAIL'}")
|
| 151 |
+
|
| 152 |
+
# Show generated reply if available
|
| 153 |
+
if reply_result and "data" in reply_result:
|
| 154 |
+
reply_data = reply_result["data"][0] if isinstance(reply_result["data"], list) else reply_result["data"]
|
| 155 |
+
if isinstance(reply_data, list) and len(reply_data) > 1:
|
| 156 |
+
suggestion = reply_data[1] # Second element is the suggestion text
|
| 157 |
+
print(f"\n💬 Generated Reply: {suggestion}")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
main()
|
| 162 |
+
|