-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.py
77 lines (63 loc) · 2.41 KB
/
demo.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
import ray
import requests
from daas.benchmark import openml_utils
from daas.server.app import Server
@ray.remote
def send_example(model_id, features, label):
# Make a prediction.
request = {"model_id": model_id, "features": features}
response = requests.post("http://localhost:8000/models/predict", json=request)
response.raise_for_status()
response = response.json()
prediction_id = response["prediction_id"]
probs = response["probs"]
# Observe the label if provided.
if label:
request = {"prediction_id": prediction_id, "label": label}
response = requests.post("http://localhost:8000/models/train", json=request)
response.raise_for_status()
print(f"prediction_id :: {prediction_id} probs :: {probs} label :: {label}")
return prediction_id, probs, label["class"]
def send_examples(model_id, df, label_col):
results = []
for record in df.to_dict(orient="records"):
record = {k: str(v) for k, v in record.items()}
label = {label_col: record.pop(label_col)}
result = send_example.remote(model_id, record, label)
results.append(result)
return ray.get(results)
def run_demo():
dataset = openml_utils.dataset_from_task(31, 9)
# Create model.
request = {
"feature_schema": {
**{key: "float" for key in dataset.numerical_cols},
**{key: "str" for key in dataset.categorical_cols},
},
"label_column": dataset.label_col,
}
response = requests.post("http://localhost:8000/models/create", json=request)
response.raise_for_status()
model_id = response.json()["model_id"]
print(f"Model id :: {model_id}")
# Train the model.
send_examples(model_id, dataset.train, dataset.label_col)
response = requests.get(f"http://localhost:8000/models/fit?model_id={model_id}")
response.raise_for_status()
# Make predictions.
correct, total = 0, 0
results = send_examples(model_id, dataset.test, dataset.label_col)
for _, probs, label in results:
best_pred, best_prob = None, 0.0
for pred, prob in probs.items():
if prob > best_prob:
best_prob = prob
best_pred = pred
if best_pred == label:
correct += 1
total += 1
print(f"Accuracy :: {correct / total:.2f}")
if __name__ == "__main__":
ray.init(address="auto", namespace="serve")
Server.deploy()
run_demo()