CoolFace
Apppublic

Jivan01/agentBox

sourceHugging Faceapache-2.0updated 6mo agoView on Hugging Face
1likes
inference.py113 linesDownload Raw Back to root
1import os2import sys3from typing import Any, Dict, List, Optional4 5from openai import OpenAI6 7 8# Required submission variables.9# Keep HF_TOKEN/LOCAL_IMAGE_NAME defined for checklist compatibility,10# but API calls must use API_BASE_URL + API_KEY injected by validator.11API_BASE_URL = os.environ["API_BASE_URL"]12API_KEY = os.environ["API_KEY"]13MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")14HF_TOKEN = os.getenv("HF_TOKEN")15LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")16 17TASK_NAME = os.getenv("TASK", "easy")18BENCHMARK = os.getenv("BENCHMARK", "codeguard")19 20 21def _bootstrap_path() -> None:22    repo_root = os.path.dirname(os.path.abspath(__file__))23    agentbox_root = os.path.join(repo_root, "AgentBox")24    if agentbox_root not in sys.path:25        sys.path.insert(0, agentbox_root)26 27 28def _fmt_bool(value: bool) -> str:29    return "true" if value else "false"30 31 32def _fmt_error(error: Optional[str]) -> str:33    return "null" if error is None else str(error)34 35 36def _clamp_score(value: float) -> float:37    return max(0.0, min(1.0, value))38 39 40def main() -> None:41    _bootstrap_path()42    from src.env import CodeGuardEnv43 44    rewards: List[float] = []45    steps: int = 046    score: float = 0.047    success: bool = False48 49    print(f"[START] task={TASK_NAME} env={BENCHMARK} model={MODEL_NAME}")50 51    env = None52    try:53        client = None54        init_error: Optional[str] = None55        try:56            # Mandatory: all LLM calls through injected LiteLLM proxy.57            client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)58        except Exception as exc:59            init_error = str(exc)60 61        env = CodeGuardEnv()62        state: Dict[str, Any] = env.reset()63        done = False64 65        while not done:66            steps += 167 68            model_error: Optional[str] = None69            if client is None:70                action = ""71                model_error = init_error72            else:73                try:74                    response = client.chat.completions.create(75                        model=MODEL_NAME,76                        messages=[77                            {"role": "system", "content": "You are a code-fixing agent."},78                            {"role": "user", "content": str(state)},79                        ],80                        temperature=0.0,81                    )82                    action = (response.choices[0].message.content or "").strip()83                except Exception as exc:84                    action = ""85                    model_error = str(exc)86 87            next_state, reward, done, info = env.step(action)88            rewards.append(float(reward))89 90            step_error = model_error or info.get("error")91            print(92                f"[STEP] step={steps} action={action} reward={float(reward):.2f} "93                f"done={_fmt_bool(done)} error={_fmt_error(step_error)}"94            )95 96            state = next_state97            score = _clamp_score(float(state.get("score", 0.0)))98            if done and float(reward) >= env.threshold:99                success = True100 101    except Exception:102        success = False103        score = 0.0104    finally:105        rewards_str = ",".join(f"{r:.2f}" for r in rewards) if rewards else "0.00"106        print(107            f"[END] success={_fmt_bool(success)} steps={steps} "108            f"score={score:.2f} rewards={rewards_str}"109        )110 111 112if __name__ == "__main__":113    main()