Spaces:
Sleeping
Sleeping
Commit Β·
1fe26af
1
Parent(s): e92bfc1
fix: make inference.py crash-proof when OPENAI_API_KEY is missing (Phase 2)
Browse files- inference.py +90 -47
inference.py
CHANGED
|
@@ -1,31 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
-
|
| 4 |
from server.env import CodeArenaEnv
|
| 5 |
from server.models import CodeArenaAction
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def run_inference():
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
Your goal is to fix the buggy code provided to you.
|
| 19 |
Ensure your code is highly efficient and fully resolves all logical, syntax, and algorithmic bugs.
|
| 20 |
Only return the fixed raw Python code. Do not output markdown blocks (like ```python). Do not explain your changes."""
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
Buggy Code:
|
| 30 |
{obs.buggy_code}
|
| 31 |
|
|
@@ -35,34 +59,53 @@ Error Log:
|
|
| 35 |
Test Results:
|
| 36 |
{obs.test_results}
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
run_inference()
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
|
| 4 |
from server.env import CodeArenaEnv
|
| 5 |
from server.models import CodeArenaAction
|
| 6 |
|
| 7 |
+
# ββ Fallback response (always valid JSON shape) βββββββββββββββββββββββββββ
|
| 8 |
+
_FALLBACK = {
|
| 9 |
+
"action": "analyze_code",
|
| 10 |
+
"explanation": "Fallback mode: running without external API.",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
def run_inference():
|
| 15 |
+
"""Run the RL inference loop. Never raises β returns valid JSON always."""
|
| 16 |
+
try:
|
| 17 |
+
print("[START] Initializing CodeArena inference logging")
|
| 18 |
+
|
| 19 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
+
|
| 21 |
+
# Only import & initialise OpenAI when a key is available
|
| 22 |
+
if api_key:
|
| 23 |
+
try:
|
| 24 |
+
from openai import OpenAI
|
| 25 |
+
client = OpenAI(api_key=api_key)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"[WARN] Could not initialise OpenAI client: {e}")
|
| 28 |
+
client = None
|
| 29 |
+
else:
|
| 30 |
+
print("[INFO] OPENAI_API_KEY not set β running in fallback mode")
|
| 31 |
+
client = None
|
| 32 |
+
|
| 33 |
+
env = CodeArenaEnv()
|
| 34 |
+
obs = env.reset()
|
| 35 |
+
|
| 36 |
+
# If no usable client, return the fallback immediately
|
| 37 |
+
if client is None:
|
| 38 |
+
print("[END] No API client available. Returning fallback response.")
|
| 39 |
+
return _FALLBACK
|
| 40 |
+
|
| 41 |
+
system_prompt = """You are an expert autonomous code repair agent.
|
| 42 |
Your goal is to fix the buggy code provided to you.
|
| 43 |
Ensure your code is highly efficient and fully resolves all logical, syntax, and algorithmic bugs.
|
| 44 |
Only return the fixed raw Python code. Do not output markdown blocks (like ```python). Do not explain your changes."""
|
| 45 |
|
| 46 |
+
done = False
|
| 47 |
+
step = 0
|
| 48 |
+
|
| 49 |
+
while not done and step < env.max_steps:
|
| 50 |
+
print(f"[STEP] Beginning Step {step + 1}")
|
| 51 |
+
|
| 52 |
+
user_prompt = f"""
|
| 53 |
Buggy Code:
|
| 54 |
{obs.buggy_code}
|
| 55 |
|
|
|
|
| 59 |
Test Results:
|
| 60 |
{obs.test_results}
|
| 61 |
"""
|
| 62 |
+
try:
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model="gpt-4o", # Replace with desired model
|
| 65 |
+
messages=[
|
| 66 |
+
{"role": "system", "content": system_prompt},
|
| 67 |
+
{"role": "user", "content": user_prompt},
|
| 68 |
+
],
|
| 69 |
+
temperature=0.2,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
proposed_fix = response.choices[0].message.content.strip()
|
| 73 |
+
# Failsafe cleanup
|
| 74 |
+
if proposed_fix.startswith("```python"):
|
| 75 |
+
proposed_fix = proposed_fix[9:]
|
| 76 |
+
if proposed_fix.startswith("```"):
|
| 77 |
+
proposed_fix = proposed_fix[3:]
|
| 78 |
+
if proposed_fix.endswith("```"):
|
| 79 |
+
proposed_fix = proposed_fix[:-3]
|
| 80 |
+
|
| 81 |
+
action = CodeArenaAction(proposed_fix=proposed_fix.strip())
|
| 82 |
+
|
| 83 |
+
obs, reward, done, info = env.step(action)
|
| 84 |
+
print(
|
| 85 |
+
f"[STEP] Action taken. Reward received: {reward:.3f}. "
|
| 86 |
+
f"Task ID: {info['task_id']}"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"[STEP] Warning: Exception occurred: {str(e)}")
|
| 91 |
+
break
|
| 92 |
+
|
| 93 |
+
step += 1
|
| 94 |
+
|
| 95 |
+
print(f"[END] Inference Complete. Executed {step} step(s).")
|
| 96 |
+
return {
|
| 97 |
+
"action": "analyze_code",
|
| 98 |
+
"explanation": f"Inference completed after {step} step(s).",
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"[ERROR] Top-level fallback triggered: {e}")
|
| 103 |
+
return {
|
| 104 |
+
"action": "analyze_code",
|
| 105 |
+
"explanation": f"Fallback due to error: {str(e)}",
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
result = run_inference()
|
| 111 |
+
print(result)
|