diff --git a/.prefectignore b/.prefectignore new file mode 100644 index 0000000..e69de29 diff --git a/data/dageiger.csv b/data/dageiger.csv index 973bcd4..9069bb9 100644 --- a/data/dageiger.csv +++ b/data/dageiger.csv @@ -1,2 +1,6 @@ epoch,cps -0,3 +0,8 +1,9 +2,9 +3,6 +4,1 \ No newline at end of file diff --git a/zk.proof b/zk.proof new file mode 100644 index 0000000..1a7698b Binary files /dev/null and b/zk.proof differ diff --git a/zkml_geiger/ML_third_attempt/motema_three/inference/Scarb.lock b/zkml_geiger/ML_third_attempt/motema_three/inference/Scarb.lock new file mode 100644 index 0000000..92f9731 --- /dev/null +++ b/zkml_geiger/ML_third_attempt/motema_three/inference/Scarb.lock @@ -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", +] diff --git a/zkml_geiger/ML_third_attempt/motema_three/inference/src/lib.cairo b/zkml_geiger/ML_third_attempt/motema_three/inference/src/lib.cairo index f3ae8c6..149d5c6 100644 --- a/zkml_geiger/ML_third_attempt/motema_three/inference/src/lib.cairo +++ b/zkml_geiger/ML_third_attempt/motema_three/inference/src/lib.cairo @@ -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) -> Tensor { -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 - } \ No newline at end of file + let kappa = FixedTrait::from_felt(10); + return NNTrait::leaky_relu(@node_input, @kappa); +} \ No newline at end of file diff --git a/zkml_geiger/__pycache__/geipy.cpython-311.pyc b/zkml_geiger/__pycache__/geipy.cpython-311.pyc index fd8e199..c0f0e2d 100644 Binary files a/zkml_geiger/__pycache__/geipy.cpython-311.pyc and b/zkml_geiger/__pycache__/geipy.cpython-311.pyc differ diff --git a/zkml_geiger/__pycache__/geipy_no_action.cpython-311.pyc b/zkml_geiger/__pycache__/geipy_no_action.cpython-311.pyc new file mode 100644 index 0000000..5297994 Binary files /dev/null and b/zkml_geiger/__pycache__/geipy_no_action.cpython-311.pyc differ diff --git a/zkml_geiger/geipy.py b/zkml_geiger/geipy.py index b8254b7..aecdaf0 100644 --- a/zkml_geiger/geipy.py +++ b/zkml_geiger/geipy.py @@ -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) diff --git a/zkml_geiger/geipy_no_action.py b/zkml_geiger/geipy_no_action.py index 96aff40..ad0a435 100644 --- a/zkml_geiger/geipy_no_action.py +++ b/zkml_geiger/geipy_no_action.py @@ -2,6 +2,7 @@ import time import pandas as pd import numpy as np +import asyncio import torch import requests from web3 import Web3 @@ -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') @@ -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: @@ -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) @@ -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, @@ -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) @@ -141,5 +160,8 @@ def motema(address): else: raise Exception("Proof verification failed.") +async def main(): + await motema("0x9567D433240681653fb4DD3E05e08D60fe54210d") + if __name__ == '__main__': - motema("0x9567D433240681653fb4DD3E05e08D60fe54210d") \ No newline at end of file + asyncio.run(main()) \ No newline at end of file diff --git a/zkml_geiger/server.py b/zkml_geiger/server.py index f9128f1..f51d149 100644 --- a/zkml_geiger/server.py +++ b/zkml_geiger/server.py @@ -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') @@ -16,13 +17,7 @@ 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 @@ -30,11 +25,30 @@ def start_flow(): @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) \ No newline at end of file diff --git a/zkml_geiger/templates/index.html b/zkml_geiger/templates/index.html index d6f0482..c6a74a3 100644 --- a/zkml_geiger/templates/index.html +++ b/zkml_geiger/templates/index.html @@ -61,12 +61,14 @@

Motema Flow

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(); \ No newline at end of file