-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_client.py
116 lines (76 loc) · 3.46 KB
/
python_client.py
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
import requests
import json
import numpy as np
import time
#import pandas as pd
import argparse
import threading
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
def sent_request(args):
url = server_url + '/' + 'predict'
begin = time.time()
#json_data = json.dumps(data.to_dict('list'),cls=NumpyEncoder)
#print('sent request with json data:')
#print(json_data)
response = requests.post(url, json=json_data)
response_dict = json.loads(response.text)
end = time.time()
print('elapse %.4f s'%(end-begin))
print(response_dict['pred_prob'])
if len(response_dict['shap_values']) > 0:
for i in range(len(response_dict['shap_values'])): # 一次解釋多筆資料
print('shap explain for {} pridict result:'.format(i))
shap_values_np = np.array(response_dict['shap_values'][i])
shap_values_np = np.squeeze(shap_values_np)
postive_feature_ind = np.where(shap_values_np>0)[0].tolist()
negative_feature_ind = np.where(shap_values_np<0)[0].tolist()
feature_names_np = np.array(response_dict['feature_names'])
positive_feature_np = feature_names_np[postive_feature_ind]
negative_feature_np = feature_names_np[negative_feature_ind]
positive_shap_np = shap_values_np[postive_feature_ind]
negative_shap_np = shap_values_np[negative_feature_ind]
positive_sort_index = np.argsort(-positive_shap_np)
negative_sort_index = np.argsort(negative_shap_np)
print('positive factors:')
print( positive_feature_np[positive_sort_index])
print(positive_shap_np[positive_sort_index])
print('\nnegative factors:')
print( negative_feature_np[negative_sort_index])
print(negative_shap_np[negative_sort_index])
print('='*50)
def multi_send(args, thread, times):
for i in range(times):
print('thread: %d' %thread)
sent_request(args)
#time.sleep(5)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'a python client test')
parser.add_argument('--data_dir', type=str, help='path to data')
parser.add_argument('--port', default='5566', help='port')
parser.add_argument('--thread', type=int, default=1, help='how many thread used to send request')
parser.add_argument('--times', type=int, default=1, help='request times in each thread')
args = parser.parse_args()
server_url = 'http://127.0.0.1:' + args.port
#data = pd.read_csv(args.data_dir)
with open(args.data_dir, 'r') as file:
json_data = json.load(file)
if args.thread == 1:
sent_request(args)
elif(args.thread >1):
print('create multi-thread to send request...')
thread_start = time.time()
for i in range(args.thread):
t = threading.Thread(target = multi_send, args=(args, i, args.times) )
t.start()
t.join()
time_cost = time.time()-thread_start
time.sleep(1)
print('finish all request, total elapse %.4f s'%time_cost)