Commit ·
ef16990
1
Parent(s): a561a86
Add CLAUDE.md project guide and HF Space deployment script
Browse filesCLAUDE.md provides architecture overview, commands, and key design
points for future Claude Code sessions. deploy_to_space.py is a
utility script for pushing updates to the HF Space.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- CLAUDE.md +88 -0
- deploy_to_space.py +25 -0
CLAUDE.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CLAUDE.md
|
| 2 |
+
|
| 3 |
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
| 4 |
+
|
| 5 |
+
## Project Overview
|
| 6 |
+
|
| 7 |
+
This is an **OpenEnv-based reinforcement learning environment** for training AI agents to optimize energy consumption and RAM usage in simulated computer systems. It deploys as a Docker-based Hugging Face Space using FastAPI.
|
| 8 |
+
|
| 9 |
+
## Commands
|
| 10 |
+
|
| 11 |
+
```bash
|
| 12 |
+
# Install dependencies (uv is the package manager)
|
| 13 |
+
uv sync
|
| 14 |
+
|
| 15 |
+
# Run the server locally
|
| 16 |
+
uv run server
|
| 17 |
+
# Or directly with uvicorn:
|
| 18 |
+
uvicorn server.app:app --reload --host 0.0.0.0 --port 8000
|
| 19 |
+
|
| 20 |
+
# Run with custom port
|
| 21 |
+
uv run server --port 8001
|
| 22 |
+
|
| 23 |
+
# Build Docker image
|
| 24 |
+
docker build -t energy-optimization-rl .
|
| 25 |
+
|
| 26 |
+
# Run Docker container
|
| 27 |
+
docker run --rm -p 8000:8000 energy-optimization-rl
|
| 28 |
+
|
| 29 |
+
# Verify graders are discoverable
|
| 30 |
+
python check_graders.py # Basic verification
|
| 31 |
+
python check_graders.py verify # Import & callable check
|
| 32 |
+
python check_graders.py json # JSON manifest output
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
There are no tests in this project. The `pyproject.toml` lists `pytest` as a dev dependency but no test directory exists.
|
| 36 |
+
|
| 37 |
+
## Architecture
|
| 38 |
+
|
| 39 |
+
### Core Flow
|
| 40 |
+
|
| 41 |
+
1. **`openenv.yaml`** — Declarative config that defines 5 tasks with their grader references (`graders:grade_*`). This is what the OpenEnv validator reads first.
|
| 42 |
+
2. **`server/app.py`** — FastAPI app created via `openenv.core.env_server.http_server.create_app()`, which wires up `/reset`, `/step`, `/state`, `/schema`, and `/ws` endpoints. Additional `/graders`, `/tasks`, `/validate` endpoints are added for validator tool detection.
|
| 43 |
+
3. **`server/he_demo_environment.py`** — The `EnergyOptimizationEnvironment` class implementing `Environment` from openenv-core. Manages state transitions, reward calculation, and task progression.
|
| 44 |
+
4. **`models.py`** — Pydantic models: `EnergyOptimizationAction`, `EnergyOptimizationObservation`, `Task`, `TaskSummary`. Also contains legacy grader functions that duplicate logic from `graders.py`.
|
| 45 |
+
|
| 46 |
+
### Dual Grader System
|
| 47 |
+
|
| 48 |
+
There are **two separate grader implementations** that must be kept in sync:
|
| 49 |
+
|
| 50 |
+
- **`graders.py`** — Self-contained, no external dependencies (uses only `getattr` on observations). This is what `openenv.yaml` references (`graders:grade_basic_ram_reduction`, etc.). **Must remain dependency-free** because the OpenEnv validator imports it in a minimal environment.
|
| 51 |
+
- **`task_graders.py`** — Imports from `models.py`, has richer scoring with weighted formulas. Used by the server endpoints and `task_registry.py`.
|
| 52 |
+
|
| 53 |
+
When modifying grader logic or adding tasks, update **both** `graders.py` and `task_graders.py`, plus the `__all__` lists.
|
| 54 |
+
|
| 55 |
+
### Grader Discovery Modules
|
| 56 |
+
|
| 57 |
+
There are multiple redundant discovery/manifest modules that the server endpoints import:
|
| 58 |
+
|
| 59 |
+
- **`grader_manifest.py`** — Lightweight manifest dict for `/graders/manifest` endpoint
|
| 60 |
+
- **`graders_manifest.py`** — Detailed manifest with scoring methodology, performance examples, and validation checklist
|
| 61 |
+
- **`grader_discovery.py`** — Discovery manifest with import paths and openenv references
|
| 62 |
+
- **`task_registry.py`** — Registry mapping task names to grader functions and metadata
|
| 63 |
+
|
| 64 |
+
### Other Root Modules
|
| 65 |
+
|
| 66 |
+
- **`client.py`** — `EnergyOptimizationEnv` subclassing `EnvClient` from openenv-core. WebSocket-based client for connecting to the server.
|
| 67 |
+
- **`inference.py`** — LLM inference script with benchmarking (Random vs Heuristic vs LLM). Uses HF API. Configured via env vars (`API_BASE_URL`, `MODEL_NAME`, `HF_TOKEN`).
|
| 68 |
+
- **`evaluate_inference.py`** — Evaluation script for LLM performance across tasks.
|
| 69 |
+
- **`gym_wrapper.py`** — `EnergyOptimizationGymEnv` wrapping the environment in a Gymnasium interface for SB3 training.
|
| 70 |
+
- **`openenv-energy-rl/`** — Standalone sub-project with a simpler `EnergyEnv` (gym-based, not OpenEnv).
|
| 71 |
+
|
| 72 |
+
### Key Design Points
|
| 73 |
+
|
| 74 |
+
- The environment starts at RAM 80%, energy 8.0 kWh, system load 0.7. Actions reduce these deterministically (no randomness — system dynamics are disabled).
|
| 75 |
+
- Reward is `intensity * 0.1` clamped to [0, 1], with task completion bonuses of `difficulty * 0.5`.
|
| 76 |
+
- Episodes end at 100 steps or when all 5 tasks are completed.
|
| 77 |
+
- The Dockerfile is multi-stage (`ghcr.io/meta-pytorch/openenv-base:latest` base), installs via `uv sync`, and runs `uvicorn` from `/app/env`.
|
| 78 |
+
- `PYTHONPATH` in Docker includes both `/app/env` and `/app` for grader discovery.
|
| 79 |
+
- HF Space config: SDK docker, port 8000, base path `/web`.
|
| 80 |
+
|
| 81 |
+
### Task Definitions
|
| 82 |
+
|
| 83 |
+
All 5 tasks are defined in three places that must stay consistent:
|
| 84 |
+
1. `openenv.yaml` (task names, descriptions, grader references, max_steps)
|
| 85 |
+
2. `_create_tasks()` in `server/he_demo_environment.py` (Task objects with targets)
|
| 86 |
+
3. Grader functions in `graders.py` and `task_graders.py` (scoring thresholds)
|
| 87 |
+
|
| 88 |
+
Tasks: basic_ram_reduction → energy_optimization → balanced_optimization → advanced_efficiency → expert_optimization (difficulty 1–5).
|
deploy_to_space.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""Deploy updated files directly to HF Space using huggingface_hub."""
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import upload_folder
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
space_repo_id = "Sushruth21/energy-optimization-space"
|
| 8 |
+
local_dir = "."
|
| 9 |
+
|
| 10 |
+
print(f"🚀 Deploying to HF Space: {space_repo_id}")
|
| 11 |
+
print(f"📁 Local directory: {local_dir}")
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
info = upload_folder(
|
| 15 |
+
repo_id=space_repo_id,
|
| 16 |
+
folder_path=local_dir,
|
| 17 |
+
repo_type="space",
|
| 18 |
+
commit_message="Fix PYTHONPATH in Dockerfile for grader discovery and add critical files",
|
| 19 |
+
multi_commits=False,
|
| 20 |
+
multi_commits_verbose=False,
|
| 21 |
+
)
|
| 22 |
+
print(f"✅ Upload successful!")
|
| 23 |
+
print(f"📝 Commit URL: {info.commit_url}")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"❌ Upload failed: {type(e).__name__}: {e}")
|