Update root inference.py to match pre-submission format
Browse files- inference.py +99 -2
inference.py
CHANGED
|
@@ -1,5 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def _bootstrap_path() -> None:
|
|
@@ -9,11 +27,90 @@ def _bootstrap_path() -> None:
|
|
| 9 |
sys.path.insert(0, agentbox_root)
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def main() -> None:
|
| 13 |
_bootstrap_path()
|
| 14 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
from typing import Any, Dict, List, Optional
|
| 4 |
+
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Required submission variables.
|
| 9 |
+
# Defaults are provided only for API_BASE_URL and MODEL_NAME.
|
| 10 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 11 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Compatibility fallback. HF_TOKEN remains the primary required variable.
|
| 17 |
+
API_KEY = HF_TOKEN or os.getenv("API_KEY") or os.getenv("OPENAI_API_KEY")
|
| 18 |
+
|
| 19 |
+
TASK_NAME = os.getenv("TASK", "easy")
|
| 20 |
+
BENCHMARK = os.getenv("BENCHMARK", "codeguard")
|
| 21 |
|
| 22 |
|
| 23 |
def _bootstrap_path() -> None:
|
|
|
|
| 27 |
sys.path.insert(0, agentbox_root)
|
| 28 |
|
| 29 |
|
| 30 |
+
def _fmt_bool(value: bool) -> str:
|
| 31 |
+
return "true" if value else "false"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _fmt_error(error: Optional[str]) -> str:
|
| 35 |
+
return "null" if error is None else str(error)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _clamp_score(value: float) -> float:
|
| 39 |
+
return max(0.0, min(1.0, value))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
def main() -> None:
|
| 43 |
_bootstrap_path()
|
| 44 |
+
from src.env import CodeGuardEnv
|
| 45 |
+
|
| 46 |
+
rewards: List[float] = []
|
| 47 |
+
steps: int = 0
|
| 48 |
+
score: float = 0.0
|
| 49 |
+
success: bool = False
|
| 50 |
+
|
| 51 |
+
print(f"[START] task={TASK_NAME} env={BENCHMARK} model={MODEL_NAME}")
|
| 52 |
+
|
| 53 |
+
env = None
|
| 54 |
+
try:
|
| 55 |
+
client = None
|
| 56 |
+
init_error: Optional[str] = None
|
| 57 |
+
if API_KEY:
|
| 58 |
+
try:
|
| 59 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 60 |
+
except Exception as exc:
|
| 61 |
+
init_error = str(exc)
|
| 62 |
+
else:
|
| 63 |
+
init_error = "Missing HF_TOKEN (or API_KEY)"
|
| 64 |
+
|
| 65 |
+
env = CodeGuardEnv()
|
| 66 |
+
state: Dict[str, Any] = env.reset()
|
| 67 |
+
done = False
|
| 68 |
+
|
| 69 |
+
while not done:
|
| 70 |
+
steps += 1
|
| 71 |
+
|
| 72 |
+
model_error: Optional[str] = None
|
| 73 |
+
if client is None:
|
| 74 |
+
action = ""
|
| 75 |
+
model_error = init_error
|
| 76 |
+
else:
|
| 77 |
+
try:
|
| 78 |
+
response = client.chat.completions.create(
|
| 79 |
+
model=MODEL_NAME,
|
| 80 |
+
messages=[
|
| 81 |
+
{"role": "system", "content": "You are a code-fixing agent."},
|
| 82 |
+
{"role": "user", "content": str(state)},
|
| 83 |
+
],
|
| 84 |
+
temperature=0.0,
|
| 85 |
+
)
|
| 86 |
+
action = (response.choices[0].message.content or "").strip()
|
| 87 |
+
except Exception as exc:
|
| 88 |
+
action = ""
|
| 89 |
+
model_error = str(exc)
|
| 90 |
+
|
| 91 |
+
next_state, reward, done, info = env.step(action)
|
| 92 |
+
rewards.append(float(reward))
|
| 93 |
+
|
| 94 |
+
step_error = model_error or info.get("error")
|
| 95 |
+
print(
|
| 96 |
+
f"[STEP] step={steps} action={action} reward={float(reward):.2f} "
|
| 97 |
+
f"done={_fmt_bool(done)} error={_fmt_error(step_error)}"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
state = next_state
|
| 101 |
+
score = _clamp_score(float(state.get("score", 0.0)))
|
| 102 |
+
if done and float(reward) >= env.threshold:
|
| 103 |
+
success = True
|
| 104 |
|
| 105 |
+
except Exception:
|
| 106 |
+
success = False
|
| 107 |
+
score = 0.0
|
| 108 |
+
finally:
|
| 109 |
+
rewards_str = ",".join(f"{r:.2f}" for r in rewards) if rewards else "0.00"
|
| 110 |
+
print(
|
| 111 |
+
f"[END] success={_fmt_bool(success)} steps={steps} "
|
| 112 |
+
f"score={score:.2f} rewards={rewards_str}"
|
| 113 |
+
)
|
| 114 |
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|