Spaces:
Sleeping
Sleeping
Abhishek Tiwari commited on
Commit ·
755655b
1
Parent(s): bc16a95
Fix inference.py output format to strictly match Step/End validation criteria per task
Browse files- inference.py +45 -69
inference.py
CHANGED
|
@@ -73,8 +73,7 @@ SYSTEM_PROMPT = textwrap.dedent(
|
|
| 73 |
|
| 74 |
|
| 75 |
def log_start(task: str, env: str, model: str) -> None:
|
| 76 |
-
print(f"[START] task={task} env={env} model={model}"
|
| 77 |
-
|
| 78 |
|
| 79 |
def log_step(
|
| 80 |
step: int,
|
|
@@ -83,23 +82,14 @@ def log_step(
|
|
| 83 |
done: bool,
|
| 84 |
error: Optional[str],
|
| 85 |
) -> None:
|
| 86 |
-
|
| 87 |
-
err_one = sanitize_one_line(err) if err != "null" else "null"
|
| 88 |
act_one = sanitize_one_line(action)
|
| 89 |
-
done_val =
|
| 90 |
-
print(
|
| 91 |
-
f"[STEP] step={step} action={act_one} reward={reward:.2f} done={done_val} error={err_one}",
|
| 92 |
-
flush=True,
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
|
| 96 |
-
def log_end(success: bool, steps: int, score: float
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
print(
|
| 100 |
-
f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}",
|
| 101 |
-
flush=True,
|
| 102 |
-
)
|
| 103 |
|
| 104 |
|
| 105 |
def sanitize_one_line(s: str) -> str:
|
|
@@ -182,37 +172,24 @@ def _make_direct_client():
|
|
| 182 |
|
| 183 |
def main() -> None:
|
| 184 |
if not API_KEY:
|
| 185 |
-
|
| 186 |
-
"[DEBUG] Set HF_TOKEN (or OPENAI_API_KEY) before running.",
|
| 187 |
-
file=sys.stderr,
|
| 188 |
-
flush=True,
|
| 189 |
-
)
|
| 190 |
-
raise RuntimeError(
|
| 191 |
-
"Missing API key: set HF_TOKEN or OPENAI_API_KEY for OpenAI-compatible client."
|
| 192 |
-
)
|
| 193 |
|
| 194 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 195 |
|
| 196 |
if ENV_CONTAINER_START:
|
| 197 |
-
print(f"[DEBUG] Using docker image: {ENV_IMAGE_NAME}", file=sys.stderr, flush=True)
|
| 198 |
env = SqlEnvClient.from_docker_image(ENV_IMAGE_NAME).sync()
|
| 199 |
else:
|
| 200 |
env = _make_direct_client()
|
| 201 |
|
| 202 |
history: List[str] = []
|
| 203 |
-
|
| 204 |
-
best_task_score: Dict[str, float] = {tid: 0.0 for tid in TASK_IDS}
|
| 205 |
-
|
| 206 |
-
steps_taken = 0
|
| 207 |
-
score = 0.0
|
| 208 |
-
success = False
|
| 209 |
-
last_done = False
|
| 210 |
-
|
| 211 |
-
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 212 |
-
|
| 213 |
try:
|
| 214 |
result = env.reset()
|
| 215 |
observation = result.observation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
for step in range(1, MAX_STEPS + 1):
|
| 218 |
if result.done:
|
|
@@ -236,48 +213,47 @@ def main() -> None:
|
|
| 236 |
try:
|
| 237 |
completion = client.chat.completions.create(**create_kwargs)
|
| 238 |
response_text = completion.choices[0].message.content or "SELECT 1"
|
| 239 |
-
except Exception
|
| 240 |
-
print(f"[DEBUG] Model request failed: {exc}", file=sys.stderr, flush=True)
|
| 241 |
response_text = "SELECT 1"
|
| 242 |
|
| 243 |
action_str = parse_model_action(response_text)
|
| 244 |
result = env.step(SqlAction(query=action_str))
|
| 245 |
observation = result.observation
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
err_raw = observation.execution_error
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
except Exception
|
| 273 |
-
|
| 274 |
-
success = False
|
| 275 |
finally:
|
| 276 |
try:
|
| 277 |
env.close()
|
| 278 |
-
except Exception
|
| 279 |
-
|
| 280 |
-
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 281 |
|
| 282 |
|
| 283 |
if __name__ == "__main__":
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def log_start(task: str, env: str, model: str) -> None:
|
| 76 |
+
print(f"[START] task={task} env={env} model={model}")
|
|
|
|
| 77 |
|
| 78 |
def log_step(
|
| 79 |
step: int,
|
|
|
|
| 82 |
done: bool,
|
| 83 |
error: Optional[str],
|
| 84 |
) -> None:
|
| 85 |
+
err_one = sanitize_one_line(error) if error else "null"
|
|
|
|
| 86 |
act_one = sanitize_one_line(action)
|
| 87 |
+
done_val = "true" if done else "false"
|
| 88 |
+
print(f"[STEP] step={step} action={act_one} reward={reward:.2f} done={done_val} error={err_one}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
def log_end(success: bool, steps: int, score: float) -> None:
|
| 91 |
+
succ_val = "true" if success else "false"
|
| 92 |
+
print(f"[END] success={succ_val} steps={steps} rewards={score:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
def sanitize_one_line(s: str) -> str:
|
|
|
|
| 172 |
|
| 173 |
def main() -> None:
|
| 174 |
if not API_KEY:
|
| 175 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 178 |
|
| 179 |
if ENV_CONTAINER_START:
|
|
|
|
| 180 |
env = SqlEnvClient.from_docker_image(ENV_IMAGE_NAME).sync()
|
| 181 |
else:
|
| 182 |
env = _make_direct_client()
|
| 183 |
|
| 184 |
history: List[str] = []
|
| 185 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
try:
|
| 187 |
result = env.reset()
|
| 188 |
observation = result.observation
|
| 189 |
+
|
| 190 |
+
current_task_id = observation.task_id
|
| 191 |
+
task_step = 1
|
| 192 |
+
log_start(task=current_task_id, env=BENCHMARK, model=MODEL_NAME)
|
| 193 |
|
| 194 |
for step in range(1, MAX_STEPS + 1):
|
| 195 |
if result.done:
|
|
|
|
| 213 |
try:
|
| 214 |
completion = client.chat.completions.create(**create_kwargs)
|
| 215 |
response_text = completion.choices[0].message.content or "SELECT 1"
|
| 216 |
+
except Exception:
|
|
|
|
| 217 |
response_text = "SELECT 1"
|
| 218 |
|
| 219 |
action_str = parse_model_action(response_text)
|
| 220 |
result = env.step(SqlAction(query=action_str))
|
| 221 |
observation = result.observation
|
| 222 |
+
|
| 223 |
+
# reward is obtained from observation task_score or directly from step
|
| 224 |
+
task_score = float(observation.task_score if hasattr(observation, 'task_score') and observation.task_score is not None else 0.0)
|
| 225 |
+
if hasattr(observation, 'reward') and observation.reward is not None:
|
| 226 |
+
task_score = float(observation.reward)
|
| 227 |
+
|
| 228 |
err_raw = observation.execution_error
|
| 229 |
+
next_task_id = getattr(observation, "task_id", None)
|
| 230 |
+
|
| 231 |
+
# task transitions or episode done signals end of current task
|
| 232 |
+
task_done = (next_task_id != current_task_id) or result.done
|
| 233 |
+
|
| 234 |
+
log_step(step=task_step, action=action_str, reward=task_score, done=task_done, error=err_raw)
|
| 235 |
+
|
| 236 |
+
history.append(f"Q: {action_str[:200]} -> R: {task_score:+.2f}")
|
| 237 |
+
|
| 238 |
+
if task_done:
|
| 239 |
+
success = (task_score >= 0.95)
|
| 240 |
+
log_end(success=success, steps=task_step, score=task_score)
|
| 241 |
+
if result.done or not next_task_id:
|
| 242 |
+
break
|
| 243 |
+
current_task_id = next_task_id
|
| 244 |
+
task_step = 1
|
| 245 |
+
log_start(task=current_task_id, env=BENCHMARK, model=MODEL_NAME)
|
| 246 |
+
history.clear()
|
| 247 |
+
else:
|
| 248 |
+
task_step += 1
|
| 249 |
+
|
| 250 |
+
except Exception:
|
| 251 |
+
pass
|
|
|
|
| 252 |
finally:
|
| 253 |
try:
|
| 254 |
env.close()
|
| 255 |
+
except Exception:
|
| 256 |
+
pass
|
|
|
|
| 257 |
|
| 258 |
|
| 259 |
if __name__ == "__main__":
|