Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
kush-alloralabs committed Jan 10, 2025
1 parent 82fa04a commit 5d30a0d
Show file tree
Hide file tree
Showing 12 changed files with 475 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# .env
TIINGO_API_KEY=your_tiingo_api_key
TIINGO_API_KEY=5da9e86c26b6f16540af23883eea67d71216abf4
TOKEN=
TRAINING_DAYS=
TIMEFRAME=
MODEL=
REGION=
DATA_PROVIDER=
CG_API_KEY=
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.11-slim AS project_env

# Install system dependencies including build essentials
RUN apt-get update && apt-get install -y \
curl \
build-essential \
gcc \
g++

# Set the working directory in the container
WORKDIR /app

# Install Python build dependencies first
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip setuptools wheel \
&& pip install Cython==3.0.11 numpy==1.24.3 \
&& pip install -r requirements.txt

FROM project_env

COPY . /app/

# Set the entrypoint command
CMD ["gunicorn", "--conf", "/app/gunicorn_conf.py", "main:app"]
29 changes: 29 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"wallet": {
"addressKeyName": "test",
"addressRestoreMnemonic": "",
"alloraHomeDir": "",
"gas": "auto",
"gasAdjustment": 1.5,
"gasPrices": 10,
"maxFees": 25000000,
"nodeRpc": "https://allora-rpc.testnet.allora.network",
"maxRetries": 5,
"retryDelay": 3,
"accountSequenceRetryDelay": 5,
"submitTx": true,
"blockDurationEstimated": 5,
"windowCorrectionFactor": 0.8
},
"worker": [
{
"topicId": 1,
"inferenceEntrypointName": "api-worker-reputer",
"loopSeconds": 5,
"parameters": {
"InferenceEndpoint": "http://inference:8000/inference/{Token}",
"Token": "ETH"
}
}
]
}
29 changes: 29 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"wallet": {
"addressKeyName": "forge-wallet",
"addressRestoreMnemonic": "degree shoe you side trigger gadget cricket estate essay bronze silent glass ranch luggage fat basic penalty garbage crazy genre weapon memory good thank",
"alloraHomeDir": "",
"gas": "auto",
"gasAdjustment": 1.5,
"gasPrices": 10,
"maxFees": 25000000,
"nodeRpc": "https://allora-rpc.testnet.allora.network",
"maxRetries": 5,
"retryDelay": 3,
"accountSequenceRetryDelay": 5,
"submitTx": true,
"blockDurationEstimated": 5,
"windowCorrectionFactor": 0.8
},
"worker": [
{
"topicId": 15,
"inferenceEntrypointName": "api-worker-reputer",
"loopSeconds": 5,
"parameters": {
"InferenceEndpoint": "http://inference:8000/inference/{Token}",
"Token": "ETH"
}
}
]
}
21 changes: 21 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

app_base_path = os.getenv("APP_BASE_PATH", default=os.getcwd())
data_base_path = os.path.join(app_base_path, "data")
model_file_path = os.path.join(data_base_path, "model.pkl")

TOKEN = os.getenv("TOKEN").upper()
TRAINING_DAYS = os.getenv("TRAINING_DAYS")
TIMEFRAME = os.getenv("TIMEFRAME")
MODEL = os.getenv("MODEL")
REGION = os.getenv("REGION").lower()
if REGION in ["us", "com", "usa"]:
REGION = "us"
else:
REGION = "com"
DATA_PROVIDER = os.getenv("DATA_PROVIDER").lower()
CG_API_KEY = os.getenv("CG_API_KEY", default=None)
47 changes: 47 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
services:
inference:
container_name: inference
env_file:
- .env
build: .
command: python -u /app/app.py
ports:
- "8000:8000"
healthcheck:
test: ["CMD", "curl", "-f", "http://inference:8000/inference/${TOKEN}"]
interval: 10s
timeout: 5s
retries: 12
volumes:
- ./inference-data:/app/data

updater:
container_name: updater
build: .
environment:
- INFERENCE_API_ADDRESS=http://inference:8000
command: >
sh -c "
while true; do
python -u /app/update_app.py;
sleep 24h;
done
"
depends_on:
inference:
condition: service_healthy

worker:
container_name: worker
image: alloranetwork/allora-offchain-node:v0.6.0
volumes:
- ./worker-data:/data
depends_on:
inference:
condition: service_healthy
env_file:
- ./worker-data/env_file

volumes:
inference-data:
worker-data:
12 changes: 12 additions & 0 deletions gunicorn_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Gunicorn config variables
loglevel = "info"
errorlog = "-" # stderr
accesslog = "-" # stdout
worker_tmp_dir = "/dev/shm"
graceful_timeout = 120
timeout = 30
keepalive = 5
worker_class = "gthread"
workers = 1
threads = 8
bind = "0.0.0.0:9000"
61 changes: 61 additions & 0 deletions init.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

set -e

if [ ! -f config.json ]; then
echo "Error: config.json file not found, please provide one"
exit 1
fi

# Install system dependencies required for building Python packages
if command -v apt-get &> /dev/null; then
apt-get update && apt-get install -y \
build-essential \
gcc \
gfortran \
python3-dev \
libopenblas-dev \
python3-pip
fi

# Install critical Python dependencies first
pip install --no-cache-dir \
setuptools==72.1.0 \
wheel==0.44.0 \
Cython==3.0.11 \
numpy==1.24.3

nodeName=$(jq -r '.wallet.addressKeyName' config.json)
if [ -z "$nodeName" ]; then
echo "No wallet name provided for the node, please provide your preferred wallet name. config.json >> wallet.addressKeyName"
exit 1
fi

# Ensure the worker-data directory exists
mkdir -p ./worker-data

json_content=$(cat ./config.json)
stringified_json=$(echo "$json_content" | jq -c .)

mnemonic=$(jq -r '.wallet.addressRestoreMnemonic' config.json)
if [ -n "$mnemonic" ]; then
echo "ALLORA_OFFCHAIN_NODE_CONFIG_JSON='$stringified_json'" > ./worker-data/env_file
echo "NAME=$nodeName" >> ./worker-data/env_file
echo "ENV_LOADED=true" >> ./worker-data/env_file
echo "wallet mnemonic already provided by you, loading config.json . Please proceed to run docker compose"
exit 1
fi

if [ ! -f ./worker-data/env_file ]; then
echo "ENV_LOADED=false" > ./worker-data/env_file
fi

ENV_LOADED=$(grep '^ENV_LOADED=' ./worker-data/env_file | cut -d '=' -f 2)
if [ "$ENV_LOADED" = "false" ]; then
json_content=$(cat ./config.json)
stringified_json=$(echo "$json_content" | jq -c .)
docker run -it --entrypoint=bash -v $(pwd)/worker-data:/data -v $(pwd)/scripts:/scripts -e NAME="${nodeName}" -e ALLORA_OFFCHAIN_NODE_CONFIG_JSON="${stringified_json}" alloranetwork/allora-chain:v0.8.0 -c "bash /scripts/init.sh"
echo "config.json saved to ./worker-data/env_file"
else
echo "config.json is already loaded, skipping the operation. You can set ENV_LOADED variable to false in ./worker-data/env_file to reload the config.json"
fi
33 changes: 33 additions & 0 deletions scripts/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -e

if allorad keys --home=/data/.allorad --keyring-backend test show $NAME > /dev/null 2>&1 ; then
echo "allora account: $NAME already imported"
else
echo "creating allora account: $NAME"
output=$(allorad keys add $NAME --home=/data/.allorad --keyring-backend test 2>&1)
address=$(echo "$output" | grep 'address:' | sed 's/.*address: //')
mnemonic=$(echo "$output" | tail -n 1)

# Parse and update the JSON string
updated_json=$(echo "$ALLORA_OFFCHAIN_NODE_CONFIG_JSON" | jq --arg name "$NAME" --arg mnemonic "$mnemonic" '
.wallet.addressKeyName = $name |
.wallet.addressRestoreMnemonic = $mnemonic
')

stringified_json=$(echo "$updated_json" | jq -c .)

echo "ALLORA_OFFCHAIN_NODE_CONFIG_JSON='$stringified_json'" > /data/env_file
echo ALLORA_OFFCHAIN_ACCOUNT_ADDRESS=$address >> /data/env_file
echo "NAME=$NAME" >> /data/env_file

echo "Updated ALLORA_OFFCHAIN_NODE_CONFIG_JSON saved to /data/env_file"
fi


if grep -q "ENV_LOADED=false" /data/env_file; then
sed -i 's/ENV_LOADED=false/ENV_LOADED=true/' /data/env_file
else
echo "ENV_LOADED=true" >> /data/env_file
fi
Loading

0 comments on commit 5d30a0d

Please sign in to comment.