chore: trying without json
Browse files- play_with_endpoint.py +77 -0
play_with_endpoint.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import time
|
3 |
+
import os, sys
|
4 |
+
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
from sklearn.datasets import make_classification
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
|
10 |
+
from concrete.ml.deployment import FHEModelClient
|
11 |
+
|
12 |
+
import requests
|
13 |
+
|
14 |
+
|
15 |
+
API_URL = "https://puqif7goarh132kl.us-east-1.aws.endpoints.huggingface.cloud"
|
16 |
+
headers = {
|
17 |
+
"Authorization": "Bearer " + os.environ.get("HF_TOKEN"),
|
18 |
+
"Content-Type": "application/octet-stream",
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
def query(payload):
|
23 |
+
response = requests.post(API_URL, headers=headers, data=payload)
|
24 |
+
return response.json()
|
25 |
+
|
26 |
+
|
27 |
+
path_to_model = Path("compiled_model")
|
28 |
+
x, y = make_classification(n_samples=1000, class_sep=2, n_features=30, random_state=42)
|
29 |
+
_, X_test, _, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
|
30 |
+
|
31 |
+
# Recover parameters for client side
|
32 |
+
fhemodel_client = FHEModelClient(path_to_model)
|
33 |
+
|
34 |
+
# Generate the keys
|
35 |
+
fhemodel_client.generate_private_and_evaluation_keys()
|
36 |
+
evaluation_keys = fhemodel_client.get_serialized_evaluation_keys()
|
37 |
+
|
38 |
+
# Test the handler
|
39 |
+
nb_good = 0
|
40 |
+
nb_samples = len(X_test)
|
41 |
+
verbose = False
|
42 |
+
time_start = time.time()
|
43 |
+
duration = 0
|
44 |
+
|
45 |
+
for i in range(nb_samples):
|
46 |
+
|
47 |
+
# Quantize the input and encrypt it
|
48 |
+
encrypted_inputs = fhemodel_client.quantize_encrypt_serialize([X_test[i]])
|
49 |
+
|
50 |
+
# Prepare the payload, including the evaluation keys which are needed server side
|
51 |
+
payload = {
|
52 |
+
"inputs": "fake",
|
53 |
+
"encrypted_inputs": encrypted_inputs,
|
54 |
+
"evaluation_keys": evaluation_keys,
|
55 |
+
}
|
56 |
+
|
57 |
+
# Run the inference on HF servers
|
58 |
+
duration -= time.time()
|
59 |
+
encrypted_prediction = query(payload)
|
60 |
+
duration += time.time()
|
61 |
+
|
62 |
+
encrypted_prediction = encrypted_prediction
|
63 |
+
|
64 |
+
# Decrypt the result and dequantize
|
65 |
+
prediction_proba = fhemodel_client.deserialize_decrypt_dequantize(encrypted_prediction)[0]
|
66 |
+
prediction = np.argmax(prediction_proba)
|
67 |
+
|
68 |
+
if verbose or True:
|
69 |
+
print(f"for {i}-th input, {prediction=} with expected {y_test[i]}")
|
70 |
+
|
71 |
+
# Measure accuracy
|
72 |
+
nb_good += y_test[i] == prediction
|
73 |
+
|
74 |
+
print(f"Accuracy on {nb_samples} samples is {nb_good * 1. / nb_samples}")
|
75 |
+
print(f"Total time: {time.time() - time_start} seconds")
|
76 |
+
print(f"Duration in inferences: {duration} seconds")
|
77 |
+
print(f"Duration per inference: {duration / nb_samples} seconds")
|