h1manshu commited on
Commit
a158b53
·
verified ·
1 Parent(s): 88ddd6f

Upload folder using huggingface_hub

Browse files
inference.py CHANGED
@@ -29,11 +29,17 @@ from code_review.client import CodeReviewEnv
29
  API_BASE_URL = "https://router.huggingface.co/v1"
30
  API_KEY = os.getenv("HF_TOKEN")
31
  MODEL_NAME = os.getenv("MODEL_NAME")
 
 
32
  MAX_STEPS = 3
33
  TEMPERATURE = 0.2
34
- MAX_TOKENS = 512
 
 
 
 
 
35
 
36
- DEBUG = True
37
  ACTION_PREFIX_RE = re.compile(
38
  r"^(action|next action)\s*[:\-]\s*",
39
  re.IGNORECASE,
@@ -195,12 +201,12 @@ def parse_action(text: str) -> Dict[str, Any]:
195
 
196
  async def run_episode(client, env):
197
  result = await env.reset()
198
-
199
  obs = result.observation
200
-
201
  final_score = 0.0
202
 
203
  for step in range(1, MAX_STEPS + 1):
 
 
204
 
205
  prompt = build_prompt(step, MAX_STEPS, obs)
206
 
@@ -210,15 +216,12 @@ async def run_episode(client, env):
210
  ]
211
 
212
  completion = safe_completion(client, messages) # still sync
213
- # print(completion)
214
  if completion is None:
215
  action = fallback_action()
216
  else:
217
  response_text = completion.choices[0].message.content or ""
218
  action_dict = parse_action(response_text)
219
 
220
- # print(response_text)
221
-
222
  action = CodeReviewAction(
223
  action_type=action_dict.get("action_type"),
224
  comment=action_dict.get("comment"),
@@ -227,20 +230,14 @@ async def run_episode(client, env):
227
  )
228
 
229
  result = await env.step(action)
230
- # print("Result === " , result)
231
 
232
  obs = result.observation
233
  reward = result.reward
234
  done = result.done
235
 
 
236
  final_score = max(final_score, reward.score if reward else 0.0)
237
 
238
- print(f"Step {step} | Action: {action} | Reward: {reward}")
239
-
240
- if done:
241
- print(f"Done in {step} steps")
242
- break
243
-
244
  return final_score
245
 
246
 
@@ -248,23 +245,22 @@ async def main():
248
  client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
249
 
250
  scores = []
251
- # log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
252
 
253
  async with CodeReviewEnv(base_url="http://localhost:8000") as env:
254
- NUM_EPISODES = 6
255
-
256
  for i in range(NUM_EPISODES):
257
- print(f"\n=== Episode {i+1} ===")
258
  env.task_index = i
259
 
260
  score = await run_episode(client, env)
261
  scores.append(score)
262
 
263
- print(f"Scores so far: {scores}")
264
- # return 0
265
 
266
- print("\nFinished all episodes")
267
- print(f"Final Scores: {scores}")
 
 
268
 
269
 
270
  if __name__ == "__main__":
 
29
  API_BASE_URL = "https://router.huggingface.co/v1"
30
  API_KEY = os.getenv("HF_TOKEN")
31
  MODEL_NAME = os.getenv("MODEL_NAME")
32
+ TASK_NAME = "code_review"
33
+ BENCHMARK = "code_review_benchmark"
34
  MAX_STEPS = 3
35
  TEMPERATURE = 0.2
36
+ MAX_TOKENS = 150
37
+ NUM_EPISODES = 6
38
+ _MAX_REWARD_PER_STEP = MAX_TOKENS * 0.1
39
+ MAX_TOTAL_REWARD = NUM_EPISODES * MAX_STEPS * _MAX_REWARD_PER_STEP
40
+ SUCCESS_SCORE_THRESHOLD = 0.1 # normalized score in [0, 1]
41
+
42
 
 
43
  ACTION_PREFIX_RE = re.compile(
44
  r"^(action|next action)\s*[:\-]\s*",
45
  re.IGNORECASE,
 
201
 
202
  async def run_episode(client, env):
203
  result = await env.reset()
 
204
  obs = result.observation
 
205
  final_score = 0.0
206
 
207
  for step in range(1, MAX_STEPS + 1):
208
+ if result.done:
209
+ break
210
 
211
  prompt = build_prompt(step, MAX_STEPS, obs)
212
 
 
216
  ]
217
 
218
  completion = safe_completion(client, messages) # still sync
 
219
  if completion is None:
220
  action = fallback_action()
221
  else:
222
  response_text = completion.choices[0].message.content or ""
223
  action_dict = parse_action(response_text)
224
 
 
 
225
  action = CodeReviewAction(
226
  action_type=action_dict.get("action_type"),
227
  comment=action_dict.get("comment"),
 
230
  )
231
 
232
  result = await env.step(action)
 
233
 
234
  obs = result.observation
235
  reward = result.reward
236
  done = result.done
237
 
238
+ log_step(step=step, action=response_text, reward=reward.score, done=done, error=None)
239
  final_score = max(final_score, reward.score if reward else 0.0)
240
 
 
 
 
 
 
 
241
  return final_score
242
 
243
 
 
245
  client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
246
 
247
  scores = []
248
+ log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
249
 
250
  async with CodeReviewEnv(base_url="http://localhost:8000") as env:
 
 
251
  for i in range(NUM_EPISODES):
252
+ print(f"\n===== Episode {i+1} =====", flush=True)
253
  env.task_index = i
254
 
255
  score = await run_episode(client, env)
256
  scores.append(score)
257
 
258
+ # print(f"[INFO] Scores so far: {scores}", flush=True)
 
259
 
260
+ total_score = sum(scores) / MAX_TOTAL_REWARD if MAX_TOTAL_REWARD > 0 else 0.0
261
+ final_score = min(max(score, 0.0), 1.0) # clamp to [0, 1]
262
+ success = final_score >= SUCCESS_SCORE_THRESHOLD
263
+ log_end(success=success, steps=NUM_EPISODES*MAX_STEPS, score=final_score, rewards=scores)
264
 
265
 
266
  if __name__ == "__main__":
openenv_code_review.egg-info/PKG-INFO ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.4
2
+ Name: openenv-code_review
3
+ Version: 0.1.0
4
+ Summary: Code Review environment for OpenEnv
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: openenv-core[core]>=0.2.1
7
+ Provides-Extra: dev
8
+ Requires-Dist: pytest>=8.0.0; extra == "dev"
9
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
openenv_code_review.egg-info/SOURCES.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ README.md
2
+ pyproject.toml
3
+ ./__init__.py
4
+ ./client.py
5
+ ./inference.py
6
+ ./models.py
7
+ openenv_code_review.egg-info/PKG-INFO
8
+ openenv_code_review.egg-info/SOURCES.txt
9
+ openenv_code_review.egg-info/dependency_links.txt
10
+ openenv_code_review.egg-info/entry_points.txt
11
+ openenv_code_review.egg-info/requires.txt
12
+ openenv_code_review.egg-info/top_level.txt
13
+ server/__init__.py
14
+ server/app.py
15
+ server/code_review_environment.py
openenv_code_review.egg-info/dependency_links.txt ADDED
@@ -0,0 +1 @@
 
 
1
+
openenv_code_review.egg-info/entry_points.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [console_scripts]
2
+ server = code_review.server.app:main
openenv_code_review.egg-info/requires.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openenv-core[core]>=0.2.1
2
+
3
+ [dev]
4
+ pytest>=8.0.0
5
+ pytest-cov>=4.0.0
openenv_code_review.egg-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ code_review
server/app.py CHANGED
@@ -35,12 +35,12 @@ except Exception as e: # pragma: no cover
35
  "openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
36
  ) from e
37
 
38
- # try:
39
- # from ..models import CodeReviewAction, CodeReviewObservation
40
- # from .code_review_environment import CodeReviewEnvironment
41
- # except ModuleNotFoundError:
42
- from models import CodeReviewAction, CodeReviewObservation
43
- from server.code_review_environment import CodeReviewEnvironment
44
 
45
 
46
  # Create the app with web interface and README integration
 
35
  "openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
36
  ) from e
37
 
38
+ try:
39
+ from ..models import CodeReviewAction, CodeReviewObservation
40
+ from .code_review_environment import CodeReviewEnvironment
41
+ except ModuleNotFoundError:
42
+ from models import CodeReviewAction, CodeReviewObservation
43
+ from server.code_review_environment import CodeReviewEnvironment
44
 
45
 
46
  # Create the app with web interface and README integration
server/code_review_environment.py CHANGED
@@ -242,15 +242,15 @@ class CodeReviewEnvironment(Environment):
242
  def grade_action(self, action, ground_truth):
243
  score = 0.0
244
 
245
- print("Action === ", action)
246
- print("Ground truth === ", ground_truth)
247
 
248
  # ------------------------------
249
  # ISSUE DETECTION (40%)
250
  # ------------------------------
251
  issue_score = self.score_issues(action.comment, ground_truth)
252
  score += 0.4 * issue_score
253
- print("After Issue Score == ", issue_score)
254
 
255
  # ------------------------------
256
  # FIX QUALITY (30%)
@@ -258,7 +258,7 @@ class CodeReviewEnvironment(Environment):
258
  fix_score = self.score_fix(action.suggested_code, ground_truth)
259
  score += 0.3 * fix_score
260
 
261
- print("After Fix Score == ", fix_score)
262
 
263
  # ------------------------------
264
  # DECISION (30%)
@@ -266,7 +266,7 @@ class CodeReviewEnvironment(Environment):
266
  decision_score = self.score_decision(action, ground_truth)
267
  score += 0.3 * decision_score
268
 
269
- print("After Decision Score == ", decision_score)
270
 
271
  # ------------------------------
272
  # CLAMP SCORE
 
242
  def grade_action(self, action, ground_truth):
243
  score = 0.0
244
 
245
+ # print("Action === ", action)
246
+ # print("Ground truth === ", ground_truth)
247
 
248
  # ------------------------------
249
  # ISSUE DETECTION (40%)
250
  # ------------------------------
251
  issue_score = self.score_issues(action.comment, ground_truth)
252
  score += 0.4 * issue_score
253
+ # print("After Issue Score == ", issue_score)
254
 
255
  # ------------------------------
256
  # FIX QUALITY (30%)
 
258
  fix_score = self.score_fix(action.suggested_code, ground_truth)
259
  score += 0.3 * fix_score
260
 
261
+ # print("After Fix Score == ", fix_score)
262
 
263
  # ------------------------------
264
  # DECISION (30%)
 
266
  decision_score = self.score_decision(action, ground_truth)
267
  score += 0.3 * decision_score
268
 
269
+ # print("After Decision Score == ", decision_score)
270
 
271
  # ------------------------------
272
  # CLAMP SCORE