update my all files
Browse files- Dockerfile +12 -3
- README.md +90 -24
- server/app.py +5 -1
Dockerfile
CHANGED
|
@@ -1,12 +1,21 @@
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
|
|
|
| 5 |
COPY requirements.txt .
|
| 6 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
| 10 |
EXPOSE 7860
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
| 3 |
+
# Create non-root user
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
USER user
|
| 6 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 7 |
+
|
| 8 |
WORKDIR /app
|
| 9 |
|
| 10 |
+
# Copy requirements first
|
| 11 |
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 13 |
|
| 14 |
+
# Copy all files
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
|
| 17 |
+
# Expose port 7860 (Hugging Face default)
|
| 18 |
EXPOSE 7860
|
| 19 |
|
| 20 |
+
# Run the server
|
| 21 |
+
CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -8,9 +8,10 @@ An OpenEnv-compliant environment that tests AI agents on financial data analysis
|
|
| 8 |
Quant-Gym is a benchmark environment where AI agents can practice:
|
| 9 |
- Fetching real-time market data
|
| 10 |
- Analyzing financial news sentiment
|
|
|
|
| 11 |
- Evaluating trading strategies with risk metrics
|
| 12 |
|
| 13 |
-
This is a
|
| 14 |
|
| 15 |
## ๐ Environment Tasks
|
| 16 |
|
|
@@ -24,55 +25,67 @@ This is a **research benchmark** for evaluating AI reasoning in financial contex
|
|
| 24 |
|
| 25 |
| Endpoint | Method | Description |
|
| 26 |
|----------|--------|-------------|
|
|
|
|
| 27 |
| `/health` | GET | Health check |
|
| 28 |
| `/reset` | POST | Reset environment to initial state |
|
| 29 |
-
| `/step` | POST | Execute an action (BUY/SELL/GET_PRICE/BACKTEST) |
|
| 30 |
| `/state` | GET | Get current environment state |
|
| 31 |
| `/tasks` | GET | List all available tasks |
|
|
|
|
| 32 |
|
| 33 |
## ๐ง Installation
|
| 34 |
|
| 35 |
### Prerequisites
|
| 36 |
- Python 3.10+
|
| 37 |
-
- Docker (for containerized deployment)
|
| 38 |
|
| 39 |
### Local Setup
|
| 40 |
|
| 41 |
```bash
|
| 42 |
# Clone the repository
|
| 43 |
-
git clone https://github.com/
|
| 44 |
cd quant-gym-openenv
|
| 45 |
|
| 46 |
# Install dependencies
|
| 47 |
pip install -r requirements.txt
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
|
| 52 |
# Start the server
|
| 53 |
-
python -m uvicorn server.app:app --host 0.0.0.0 --port 8000
|
| 54 |
|
| 55 |
|
| 56 |
๐ฎ Action Schema
|
| 57 |
The agent can take the following actions:
|
|
|
|
|
|
|
| 58 |
{
|
| 59 |
-
"type": "BUY | SELL | GET_PRICE | BACKTEST",
|
| 60 |
"amount": 10,
|
| 61 |
"explanation": "RSI indicates oversold condition",
|
| 62 |
"strategy": "momentum"
|
| 63 |
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
๐๏ธ Observation Schema
|
| 68 |
The environment returns:
|
| 69 |
|
|
|
|
| 70 |
{
|
| 71 |
-
"timestamp": "
|
| 72 |
-
"price":
|
| 73 |
"balance": 8500.00,
|
| 74 |
"holdings": 10,
|
| 75 |
-
"portfolio_value":
|
| 76 |
"last_news": {
|
| 77 |
"headline": "Apple announces new AI chip",
|
| 78 |
"sentiment": "positive"
|
|
@@ -84,23 +97,76 @@ The environment returns:
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
quant-gym-openenv/
|
| 92 |
-
โโโ
|
| 93 |
-
โโโ
|
| 94 |
-
โโโ
|
| 95 |
-
โโโ
|
|
|
|
|
|
|
| 96 |
โโโ server/
|
| 97 |
-
โ โโโ app.py
|
| 98 |
-
โ โโโ environment.py
|
| 99 |
-
โ โโโ Dockerfile # Container config
|
| 100 |
โ โโโ data/
|
| 101 |
-
โ โโโ prices.csv
|
| 102 |
-
โ โโโ news.json
|
| 103 |
โโโ graders/
|
| 104 |
-
โโโ task1_grader.py
|
| 105 |
-
โโโ task2_grader.py
|
| 106 |
-
โโโ task3_grader.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
Quant-Gym is a benchmark environment where AI agents can practice:
|
| 9 |
- Fetching real-time market data
|
| 10 |
- Analyzing financial news sentiment
|
| 11 |
+
- Executing buy/sell trades
|
| 12 |
- Evaluating trading strategies with risk metrics
|
| 13 |
|
| 14 |
+
**This is a research benchmark for evaluating AI reasoning in financial contexts, not a trading tool.**
|
| 15 |
|
| 16 |
## ๐ Environment Tasks
|
| 17 |
|
|
|
|
| 25 |
|
| 26 |
| Endpoint | Method | Description |
|
| 27 |
|----------|--------|-------------|
|
| 28 |
+
| `/` | GET | Welcome message |
|
| 29 |
| `/health` | GET | Health check |
|
| 30 |
| `/reset` | POST | Reset environment to initial state |
|
| 31 |
+
| `/step` | POST | Execute an action (BUY/SELL/GET_PRICE/BACKTEST/GET_NEWS) |
|
| 32 |
| `/state` | GET | Get current environment state |
|
| 33 |
| `/tasks` | GET | List all available tasks |
|
| 34 |
+
| `/docs` | GET | Interactive API documentation (FastAPI) |
|
| 35 |
|
| 36 |
## ๐ง Installation
|
| 37 |
|
| 38 |
### Prerequisites
|
| 39 |
- Python 3.10+
|
| 40 |
+
- Docker (optional, for containerized deployment)
|
| 41 |
|
| 42 |
### Local Setup
|
| 43 |
|
| 44 |
```bash
|
| 45 |
# Clone the repository
|
| 46 |
+
git clone https://github.com/Astocoder/quant-gym-openenv.git
|
| 47 |
cd quant-gym-openenv
|
| 48 |
|
| 49 |
# Install dependencies
|
| 50 |
pip install -r requirements.txt
|
| 51 |
|
| 52 |
+
# Set up Hugging Face token (optional, for LLM features)
|
| 53 |
+
echo 'HF_TOKEN=your_hf_token_here' > .env
|
| 54 |
|
| 55 |
# Start the server
|
| 56 |
+
python -m uvicorn server.app:app --host 0.0.0.0 --port 8000 --reload
|
| 57 |
|
| 58 |
|
| 59 |
๐ฎ Action Schema
|
| 60 |
The agent can take the following actions:
|
| 61 |
+
|
| 62 |
+
json
|
| 63 |
{
|
| 64 |
+
"type": "BUY | SELL | GET_PRICE | BACKTEST | GET_NEWS",
|
| 65 |
"amount": 10,
|
| 66 |
"explanation": "RSI indicates oversold condition",
|
| 67 |
"strategy": "momentum"
|
| 68 |
}
|
| 69 |
|
| 70 |
+
Action Examples
|
| 71 |
+
Action Description
|
| 72 |
+
{"type": "GET_PRICE"} Get current stock price
|
| 73 |
+
{"type": "BUY", "amount": 10} Buy 10 shares
|
| 74 |
+
{"type": "SELL", "amount": 5} Sell 5 shares
|
| 75 |
+
{"type": "GET_NEWS", "explanation": "your analysis"} Get news with analysis
|
| 76 |
+
{"type": "BACKTEST", "strategy": "momentum"} Backtest momentum strategy
|
| 77 |
|
| 78 |
|
| 79 |
๐๏ธ Observation Schema
|
| 80 |
The environment returns:
|
| 81 |
|
| 82 |
+
json
|
| 83 |
{
|
| 84 |
+
"timestamp": "step_5",
|
| 85 |
+
"price": 155.00,
|
| 86 |
"balance": 8500.00,
|
| 87 |
"holdings": 10,
|
| 88 |
+
"portfolio_value": 10050.00,
|
| 89 |
"last_news": {
|
| 90 |
"headline": "Apple announces new AI chip",
|
| 91 |
"sentiment": "positive"
|
|
|
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
+
๐ Running the Baseline Agent
|
| 101 |
+
# Set your Hugging Face token
|
| 102 |
+
export HF_TOKEN="your_hf_token_here"
|
| 103 |
+
|
| 104 |
+
# Run inference
|
| 105 |
+
python inference.py
|
| 106 |
+
|
| 107 |
+
Expected Output:-
|
| 108 |
+
[INFO] HF_TOKEN found (length: 37 chars)
|
| 109 |
+
[START] task=quant-gym env=quant-gym model=meta-llama/Llama-3.2-3B-Instruct
|
| 110 |
+
[STEP] step=1 action=BUY 5 reward=0.15 done=false error=null
|
| 111 |
+
[STEP] step=2 action=GET_PRICE reward=0.05 done=false error=null
|
| 112 |
+
[STEP] step=3 action=SELL 5 reward=0.20 done=false error=null
|
| 113 |
+
...
|
| 114 |
+
[END] success=true steps=10 score=0.650 rewards=...
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
๐ณ Docker Deployment
|
| 118 |
+
Build and run with Docker:
|
| 119 |
+
# Build the image
|
| 120 |
+
docker build -t quant-gym .
|
| 121 |
|
| 122 |
+
# Run the container
|
| 123 |
+
docker run -p 7860:7860 quant-gym
|
| 124 |
|
| 125 |
+
Then access the API at http://localhost:7860
|
| 126 |
|
| 127 |
+
๐ Hugging Face Space
|
| 128 |
+
Live demo: https://huggingface.co/spaces/Astocoder/quant-gym
|
| 129 |
+
|
| 130 |
+
๐ Project Structure
|
| 131 |
quant-gym-openenv/
|
| 132 |
+
โโโ Dockerfile # Container configuration
|
| 133 |
+
โโโ inference.py # Baseline agent script
|
| 134 |
+
โโโ models.py # Pydantic schemas
|
| 135 |
+
โโโ openenv.yaml # OpenEnv configuration
|
| 136 |
+
โโโ requirements.txt # Python dependencies
|
| 137 |
+
โโโ README.md # This file
|
| 138 |
โโโ server/
|
| 139 |
+
โ โโโ app.py # FastAPI server
|
| 140 |
+
โ โโโ environment.py # Trading logic
|
|
|
|
| 141 |
โ โโโ data/
|
| 142 |
+
โ โโโ prices.csv # Market data
|
| 143 |
+
โ โโโ news.json # News headlines
|
| 144 |
โโโ graders/
|
| 145 |
+
โโโ task1_grader.py # Price fetch grader
|
| 146 |
+
โโโ task2_grader.py # News analysis grader
|
| 147 |
+
โโโ task3_grader.py # Backtest grader
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
๐ Environment Variables
|
| 151 |
+
Variable Description Default
|
| 152 |
+
HF_TOKEN Hugging Face API token None (optional)
|
| 153 |
+
API_BASE_URL HF API endpoint https://api-inference.huggingface.co/v1
|
| 154 |
+
MODEL_NAME LLM model name meta-llama/Llama-3.2-3B-Instruct
|
| 155 |
+
BASE_URL Quant-Gym API URL http://localhost:8000
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
๐ Evaluation Criteria
|
| 159 |
+
OpenEnv Compliance: Full implementation of step()/reset()/state() APIs
|
| 160 |
+
|
| 161 |
+
Task Completion: All 3 tasks return scores between 0.0-1.0
|
| 162 |
+
|
| 163 |
+
Reward Function: Partial progress signals for meaningful learning
|
| 164 |
+
|
| 165 |
+
Reproducibility: Static data ensures consistent results
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
โ ๏ธ Disclaimer
|
| 169 |
+
This is a research benchmark environment for evaluating AI agent reasoning. It does not provide financial advice or real trading recommendations. All data is for simulation purposes only.
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
server/app.py
CHANGED
|
@@ -2,11 +2,15 @@ import sys
|
|
| 2 |
import os
|
| 3 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 4 |
|
| 5 |
-
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from typing import Optional, Dict, Any, List
|
| 9 |
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Simple models for the API
|
| 12 |
class ActionType(str, Enum):
|
|
|
|
| 2 |
import os
|
| 3 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 4 |
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from typing import Optional, Dict, Any, List
|
| 9 |
from enum import Enum
|
| 10 |
+
from server.app import app as quant_gym_app
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
app = quant_gym_app
|
| 14 |
|
| 15 |
# Simple models for the API
|
| 16 |
class ActionType(str, Enum):
|