-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_post_request.py
68 lines (51 loc) · 1.84 KB
/
test_post_request.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
import json
import requests
import argparse
import numpy as np
from sklearn.model_selection import train_test_split
from modeling.data_utils import WaterPotabilityDataLoader
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
def send_post_reqest(ARGS):
water_pot_dataset = WaterPotabilityDataLoader(ARGS.file_csv)
water_pot_dataset.read_csv_file()
water_pot_dataset.split_data()
list_cols = water_pot_dataset.df_csv.columns[:-1]
X_test, Y_test = water_pot_dataset.get_data_from_data_frame(which_set="test")
print(X_test.shape)
url = "http://0.0.0.0:5000/predict"
# the endpoint of the post request
headers = {"Content-type": "application/json"}
# additional headers to indicate the content type of the post request
# perform 20 post requests
for i in range(0, ARGS.num_requests):
list_values = list(X_test[i, :])
encoded_data = dict(zip(list_cols, list_values))
print(encoded_data)
result = requests.post(url, data=json.dumps(encoded_data), headers=headers)
print(f"{json.loads(result.text)} \n")
# print(f"{type(json.loads(result.text))} \n")
return
def main():
file_csv = "dataset/water_potability.csv"
num_requests = 20
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--file_csv", default=file_csv, type=str, help="full path to dataset csv file"
)
parser.add_argument(
"--num_requests",
default=num_requests,
type=int,
help="number of post requests to send",
)
ARGS, unparsed = parser.parse_known_args()
send_post_reqest(ARGS)
return
if __name__ == "__main__":
main()