Spaces:
Sleeping
Sleeping
abhishekrs4
commited on
Commit
•
b75711c
1
Parent(s):
83deef6
added script to test sending the post request to hugging_face deployed fast_api application
Browse files- test_post_request.py +59 -0
test_post_request.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import argparse
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
|
8 |
+
from modeling.ml_model_dev import read_csv_file
|
9 |
+
|
10 |
+
class NumpyEncoder(json.JSONEncoder):
|
11 |
+
def default(self, obj):
|
12 |
+
if isinstance(obj, np.ndarray):
|
13 |
+
return obj.tolist()
|
14 |
+
return json.JSONEncoder.default(self, obj)
|
15 |
+
|
16 |
+
def send_post_reqest(ARGS):
|
17 |
+
df_csv = read_csv_file(ARGS.file_csv)
|
18 |
+
|
19 |
+
df_train, df_test = train_test_split(df_csv, test_size=0.1, random_state=4)
|
20 |
+
list_cols = df_train.columns[:-1]
|
21 |
+
X_test, Y_test = df_test.to_numpy()[:, :-1], df_test.to_numpy()[:, -1:]
|
22 |
+
print(X_test.shape)
|
23 |
+
|
24 |
+
url = "https://abhishekrs4-ml-water-potability.hf.space/predict"
|
25 |
+
# the endpoint of the post request
|
26 |
+
|
27 |
+
headers = {'Content-type': 'application/json'}
|
28 |
+
# additional headers to indicate the content type of the post request
|
29 |
+
|
30 |
+
# perform 20 post requests
|
31 |
+
for i in range(0, ARGS.num_requests):
|
32 |
+
list_values = list(X_test[i, :])
|
33 |
+
encoded_data = dict(zip(list_cols, list_values))
|
34 |
+
print(encoded_data)
|
35 |
+
result = requests.post(url, data=json.dumps(encoded_data), headers=headers)
|
36 |
+
print(f"{json.loads(result.text)} \n")
|
37 |
+
# print(f"{type(json.loads(result.text))} \n")
|
38 |
+
return
|
39 |
+
|
40 |
+
def main():
|
41 |
+
file_csv = "dataset/water_potability.csv"
|
42 |
+
num_requests = 20
|
43 |
+
|
44 |
+
parser = argparse.ArgumentParser(
|
45 |
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
46 |
+
)
|
47 |
+
|
48 |
+
parser.add_argument("--file_csv", default=file_csv,
|
49 |
+
type=str, help="full path to dataset csv file")
|
50 |
+
parser.add_argument("--num_requests", default=num_requests,
|
51 |
+
type=int, help="number of post requests to send")
|
52 |
+
|
53 |
+
ARGS, unparsed = parser.parse_known_args()
|
54 |
+
send_post_reqest(ARGS)
|
55 |
+
|
56 |
+
return
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
main()
|