-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_api_usage.py
More file actions
305 lines (249 loc) · 9.09 KB
/
example_api_usage.py
File metadata and controls
305 lines (249 loc) · 9.09 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Example script demonstrating how to use the Name Matching API.
This script shows:
1. How to make single predictions
2. How to make batch predictions
3. How to handle responses and errors
4. How to work with different thresholds
Usage:
# Start the API first
python app.py
# In another terminal, run this script
python example_api_usage.py
"""
import requests
def check_api_health(base_url: str = "http://localhost:5001") -> bool:
"""
Check if the API is running and healthy.
:param base_url: Base URL of the API
:return: True if API is healthy, False otherwise
"""
try:
response = requests.get(f"{base_url}/health", timeout=5)
if response.status_code == 200:
print("✓ API is healthy and running")
return True
else:
print(f"✗ API returned status code: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"✗ Could not connect to API: {e}")
print(" Make sure the API is running: python app.py")
return False
def get_model_info(base_url: str = "http://localhost:5001") -> None:
"""
Get and display model information.
:param base_url: Base URL of the API
"""
try:
response = requests.get(f"{base_url}/info", timeout=5)
if response.status_code == 200:
data = response.json()
print("\nModel Information:")
print(f" Type: {data['model']['type']}")
print(f" Number of features: {data['model']['num_features']}")
print(f" Features: {', '.join(data['model']['features'])}")
else:
print(f"Failed to get model info: {response.status_code}")
except Exception as e:
print(f"Error getting model info: {e}")
def example_single_prediction(base_url: str = "http://localhost:5001") -> None:
"""
Example of making a single prediction.
:param base_url: Base URL of the API
"""
print("\n" + "=" * 60)
print("Example 1: Single Prediction")
print("=" * 60)
request_data = {
"CUST_NAME": "John Smith",
"COUNTERPART_NAME": "J. Smith",
"FT_NO": "FT12345",
"threshold": 0.85,
}
print(f"\nRequest:")
print(f" Customer Name: {request_data['CUST_NAME']}")
print(f" Counterpart Name: {request_data['COUNTERPART_NAME']}")
print(f" Transaction: {request_data['FT_NO']}")
print(f" Threshold: {request_data['threshold']}")
try:
response = requests.post(f"{base_url}/predict", json=request_data, timeout=30)
if response.status_code == 200:
data = response.json()
result = data["result"]
print(f"\nResponse:")
print(f" Status: {data['status']}")
print(f" Match Label: {result['match_label']}")
print(f" Prediction: {result['prediction']}")
print(
f" Probability: {result['probability']:.4f} ({result['probability']*100:.2f}%)"
)
print(f" Features:")
for feature, value in result["features"].items():
print(f" - {feature}: {value:.4f}")
else:
print(f"\nError: {response.status_code}")
print(response.json())
except Exception as e:
print(f"Error: {e}")
def example_batch_prediction(base_url: str = "http://localhost:5001") -> None:
"""
Example of making batch predictions.
:param base_url: Base URL of the API
"""
print("\n" + "=" * 60)
print("Example 2: Batch Prediction")
print("=" * 60)
request_data = {
"pairs": [
{
"CUST_NAME": "John Smith",
"COUNTERPART_NAME": "J. Smith",
"FT_NO": "FT001",
},
{
"CUST_NAME": "Apple Inc.",
"COUNTERPART_NAME": "Apple Corporation",
"FT_NO": "FT002",
},
{
"CUST_NAME": "Microsoft Corporation",
"COUNTERPART_NAME": "Amazon Web Services",
"FT_NO": "FT003",
},
{
"CUST_NAME": "Jane Marie Doe",
"COUNTERPART_NAME": "Jane M. Doe",
"FT_NO": "FT004",
},
],
"threshold": 0.85,
}
print(f"\nRequest: {len(request_data['pairs'])} name pairs")
for i, pair in enumerate(request_data["pairs"], 1):
print(f" {i}. {pair['CUST_NAME']} <-> {pair['COUNTERPART_NAME']}")
try:
response = requests.post(
f"{base_url}/predict/batch", json=request_data, timeout=60
)
if response.status_code == 200:
data = response.json()
print(f"\nResponse:")
print(f" Status: {data['status']}")
print(f" Count: {data['count']}")
print(f"\nResults:")
for result in data["results"]:
print(f"\n Transaction: {result['ft_no']}")
print(f" Names: {result['name_x']} <-> {result['name_y']}")
print(f" Match: {result['match_label']}")
print(
f" Probability: {result['probability']:.4f} ({result['probability']*100:.2f}%)"
)
else:
print(f"\nError: {response.status_code}")
print(response.json())
except Exception as e:
print(f"Error: {e}")
def example_threshold_comparison(base_url: str = "http://localhost:5001") -> None:
"""
Example showing how different thresholds affect predictions.
:param base_url: Base URL of the API
"""
print("\n" + "=" * 60)
print("Example 3: Threshold Comparison")
print("=" * 60)
test_pair = {
"CUST_NAME": "Jane Doe",
"COUNTERPART_NAME": "J. Doe",
"FT_NO": "FT_THRESHOLD_TEST",
}
thresholds = [0.5, 0.7, 0.85, 0.95]
print(
f"\nTesting with: {test_pair['CUST_NAME']} <-> {test_pair['COUNTERPART_NAME']}"
)
print(f"\nThreshold Comparison:")
try:
for threshold in thresholds:
request_data = {**test_pair, "threshold": threshold}
response = requests.post(
f"{base_url}/predict", json=request_data, timeout=30
)
if response.status_code == 200:
result = response.json()["result"]
match_symbol = "✓" if result["prediction"] == 1 else "✗"
print(
f" {match_symbol} Threshold {threshold:.2f}: "
f"{result['match_label']:10s} (prob: {result['probability']:.4f})"
)
else:
print(f" Error at threshold {threshold}")
except Exception as e:
print(f"Error: {e}")
def example_error_handling(base_url: str = "http://localhost:5001") -> None:
"""
Example showing error handling for invalid inputs.
:param base_url: Base URL of the API
"""
print("\n" + "=" * 60)
print("Example 4: Error Handling")
print("=" * 60)
# Test with empty name
print("\n1. Testing with empty customer name:")
request_data = {
"CUST_NAME": "",
"COUNTERPART_NAME": "John Doe",
"FT_NO": "FT_ERROR_1",
}
try:
response = requests.post(f"{base_url}/predict", json=request_data, timeout=30)
data = response.json()
print(f" Status Code: {response.status_code}")
print(f" Response: {data['status']} - {data['message']}")
except Exception as e:
print(f" Error: {e}")
# Test with invalid threshold
print("\n2. Testing with invalid threshold:")
request_data = {
"CUST_NAME": "John Doe",
"COUNTERPART_NAME": "Jane Doe",
"FT_NO": "FT_ERROR_2",
"threshold": 1.5, # Invalid: > 1
}
try:
response = requests.post(f"{base_url}/predict", json=request_data, timeout=30)
data = response.json()
print(f" Status Code: {response.status_code}")
print(f" Response: {data['status']} - {data['message']}")
except Exception as e:
print(f" Error: {e}")
# Test with missing field
print("\n3. Testing with missing counterpart name:")
request_data = {"CUST_NAME": "John Doe", "FT_NO": "FT_ERROR_3"}
try:
response = requests.post(f"{base_url}/predict", json=request_data, timeout=30)
data = response.json()
print(f" Status Code: {response.status_code}")
print(f" Response: {data['status']} - {data['message']}")
except Exception as e:
print(f" Error: {e}")
def main():
"""Run all examples."""
base_url = "http://localhost:5001"
print("\n" + "=" * 60)
print("Name Matching API - Usage Examples")
print("=" * 60)
# Check if API is running
if not check_api_health(base_url):
return
# Get model information
get_model_info(base_url)
# Run examples
example_single_prediction(base_url)
example_batch_prediction(base_url)
example_threshold_comparison(base_url)
example_error_handling(base_url)
print("\n" + "=" * 60)
print("All examples completed!")
print("=" * 60 + "\n")
if __name__ == "__main__":
main()