-
Notifications
You must be signed in to change notification settings - Fork 0
/
giza-BETF-short-LIN-wip.py
384 lines (260 loc) · 11.9 KB
/
giza-BETF-short-LIN-wip.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
# coding: utf-8
# ### testing the XGBoost Diabetes example to transpile
# ##### used conda env giza from ll laptop
# ## Create and Train an LR Model
# ### We'll start by creating a simple XGBoost model using Scikit-Learn and train it on ship ETA dataset
# In[2]:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from giza.datasets import DatasetsLoader
from giza.agents import GizaAgent
from giza.zkcook import serialize_model
import numpy as np
import pandas as pd
# import matplotlib.pyplot as plt
# import seaborn as sns
# In[3]:
data = pd.read_csv('BETF-Final.csv')
data
# In[4]:
data.shape
# In[5]:
#X, y = data.data, data.target
# Drop rows with missing values
df_cleaned = data.dropna(subset=['TotalETF'])
# Prepare your data
X = df_cleaned[['TotalETF']]
y = df_cleaned['ClosingPrice']
# In[6]:
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a linear regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# In[11]:
X_test
# In[7]:
predictions = model.predict(X_test)
# In[8]:
predictions
# In[10]:
value = 453.1
# Reshape into a 2D array with a single feature
reshaped_value = np.array(value).reshape(-1, 1)
# Make the prediction
predictions2 = model.predict(reshaped_value)
print(predictions2)
# In[12]:
reshaped_value
# In[14]:
inputTotalETF = 453.1
input0 = np.array([[inputTotalETF]]).astype(np.float32)
input0
# In[16]:
input0.flatten()[0]
# In[6]:
get_ipython().system('pip install skl2onnx')
# In[7]:
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
# Define the initial types for the ONNX model
#initial_type = [('float_input', FloatTensorType([None, X_train.shape[1] ]))]
initial_type = [('float_input', FloatTensorType([None, X_train.shape[1] ]))]
# Convert the scikit-learn model to ONNX
onnx_model = convert_sklearn(model, initial_types=initial_type)
# Save the ONNX model to a file
with open("linear_regression-betf6.onnx", "wb") as f:
f.write(onnx_model.SerializeToString())
# ## Save the model
# ### Save the model in Json format
# ## Transpile your model to Orion Cairo
# ### We will use Giza-CLI to transpile our saved model to Orion Cairo.
# $ giza transpile linear_regression-betf4.onnx --output-path verifiable_betf_lr4
# [giza][2024-06-05 17:29:54.869] No model id provided, checking if model exists ✅
# [giza][2024-06-05 17:29:54.880] Model name is: linear_regression-betf
# [giza][2024-06-05 17:29:55.610] Model Created with id -> 724! ✅
# [giza][2024-06-05 17:29:56.365] Version Created with id -> 1! ✅
# [giza][2024-06-05 17:29:56.375] Sending model for transpilation ✅
# [giza][2024-06-05 17:30:29.406] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅
# [giza][2024-06-05 17:30:31.389] Downloading model ✅
# [giza][2024-06-05 17:30:31.430] model saved at: verifiable_betf_lr
# In[ ]:
$ giza transpile linear_regression-betf4.onnx --output-path verifiable_betf_lr4
[giza][2024-06-06 18:04:42.051] No model id provided, checking if model exists ✅
[giza][2024-06-06 18:04:42.058] Model name is: linear_regression-betf4
[giza][2024-06-06 18:04:42.599] Model Created with id -> 732! ✅
[giza][2024-06-06 18:04:43.332] Version Created with id -> 1! ✅
[giza][2024-06-06 18:04:43.337] Sending model for transpilation ✅
[giza][2024-06-06 18:05:27.067] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅
[giza][2024-06-06 18:05:28.039] Downloading model ✅
[giza][2024-06-06 18:05:28.064] model saved at: verifiable_betf_lr4
# $ giza transpile linear_regression-betf6.onnx --output-path verifiable_betf_lr6
# [giza][2024-06-07 14:06:03.167] No model id provided, checking if model exists ✅
# [giza][2024-06-07 14:06:03.177] Model name is: linear_regression-betf6
# [giza][2024-06-07 14:06:03.910] Model Created with id -> 739! ✅
# [giza][2024-06-07 14:06:04.731] Version Created with id -> 1! ✅
# [giza][2024-06-07 14:06:04.738] Sending model for transpilation ✅
# [giza][2024-06-07 14:06:48.289] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅
# [giza][2024-06-07 14:06:49.131] Downloading model ✅
# [giza][2024-06-07 14:06:49.163] model saved at: verifiable_betf_lr6
# ## Deploy an inference endpoint # 1
# ### Now that our model is transpiled to Cairo we can deploy an endpoint to run verifiable inferences. We will use Giza CLI again to run and deploy an endpoint. Ensure to replace model-id and version-id with your ids provided during transpilation.
#
# $ giza endpoints deploy --model-id 724 --version-id 1
# ▰▰▰▰▰▰▰ Creating endpoint!
# [giza][2024-06-05 17:33:42.606] Endpoint is successful ✅
# [giza][2024-06-05 17:33:42.618] Endpoint created with id -> 285 ✅
# [giza][2024-06-05 17:33:42.623] Endpoint created with endpoint URL: https://endpoint-giza1-724-1-79763323-7i3yxzspbq-ew.a.run.app 🎉
#
# $ giza endpoints deploy --model-id 732 --version-id 1
# [giza][2024-06-07 13:52:42.178] Endpoint for model id 732 and version id 1 already exists! ✅
# [giza][2024-06-07 13:52:42.188] Endpoint id -> 312 ✅
# [giza][2024-06-07 13:52:42.190] You can start doing inferences at: None 🚀
# $ giza endpoints deploy --model-id 739 --version-id 1
# ▰▱▱▱▱▱▱ Creating endpoint!
# [giza][2024-06-07 14:09:34.112] Endpoint is successful ✅
# [giza][2024-06-07 14:09:34.118] Endpoint created with id -> 314 ✅
# [giza][2024-06-07 14:09:34.120] Endpoint created with endpoint URL: https://endpoint-giza1-739-1-a8a598d9-7i3yxzspbq-ew.a.run.app 🎉
# ## Create Agent
# ### (you can create this after running verifiable inference, get and download proof and verify proof - need before agent run only)
# giza agents create --model-id 724 --version-id 1 --endpoint-id 285 --name BETF1 --description BETF1
# $ giza agents create --model-id 724 --version-id 1 --endpoint-id 285 --name BETF1 --description BETF1
# [giza][2024-06-06 17:45:28.697] Creating agent ✅
# [giza][2024-06-06 17:45:28.705] Using endpoint id to create agent, retrieving model id and version id
# [giza][2024-06-06 17:45:29.315] Select an existing account to create the agent.
# [giza][2024-06-06 17:45:29.318] Available accounts are:
# ┌──────────┐
# │ Accounts │
# ├──────────┤
# │ giza1 │
# └──────────┘
# Enter the account name: giza1
# {
# "id": 71,
# "name": "BETF1",
# "description": "BETF1",
# "parameters": {
# "model_id": 724,
# "version_id": 1,
# "endpoint_id": 285,
# "account": "giza1"
# },
# "created_date": "2024-06-07T00:45:34.144701",
# "last_update": "2024-06-07T00:45:34.144701"
# }
# $ giza agents create --model-id 732 --version-id 1 --endpoint-id 312 --name BETF4 --description BETF4
# [giza][2024-06-07 13:55:43.554] Creating agent ✅
# [giza][2024-06-07 13:55:43.564] Using endpoint id to create agent, retrieving model id and version id
# [giza][2024-06-07 13:55:44.077] Select an existing account to create the agent.
# [giza][2024-06-07 13:55:44.082] Available accounts are:
# ┌──────────┐
# │ Accounts │
# ├──────────┤
# │ giza1 │
# └──────────┘
# Enter the account name: giza1
# {
# "id": 72,
# "name": "BETF4",
# "description": "BETF4",
# "parameters": {
# "model_id": 732,
# "version_id": 1,
# "endpoint_id": 312,
# "account": "giza1"
# },
# "created_date": "2024-06-07T20:55:48.197225",
# "last_update": "2024-06-07T20:55:48.197225"
# }
# giza agents create --model-id 739 --version-id 1 --endpoint-id 314 --name BETF6 --description BETF6
#
# $ giza agents create --model-id 739 --version-id 1 --endpoint-id 314 --name BETF6 --description BETF6
# [giza][2024-06-07 14:18:48.002] Creating agent ✅
# [giza][2024-06-07 14:18:48.010] Using endpoint id to create agent, retrieving model id and version id
# [giza][2024-06-07 14:18:48.557] Select an existing account to create the agent.
# [giza][2024-06-07 14:18:48.561] Available accounts are:
# ┌──────────┐
# │ Accounts │
# ├──────────┤
# │ giza1 │
# └──────────┘
# Enter the account name: giza1
# {
# "id": 74,
# "name": "BETF6",
# "description": "BETF6",
# "parameters": {
# "model_id": 739,
# "version_id": 1,
# "endpoint_id": 314,
# "account": "giza1"
# },
# "created_date": "2024-06-07T21:18:52.209538",
# "last_update": "2024-06-07T21:18:52.209538"
# }
# In[ ]:
get_ipython().system('giza users me')
# In[ ]:
# ## Run a verifiable inference
# ##### To streamline verifiable inference, you might consider using the endpoint URL obtained after transpilation. However, this approach requires manual serialization of the input for the Cairo program and handling the deserialization process. To make this process more user-friendly and keep you within a Python environment, we've introduced a Python SDK designed to facilitate the creation of ML workflows and execution of verifiable predictions. When you initiate a prediction, our system automatically retrieves the endpoint URL you deployed earlier, converts your input into Cairo-compatible format, executes the prediction, and then converts the output back into a numpy object.
# In[8]:
from giza.agents.model import GizaModel
MODEL_ID = 739 # Update with your model ID
VERSION_ID = 1 # Update with your version ID
def prediction(input, model_id, version_id):
model = GizaModel(id=model_id, version=version_id)
(result, proof_id) = model.predict(
input_feed={'input': input}, verifiable=True
)
return result, proof_id
def execution():
# The input data type should match the model's expected input
inputTotalETF = 453.1;
input = np.array([[inputTotalETF]]).astype(np.float32)
#input = np.array([[31,28.1]]).astype(np.float32)
(result, proof_id) = prediction(input, MODEL_ID, VERSION_ID)
print(
f"Predicted value for input {input.flatten()[0]} is {result[0].flatten()[0]}")
print(f"Proof ID: {proof_id}")
return result, proof_id
execution()
# 🚀 Starting deserialization process...
# ✅ Deserialization completed! 🎉
# Predicted value for input 453.1000061035156 is 60012.989669799805
# Proof ID: 2ccaab8cc19642de988fd7e37f16f3e0
# (array([[60012.9896698]]), '2ccaab8cc19642de988fd7e37f16f3e0')
# ## Get and Download the proof
# #### Initiating a verifiable inference sets off a proving job on our server, sparing you the complexities of installing and configuring the prover yourself. Upon completion, you can download your proof.
#
# First, let's check the status of the proving job to ensure that it has been completed.
# $ giza endpoints get-proof --endpoint-id 314 --proof-id "2ccaab8cc19642de988fd7e37f16f3e0"
# [giza][2024-06-07 14:11:49.529] Getting proof from endpoint 314 ✅
# {
# "id": 1079,
# "job_id": 1252,
# "metrics": {
# "proving_time": 19.864916
# },
# "created_date": "2024-06-07T21:11:22.794625"
# }
# Once the proof is ready, you can download it.
# $ giza endpoints download-proof --endpoint-id 314 --proof-id "2ccaab8cc19642de988fd7e37f16f3e0" --output-path giza-BETF-short-LIN-wip.proof
#
#
# $ giza endpoints download-proof --endpoint-id 314 --proof-id "2ccaab8cc19642de988fd7e37f16f3e0" --output-path giza-BETF-short-LIN-wip.proof
# [giza][2024-06-07 14:13:22.350] Getting proof from endpoint 314 ✅
# [giza][2024-06-07 14:13:25.223] Proof downloaded to giza-BETF-short-LIN-wip.proof ✅
#
# ## Verify the proof
# #### Finally, you can verify the proof.
# $ giza verify --proof-id 1079
#
# $ giza verify --proof-id 1079
# [giza][2024-06-07 14:14:02.287] Verifying proof...
# [giza][2024-06-07 14:14:03.884] Verification result: True
# [giza][2024-06-07 14:14:03.888] Verification time: 0.443269482
#
# In[ ]: