Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +8 -12
inference.py
CHANGED
|
@@ -1,17 +1,20 @@
|
|
| 1 |
-
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import textwrap
|
| 4 |
|
| 5 |
-
# ----------------------------------------------------------------------
|
| 6 |
-
# Always define fallback values – do NOT exit early
|
| 7 |
-
# ----------------------------------------------------------------------
|
| 8 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://dummy.api")
|
| 9 |
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY", "dummy-key")
|
| 10 |
MODEL_NAME = os.getenv("MODEL_NAME", "dummy-model")
|
| 11 |
MAX_STEPS = 5
|
| 12 |
FALLBACK_ACTION = "skip"
|
| 13 |
|
| 14 |
-
# We'll import the environment only after setting dummy env vars
|
| 15 |
from environment import CodeReviewEnv
|
| 16 |
from models import Action
|
| 17 |
|
|
@@ -49,8 +52,6 @@ def parse_model_action(text):
|
|
| 49 |
return Action(action_type="write_comment", comment_text=text)
|
| 50 |
|
| 51 |
def main():
|
| 52 |
-
# Even if API credentials are missing, we still run the loop with fallback actions.
|
| 53 |
-
# We'll try to create an OpenAI client only if the base URL seems valid.
|
| 54 |
try:
|
| 55 |
from openai import OpenAI
|
| 56 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY) if API_BASE_URL != "https://dummy.api" else None
|
|
@@ -68,7 +69,6 @@ def main():
|
|
| 68 |
step = 0
|
| 69 |
final_reward = 0.0
|
| 70 |
|
| 71 |
-
# [START] marker
|
| 72 |
sys.stdout.write(f"[START] task={task}\n")
|
| 73 |
sys.stdout.flush()
|
| 74 |
|
|
@@ -77,7 +77,6 @@ def main():
|
|
| 77 |
prompt = build_user_prompt(step, obs, history)
|
| 78 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": prompt}]
|
| 79 |
|
| 80 |
-
# Try to get a model response, but fallback immediately if client is None or fails
|
| 81 |
response_text = FALLBACK_ACTION
|
| 82 |
if client is not None:
|
| 83 |
try:
|
|
@@ -89,20 +88,17 @@ def main():
|
|
| 89 |
)
|
| 90 |
response_text = resp.choices[0].message.content or FALLBACK_ACTION
|
| 91 |
except Exception:
|
| 92 |
-
# Any API error → fallback
|
| 93 |
pass
|
| 94 |
|
| 95 |
action = parse_model_action(response_text)
|
| 96 |
obs, reward, done, _ = env.step(action)
|
| 97 |
final_reward = reward.value
|
| 98 |
|
| 99 |
-
# [STEP] marker
|
| 100 |
sys.stdout.write(f"[STEP] step={step} reward={final_reward:.3f}\n")
|
| 101 |
sys.stdout.flush()
|
| 102 |
|
| 103 |
history.append(f"Step {step}: {action.action_type}")
|
| 104 |
|
| 105 |
-
# [END] marker
|
| 106 |
sys.stdout.write(f"[END] task={task} score={final_reward:.3f} steps={step}\n")
|
| 107 |
sys.stdout.flush()
|
| 108 |
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
+
|
| 3 |
+
# Redirect all print calls from imported modules to stderr
|
| 4 |
+
_original_print = print
|
| 5 |
+
def print(*args, **kwargs):
|
| 6 |
+
kwargs.setdefault('file', sys.stderr)
|
| 7 |
+
_original_print(*args, **kwargs)
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
import textwrap
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://dummy.api")
|
| 13 |
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY", "dummy-key")
|
| 14 |
MODEL_NAME = os.getenv("MODEL_NAME", "dummy-model")
|
| 15 |
MAX_STEPS = 5
|
| 16 |
FALLBACK_ACTION = "skip"
|
| 17 |
|
|
|
|
| 18 |
from environment import CodeReviewEnv
|
| 19 |
from models import Action
|
| 20 |
|
|
|
|
| 52 |
return Action(action_type="write_comment", comment_text=text)
|
| 53 |
|
| 54 |
def main():
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
from openai import OpenAI
|
| 57 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY) if API_BASE_URL != "https://dummy.api" else None
|
|
|
|
| 69 |
step = 0
|
| 70 |
final_reward = 0.0
|
| 71 |
|
|
|
|
| 72 |
sys.stdout.write(f"[START] task={task}\n")
|
| 73 |
sys.stdout.flush()
|
| 74 |
|
|
|
|
| 77 |
prompt = build_user_prompt(step, obs, history)
|
| 78 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": prompt}]
|
| 79 |
|
|
|
|
| 80 |
response_text = FALLBACK_ACTION
|
| 81 |
if client is not None:
|
| 82 |
try:
|
|
|
|
| 88 |
)
|
| 89 |
response_text = resp.choices[0].message.content or FALLBACK_ACTION
|
| 90 |
except Exception:
|
|
|
|
| 91 |
pass
|
| 92 |
|
| 93 |
action = parse_model_action(response_text)
|
| 94 |
obs, reward, done, _ = env.step(action)
|
| 95 |
final_reward = reward.value
|
| 96 |
|
|
|
|
| 97 |
sys.stdout.write(f"[STEP] step={step} reward={final_reward:.3f}\n")
|
| 98 |
sys.stdout.flush()
|
| 99 |
|
| 100 |
history.append(f"Step {step}: {action.action_type}")
|
| 101 |
|
|
|
|
| 102 |
sys.stdout.write(f"[END] task={task} score={final_reward:.3f} steps={step}\n")
|
| 103 |
sys.stdout.flush()
|
| 104 |
|