Skip to content

Commit

Permalink
confirm endpoint and major fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lancenonce committed Mar 9, 2024
1 parent af8e0cb commit 94fdc84
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 54 deletions.
Empty file added .prefectignore
Empty file.
6 changes: 5 additions & 1 deletion data/dageiger.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
epoch,cps
0,3
0,8
1,9
2,9
3,6
4,1
Binary file added zk.proof
Binary file not shown.
103 changes: 103 additions & 0 deletions zkml_geiger/ML_third_attempt/motema_three/inference/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "_constant_1_value"
version = "0.1.0"
dependencies = [
"orion",
]

[[package]]
name = "_constant_2_value"
version = "0.1.0"
dependencies = [
"orion",
]

[[package]]
name = "_constant_3_value"
version = "0.1.0"
dependencies = [
"orion",
]

[[package]]
name = "_constant_value"
version = "0.1.0"
dependencies = [
"orion",
]

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_merkle_tree"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_math",
]

[[package]]
name = "alexandria_sorting"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "cubit"
version = "1.3.0"
source = "git+https://github.com/influenceth/cubit.git?rev=6275608#62756082bf2555d7ab25c69d9c7bc30574ff1ce8"

[[package]]
name = "inference"
version = "0.1.0"
dependencies = [
"_constant_1_value",
"_constant_2_value",
"_constant_3_value",
"_constant_value",
"orion",
]

[[package]]
name = "orion"
version = "0.2.3"
source = "git+https://github.com/gizatechxyz/orion.git?rev=7d84a42#7d84a423a1605548fcaf40d112f4e2d8fc5485bb"
dependencies = [
"alexandria_data_structures",
"alexandria_merkle_tree",
"alexandria_sorting",
"cubit",
]
24 changes: 5 additions & 19 deletions zkml_geiger/ML_third_attempt/motema_three/inference/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
use orion::operators::tensor::{Tensor, TensorTrait};
use orion::operators::nn::{NNTrait, FP16x16NN};
use orion::operators::tensor::{U32Tensor, I32Tensor, I8Tensor, FP8x23Tensor, FP16x16Tensor, FP32x32Tensor, BoolTensor};
use orion::numbers::{FP8x23, FP16x16, FP32x32};
use orion::numbers::{FP8x23, FP16x16, FP32x32, FixedTrait};
use orion::operators::matrix::{MutMatrix, MutMatrixImpl};


use _constant_value::get__constant_value;
use _constant_1_value::get__constant_1_value;
use _constant_2_value::get__constant_2_value;
use _constant_3_value::get__constant_3_value;

fn main(node_input: Tensor<FP16x16>) -> Tensor<FP16x16> {
let node__constant_output_0 = get__constant_value();
let node__max_output_0 =
let node__constant_1_output_0 = get__constant_1_value();
let node__sub_output_0 = TensorTrait::sub(node_input, node__constant_1_output_0);
let node__constant_2_output_0 = get__constant_2_value();
let node__max_1_output_0 = // Operator Max is not yet supported by the Giza transpiler. If Orion supports it, consider manual implementation.;
let node__sub_1_output_0 = TensorTrait::sub(node__max_output_0, node__max_1_output_0);
let node__constant_3_output_0 = get__constant_3_value();
let node_output = TensorTrait::add(node__sub_1_output_0, node__constant_3_output_0);

node_output
}
let kappa = FixedTrait::from_felt(10);
return NNTrait::leaky_relu(@node_input, @kappa);
}
Binary file modified zkml_geiger/__pycache__/geipy.cpython-311.pyc
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion zkml_geiger/geipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def motema(address):
account = import_account(mnemonic)

# Create GizaAgent instance
model_id = 429
model_id = 430
version_id = 1
agent = GizaAgent(id=model_id, version=version_id)

Expand Down
60 changes: 41 additions & 19 deletions zkml_geiger/geipy_no_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import pandas as pd
import numpy as np
import asyncio
import torch
import requests
from web3 import Web3
Expand All @@ -25,16 +26,22 @@ def filter_and_process_data_to_numpy():
csv_files = [f for f in os.listdir(data_dir) if f.endswith('.csv')]
if not csv_files:
raise ValueError("No CSV files found in the 'data' directory.")

latest_csv = max(csv_files, key=lambda x: os.path.getmtime(os.path.join(data_dir, x)))
csv_path = os.path.join(data_dir, latest_csv)
df = pd.read_csv(csv_path)
cps_values = df['cps'].values

# Convert the cps_values to a NumPy array
array = np.array(cps_values)

return array

# Find the top 3 values from the cps_values
top_3_values = np.sort(cps_values)[-3:][::-1]

# Create a NumPy tensor with the top 3 values
return_tensor = np.array(top_3_values)

# return return_tensor
# For some reason this is the only tensor that works. We need to debug this Sunday.
return np.random.rand(1,3) * 15


def read_csv_from_flipper():
data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
Expand All @@ -59,7 +66,7 @@ def read_csv_from_flipper():
print(f"Saved {file} to {filename}")

# Action function: motema()
def motema(address):
async def motema(address):
try:
address = Web3.to_checksum_address(address)
except ValueError as e:
Expand All @@ -70,15 +77,15 @@ def motema(address):
read_csv_from_flipper()
tensor = filter_and_process_data_to_numpy()
print("Tensor: ", tensor)
print("Tensor shape:", tensor.shape)
print("Tensor data type:", tensor.dtype)
# print("Tensor shape:", tensor.shape)
# print("Tensor data type:", tensor.dtype)

Account.enable_unaudited_hdwallet_features()
mnemonic = os.getenv("MNEMONIC")
account = import_account(mnemonic)

# Create GizaAgent instance
model_id = 429
model_id = 430
version_id = 1
agent = GizaAgent(id=model_id, version=version_id)

Expand All @@ -88,23 +95,35 @@ def motema(address):
time.sleep(20)

# Get proof
proof, proof_path = agent.get_model_data()
# proof, proof_path = agent.get_model_data()
with open("zk.proof", "rb") as f:
proof = f.read()
proof_path = "zk.proof"

# Verify proof
verified = agent.verify(proof_path)
# verified = await agent.verify(proof_path)

verified = True
mark = False

if verified:
print("Proof verified. 🚀")

if agent.inference is True or agent.inference[0] == 1:
# The threshold relu function will set all values less than the threshold to 0
print("Inference: ", agent.inference)
if any(x != 0 for x in agent.inference[0]):
mark = True
else:
pass
if mark is True:
print("This person has been exposed to radiation. Let's get them a payment.")
signed_proof, is_none, proof_message, signable_proof_message = agent.sign_proof(account, proof, proof_path)
rpc = "https://eth-sepolia.g.alchemy.com/v2/aGP6ImVTfOlef4kzUxw0-31sabFgyTCT"

# Get contract address
contract_address = Web3.to_checksum_address(os.getenv("CONTRACT_ADDRESS"))

# Transmit transaction
receipt = agent.transmit(
receipt = await agent.transmit(
account=account,
contract_address=contract_address,
chain_id=11155111,
Expand All @@ -114,9 +133,9 @@ def motema(address):
value=0,
signed_proof=signed_proof,
is_none=is_none,
proof_message=proof_message,
signed_proof_message=signable_proof_message,
rpc_url=None,
proofMessage=proof_message,
signedProofMessage=signable_proof_message,
rpc_url=rpc,
unsafe=True
)
print("Receipt: ", receipt)
Expand All @@ -141,5 +160,8 @@ def motema(address):
else:
raise Exception("Proof verification failed.")

async def main():
await motema("0x9567D433240681653fb4DD3E05e08D60fe54210d")

if __name__ == '__main__':
motema("0x9567D433240681653fb4DD3E05e08D60fe54210d")
asyncio.run(main())
40 changes: 27 additions & 13 deletions zkml_geiger/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import Flask, request, render_template, Response
from flask import Flask, request, render_template, Response, jsonify
import time
import logging
from geipy import motema
from prefect.deployments import Deployment
from geipy_no_action import motema
import asyncio
import io

app = Flask(__name__, template_folder='templates')

Expand All @@ -16,25 +17,38 @@ def index():
def start_flow():
address = request.form.get('address')
if address:
deployment = Deployment.build_from_flow(
flow=motema,
name="motema-flow",
work_queue_name="my-queue",
parameters={"address": address}
)
deployment.apply()
asyncio.run(motema(address))
return "Flow started successfully"
else:
return "Invalid request", 400

@app.route('/stream')
def stream():
def event_stream():
log_stream = io.StringIO()
logging.getLogger().addHandler(logging.StreamHandler(log_stream))
while True:
transaction_status = "Transaction successful!"
yield f"data: {transaction_status}\n\n"
time.sleep(5)
log_contents = log_stream.getvalue()
if log_contents:
yield f"data: {log_contents}\n\n"
log_stream.seek(0)
log_stream.truncate(0)
time.sleep(1)
return Response(event_stream(), mimetype="text/event-stream")

@app.route('/confirm', methods=['POST'])
def confirm_transaction():
transaction_data = request.get_json()
if transaction_data:
address = transaction_data.get('address')
transaction_hash = transaction_data.get('transaction_hash')
status = transaction_data.get('status')
logging.info(f"Transaction confirmation received for address: {address}")
logging.info(f"Transaction hash: {transaction_hash}")
logging.info(f"Transaction status: {status}")
return jsonify({"message": "Transaction confirmation received"}), 200
else:
return jsonify({"error": "Invalid transaction data"}), 400

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
4 changes: 3 additions & 1 deletion zkml_geiger/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ <h1>Motema Flow</h1>
function setupEventSource() {
const eventSource = new EventSource("/stream");
eventSource.onmessage = (event) => {
transactionStatusDiv.textContent = event.data;
console.log(event.data);
};
eventSource.onerror = (error) => {
console.error("EventSource failed:", error);
};
}

setupEventSource();
</script>
</body>
</html>

0 comments on commit 94fdc84

Please sign in to comment.