Spaces:
Running
Running
refactor: update default model and API endpoint, enhance error handling, and add close method for compatibility
Browse files44 unmodified lines45464748495051521 unmodified line54555657585960616295 unmodified lines1581591601611621634 unmodified lines16816917017117217317417517617745 unmodified lines22322422522622722822923023179 unmodified lines3113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533544 unmodified lines3593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923931 unmodified line39539639739839940040140240340440540640740840941041141241341441541642 unmodified lines4594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984996 unmodified lines5065075085095105115125135145155165175185195205215225235245255265275285295305315325335345359 unmodified lines54554654754854955055144 unmodified lines# ── Constants ──────────────────────────────────────────────────────────────ENV_URL = os.getenv("ENV_URL", "http://localhost:7860")MODEL_NAME = os.getenv("MODEL_NAME", "<your-active-model>")API_BASE_URL = os.getenv("API_BASE_URL", "<your-active-endpoint>")# ── Environment Variable Handling ─────────────────────────────────────────# The LLM API credential is read from HF_TOKEN or OPENAI_API_KEY environment variables1 unmodified line# Primary: HF_TOKEN# Fallback: OPENAI_API_KEY (for local testing/development)HF_TOKEN = os.getenv("HF_TOKEN")OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or HF_TOKENif not OPENAI_API_KEY:print("[WARN] No HF_TOKEN or OPENAI_API_KEY set - will use heuristic mode if --fast-mode is set")DEFAULT_EPISODES = 1DEFAULT_SEED_BASE = 1000MAX_RETRIES = 395 unmodified linesprint(f"[ERROR] Failed to get state: {e}", file=sys.stderr)return None# ── LLM agent ───────────────────────────────────────────────────────────────4 unmodified linesdef __init__(self):# Initialize OpenAI client with credentials from HF_TOKEN (per hackathon spec)# The OPENAI_API_KEY variable contains the HF_TOKEN value passed by evaluatorsself.client = OpenAI(base_url=API_BASE_URL,api_key=OPENAI_API_KEY or "dummy-key-for-fast-mode",)self.model = MODEL_NAMEself.fallback_mode = False45 unmodified linesreturn self._clamp_action(action)except Exception as e:err_str = str(e)print(f" [LLM attempt {attempt+1}/{MAX_RETRIES}] error: {err_str}")if "402" in err_str or "depleted" in err_str:print(" [WARN] Hugging Face free credits depleted! Switching to local heuristic agent for the rest of the simulation.")self.fallback_mode = Truereturn self._heuristic_action(obs)time.sleep(1)79 unmodified lines...[END] success=<true|false> steps=<n> rewards=<r1,r2,...>"""reset_resp = env_client.reset(task_id=task_id, seed=seed)if reset_resp is None:print(f"[END] success=false steps=0 rewards=", flush=True)return {"task_id": task_id,"seed": seed,"total_reward": 0.0,"total_steps": 0,"elapsed_sec": 0.0,"score": 0.0,"sub_scores": {},"exploit_detected": False,}obs_list = reset_resp.get("observations", [{}])obs = obs_list[0] if obs_list else {}task_name = f"gridmind-task-{task_id}"# Emit [START] with required fieldsprint(f"[START] task={task_name} env=gridmind model={MODEL_NAME}", flush=True)total_reward = 0.0total_steps = 0start_time = time.time()step_resp: dict[str, Any] = {}step_limit = EPISODE_STEPS if max_steps is None else min(max_steps, EPISODE_STEPS)llm_reuse_remaining = 0cached_action = agent._default_action()step_rewards: list[float] = []last_error: str | None = Nonewhile not step_resp.get("done", False):if total_steps >= step_limit:breaktry:if fast_mode:action = agent._heuristic_action(obs)else:4 unmodified linesstep_resp = env_client.step(action)if step_resp is None or not isinstance(step_resp, dict) or "observation" not in step_resp:last_error = "invalid step response from environment"print(f"[STEP] step={total_steps + 1} action=null "f"reward=0.00 done=true error=\"{last_error}\"",flush=True)breakif not fast_mode:llm_reuse_remaining -= 1obs = step_resp["observation"]reward = float(step_resp["reward"])total_reward += rewardstep_rewards.append(reward)total_steps += 1done = bool(step_resp.get("done", False))# Emit [STEP] with required fields (action as compact JSON, reward to 2 decimals)action_json = json.dumps(action, separators=(',', ':'))error_field = "null" if last_error is None else f'"{last_error}"'print(f"[STEP] step={total_steps} action={action_json} "f"reward={reward:.2f} done={'true' if done else 'false'} error={error_field}",flush=True)last_error = None # Clear error after successful stepif verbose and total_steps % 16 == 0:print(f" step={total_steps:02d} price=${obs['current_price']:.3f} "1 unmodified linef"stress={obs['grid_stress_signal']:.2f} "f"cost=${obs['cumulative_cost']:.2f}",flush=True,)except Exception as e:last_error = str(e)print(f"[STEP] step={total_steps + 1} action=null "f"reward=0.00 done=true error=\"{last_error}\"",flush=True)breakelapsed = time.time() - start_timegrade = env_client.grade()# Emit [END] with required fieldssuccess = last_error is None and step_resp.get("done", False)rewards_str = ",".join(f"{r:.2f}" for r in step_rewards)print(f"[END] success={'true' if success else 'false'} steps={total_steps} rewards={rewards_str}",42 unmodified lines)args = parser.parse_args()# Validate API key AFTER argparse (allows --fast-mode to bypass)if not OPENAI_API_KEY and not args.fast_mode:print("[WARN] No API key set, switching to fast-mode (heuristic)", file=sys.stderr)args.fast_mode = Trueprint("=" * 60)print("GridMind-RL Baseline Inference")print(f" Model: {MODEL_NAME}")print(f" API: {API_BASE_URL}")print(f" Env: {args.env_url}")print(f" Episodes per task: {args.episodes}")print(f" Fast mode: {args.fast_mode} | LLM every: {args.llm_every} steps")print("=" * 60)env_client = GridMindEnvClient(base_url=args.env_url)print("\nWaiting for environment server...")for attempt in range(30):if env_client.health():print(" [OK] Environment server is healthy")breaktime.sleep(2)if attempt == 29:print(" [FAIL] Environment server not reachable. Exiting.")sys.exit(1)agent = LLMAgent()all_results: list[dict[str, Any]] = []for task_id in [1, 2, 3]:print(f"\n-- Task {task_id}: {TASK_DESCRIPTIONS[task_id][:60]}...")task_scores: list[float] = []for ep in range(args.episodes):seed = DEFAULT_SEED_BASE + task_id * 100 + epprint(f" Episode {ep+1}/{args.episodes} (seed={seed})")result = run_episode(env_client,agent,6 unmodified lines)task_scores.append(float(result["score"]))all_results.append(result)print(f" → score={result['score']:.4f} | reward={result['total_reward']:.3f} | "f"{result['elapsed_sec']:.1f}s | steps={result['total_steps']}")avg_score = sum(task_scores) / len(task_scores)print(f" Task {task_id} average score: {avg_score:.4f}")print("\n" + "=" * 60)print("BASELINE SCORES SUMMARY")print("=" * 60)print(f"{'Task':<10} {'Model':<30} {'Score':<10} {'Episodes':<10}")print("-" * 60)task_avgs: dict[int, float] = {}for task_id in [1, 2, 3]:scores = [float(r["score"]) for r in all_results if r["task_id"] == task_id]avg = sum(scores) / len(scores) if scores else 0.0task_avgs[task_id] = avgprint(f"Task {task_id:<6} {MODEL_NAME:<30} {avg:<10.4f} {len(scores)}")print("-" * 60)overall = sum(task_avgs.values()) / len(task_avgs)print(f"{'Overall':<10} {'':<30} {overall:<10.4f}")output = {"model": MODEL_NAME,9 unmodified lines}with open(args.output, "w", encoding="utf-8") as f:json.dump(output, f, indent=2)print(f"\n[OK] Results saved to {args.output}")if __name__ == "__main__":44 unmodified lines45464748495051521 unmodified line5455565758596095 unmodified lines1561571581591601611621631641654 unmodified lines17017117217317417517645 unmodified lines22222322422522622722822923023123223379 unmodified lines3133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443454 unmodified lines3503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803811 unmodified line38338438538638738838939039139239339439539639739839940040140240340440540640740840941041141242 unmodified lines4554564574584594604614624634644654664674684694704714724734744754764774784794804816 unmodified lines4884894904914924934944954964974984995005019 unmodified lines51151251351451551644 unmodified lines# ── Constants ──────────────────────────────────────────────────────────────ENV_URL = os.getenv("ENV_URL", "http://localhost:7860")MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1")# ── Environment Variable Handling ─────────────────────────────────────────# The LLM API credential is read from HF_TOKEN or OPENAI_API_KEY environment variables1 unmodified line# Primary: HF_TOKEN# Fallback: OPENAI_API_KEY (for local testing/development)HF_TOKEN = os.getenv("HF_TOKEN")OPENAI_API_KEY = HF_TOKENDEFAULT_EPISODES = 1DEFAULT_SEED_BASE = 1000MAX_RETRIES = 395 unmodified linesprint(f"[ERROR] Failed to get state: {e}", file=sys.stderr)return Nonedef close(self) -> None:"""Compatibility close hook for episode-finalization contract."""return None# ── LLM agent ───────────────────────────────────────────────────────────────4 unmodified linesdef __init__(self):# Initialize OpenAI client with credentials from HF_TOKEN (per hackathon spec)# The OPENAI_API_KEY variable contains the HF_TOKEN value passed by evaluatorsself.client = OpenAI(base_url=API_BASE_URL, api_key=OPENAI_API_KEY)self.model = MODEL_NAMEself.fallback_mode = False45 unmodified linesreturn self._clamp_action(action)except Exception as e:err_str = str(e)print(f" [LLM attempt {attempt+1}/{MAX_RETRIES}] error: {err_str}", file=sys.stderr)if "402" in err_str or "depleted" in err_str:print(" [WARN] Hugging Face free credits depleted! Switching to local heuristic agent for the rest of the simulation.",file=sys.stderr,)self.fallback_mode = Truereturn self._heuristic_action(obs)time.sleep(1)79 unmodified lines...[END] success=<true|false> steps=<n> rewards=<r1,r2,...>"""task_name = f"gridmind-task-{task_id}"print(f"[START] task={task_name} env=gridmind model={MODEL_NAME}", flush=True)total_reward = 0.0total_steps = 0start_time = time.time()step_resp: dict[str, Any] = {"done": False}step_limit = EPISODE_STEPS if max_steps is None else min(max_steps, EPISODE_STEPS)llm_reuse_remaining = 0cached_action = agent._default_action()step_rewards: list[float] = []success = Falseobs: dict[str, Any] = {}try:reset_resp = env_client.reset(task_id=task_id, seed=seed)if reset_resp is None:raise RuntimeError("reset failed")obs_list = reset_resp.get("observations", [{}])obs = obs_list[0] if obs_list else {}while not step_resp.get("done", False):if total_steps >= step_limit:breakif fast_mode:action = agent._heuristic_action(obs)else:4 unmodified linesstep_resp = env_client.step(action)if step_resp is None or not isinstance(step_resp, dict) or "observation" not in step_resp:print(f"[STEP] step={total_steps + 1} action=null "f"reward=0.00 done=true error=invalid step response from environment",flush=True)breakif not fast_mode:llm_reuse_remaining -= 1obs = step_resp["observation"]reward = float(step_resp["reward"])total_reward += rewardstep_rewards.append(reward)total_steps += 1done = bool(step_resp.get("done", False))action_json = json.dumps(action, separators=(',', ':'))last_action_error = step_resp.get("last_action_error")error_field = "null" if last_action_error is None else str(last_action_error)print(f"[STEP] step={total_steps} action={action_json} "f"reward={reward:.2f} done={'true' if done else 'false'} error={error_field}",flush=True)if verbose and total_steps % 16 == 0:print(f" step={total_steps:02d} price=${obs['current_price']:.3f} "1 unmodified linef"stress={obs['grid_stress_signal']:.2f} "f"cost=${obs['cumulative_cost']:.2f}",flush=True,file=sys.stderr,)success = bool(step_resp.get("done", False))except Exception as e:err = str(e)if not err:err = "unknown error"if "\n" in err:err = err.replace("\n", " ")if "\r" in err:err = err.replace("\r", " ")if total_steps < step_limit:print(f"[STEP] step={total_steps + 1} action=null "f"reward=0.00 done=true error={err}",flush=True)finally:env_client.close()elapsed = time.time() - start_timegrade = env_client.grade()rewards_str = ",".join(f"{r:.2f}" for r in step_rewards)print(f"[END] success={'true' if success else 'false'} steps={total_steps} rewards={rewards_str}",42 unmodified lines)args = parser.parse_args()if not HF_TOKEN:print("HF_TOKEN is required.", file=sys.stderr)sys.exit(1)env_client = GridMindEnvClient(base_url=args.env_url)for attempt in range(30):if env_client.health():breaktime.sleep(2)if attempt == 29:print("Environment server not reachable.", file=sys.stderr)sys.exit(1)agent = LLMAgent()all_results: list[dict[str, Any]] = []for task_id in [1, 2, 3]:task_scores: list[float] = []for ep in range(args.episodes):seed = DEFAULT_SEED_BASE + task_id * 100 + epresult = run_episode(env_client,agent,6 unmodified lines)task_scores.append(float(result["score"]))all_results.append(result)_ = sum(task_scores) / len(task_scores)task_avgs: dict[int, float] = {}for task_id in [1, 2, 3]:scores = [float(r["score"]) for r in all_results if r["task_id"] == task_id]avg = sum(scores) / len(scores) if scores else 0.0task_avgs[task_id] = avgoverall = sum(task_avgs.values()) / len(task_avgs)output = {"model": MODEL_NAME,9 unmodified lines}with open(args.output, "w", encoding="utf-8") as f:json.dump(output, f, indent=2)if __name__ == "__main__":
inference.py+60-95