-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_deploy_model_api.py
executable file
·60 lines (45 loc) · 1.67 KB
/
test_deploy_model_api.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
#!/usr/bin/env python3
import sys, bz2, uuid, requests, json, pickle
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from joblib import dump
# defining the api-endpoint
API_DEPLOY_ENDPOINT = 'http://localhost:9090/api/deploy'
#headers = {"Authorization": "Bearer user"}
# your API key here
API_KEY = "376d873c859d7f9f268e1b9be883745b"
# load the data set
iris = load_iris()
dataframe_load = pd.DataFrame(iris.data)
dataframe_load.columns = iris.feature_names
data_label = iris.target
dataframe = dataframe_load.assign(LABEL=data_label)
dataframe_train, dataframe_test = train_test_split(dataframe, test_size=0.33)
dataframe_train = dataframe_train.reset_index(drop=True)
dataframe_test = dataframe_test.reset_index(drop=True)
LABEL_COLUMN = 'LABEL'
columns = [LABEL_COLUMN]
X_train = dataframe_train.drop(columns, axis=1, inplace=False)
y_train = dataframe_train.filter(columns, axis=1)
X_test = dataframe_test.drop(columns, axis=1, inplace=False)
y_test = dataframe_test.filter(columns, axis=1)
from sklearn.linear_model import LogisticRegression
input_variables = {'solver': 'lbfgs', 'multi_class': 'auto', 'max_iter': 1000}
clf = LogisticRegression(**input_variables)
print("classifier:\n", clf)
clf.fit(X_train.values, y_train.values.ravel())
print("score:\n", clf.score(X_test.values, y_test.values.ravel()))
dump(clf, 'model.joblib')
fin = open('model.joblib', 'rb')
files = {'model_file': fin}
data = {
'api_token': API_KEY
}
try:
req = requests.post(API_DEPLOY_ENDPOINT, data=data, files=files) #, headers=headers)
print(req.text)
finally:
fin.close()