Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flask
waitress
waitress
requests
219 changes: 116 additions & 103 deletions serve.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,127 @@
import hashlib
import json
import sys

from flask import Flask, jsonify
from flask import Flask, jsonify, request
import os

from datetime import datetime, timezone
import time
import json

from waitress import serve

app = Flask(__name__)

accessible_api_keys = [
'xxxx'
]


def get_api_key():
# Get the API key from the query parameters or request headers
if "api_key" in request.json:
api_key = request.json["api_key"]
else:
api_key = request.headers.get('Authorization')
if api_key:
api_key = api_key.split(' ')[1] # Remove 'Bearer ' prefix
return api_key


def get_file(f, attempts=3):
output_json_path = os.path.abspath(os.path.join(path, f))
if not os.path.exists(output_json_path):
return None

for attempt_number in range(attempts):
try:
with open(output_json_path, "r") as file:
data = json.load(file)
return data
except json.JSONDecodeError as e:
if attempt_number == attempts - 1:
print(f"serve.py Failed to decode JSON after multiple attempts: {e}")
raise
else:
print(f"serve.py Attempt {attempt_number + 1} failed with JSONDecodeError, retrying...")
time.sleep(1) # Wait before retrying
except Exception as e:
print(f"serve.py Unexpected error reading file: {e}")
raise

# Endpoint to read and serve JSON data from outputs.json
@app.route("/miner-positions", methods=["GET"])
def get_miner_positions():
api_key = get_api_key()

# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401

f = "outputs/output.json"
data = get_file(f)

if data is None:
return f"{f} not found", 404
else:
return jsonify(data)


# serve miner positions v2 now named validator checkpoint
@app.route("/validator-checkpoint", methods=["GET"])
def get_validator_checkpoint():
api_key = get_api_key()

# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401

f = "../runnable/validator_checkpoint.json"
data = get_file(f)

if data is None:
return f"{f} not found", 404
else:
return jsonify(data)


@app.route("/eliminations", methods=["GET"])
def get_eliminations():
api_key = get_api_key()

# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401

f = "eliminations.json"
data = get_file(f)

if data is None:
return f"{f} not found", 404
else:
return jsonify(data)


@app.route("/miner-copying", methods=["GET"])
def get_miner_copying():
api_key = get_api_key()

# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401

f = "miner_copying.json"
data = get_file(f)

# Endpoint to read and serve JSON data from cmw.json
@app.route("/cmw", methods=["GET"])
def get_cmw_data():
cmw_json_path = os.path.abspath(os.path.join(path, "outputs/cmw.json"))
if os.path.exists(cmw_json_path):
with open(cmw_json_path, "r") as file:
data = file.read()
return jsonify(data)
else:
return f"{cmw_json_path} not found", 404


# Endpoint to read and serve JSON data from latest_predictions.json
@app.route("/predictions", methods=["GET"])
def get_predictions_data():
predictions_json_path = os.path.abspath(
os.path.join(path, "outputs/latest_predictions.json")
)
if os.path.exists(predictions_json_path):
with open(predictions_json_path, "r") as file:
data = file.read()
return jsonify(data)
else:
return f"{predictions_json_path} not found", 404


@app.route("/weights", methods=["GET"])
def get_weights_data():
predictions_json_path = os.path.abspath(
os.path.join(path, "weights/valiweights.json")
)
if os.path.exists(predictions_json_path):
with open(predictions_json_path, "r") as file:
data = file.read()
return jsonify(data)
else:
return f"{predictions_json_path} not found", 404


@app.route("/unique-predictions", methods=["GET"])
def get_unique_predictions_data():
predictions_json_path = os.path.abspath(
os.path.join(path, "outputs/latest_predictions.json")
)
results = {}
if os.path.exists(predictions_json_path):
with open(predictions_json_path, "r") as file:
results = json.loads(file.read())

predictions = []
preds_to_miners = {}

ts_list = []
start_ms = results["BTCUSD-5m"][0]["start"]

ts_list.append(start_ms)

for i in range(1, 100):
ts_list.append(start_ms + i * 60000 * 5)

for ts in ts_list:
print(datetime.utcfromtimestamp(ts / 1000).replace(tzinfo=timezone.utc))

for v in results["BTCUSD-5m"]:
hashed_preds = hashlib.sha256(str(v["predictions"]).encode()).hexdigest()
if hashed_preds not in predictions:
predictions.append(hashed_preds)
preds_to_miners[v["miner_uid"]] = v["predictions"]

return_results_dict = {i: {"timestamp": ts_list[i]} for i in range(0, 100)}

for key, value in preds_to_miners.items():
for i, v in enumerate(value):
return_results_dict[i][key] = v

return jsonify({"unique_predictions": [return_results_dict[i] for i in range(0, 100)]})


@app.route("/latest-cmw", methods=["GET"])
def get_latest_cmw():
directory = os.path.abspath(
os.path.join(path, "backups/")
)
# Get list of all files in the directory
files = os.listdir(directory)
# Filter out directories, leave only files
files = [os.path.join(directory, file) for file in files if os.path.isfile(os.path.join(directory, file))]
# Get the latest file based on creation time
latest_file = max(files, key=os.path.getctime)

if os.path.exists(latest_file):
with open(latest_file, "r") as file:
data = file.read()
return jsonify(data)
else:
return f"{latest_file} not found", 404
if data is None:
return f"{f} not found", 404
else:
return jsonify(data)


if __name__ == "__main__":
path = "time-series-prediction-subnet/validation/"
serve(app, host="0.0.0.0", port=80)
# sys.argv[0] is the script name itself
# Arguments start from sys.argv[1]
if len(sys.argv) > 1:
path = sys.argv[1]
else:
path = "../proprietary-trading-network/validation/"
print(path)
serve(app, host="127.0.0.1", port=48888)