Spaces:
Sleeping
Sleeping
changes
Browse files- inference.py +26 -5
inference.py
CHANGED
|
@@ -87,6 +87,13 @@ def log_end(success: bool, steps: int, score: float, rewards: list[float]) -> No
|
|
| 87 |
)
|
| 88 |
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def estimate_max_flow_score(timeline: list[int]) -> float:
|
| 91 |
slot_count = len(timeline)
|
| 92 |
if slot_count <= 0:
|
|
@@ -180,12 +187,15 @@ def coerce_action(raw_text: str, observation: dict[str, Any]) -> dict[str, int]:
|
|
| 180 |
|
| 181 |
|
| 182 |
def get_model_action(
|
| 183 |
-
client: OpenAI,
|
| 184 |
step: int,
|
| 185 |
observation: dict[str, Any],
|
| 186 |
rewards: list[float],
|
| 187 |
history: list[str],
|
| 188 |
) -> dict[str, int]:
|
|
|
|
|
|
|
|
|
|
| 189 |
user_prompt = build_user_prompt(step, observation, rewards, history)
|
| 190 |
try:
|
| 191 |
completion = client.chat.completions.create(
|
|
@@ -214,18 +224,23 @@ async def create_env() -> GenericEnvClient:
|
|
| 214 |
|
| 215 |
|
| 216 |
async def main() -> None:
|
| 217 |
-
|
| 218 |
-
client = OpenAI(base_url=API_BASE_URL, api_key=api_key)
|
| 219 |
env = None
|
| 220 |
rewards: list[float] = []
|
| 221 |
history: list[str] = []
|
| 222 |
steps_taken = 0
|
| 223 |
success = False
|
| 224 |
score = 0.0
|
|
|
|
| 225 |
|
| 226 |
log_start(TASK_NAME, BENCHMARK, MODEL_NAME)
|
| 227 |
|
| 228 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
env = await create_env()
|
| 230 |
result = await env.reset()
|
| 231 |
observation = dict(result.observation)
|
|
@@ -261,9 +276,11 @@ async def main() -> None:
|
|
| 261 |
break
|
| 262 |
|
| 263 |
total_reward = math.fsum(rewards)
|
| 264 |
-
score = normalize_score(total_reward, observation
|
| 265 |
score = round(score, 2)
|
| 266 |
success = score > 0.0
|
|
|
|
|
|
|
| 267 |
finally:
|
| 268 |
if env is not None:
|
| 269 |
try:
|
|
@@ -274,4 +291,8 @@ async def main() -> None:
|
|
| 274 |
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
|
| 90 |
+
def log_error(stage: str, error: Exception) -> None:
|
| 91 |
+
print(
|
| 92 |
+
f"[ERROR] stage={_sanitize_field(stage)} error={_sanitize_field(error)}",
|
| 93 |
+
flush=True,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
def estimate_max_flow_score(timeline: list[int]) -> float:
|
| 98 |
slot_count = len(timeline)
|
| 99 |
if slot_count <= 0:
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
def get_model_action(
|
| 190 |
+
client: OpenAI | None,
|
| 191 |
step: int,
|
| 192 |
observation: dict[str, Any],
|
| 193 |
rewards: list[float],
|
| 194 |
history: list[str],
|
| 195 |
) -> dict[str, int]:
|
| 196 |
+
if client is None:
|
| 197 |
+
return choose_fallback_action(observation)
|
| 198 |
+
|
| 199 |
user_prompt = build_user_prompt(step, observation, rewards, history)
|
| 200 |
try:
|
| 201 |
completion = client.chat.completions.create(
|
|
|
|
| 224 |
|
| 225 |
|
| 226 |
async def main() -> None:
|
| 227 |
+
client: OpenAI | None = None
|
|
|
|
| 228 |
env = None
|
| 229 |
rewards: list[float] = []
|
| 230 |
history: list[str] = []
|
| 231 |
steps_taken = 0
|
| 232 |
success = False
|
| 233 |
score = 0.0
|
| 234 |
+
observation: dict[str, Any] = {}
|
| 235 |
|
| 236 |
log_start(TASK_NAME, BENCHMARK, MODEL_NAME)
|
| 237 |
|
| 238 |
try:
|
| 239 |
+
if HF_TOKEN:
|
| 240 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 241 |
+
else:
|
| 242 |
+
log_error("startup", RuntimeError("Missing HF_TOKEN; using fallback policy"))
|
| 243 |
+
|
| 244 |
env = await create_env()
|
| 245 |
result = await env.reset()
|
| 246 |
observation = dict(result.observation)
|
|
|
|
| 276 |
break
|
| 277 |
|
| 278 |
total_reward = math.fsum(rewards)
|
| 279 |
+
score = normalize_score(total_reward, observation)
|
| 280 |
score = round(score, 2)
|
| 281 |
success = score > 0.0
|
| 282 |
+
except Exception as error:
|
| 283 |
+
log_error("runtime", error)
|
| 284 |
finally:
|
| 285 |
if env is not None:
|
| 286 |
try:
|
|
|
|
| 291 |
|
| 292 |
|
| 293 |
if __name__ == "__main__":
|
| 294 |
+
try:
|
| 295 |
+
asyncio.run(main())
|
| 296 |
+
except Exception as error:
|
| 297 |
+
log_error("fatal", error)
|
| 298 |
+
log_end(success=False, steps=0, score=0.0, rewards=[])
|