-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model_serving.py
More file actions
165 lines (134 loc) · 4.55 KB
/
Copy pathtest_model_serving.py
File metadata and controls
165 lines (134 loc) · 4.55 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
#!/usr/bin/env python3
"""
Test script for MLflow model serving endpoint.
Usage:
python test_model_serving.py [--port PORT] [--run-id RUN_ID]
"""
import requests
import json
import numpy as np
import argparse
def test_model_endpoint(port=5001, num_samples=5):
"""
Test the MLflow model serving endpoint.
Args:
port: Port number where the model is served
num_samples: Number of test samples to send
"""
url = f"http://localhost:{port}/invocations"
headers = {"Content-Type": "application/json"}
print("=" * 80)
print(" MLflow Model Serving Test")
print("=" * 80)
print(f"\nEndpoint: {url}")
# Generate random test data (20 features to match the tutorial models)
print(f"\nGenerating {num_samples} test samples with 20 features...")
test_data = np.random.randn(num_samples, 20).tolist()
payload = {
"inputs": test_data
}
print(f"Data shape: ({num_samples}, 20)")
print("\nSending request...")
try:
response = requests.post(
url,
headers=headers,
data=json.dumps(payload),
timeout=10
)
if response.status_code == 200:
print("\n" + "=" * 80)
print(" ✓ SUCCESS!")
print("=" * 80)
predictions = response.json()
if isinstance(predictions, dict) and 'predictions' in predictions:
preds = predictions['predictions']
else:
preds = predictions
print(f"\nReceived {len(preds)} predictions:")
print("-" * 80)
for i, pred in enumerate(preds, 1):
print(f"Sample {i}: {pred}")
print("\n✓ Model is serving correctly!")
return True
else:
print("\n" + "=" * 80)
print(" ✗ ERROR")
print("=" * 80)
print(f"\nStatus Code: {response.status_code}")
print(f"Response: {response.text}")
return False
except requests.exceptions.ConnectionError:
print("\n" + "=" * 80)
print(" ✗ CONNECTION ERROR")
print("=" * 80)
print("\nCould not connect to the server.")
print("\nMake sure the model is serving:")
print(f" python -m mlflow models serve -m MODEL_URI -p {port} --no-conda")
return False
except requests.exceptions.Timeout:
print("\n" + "=" * 80)
print(" ✗ TIMEOUT ERROR")
print("=" * 80)
print("\nRequest timed out. The server might be starting up.")
print("Wait a few seconds and try again.")
return False
except Exception as e:
print("\n" + "=" * 80)
print(" ✗ UNEXPECTED ERROR")
print("=" * 80)
print(f"\nError: {str(e)}")
import traceback
traceback.print_exc()
return False
def get_health_check(port=5001):
"""Check if the server is running."""
try:
response = requests.get(f"http://localhost:{port}/ping", timeout=2)
return response.status_code == 200
except:
return False
def main():
"""Main function."""
parser = argparse.ArgumentParser(
description="Test MLflow model serving endpoint"
)
parser.add_argument(
"--port",
type=int,
default=5001,
help="Port number (default: 5001)"
)
parser.add_argument(
"--samples",
type=int,
default=5,
help="Number of test samples (default: 5)"
)
args = parser.parse_args()
# Check if server is up
print("Checking if server is running...")
if get_health_check(args.port):
print("✓ Server is running\n")
else:
print("⚠ Server health check failed")
print(f"Make sure model is serving on port {args.port}\n")
# Test the endpoint
success = test_model_endpoint(args.port, args.samples)
if success:
print("\n" + "=" * 80)
print(" Test completed successfully!")
print("=" * 80)
print("\nYou can now make predictions using:")
print(f" curl -X POST http://localhost:{args.port}/invocations \\")
print(" -H 'Content-Type: application/json' \\")
print(" -d '{\"inputs\": [[...20 features...]]}'")
return 0
else:
print("\n" + "=" * 80)
print(" Test failed")
print("=" * 80)
return 1
if __name__ == "__main__":
import sys
sys.exit(main())