Spaces:
Sleeping
Sleeping
Update inference.py
Browse files112345678910115671213141516171418192031 unmodified lines525354525355565711 unmodified lines697071717273742 unmodified lines777879808081825 unmodified lines8889909291929394959699979899100101105102103104import osimport 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 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 Action
31 unmodified lines return 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 OpenAI client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY) if API_BASE_URL != "https://dummy.api" else None11 unmodified lines step = 0 final_reward = 0.0
# [START] marker sys.stdout.write(f"[START] task={task}\n") sys.stdout.flush()
2 unmodified lines prompt = 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 fails response_text = FALLBACK_ACTION if client is not None: try:5 unmodified lines ) response_text = resp.choices[0].message.content or FALLBACK_ACTION except Exception: # Any API error → fallback pass
action = parse_model_action(response_text) obs, reward, done, _ = env.step(action) final_reward = reward.value
# [STEP] marker sys.stdout.write(f"[STEP] step={step} reward={final_reward:.3f}\n") sys.stdout.flush()
history.append(f"Step {step}: {action.action_type}")
# [END] marker sys.stdout.write(f"[END] task={task} score={final_reward:.3f} steps={step}\n") sys.stdout.flush()
inference.py+8-12