Siddh12334 commited on
Commit
0eb0abc
·
1 Parent(s): 8c7e984

feat: optimize Space — dockerignore, dotenv secrets, multi-worker uvicorn

Browse files
Files changed (4) hide show
  1. .dockerignore +14 -0
  2. .env.example +15 -0
  3. Dockerfile +12 -2
  4. environment/server.py +11 -3
.dockerignore ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ .env
6
+ .git/
7
+ .gitignore
8
+ .gitattributes
9
+ CLAUDE.md
10
+ assets/
11
+ training/
12
+ eval/
13
+ requirements.txt
14
+ data/facts.json
.env.example ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copy to .env and fill in values for local development
2
+ # In HF Spaces, set these as Space secrets (Settings → Variables and secrets)
3
+
4
+ # Number of corrupt docs per episode (1-4). Leave blank for random per episode.
5
+ DIFFICULTY=
6
+
7
+ # Max concurrent environment sessions
8
+ MAX_CONCURRENT_ENVS=64
9
+
10
+ # WandB (only needed for training, not the server)
11
+ WANDB_API_KEY=
12
+
13
+ # HuggingFace (only needed for training, not the server)
14
+ HF_TOKEN=
15
+ HF_HUB_MODEL_ID=
Dockerfile CHANGED
@@ -1,8 +1,18 @@
1
  FROM python:3.11-slim
 
2
  WORKDIR /app
 
 
3
  COPY requirements-server.txt .
4
  RUN pip install --no-cache-dir -r requirements-server.txt
 
 
5
  COPY . .
6
- RUN python -c "from data.loader import build_fact_database; build_fact_database()" || true
 
 
 
7
  EXPOSE 7860
8
- CMD ["uvicorn", "environment.server:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
1
  FROM python:3.11-slim
2
+
3
  WORKDIR /app
4
+
5
+ # Install deps first (cached layer — only invalidated when requirements change)
6
  COPY requirements-server.txt .
7
  RUN pip install --no-cache-dir -r requirements-server.txt
8
+
9
+ # Copy source (excludes venv/, training/, assets/, .git/ via .dockerignore)
10
  COPY . .
11
+
12
+ # Pre-generate facts.json at build time; falls back gracefully if network is down
13
+ RUN python -m data.loader || echo "facts.json generation skipped — will use fallback facts"
14
+
15
  EXPOSE 7860
16
+
17
+ # 2 workers: handles concurrent sessions without threading issues
18
+ CMD ["uvicorn", "environment.server:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "2"]
environment/server.py CHANGED
@@ -1,16 +1,24 @@
 
 
1
  from openenv.core import create_app
2
  import uvicorn
3
 
 
 
4
  from environment.actions import ContextCorruptionAction, EpisodeObservation
5
  from environment.env import ContextCorruptionEnv
6
 
 
 
 
 
7
  app = create_app(
8
- env=lambda: ContextCorruptionEnv(),
9
  action_cls=ContextCorruptionAction,
10
  observation_cls=EpisodeObservation,
11
  env_name="ContextCorruption-Env",
12
- max_concurrent_envs=64,
13
  )
14
 
15
  if __name__ == "__main__":
16
- uvicorn.run("environment.server:app", host="0.0.0.0", port=8000, reload=False)
 
1
+ import os
2
+ from dotenv import load_dotenv
3
  from openenv.core import create_app
4
  import uvicorn
5
 
6
+ load_dotenv()
7
+
8
  from environment.actions import ContextCorruptionAction, EpisodeObservation
9
  from environment.env import ContextCorruptionEnv
10
 
11
+ _difficulty_env = os.getenv("DIFFICULTY")
12
+ _difficulty = int(_difficulty_env) if _difficulty_env else None
13
+ _max_sessions = int(os.getenv("MAX_CONCURRENT_ENVS", "64"))
14
+
15
  app = create_app(
16
+ env=lambda: ContextCorruptionEnv(difficulty=_difficulty),
17
  action_cls=ContextCorruptionAction,
18
  observation_cls=EpisodeObservation,
19
  env_name="ContextCorruption-Env",
20
+ max_concurrent_envs=_max_sessions,
21
  )
22
 
23
  if __name__ == "__main__":
24
+ uvicorn.run("environment.server:app", host="0.0.0.0", port=7860, reload=False)