test_curl_examples.sh
4.57 KB
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
#!/bin/bash
###############################################################################
# CN-CLIP REST API 快速测试脚本
#
# 用途:
# 测试 REST API 的各种功能
#
# 使用方法:
# ./examples/test_curl_examples.sh
#
###############################################################################
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
API_URL="http://localhost:51000"
TEST_IMAGE="https://oss.essa.cn/98532128-cf8e-456c-9e30-6f2a5ea0c19f.jpg"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}CN-CLIP REST API 测试${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# 测试 1: 健康检查
echo -e "${BLUE}测试 1: 健康检查${NC}"
echo "curl ${API_URL}/health"
echo ""
# 测试 2: 编码文本
echo -e "${BLUE}测试 2: 编码文本${NC}"
echo "curl -X POST ${API_URL}/encode/text \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"texts\": [\"一只可爱的猫咪\"]}'"
echo ""
response=$(curl -s -X POST "${API_URL}/encode/text" \
-H "Content-Type: application/json" \
-d '{"texts": ["一只可爱的猫咪"]}')
echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"
if echo "$response" | grep -q '"shape": \[1, 1024\]'; then
echo -e "${GREEN}✓ 文本编码成功${NC}"
else
echo -e "${RED}✗ 文本编码失败${NC}"
fi
echo ""
echo "按 Enter 继续..."
read
# 测试 3: 编码图像
echo -e "${BLUE}测试 3: 编码图像${NC}"
echo "curl -X POST ${API_URL}/encode/image \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"images\": [\"${TEST_IMAGE}\"]}'"
echo ""
response=$(curl -s -X POST "${API_URL}/encode/image" \
-H "Content-Type: application/json" \
-d "{\"images\": [\"${TEST_IMAGE}\"]}")
echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"
if echo "$response" | grep -q '"shape": \[1, 1024\]'; then
echo -e "${GREEN}✓ 图像编码成功${NC}"
else
echo -e "${RED}✗ 图像编码失败${NC}"
fi
echo ""
echo "按 Enter 继续..."
read
# 测试 4: 批量编码
echo -e "${BLUE}测试 4: 批量编码${NC}"
echo "curl -X POST ${API_URL}/encode/text \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"texts\": [\"文本1\", \"文本2\", \"文本3\"]}'"
echo ""
response=$(curl -s -X POST "${API_URL}/encode/text" \
-H "Content-Type: application/json" \
-d '{"texts": ["文本1", "文本2", "文本3"]}')
echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"
if echo "$response" | grep -q '"shape": \[3, 1024\]'; then
echo -e "${GREEN}✓ 批量编码成功${NC}"
else
echo -e "${RED}✗ 批量编码失败${NC}"
fi
echo ""
echo "按 Enter 继续..."
read
# 测试 5: 相似度计算
echo -e "${BLUE}测试 5: 相似度计算${NC}"
echo "curl -X POST ${API_URL}/similarity \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"text\": \"可爱的猫咪\", \"texts\": [\"一只可爱的小猫\", \"美丽的高山\"]}'"
echo ""
response=$(curl -s -X POST "${API_URL}/similarity" \
-H "Content-Type: application/json" \
-d '{"text": "可爱的猫咪", "texts": ["一只可爱的小猫", "美丽的高山"]}')
echo "$response" | python3 -m json.tool 2>/dev/null || echo "$response"
if echo "$response" | grep -q '"text_similarities"'; then
echo -e "${GREEN}✓ 相似度计算成功${NC}"
else
echo -e "${RED}✗ 相似度计算失败${NC}"
fi
echo ""
echo "按 Enter 继续..."
read
# 测试 6: 混合编码
echo -e "${BLUE}测试 6: 混合编码(文本+图像)${NC}"
echo "curl -X POST ${API_URL}/encode/mixed \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"data\": [\"一只可爱的猫咪\", \"${TEST_IMAGE}\"]}'"
echo ""
response=$(curl -s -X POST "${API_URL}/encode/mixed" \
-H "Content-Type: application/json" \
-d "{\"data\": [\"一只可爱的猫咪\", \"${TEST_IMAGE}\"]}")
echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"
if echo "$response" | grep -q '"shape": \[2, 1024\]'; then
echo -e "${GREEN}✓ 混合编码成功${NC}"
else
echo -e "${RED}✗ 混合编码失败${NC}"
fi
# 总结
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}测试总结${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "${GREEN}✓ 所有测试完成!${NC}"
echo ""
echo -e "下一步:"
echo " 1. 查看使用文档: cat docs/CNCLIP_CURL_GUIDE.md"
echo " 2. 在你的代码中调用 API"
echo " 3. 集成到你的应用"
echo ""