-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_example.py
More file actions
259 lines (226 loc) · 8.49 KB
/
test_example.py
File metadata and controls
259 lines (226 loc) · 8.49 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
"""
Example script to test the FastAPI SageMaker Hugging Face integration
"""
import requests
import json
import time
from typing import Dict, Any
# API base URL
BASE_URL = "http://localhost:8000"
def test_health_check():
"""Test the health check endpoint"""
print("Testing health check...")
try:
response = requests.get(f"{BASE_URL}/health")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Health check failed: {e}")
return False
def test_model_info():
"""Test the model info endpoint"""
print("\nTesting model info...")
try:
response = requests.get(f"{BASE_URL}/model/info")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Model info failed: {e}")
return False
def test_single_prediction():
"""Test single prediction endpoint with question-answering data"""
print("\nTesting single prediction...")
# Example question-answering data matching the Hugging Face model format
test_data = {
"data": {
"question": "Which name is also used to describe the Amazon rainforest in English?",
"context": "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America."
},
"request_id": "test-qa-001",
"metadata": {
"source": "test-script",
"model_type": "question-answering",
"timestamp": time.time()
}
}
try:
response = requests.post(
f"{BASE_URL}/predict",
json=test_data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Single prediction failed: {e}")
return False
def test_single_prediction_alternative_format():
"""Test single prediction with alternative input format"""
print("\nTesting single prediction (alternative format)...")
# Alternative format with 'inputs' wrapper
test_data = {
"data": {
"inputs": {
"question": "What is the capital of France?",
"context": "Paris is the capital and most populous city of France. It is known for its art, fashion, gastronomy and culture."
}
},
"request_id": "test-qa-002"
}
try:
response = requests.post(
f"{BASE_URL}/predict",
json=test_data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Alternative format prediction failed: {e}")
return False
def test_batch_prediction():
"""Test batch prediction endpoint with question-answering data"""
print("\nTesting batch prediction...")
# Example batch data with multiple question-answering pairs
batch_data = [
{
"data": {
"question": "What is machine learning?",
"context": "Machine learning is a subset of artificial intelligence that enables computers to learn and make decisions from data without being explicitly programmed."
},
"request_id": "batch-qa-001"
},
{
"data": {
"question": "Where is the Eiffel Tower located?",
"context": "The Eiffel Tower is a wrought-iron lattice tower located on the Champ de Mars in Paris, France. It was constructed from 1887 to 1889."
},
"request_id": "batch-qa-002"
},
{
"data": {
"question": "What is the largest planet in our solar system?",
"context": "Jupiter is the largest planet in our solar system. It is a gas giant with a mass more than twice that of Saturn."
},
"request_id": "batch-qa-003"
}
]
try:
response = requests.post(
f"{BASE_URL}/predict/batch",
json=batch_data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Batch prediction failed: {e}")
return False
def test_root_endpoint():
"""Test the root endpoint"""
print("\nTesting root endpoint...")
try:
response = requests.get(f"{BASE_URL}/")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Root endpoint failed: {e}")
return False
def test_documentation_endpoints():
"""Test API documentation endpoints"""
print("\nTesting documentation endpoints...")
endpoints = [
("/docs", "Swagger UI"),
("/redoc", "ReDoc")
]
results = []
for endpoint, name in endpoints:
try:
response = requests.get(f"{BASE_URL}{endpoint}")
status = "✅ Available" if response.status_code == 200 else "❌ Not available"
print(f"{name} ({endpoint}): {status}")
results.append(response.status_code == 200)
except Exception as e:
print(f"{name} ({endpoint}): ❌ Error - {e}")
results.append(False)
return all(results)
def main():
"""Run all tests"""
print("=" * 60)
print("FastAPI SageMaker Hugging Face Integration Test Suite")
print("=" * 60)
tests = [
("Root Endpoint", test_root_endpoint),
("Health Check", test_health_check),
("Model Info", test_model_info),
("Single Prediction", test_single_prediction),
("Single Prediction (Alternative Format)", test_single_prediction_alternative_format),
("Batch Prediction", test_batch_prediction),
("Documentation Endpoints", test_documentation_endpoints),
]
results = []
for test_name, test_func in tests:
print(f"\n{'='*20} {test_name} {'='*20}")
try:
success = test_func()
results.append((test_name, success))
except Exception as e:
print(f"Test {test_name} failed with exception: {e}")
results.append((test_name, False))
# Summary
print("\n" + "=" * 60)
print("TEST SUMMARY")
print("=" * 60)
passed = 0
total = len(results)
for test_name, success in results:
status = "✅ PASS" if success else "❌ FAIL"
print(f"{test_name}: {status}")
if success:
passed += 1
print(f"\nOverall: {passed}/{total} tests passed")
if passed == total:
print("🎉 All tests passed!")
else:
print("⚠️ Some tests failed. Check the output above for details.")
print("\n" + "=" * 60)
print("USAGE EXAMPLES")
print("=" * 60)
print("""
Example curl commands for testing:
1. Single Question-Answering:
curl -X POST "http://localhost:8000/predict" \\
-H "Content-Type: application/json" \\
-d '{
"data": {
"question": "What is the capital of France?",
"context": "Paris is the capital and most populous city of France."
},
"request_id": "test-001"
}'
2. Batch Question-Answering:
curl -X POST "http://localhost:8000/predict/batch" \\
-H "Content-Type: application/json" \\
-d '[
{
"data": {
"question": "What is machine learning?",
"context": "Machine learning is a subset of artificial intelligence."
},
"request_id": "batch-001"
}
]'
3. Check Model Info:
curl -X GET "http://localhost:8000/model/info"
4. Health Check:
curl -X GET "http://localhost:8000/health"
""")
if __name__ == "__main__":
main()