Spaces:
Sleeping
Sleeping
Update inference.py
Browse files123456789101112131415161731 unmodified lines495051525354555611 unmodified lines686970717273742 unmodified lines777879808182835 unmodified lines8990919293949596979899100101102103104105106107108import osimport sysimport textwrap# ----------------------------------------------------------------------# Always define fallback values – do NOT exit early# ----------------------------------------------------------------------API_BASE_URL = os.getenv("API_BASE_URL", "https://dummy.api")API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY", "dummy-key")MODEL_NAME = os.getenv("MODEL_NAME", "dummy-model")MAX_STEPS = 5FALLBACK_ACTION = "skip"# We'll import the environment only after setting dummy env varsfrom environment import CodeReviewEnvfrom models import Action31 unmodified linesreturn Action(action_type="write_comment", comment_text=text)def main():# Even if API credentials are missing, we still run the loop with fallback actions.# We'll try to create an OpenAI client only if the base URL seems valid.try:from openai import OpenAIclient = OpenAI(base_url=API_BASE_URL, api_key=API_KEY) if API_BASE_URL != "https://dummy.api" else None11 unmodified linesstep = 0final_reward = 0.0# [START] markersys.stdout.write(f"[START] task={task}\n")sys.stdout.flush()2 unmodified linesprompt = build_user_prompt(step, obs, history)messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": prompt}]# Try to get a model response, but fallback immediately if client is None or failsresponse_text = FALLBACK_ACTIONif client is not None:try:5 unmodified lines)response_text = resp.choices[0].message.content or FALLBACK_ACTIONexcept Exception:# Any API error → fallbackpassaction = parse_model_action(response_text)obs, reward, done, _ = env.step(action)final_reward = reward.value# [STEP] markersys.stdout.write(f"[STEP] step={step} reward={final_reward:.3f}\n")sys.stdout.flush()history.append(f"Step {step}: {action.action_type}")# [END] markersys.stdout.write(f"[END] task={task} score={final_reward:.3f} steps={step}\n")sys.stdout.flush()123456789101112131415161718192031 unmodified lines52535455565711 unmodified lines6970717273742 unmodified lines7778798081825 unmodified lines888990919293949596979899100101102103104import sys# Redirect all print calls from imported modules to stderr_original_print = printdef print(*args, **kwargs):kwargs.setdefault('file', sys.stderr)_original_print(*args, **kwargs)import osimport textwrapAPI_BASE_URL = os.getenv("API_BASE_URL", "https://dummy.api")API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY", "dummy-key")MODEL_NAME = os.getenv("MODEL_NAME", "dummy-model")MAX_STEPS = 5FALLBACK_ACTION = "skip"from environment import CodeReviewEnvfrom models import Action31 unmodified linesreturn Action(action_type="write_comment", comment_text=text)def main():try:from openai import OpenAIclient = OpenAI(base_url=API_BASE_URL, api_key=API_KEY) if API_BASE_URL != "https://dummy.api" else None11 unmodified linesstep = 0final_reward = 0.0sys.stdout.write(f"[START] task={task}\n")sys.stdout.flush()2 unmodified linesprompt = build_user_prompt(step, obs, history)messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": prompt}]response_text = FALLBACK_ACTIONif client is not None:try:5 unmodified lines)response_text = resp.choices[0].message.content or FALLBACK_ACTIONexcept Exception:passaction = parse_model_action(response_text)obs, reward, done, _ = env.step(action)final_reward = reward.valuesys.stdout.write(f"[STEP] step={step} reward={final_reward:.3f}\n")sys.stdout.flush()history.append(f"Step {step}: {action.action_type}")sys.stdout.write(f"[END] task={task} score={final_reward:.3f} steps={step}\n")sys.stdout.flush()
inference.py+8-12