ncncomplete commited on
Commit
b5ee03f
·
verified ·
1 Parent(s): 3782916

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. server/task_bank.py +44 -2
server/task_bank.py CHANGED
@@ -3,6 +3,8 @@
3
  from __future__ import annotations
4
 
5
  from dataclasses import dataclass
 
 
6
  from typing import Dict, List, Tuple
7
 
8
  try:
@@ -85,6 +87,7 @@ TASKS: Dict[str, CodeReviewTask] = {
85
 
86
 
87
  EPISODE_SCORES: Dict[tuple[str, str], float] = {}
 
88
 
89
 
90
  def list_tasks() -> List[Dict[str, str]]:
@@ -149,9 +152,48 @@ def grade_action(action: CodeAction, task: CodeReviewTask) -> tuple[float, str]:
149
 
150
  def record_episode_score(task_id: str, episode_id: str, score: float) -> None:
151
  """Persist normalized score for grader endpoint."""
152
- EPISODE_SCORES[(task_id, episode_id)] = max(0.0, min(1.0, float(score)))
 
 
153
 
154
 
155
  def get_episode_score(task_id: str, episode_id: str) -> float:
156
  """Read score for task/episode pair."""
157
- return EPISODE_SCORES.get((task_id, episode_id), 0.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from __future__ import annotations
4
 
5
  from dataclasses import dataclass
6
+ import json
7
+ import os
8
  from typing import Dict, List, Tuple
9
 
10
  try:
 
87
 
88
 
89
  EPISODE_SCORES: Dict[tuple[str, str], float] = {}
90
+ SCORE_FILE = os.getenv("CODING_ENV_SCORE_FILE", "/tmp/coding_env_episode_scores.json")
91
 
92
 
93
  def list_tasks() -> List[Dict[str, str]]:
 
152
 
153
  def record_episode_score(task_id: str, episode_id: str, score: float) -> None:
154
  """Persist normalized score for grader endpoint."""
155
+ normalized = max(0.0, min(1.0, float(score)))
156
+ EPISODE_SCORES[(task_id, episode_id)] = normalized
157
+ _persist_score(task_id, episode_id, normalized)
158
 
159
 
160
  def get_episode_score(task_id: str, episode_id: str) -> float:
161
  """Read score for task/episode pair."""
162
+ in_memory = EPISODE_SCORES.get((task_id, episode_id))
163
+ if in_memory is not None:
164
+ return in_memory
165
+ return _load_persisted_score(task_id, episode_id)
166
+
167
+
168
+ def _persist_score(task_id: str, episode_id: str, score: float) -> None:
169
+ key = f"{task_id}::{episode_id}"
170
+ payload: Dict[str, float] = {}
171
+ if os.path.exists(SCORE_FILE):
172
+ try:
173
+ with open(SCORE_FILE, "r", encoding="utf-8") as f:
174
+ loaded = json.load(f)
175
+ if isinstance(loaded, dict):
176
+ payload = {
177
+ str(k): float(v)
178
+ for k, v in loaded.items()
179
+ if isinstance(v, (int, float))
180
+ }
181
+ except Exception:
182
+ payload = {}
183
+
184
+ payload[key] = float(score)
185
+ with open(SCORE_FILE, "w", encoding="utf-8") as f:
186
+ json.dump(payload, f)
187
+
188
+
189
+ def _load_persisted_score(task_id: str, episode_id: str) -> float:
190
+ if not os.path.exists(SCORE_FILE):
191
+ return 0.0
192
+ try:
193
+ with open(SCORE_FILE, "r", encoding="utf-8") as f:
194
+ loaded = json.load(f)
195
+ key = f"{task_id}::{episode_id}"
196
+ value = loaded.get(key, 0.0) if isinstance(loaded, dict) else 0.0
197
+ return max(0.0, min(1.0, float(value)))
198
+ except Exception:
199
+ return 0.0